/* Like getJobFolder but with some prefix and suffix text * to get an *.js file. * */ onion_connection_status OnionServer::getJobFolderWrapped( Onion::Request &req, Onion::Response &res ){ res.write("json_job_files = ", 17); onion_connection_status ret = getJobFolder(req, res); res.write(";", 1); return ret; }
/* Like 'search_file' */ onion_connection_status OnionServer::getSettingKinectWrapped( Onion::Request &req, Onion::Response &res) { const char* path = onion_request_get_fullpath( req.c_handler() ); #ifdef VERBOSE printf("Request of %s.\n",path); #endif std::string filename("./html/"); filename.append(path); std::ifstream file(filename.c_str()); std::string line; if( file.is_open()){ /* Create header with mime type and charset information for several file extensions. * This is just a workaround. There should be an automatic mechanicm * in libonion. */ int periodPos = filename.find_last_of('.'); std::string extension = filename.substr(periodPos+1); std::string key("Content-Type"); std::string defaultType("text/html; charset: utf-8"); std::string mime = m_mimedict.get( extension , defaultType ) ; res.setHeader(key,mime); onion_response_write_headers(res.c_handler());// missing in cpp bindings? //res.writeHeaders();//this was added by me... try{ /* res.write("json_kinect = ", 14); const char* kinect = m_settingKinect.getConfig(true); size_t len = strlen( kinect ); res.write(kinect, (int) len ); res.write(";\n", 2 ); */ while (std::getline(file, line)) { res.write( line.c_str(), line.size() ); res.write("\n", 1 ); } }//catch ( const boost::iobase::failure &ex ) catch ( const std::exception & ex ){ std::cerr << "Can not read " << filename << std::endl; res.write( ErrReadFailed.c_str(), ErrReadFailed.size()); } }else{ res.write( ErrNotFound.c_str(), ErrNotFound.size()); } return OCS_PROCESSED; }
/* Like getJobTimings but with some prefix and suffix text * to get an *.js file. * Moreover, the sending of all state fields will forced. * */ onion_connection_status OnionServer::getJobTimingsWrapped( Onion::Request &req, Onion::Response &res ){ int actionid = 12; res.write("job_timings = ", 14); if( ! updateSignal(&req, actionid, &res) ){ //signals did not write into response. Write default reply. std::string reply("'Request not handled.'"); res.write( reply.c_str(), reply.size() ); } res.write(";", 1); return OCS_PROCESSED; }
/* * Returns json struct of current settings. */ onion_connection_status OnionServer::getSettingKinect( Onion::Request &req, Onion::Response &res ){ const char* kinect = m_settingKinect.getConfig(true); size_t len = strlen( kinect ); res.write(kinect, (int) len ); return OCS_PROCESSED; }
/* * Returns json struct of current settings. */ onion_connection_status OnionServer::getB9CreatorSettings( Onion::Request &req, Onion::Response &res ){ const char* b9Creator = m_b9CreatorSettings.getConfig(true); size_t len = strlen( b9Creator ); res.write(b9Creator, (int) len ); return OCS_PROCESSED; }
/* SendSignal with actionid=HTTP_ACTION_GET_PREVIEW_IMAGE to get png image from * DisplayManager. */ onion_connection_status OnionServer::preview( Onion::Request &req, Onion::Response &res ){ int actionid = HTTP_ACTION_GET_PREVIEW_IMAGE; if( -3 == updateSignal(&req, actionid, &res) ){ //signals did not wrote into response. Write default reply. std::string reply("Could not generate Image."); res.write( reply.c_str(), reply.size() ); } return OCS_PROCESSED; }
/* * Parse data from client. Use actionid-arg to distinct different * cases. */ onion_connection_status OnionServer::updateData( Onion::Request &req, Onion::Response &res) { /* Default reply is 'reload' which force reload * of complete website. In mosty cases this string will replaced * by one of the signal handlers. */ int actionid = atoi( onion_request_get_queryd(req.c_handler(), "actionid","0") ); const int updateResult = updateSignal( &req, actionid, &res); if( -3 == updateResult ){ // Nothing was written. Write default reply (empty string). res.write("", 0); } else if( -2 == updateResult ){ // Nothing was written and reload should be forced. std::string reply("reload"); res.write(reply.c_str(), reply.size() ); } return OCS_PROCESSED; }
/* Returns json struct of filenames in job files folder. */ onion_connection_status OnionServer::getJobFolder( Onion::Request &req, Onion::Response &res ){ std::string &folder = m_b9CreatorSettings.m_b9jDir; std::ostringstream json_reply; fs::path full_path = fs::system_complete( fs::path( folder ) ); unsigned long file_count = 0; json_reply << "{ \"name\" : \"" << folder << "\", \"content\" : [" ; if( !fs::exists( full_path ) ){ std::cout << "Not found: " << full_path.filename() << std::endl; json_reply << "\"none\""; }else if ( !fs::is_directory( full_path ) ){ std::cout << "Path is no directory: " << full_path.filename() << std::endl; json_reply << "\"none\""; }else{ fs::directory_iterator end_iter; for ( fs::directory_iterator dir_itr( full_path ); dir_itr != end_iter; ++dir_itr ) { try { if (! fs::is_directory( dir_itr->status() ) ) { //regluar file or symbolic link if( file_count ) json_reply << ", " << std::endl; json_reply << "{ \"" << file_count << "\": " \ << dir_itr->path().filename() << " }"; ++file_count; } /*Remark: The index numbers are sourounded by "'s * to avoid problems on the javascript side. * I.E. {"0" : "filename" }. * */ } catch ( const std::exception & ex ) { std::cout << dir_itr->path().filename() << " " << ex.what() << std::endl; } } } json_reply << "] }" ; std::string json_replyStr = json_reply.str(); size_t len = json_replyStr.size(); res.write(json_replyStr.c_str(), (int) len ); return OCS_PROCESSED; }
/* * Convert all enties of message queue into json code and send this file * to the client. * Unused fragment of TinyPrint app. */ onion_connection_status OnionServer::getPrinterMessages( Onion::Request &req, Onion::Response &res ){ Messages &q = m_settingKinect.m_queues; cJSON* tmp = jsonMessages("serialMessages", q.m_messageQueue); if( tmp != NULL ){ char* json_serialMessages = cJSON_Print( tmp ); size_t len = strlen( json_serialMessages ); res.write(json_serialMessages, (int) len); cJSON_Delete(tmp); tmp = NULL; free(json_serialMessages); json_serialMessages = NULL; }else{ const char* json_serialMessages = "(OnionServer) Serial Messages Error"; size_t len = strlen( json_serialMessages ); res.write(json_serialMessages, (int) len); //Do not free json_serialMessages in this case. It's point to static string here. } return OCS_PROCESSED; }
/* * Convert all enties of message queue into json code and send this file * to the client. */ onion_connection_status OnionServer::getPrinterMessages( Onion::Request &req, Onion::Response &res ){ Messages &q = m_b9CreatorSettings.m_queues; cJSON* tmp = jsonMessages("serialMessages", q.m_messageQueue); if( tmp != NULL ){ char* json_serialMessages = cJSON_Print( tmp ); size_t len = strlen( json_serialMessages ); res.write(json_serialMessages, (int) len); cJSON_Delete(tmp); tmp = NULL; free(json_serialMessages); json_serialMessages = NULL; }else{ const char* json_serialMessages = "(OnionServer) Serial Messages Error"; size_t len = strlen( json_serialMessages ); res.write(json_serialMessages, (int) len); //here no free of json_serialMessages, because it's point to static string } return OCS_PROCESSED; }
/* * Parse data from client. Use actionid-arg to distinct different * cases. */ onion_connection_status OnionServer::updateData( Onion::Request &req, Onion::Response &res) { /* Default reply is 'reload' which force reload * of complete website. In mosty cases this string will replaced * by one of the signal handlers. */ int actionid = atoi( onion_request_get_queryd(req.c_handler(), "actionid","0") ); if( ! updateSignal( &req, actionid, &res) ){ // Signal returns true if at least one handler writes into res. // Write default reply, if nothing was written. std::string reply("reload"); res.write(reply.c_str(), reply.size() ); } return OCS_PROCESSED; }