void web_pipeline_set(struct evhttp_request *req, void *arg) { moModule *module; struct evkeyvalq headers; const char *uri; uri = evhttp_request_uri(req); evhttp_parse_query(uri, &headers); if ( evhttp_find_header(&headers, "objectname") == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "missing objectname"); } if ( evhttp_find_header(&headers, "name") == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "missing name"); } if ( evhttp_find_header(&headers, "value") == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "missing value"); } module = module_search(evhttp_find_header(&headers, "objectname"), pipeline); if ( module == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "object not found"); } module->property(evhttp_find_header(&headers, "name")).set( (const std::string &)evhttp_find_header(&headers, "value")); evhttp_clear_headers(&headers); web_message(req, "ok"); }
void web_pipeline_get(struct evhttp_request *req, void *arg) { moModule *module; struct evkeyvalq headers; const char *uri; uri = evhttp_request_uri(req); if ( uri == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "unable to retreive uri"); } evhttp_parse_query(uri, &headers); if ( evhttp_find_header(&headers, "objectname") == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "missing objectname"); } if ( evhttp_find_header(&headers, "name") == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "missing name"); } module = pipeline->getModuleById(evhttp_find_header(&headers, "objectname")); if ( module == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "object not found"); } web_message(req, module->property(evhttp_find_header(&headers, "name")).asString().c_str()); evhttp_clear_headers(&headers); }
void web_pipeline_create(struct evhttp_request *req, void *arg) { moModule *module; struct evkeyvalq headers; const char *uri; uri = evhttp_request_uri(req); if ( uri == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "unable to retreive uri"); } evhttp_parse_query(uri, &headers); if ( evhttp_find_header(&headers, "objectname") == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "missing objectname"); } module = moFactory::getInstance()->create(evhttp_find_header(&headers, "objectname")); if ( module == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "invalid objectname"); } pipeline->addElement(module); evhttp_clear_headers(&headers); web_message(req, module->property("id").asString().c_str()); }
void web_pipeline_remove(struct evhttp_request *req, void *arg) { moModule *module; moDataStream *ds; struct evkeyvalq headers; const char *uri; uri = evhttp_request_uri(req); if ( uri == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "unable to retreive uri"); } evhttp_parse_query(uri, &headers); if ( evhttp_find_header(&headers, "objectname") == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "missing objectname"); } module = pipeline->getModuleById(evhttp_find_header(&headers, "objectname")); if ( module == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "object not found"); } pipeline->stop(); module->stop(); // disconnect inputs if ( module->getInputCount() ) { for ( int i = 0; i < module->getInputCount(); i++ ) { ds = module->getInput(i); if ( ds == NULL ) continue; ds->removeObserver(module); } } // disconnect output if ( module->getOutputCount() ) { for ( int i = 0; i < module->getOutputCount(); i++ ) { ds = module->getOutput(i); if ( ds == NULL ) continue; ds->removeObservers(); } } // remove element from pipeline pipeline->removeElement(module); delete module; web_message(req, "ok"); evhttp_clear_headers(&headers); }
void web_pipeline_stream(struct evhttp_request *req, void *arg) { struct timeval when = { 0, 20 }; struct evkeyvalq headers; const char *uri; int idx = 0; moModule *module = NULL; uri = evhttp_request_uri(req); evhttp_parse_query(uri, &headers); if ( evhttp_find_header(&headers, "objectname") == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "missing objectname"); } module = module_search(evhttp_find_header(&headers, "objectname"), pipeline); if ( module == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "object not found"); } if ( evhttp_find_header(&headers, "index") != NULL ) idx = atoi(evhttp_find_header(&headers, "index")); if ( idx < 0 || idx >= module->getOutputCount() ) { evhttp_clear_headers(&headers); return web_error(req, "invalid index"); } struct chunk_req_state *state = (struct chunk_req_state*)malloc(sizeof(struct chunk_req_state)); memset(state, 0, sizeof(struct chunk_req_state)); state->req = req; state->closed = false; state->stream = new otStreamModule(); state->delay = 100; if ( evhttp_find_header(&headers, "scale") != NULL ) state->stream->property("scale").set(evhttp_find_header(&headers, "scale")); if ( evhttp_find_header(&headers, "delay") != NULL ) state->delay = atoi(evhttp_find_header(&headers, "delay")); state->stream->setInput(module->getOutput(idx)); evhttp_add_header(req->output_headers, "Content-Type", "multipart/x-mixed-replace; boundary=mjpegstream"); evhttp_send_reply_start(req, HTTP_OK, "Everything is fine"); evhttp_connection_set_closecb(req->evcon, web_pipeline_stream_close, state); event_once(-1, EV_TIMEOUT, web_pipeline_stream_trickle, state, &when); }
void web_pipeline_connect(struct evhttp_request *req, void *arg) { moModule *in, *out; int inidx = 0, outidx = 0; struct evkeyvalq headers; const char *uri; uri = evhttp_request_uri(req); if ( uri == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "unable to retreive uri"); } evhttp_parse_query(uri, &headers); if ( evhttp_find_header(&headers, "out") == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "missing out"); } if ( evhttp_find_header(&headers, "in") == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "missing in"); } if ( evhttp_find_header(&headers, "outidx") != NULL ) outidx = atoi(evhttp_find_header(&headers, "outidx")); if ( evhttp_find_header(&headers, "inidx") != NULL ) inidx = atoi(evhttp_find_header(&headers, "inidx")); in = pipeline->getModuleById(evhttp_find_header(&headers, "in")); out = pipeline->getModuleById(evhttp_find_header(&headers, "out")); if ( in == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "in object not found"); } if ( out == NULL && strcmp(evhttp_find_header(&headers, "out"), "NULL") != 0 ) { evhttp_clear_headers(&headers); return web_error(req, "out object not found"); } if ( strcmp(evhttp_find_header(&headers, "out"), "NULL") == 0 ) in->setInput(NULL, inidx); else in->setInput(out->getOutput(outidx), inidx); evhttp_clear_headers(&headers); web_message(req, "ok"); }
/* $begin detach */ void web_pthread_detach(pthread_t tid) { int rc; if ((rc = pthread_detach(tid)) != 0) web_error("Pthread_detach error"); }
void web_pthread_join(pthread_t tid, void **thread_return) { int rc; if ((rc = pthread_join(tid, thread_return)) != 0) web_error("Pthread_join error"); }
void web_pthread_cancel(pthread_t tid) { int rc; if ((rc = pthread_cancel(tid)) != 0) web_error("Pthread_cancel error"); }
void web_close(int fd) { int rc; if ((rc = close(fd)) < 0) web_error("Close error"); }
ssize_t web_rio_readlineb(rio_t *rp, void *usrbuf, size_t maxlen) { ssize_t rc; if ((rc = rio_readlineb(rp, usrbuf, maxlen)) < 0) web_error("Rio_readlineb error"); return rc; }
void web_pthread_create(pthread_t *tidp, pthread_attr_t *attrp, void * (*routine)(void *), void *argp) { int rc; if ((rc = pthread_create(tidp, attrp, routine, argp)) != 0) web_error("Pthread_create error"); }
ssize_t web_rio_readnb(rio_t *rp, void *usrbuf, size_t n) { ssize_t rc; if ((rc = rio_readnb(rp, usrbuf, n)) < 0) web_error("Rio_readnb error"); return rc; }
void *web_calloc(size_t nmemb, size_t size) { void *p; if ((p = calloc(nmemb, size)) == NULL) web_error("Calloc error"); return p; }
void *web_malloc(size_t size) { void *p; if ((p = malloc(size)) == NULL) web_error("Malloc error"); return p; }
int web_open_listenfd(char *port) { int rc; if ((rc = open_listenfd(port)) < 0) web_error("Open_listenfd error"); return rc; }
int web_open_clientfd(char *hostname, char *port) { int rc; if ((rc = open_clientfd(hostname, port)) < 0) web_error("Open_clientfd error"); return rc; }
int web_accept(int s, struct sockaddr *addr, socklen_t *addrlen) { int rc; if ((rc = accept(s, addr, addrlen)) < 0) web_error("Accept error"); return rc; }
void web_file(struct evhttp_request *req, void *arg) { FILE *fd; int readidx = 0, ret; long filesize = 0; struct evbuffer *evb; char filename[256], *buf; /* web_file accept only file from gui */ if ( strstr(req->uri, "/gui/") != req->uri ) { evhttp_send_error(req, 404, "Not found"); return; } if ( strstr(req->uri, "..") != NULL ) { evhttp_send_error(req, 403, "Security error"); return; } snprintf(filename, sizeof(filename), "%s/%s", MO_GUIDIR, req->uri + sizeof("/gui/") - 1); printf("GET %s\n", filename); fd = fopen(filename, "rb"); if ( fd == NULL ) { evhttp_send_error(req, 404, "Not found"); return; } fseek(fd, 0, SEEK_END); filesize = ftell(fd); fseek(fd, 0, SEEK_SET); buf = (char*)malloc(filesize); if ( buf == NULL ) { fclose(fd); web_error(req, "memory error"); return; } while ( readidx < filesize ) { ret = fread(&buf[readidx], 1, filesize - readidx, fd); if ( ret <= 0 ) { perror("guifile"); return; } readidx += ret; } fclose(fd); if ( strncmp(filename + strlen(filename) - 2, "js", 2) == 0 ) evhttp_add_header(req->output_headers, "Content-Type", "application/javascript"); else if ( strncmp(filename + strlen(filename) - 3, "css", 3) == 0 ) evhttp_add_header(req->output_headers, "Content-Type", "text/css"); else if ( strncmp(filename + strlen(filename) - 3, "png", 3) == 0 ) evhttp_add_header(req->output_headers, "Content-Type", "image/png"); else evhttp_add_header(req->output_headers, "Content-Type", "text/html"); evb = evbuffer_new(); evbuffer_add(evb, buf, filesize); evhttp_send_reply(req, HTTP_OK, "Everything is fine", evb); evbuffer_free(evb); free(buf); }
void web_sem_init(sem_t *sem, int pshared, unsigned int value) { if (sem_init(sem, pshared, value) < 0) web_error("Sem_init error"); }
void web_factory_desribe(struct evhttp_request *req, void *arg) { std::map<std::string, moProperty*>::iterator it; cJSON *root, *mod, *properties, *io, *array; moDataStream *ds; moModule *module; struct evkeyvalq headers; const char *uri; uri = evhttp_request_uri(req); evhttp_parse_query(uri, &headers); if ( evhttp_find_header(&headers, "name") == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "missing name"); } module = moFactory::getInstance()->create(evhttp_find_header(&headers, "name")); if ( module == NULL ) { evhttp_clear_headers(&headers); return web_error(req, "invalid name"); } root = cJSON_CreateObject(); cJSON_AddNumberToObject(root, "success", 1); cJSON_AddStringToObject(root, "message", "ok"); cJSON_AddItemToObject(root, "describe", mod=cJSON_CreateObject()); cJSON_AddStringToObject(mod, "name", module->getName().c_str()); cJSON_AddStringToObject(mod, "description", module->getDescription().c_str()); cJSON_AddStringToObject(mod, "author", module->getAuthor().c_str()); cJSON_AddNumberToObject(mod, "running", module->isStarted() ? 1 : 0); cJSON_AddItemToObject(mod, "properties", properties=cJSON_CreateObject()); for ( it = module->getProperties().begin(); it != module->getProperties().end(); it++ ) { cJSON_AddStringToObject(properties, it->first.c_str(), it->second->asString().c_str()); } if ( module->getInputCount() ) { cJSON_AddItemToObject(mod, "inputs", array=cJSON_CreateArray()); for ( int i = 0; i < module->getInputCount(); i++ ) { ds = module->getInput(i); cJSON_AddItemToArray(array, io=cJSON_CreateObject()); cJSON_AddNumberToObject(io, "index", i); cJSON_AddStringToObject(io, "name", module->getInputInfos(i)->getName().c_str()); cJSON_AddStringToObject(io, "type", module->getInputInfos(i)->getType().c_str()); } } if ( module->getOutputCount() ) { cJSON_AddItemToObject(mod, "outputs", array=cJSON_CreateArray()); for ( int i = 0; i < module->getOutputCount(); i++ ) { ds = module->getOutput(i); cJSON_AddItemToArray(array, io=cJSON_CreateObject()); cJSON_AddNumberToObject(io, "index", i); cJSON_AddStringToObject(io, "name", module->getOutputInfos(i)->getName().c_str()); cJSON_AddStringToObject(io, "type", module->getOutputInfos(i)->getType().c_str()); } } delete module; evhttp_clear_headers(&headers); web_json(req, root); }
void web_P(sem_t *sem) { if (sem_wait(sem) < 0) web_error("P error"); }
Message receive( web::socket const & s , std::chrono::seconds timeout_in ) { static_assert( elib::aux::is_same<Message, request>::value || elib::aux::is_same<Message, response>::value , "The only two choices" ); Message msg; data_type buff(10024); data_type remaining; const auto timeout_at = std::chrono::system_clock::now() + timeout_in; auto timeout_receive = [&]() { std::error_code ec; while (std::chrono::system_clock::now() < timeout_at) { ec.clear(); ::ssize_t ret = web::receive(s, buff, ec); if (ec.value() == EAGAIN || ec.value() == EWOULDBLOCK) continue; if (ec) { ELIB_THROW_EXCEPTION(socket_error( elib::fmt( "http::receive failed with error %s" , ec.message() ) , ec )); } ELIB_ASSERT(ret >= 0); std::copy_n( buff.begin() , static_cast<std::size_t>(ret) , std::back_inserter(remaining) ); return ret; } ELIB_THROW_EXCEPTION(web_error( "http::receive timed out" )); }; // get header while (true) { timeout_receive(); auto pos = parse(remaining.begin(), remaining.end(), msg.header); if (pos == remaining.begin()) continue; remaining.erase(remaining.begin(), pos); break; } // get fields and newline while (true) { auto field_end = parse(remaining.begin(), remaining.end(), msg.fields); remaining.erase(remaining.begin(), field_end); auto newl_end = parse_newl(remaining.begin(), remaining.end()); bool have_newl = newl_end != remaining.begin(); remaining.erase(remaining.begin(), newl_end); if (have_newl) break; // receive must only happen if we fail to parse till the // newline break. othewise we could be waiting on a message // we already have timeout_receive(); } std::size_t const con_size = content_size(msg); ELIB_ASSERT(con_size >= remaining.size()); // get remaining data if needed while (remaining.size() != con_size) { timeout_receive(); } msg.data.insert(msg.data.end(), remaining.begin(), remaining.end()); return msg; }
void web_V(sem_t *sem) { if (sem_post(sem) < 0) web_error("V error"); }
void web_rio_writen(int fd, void *usrbuf, size_t n) { if (rio_writen(fd, usrbuf, n) != n) web_error("Rio_writen error"); }