diff options
Diffstat (limited to 'src')
25 files changed, 861 insertions, 774 deletions
diff --git a/src/uscxml/Factory.cpp b/src/uscxml/Factory.cpp index d14b91a..94049ea 100644 --- a/src/uscxml/Factory.cpp +++ b/src/uscxml/Factory.cpp @@ -28,6 +28,10 @@ # include "uscxml/plugins/invoker/modality/miles/SpatialAudio.h" # endif +# ifdef FFMPEG_FOUND +# include "uscxml/plugins/invoker/ffmpeg/FFMPEGInvoker.h" +# endif + # ifdef V8_FOUND # include "uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.h" # endif @@ -42,6 +46,7 @@ # include "uscxml/plugins/element/fetch/FetchElement.h" # include "uscxml/plugins/element/response/ResponseElement.h" +# include "uscxml/plugins/element/postpone/PostponeElement.h" #endif @@ -96,6 +101,13 @@ Factory::Factory() { } #endif +#ifdef FFMPEG_FOUND + { + FFMPEGInvoker* invoker = new FFMPEGInvoker(); + registerInvoker(invoker); + } +#endif + #ifdef OPENSCENEGRAPH_FOUND { OSGInvoker* invoker = new OSGInvoker(); @@ -158,6 +170,10 @@ Factory::Factory() { ResponseElement* element = new ResponseElement(); registerExecutableContent(element); } + { + PostponeElement* element = new PostponeElement(); + registerExecutableContent(element); + } #endif } diff --git a/src/uscxml/Interpreter.cpp b/src/uscxml/Interpreter.cpp index 4e5b279..1e7c84e 100644 --- a/src/uscxml/Interpreter.cpp +++ b/src/uscxml/Interpreter.cpp @@ -137,13 +137,16 @@ void Interpreter::startPrefixMapping(const std::string& prefix, const std::strin LOG(INFO) << "Mapped default namespace to 'scxml:'"; _xpathPrefix = "scxml:"; _nsContext.addNamespaceDeclaration(uri, "scxml"); + _nsToPrefix[uri] = "scxml"; } else { _xpathPrefix = prefix + ":"; _xmlNSPrefix = _xpathPrefix; _nsContext.addNamespaceDeclaration(uri, prefix); + _nsToPrefix[uri] = prefix; } } else { _nsContext.addNamespaceDeclaration(uri, prefix); + _nsToPrefix[uri] = prefix; } } @@ -462,14 +465,14 @@ void Interpreter::mainEventLoop() { if (_internalQueue.size() == 0) { _stable = true; } else { - Event internalEvent = _internalQueue.front(); + _currEvent = _internalQueue.front(); _internalQueue.pop_front(); #if VERBOSE - std::cout << "Received internal event " << internalEvent.name << std::endl; + std::cout << "Received internal event " << _currEvent.name << std::endl; #endif if (_dataModel) - _dataModel.setEvent(internalEvent); - enabledTransitions = selectTransitions(internalEvent.name); + _dataModel.setEvent(_currEvent); + enabledTransitions = selectTransitions(_currEvent.name); } } if (!enabledTransitions.empty()) { @@ -509,20 +512,20 @@ void Interpreter::mainEventLoop() { runOnMainThread(200); } - Event externalEvent = _externalQueue.pop(); + _currEvent = _externalQueue.pop(); #if VERBOSE - std::cout << "Received externalEvent event " << externalEvent.name << std::endl; + std::cout << "Received externalEvent event " << _currEvent.name << std::endl; #endif - externalEvent.type = Event::EXTERNAL; // make sure it is set to external + _currEvent.type = Event::EXTERNAL; // make sure it is set to external if (!_running) exitInterpreter(); - if (_dataModel && boost::iequals(externalEvent.name, "cancel.invoke." + _sessionId)) + if (_dataModel && boost::iequals(_currEvent.name, "cancel.invoke." + _sessionId)) break; if (_dataModel) try { - _dataModel.setEvent(externalEvent); + _dataModel.setEvent(_currEvent); } catch (Event e) { LOG(ERROR) << "Syntax error while setting external event:" << std::endl << e << std::endl; } @@ -537,7 +540,7 @@ void Interpreter::mainEventLoop() { invokeId = _dataModel.evalAsString(ATTR(invokeElem, "idlocation")); std::string autoForward = invokeElem.getAttribute("autoforward"); - if (boost::iequals(invokeId, externalEvent.invokeid)) { + if (boost::iequals(invokeId, _currEvent.invokeid)) { Arabica::XPath::NodeSet<std::string> finalizes = filterChildElements(_xmlNSPrefix + "finalize", invokeElem); for (int k = 0; k < finalizes.size(); k++) { @@ -547,11 +550,15 @@ void Interpreter::mainEventLoop() { } if (boost::iequals(autoForward, "true")) { - _invokers[invokeId].send(externalEvent); + try { + _invokers[invokeId].send(_currEvent); + } catch(...) { + LOG(ERROR) << "Exception caught while sending event to invoker " << invokeId; + } } } } - enabledTransitions = selectTransitions(externalEvent.name); + enabledTransitions = selectTransitions(_currEvent.name); if (!enabledTransitions.empty()) microstep(enabledTransitions); } @@ -790,7 +797,11 @@ void Interpreter::delayedSend(void* userdata, std::string eventName) { std::string invokeId = sendReq.target.substr(2, sendReq.target.length() - 2); if (INSTANCE->_invokers.find(invokeId) != INSTANCE->_invokers.end()) { tthread::lock_guard<tthread::mutex> lock(INSTANCE->_mutex); - INSTANCE->_invokers[invokeId].send(sendReq); + try { + INSTANCE->_invokers[invokeId].send(sendReq); + } catch(...) { + LOG(ERROR) << "Exception caught while sending event to invoker " << invokeId; + } } else { LOG(ERROR) << "Can not send to invoked component '" << invokeId << "', no such invokeId" << std::endl; } @@ -799,7 +810,11 @@ void Interpreter::delayedSend(void* userdata, std::string eventName) { } else { IOProcessor ioProc = INSTANCE->getIOProcessor(sendReq.type); if (ioProc) { - ioProc.send(sendReq); + try { + ioProc.send(sendReq); + } catch(...) { + LOG(ERROR) << "Exception caught while sending event to ioprocessor " << sendReq.type; + } } } assert(INSTANCE->_sendIds.find(sendReq.sendid) != INSTANCE->_sendIds.end()); @@ -905,9 +920,17 @@ void Interpreter::invoke(const Arabica::DOM::Node<std::string>& element) { invoker.setInterpreter(this); _invokers[invokeReq.invokeid] = invoker; LOG(INFO) << "Added invoker " << invokeReq.type << " at " << invokeReq.invokeid; - invoker.invoke(invokeReq); + try { + invoker.invoke(invokeReq); + } catch(...) { + LOG(ERROR) << "Exception caught while sending invoke requst to invoker " << invokeReq.invokeid; + } if (_dataModel) { - _dataModel.assign("_invokers['" + invokeReq.invokeid + "']", invoker.getDataModelVariables()); + try { + _dataModel.assign("_invokers['" + invokeReq.invokeid + "']", invoker.getDataModelVariables()); + } catch(...) { + LOG(ERROR) << "Exception caught while assigning datamodel variables from invoker " << invokeReq.invokeid; + } } } catch (...) { LOG(ERROR) << "Invoker " << invokeReq.type << " threw an exception"; @@ -977,7 +1000,6 @@ Arabica::XPath::NodeSet<std::string> Interpreter::selectTransitions(const std::s goto LOOP; } } else { - LOG(ERROR) << "Transition has neither event nor eventexpr attribute"; goto LOOP; } @@ -1243,6 +1265,7 @@ void Interpreter::executeContent(const Arabica::DOM::Node<std::string>& content) if (false) { } else if (boost::iequals(TAGNAME(content), _xmlNSPrefix + "onentry") || boost::iequals(TAGNAME(content), _xmlNSPrefix + "onexit") || + boost::iequals(TAGNAME(content), _xmlNSPrefix + "finalize") || boost::iequals(TAGNAME(content), _xmlNSPrefix + "transition")) { // --- CONVENIENCE LOOP -------------------------- NodeList<std::string> executable = content.getChildNodes(); diff --git a/src/uscxml/Interpreter.h b/src/uscxml/Interpreter.h index 48e08b4..318af95 100644 --- a/src/uscxml/Interpreter.h +++ b/src/uscxml/Interpreter.h @@ -129,13 +129,27 @@ public: Arabica::XPath::StandardNamespaceContext<std::string>& getNSContext() { return _nsContext; } + std::string getXMLPrefixForNS(const std::string& ns) { + if (_nsToPrefix.find(ns) != _nsToPrefix.end()) + return _nsToPrefix[ns] + ":"; + return ""; + } - void receive(Event& event) { - _externalQueue.push(event); + void receive(const Event& event, bool toFront = false) { + if (toFront) { + _externalQueue.push_front(event); + } else { + _externalQueue.push(event); + } } - void receiveInternal(Event& event) { + void receiveInternal(const Event& event) { _internalQueue.push_back(event); } + + Event getCurrentEvent() { + return _currEvent; + } + Arabica::XPath::NodeSet<std::string> getConfiguration() { return _configuration; } @@ -202,8 +216,9 @@ protected: Arabica::XPath::XPath<std::string> _xpath; Arabica::XPath::StandardNamespaceContext<std::string> _nsContext; std::string _xmlNSPrefix; // the actual prefix for elements in the xml file - std::string _xpathPrefix; // prefix mapped for xpath, "scxml" is _xmlNSPrefix is empty but _nsURL set + std::string _xpathPrefix; // prefix mapped for xpath, "scxml" is _xmlNSPrefix is empty but _nsURL set std::string _nsURL; // ough to be "http://www.w3.org/2005/07/scxml" + std::map<std::string, std::string> _nsToPrefix; bool _running; bool _done; @@ -219,6 +234,8 @@ protected: uscxml::concurrency::BlockingQueue<Event>* _parentQueue; DelayedEventQueue* _sendQueue; + Event _currEvent; + HTTPServletInvoker* _httpServlet; std::set<InterpreterMonitor*> _monitors; diff --git a/src/uscxml/Message.h b/src/uscxml/Message.h index a746710..c109063 100644 --- a/src/uscxml/Message.h +++ b/src/uscxml/Message.h @@ -62,6 +62,9 @@ public: Event() : type(INTERNAL) {} Event(const Arabica::DOM::Node<std::string>& xmlString) : type(INTERNAL) {}; + bool operator< (const Event& other) const { + return this < &other; + } std::string name; Type type; diff --git a/src/uscxml/URL.cpp b/src/uscxml/URL.cpp index 024bb0c..d410bcf 100644 --- a/src/uscxml/URL.cpp +++ b/src/uscxml/URL.cpp @@ -416,7 +416,6 @@ void URLFetcher::perform() { { tthread::lock_guard<tthread::recursive_mutex> lock(_mutex); if (_handlesToURLs.empty()) { - std::cout << "Waiting for work" << std::endl; _condVar.wait(_mutex); } curl_multi_perform(_multiHandle, &stillRunning); diff --git a/src/uscxml/URL.cpp.old b/src/uscxml/URL.cpp.old deleted file mode 100644 index 20c9a18..0000000 --- a/src/uscxml/URL.cpp.old +++ /dev/null @@ -1,519 +0,0 @@ -#include <algorithm> -#include <assert.h> -#include <iostream> -#include <fstream> - -#include <glog/logging.h> -#include <boost/algorithm/string.hpp> - -#include <stdio.h> -#include <string.h> -#ifndef WIN32 -#include <sys/time.h> -#endif -#include <stdlib.h> -#include <errno.h> - -#include "uscxml/Common.h" -#include "URL.h" - -#include "uscxml/config.h" - -#include <stdio.h> /* defines FILENAME_MAX */ -#ifdef _WIN32 -#include <fcntl.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <io.h> -#include <direct.h> -#define getcwd _getcwd -#else -#include <unistd.h> -#endif - -#include <cstdlib> // mkstemp -#ifdef HAS_UNISTD_H -#include <unistd.h> // mkstemp legacy -#endif - -namespace uscxml { - -URLImpl::~URLImpl() { - if (_localFile.length() > 0) - remove(_localFile.c_str()); -} - -const bool URLImpl::toAbsoluteCwd() { - char currPath[FILENAME_MAX]; - if (!getcwd(currPath, sizeof(currPath))) { - return false; - } - currPath[sizeof(currPath) - 1] = '\0'; /* not really required */ - return toAbsolute(std::string("file://" + std::string(currPath) + "/")); -} - -std::string URLImpl::getLocalFilename(const std::string& suffix) { - if (_localFile.length() > 0) - return _localFile; - - if (_uri.scheme().compare("file") == 0) - return _uri.path(); - - // try hard to find a temporary directory - const char* tmpDir = NULL; - if (tmpDir == NULL) - tmpDir = getenv("TMPDIR"); - if (tmpDir == NULL) - tmpDir = getenv("TMP"); - if (tmpDir == NULL) - tmpDir = getenv("TEMP"); - if (tmpDir == NULL) - tmpDir = getenv("USERPROFILE"); - if (tmpDir == NULL) - tmpDir = "/tmp"; - - char* tmpl = (char*)malloc(strlen(tmpDir) + 11 + suffix.length()); - char* writePtr = tmpl; - memcpy(writePtr, tmpDir, strlen(tmpDir)); - writePtr += strlen(tmpDir); - memcpy(writePtr, "scxmlXXXXXX", 11); - writePtr += 11; - memcpy(writePtr, suffix.c_str(), suffix.length()); - writePtr += suffix.length(); - tmpl[writePtr - tmpl] = 0; - -#ifdef _WIN32 - _mktemp_s(tmpl, strlen(tmpl) + 1); - int fd = _open(tmpl, _O_CREAT, _S_IREAD | _S_IWRITE); -#else - int fd = mkstemps(tmpl, suffix.length()); -#endif - if (fd < 0) { - LOG(ERROR) << "mkstemp " << tmpl << ": " << strerror(errno) << std::endl; - return ""; - } -#ifdef WIN32 - _close(fd); -#else - close(fd); -#endif - return std::string(tmpl); -} - -boost::shared_ptr<URLImpl> URLImpl::toLocalFile(const std::string& content, const std::string& suffix) { - boost::shared_ptr<URLImpl> urlImpl = boost::shared_ptr<URLImpl>(new URLImpl()); - urlImpl->_localFile = urlImpl->getLocalFilename(suffix); - urlImpl->_uri = std::string("file://") + urlImpl->_localFile; - - std::ofstream file(urlImpl->_localFile.c_str(), std::ios_base::out); - if(file.is_open()) { - file << content; - file.close(); - } else { - return boost::shared_ptr<URLImpl>(); - } - - return urlImpl; -} - -const bool URLImpl::toAbsolute(const std::string& baseUrl) { - if (_uri.is_absolute()) - return true; - _uri = Arabica::io::URI(baseUrl, _uri.as_string()); - if (!_uri.is_absolute()) - return false; - return true; -} - -const std::string URLImpl::asLocalFile(const std::string& suffix, bool reload) { - // this is already a local file - if (_uri.scheme().compare("file") == 0) - return _uri.path(); - - if (_localFile.length() > 0 && !reload) - return _localFile; - - if (_localFile.length() > 0) - remove(_localFile.c_str()); - - _localFile = getLocalFilename(suffix); - - std::ofstream file(_localFile.c_str(), std::ios_base::out); - if(file.is_open()) { - file << URL(this->shared_from_this()); - file.close(); - } else { - _localFile = ""; - } - - return _localFile; -} - -std::ostream & operator<<(std::ostream & stream, const URL& url) { - - std::string urlString = url.asString(); - std::string fileURL = "file://"; - - // strip file:// to support relative filenames - if(urlString.substr(0, fileURL.size()) == fileURL) { - urlString = urlString.substr(fileURL.size()); -#ifdef _WIN32 - urlString = urlString.substr(0,1) + ":" + urlString.substr(1); -// std::replace( urlString.begin(), urlString.end(), '/', '\\'); -#endif - } -// LOG(ERROR) << "Trying to open " << urlString; - URL_FILE *handle = url_fopen(urlString.c_str(), "r"); - - if(!handle) { - LOG(ERROR) << "Cannot open URL " << url.asString(); - return stream; - } - - int nread; - char buffer[256]; - - do { - nread = url_fread(buffer, 1,sizeof(buffer), handle); - stream.write(buffer, nread); - } while(nread); - - url_fclose(handle); - return stream; -} - -/* we use a global one for convenience */ -CURLM *multi_handle; - -/* curl calls this routine to get more data */ -static size_t write_callback(char *buffer, - size_t size, - size_t nitems, - void *userp) { - char *newbuff; - size_t rembuff; - - URL_FILE *url = (URL_FILE *)userp; - size *= nitems; - - rembuff=url->buffer_len - url->buffer_pos; /* remaining space in buffer */ - - if(size > rembuff) { - /* not enough space in buffer */ - newbuff=(char*)realloc(url->buffer,url->buffer_len + (size - rembuff)); - if(newbuff==NULL) { - fprintf(stderr,"callback buffer grow failed\n"); - size=rembuff; - } else { - /* realloc suceeded increase buffer size*/ - url->buffer_len+=size - rembuff; - url->buffer=newbuff; - } - } - - memcpy(&url->buffer[url->buffer_pos], buffer, size); - url->buffer_pos += size; - - return size; -} - -/* use to attempt to fill the read buffer up to requested number of bytes */ -static int fill_buffer(URL_FILE *file, size_t want) { - fd_set fdread; - fd_set fdwrite; - fd_set fdexcep; - struct timeval timeout; - int rc; - - /* only attempt to fill buffer if transactions still running and buffer - * doesnt exceed required size already - */ - if((!file->still_running) || (file->buffer_pos > want)) - return 0; - - /* attempt to fill buffer */ - do { - int maxfd = -1; - long curl_timeo = -1; - - FD_ZERO(&fdread); - FD_ZERO(&fdwrite); - FD_ZERO(&fdexcep); - - /* set a suitable timeout to fail on */ - timeout.tv_sec = 60; /* 1 minute */ - timeout.tv_usec = 0; - - curl_multi_timeout(multi_handle, &curl_timeo); - if(curl_timeo >= 0) { - timeout.tv_sec = curl_timeo / 1000; - if(timeout.tv_sec > 1) - timeout.tv_sec = 1; - else - timeout.tv_usec = (curl_timeo % 1000) * 1000; - } - - /* get file descriptors from the transfers */ - curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); - - /* In a real-world program you OF COURSE check the return code of the - function calls. On success, the value of maxfd is guaranteed to be - greater or equal than -1. We call select(maxfd + 1, ...), specially - in case of (maxfd == -1), we call select(0, ...), which is basically - equal to sleep. */ - - rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); - - switch(rc) { - case -1: - /* select error */ - break; - - case 0: - default: - /* timeout or readable/writable sockets */ - curl_multi_perform(multi_handle, &file->still_running); - break; - } - } while(file->still_running && (file->buffer_pos < want)); - return 1; -} - -/* use to remove want bytes from the front of a files buffer */ -static int use_buffer(URL_FILE *file,int want) { - /* sort out buffer */ - if((file->buffer_pos - want) <=0) { - /* ditch buffer - write will recreate */ - if(file->buffer) - free(file->buffer); - - file->buffer=NULL; - file->buffer_pos=0; - file->buffer_len=0; - } else { - /* move rest down make it available for later */ - memmove(file->buffer, - &file->buffer[want], - (file->buffer_pos - want)); - - file->buffer_pos -= want; - } - return 0; -} - -URL_FILE *url_fopen(const char *url,const char *operation) { - /* this code could check for URLs or types in the 'url' and - basicly use the real fopen() for standard files */ - - URL_FILE *file; - (void)operation; - - file = (URL_FILE*)malloc(sizeof(URL_FILE)); - if(!file) - return NULL; - - memset(file, 0, sizeof(URL_FILE)); - - if((file->handle.file=fopen(url,operation))) - file->type = CFTYPE_FILE; /* marked as URL */ - - else { - file->type = CFTYPE_CURL; /* marked as URL */ - file->handle.curl = curl_easy_init(); - - curl_easy_setopt(file->handle.curl, CURLOPT_URL, url); - curl_easy_setopt(file->handle.curl, CURLOPT_WRITEDATA, file); - curl_easy_setopt(file->handle.curl, CURLOPT_VERBOSE, 0L); - curl_easy_setopt(file->handle.curl, CURLOPT_WRITEFUNCTION, write_callback); - - if(!multi_handle) - multi_handle = curl_multi_init(); - - curl_multi_add_handle(multi_handle, file->handle.curl); - - /* lets start the fetch */ - curl_multi_perform(multi_handle, &file->still_running); - - if((file->buffer_pos == 0) && (!file->still_running)) { - /* if still_running is 0 now, we should return NULL */ - - /* make sure the easy handle is not in the multi handle anymore */ - curl_multi_remove_handle(multi_handle, file->handle.curl); - - /* cleanup */ - curl_easy_cleanup(file->handle.curl); - - free(file); - - file = NULL; - } - } - return file; -} - -int url_fclose(URL_FILE *file) { - int ret=0;/* default is good return */ - - switch(file->type) { - case CFTYPE_FILE: - ret=fclose(file->handle.file); /* passthrough */ - break; - - case CFTYPE_CURL: - /* make sure the easy handle is not in the multi handle anymore */ - curl_multi_remove_handle(multi_handle, file->handle.curl); - - /* cleanup */ - curl_easy_cleanup(file->handle.curl); - break; - - default: /* unknown or supported type - oh dear */ - ret=EOF; - errno=EBADF; - break; - } - - if(file->buffer) - free(file->buffer);/* free any allocated buffer space */ - - free(file); - - return ret; -} - -int url_feof(URL_FILE *file) { - int ret=0; - - switch(file->type) { - case CFTYPE_FILE: - ret=feof(file->handle.file); - break; - - case CFTYPE_CURL: - if((file->buffer_pos == 0) && (!file->still_running)) - ret = 1; - break; - - default: /* unknown or supported type - oh dear */ - ret=-1; - errno=EBADF; - break; - } - return ret; -} - -size_t url_fread(void *ptr, size_t size, size_t nmemb, URL_FILE *file) { - size_t want; - - switch(file->type) { - case CFTYPE_FILE: - want=fread(ptr,size,nmemb,file->handle.file); - break; - - case CFTYPE_CURL: - want = nmemb * size; - - fill_buffer(file,want); - - /* check if theres data in the buffer - if not fill_buffer() - * either errored or EOF */ - if(!file->buffer_pos) - return 0; - - /* ensure only available data is considered */ - if(file->buffer_pos < want) - want = file->buffer_pos; - - /* xfer data to caller */ - memcpy(ptr, file->buffer, want); - - use_buffer(file,want); - - want = want / size; /* number of items */ - break; - - default: /* unknown or supported type - oh dear */ - want=0; - errno=EBADF; - break; - - } - return want; -} - -char *url_fgets(char *ptr, size_t size, URL_FILE *file) { - size_t want = size - 1;/* always need to leave room for zero termination */ - size_t loop; - - switch(file->type) { - case CFTYPE_FILE: - ptr = fgets(ptr,size,file->handle.file); - break; - - case CFTYPE_CURL: - fill_buffer(file,want); - - /* check if theres data in the buffer - if not fill either errored or - * EOF */ - if(!file->buffer_pos) - return NULL; - - /* ensure only available data is considered */ - if(file->buffer_pos < want) - want = file->buffer_pos; - - /*buffer contains data */ - /* look for newline or eof */ - for(loop=0; loop < want; loop++) { - if(file->buffer[loop] == '\n') { - want=loop+1;/* include newline */ - break; - } - } - - /* xfer data to caller */ - memcpy(ptr, file->buffer, want); - ptr[want]=0;/* allways null terminate */ - - use_buffer(file,want); - - break; - - default: /* unknown or supported type - oh dear */ - ptr=NULL; - errno=EBADF; - break; - } - - return ptr;/*success */ -} - -void url_rewind(URL_FILE *file) { - switch(file->type) { - case CFTYPE_FILE: - rewind(file->handle.file); /* passthrough */ - break; - - case CFTYPE_CURL: - /* halt transaction */ - curl_multi_remove_handle(multi_handle, file->handle.curl); - - /* restart */ - curl_multi_add_handle(multi_handle, file->handle.curl); - - /* ditch buffer - write will recreate - resets stream pos*/ - if(file->buffer) - free(file->buffer); - - file->buffer=NULL; - file->buffer_pos=0; - file->buffer_len=0; - - break; - - default: /* unknown or supported type - oh dear */ - break; - } -} - -}
\ No newline at end of file diff --git a/src/uscxml/URL.h.old b/src/uscxml/URL.h.old deleted file mode 100644 index 9ff24c5..0000000 --- a/src/uscxml/URL.h.old +++ /dev/null @@ -1,154 +0,0 @@ -#ifndef URL_H_27HPRH76 -#define URL_H_27HPRH76 - -#include <string> -#include <sstream> -#include <curl/curl.h> - -// use arabica URL parser -#include <io/uri.hpp> - -#include <boost/shared_ptr.hpp> -#include <boost/enable_shared_from_this.hpp> - -namespace uscxml { - -class URLImpl : public boost::enable_shared_from_this<URLImpl> { -public: - URLImpl() {} - URLImpl(const std::string uri) : _uri(uri) {} - virtual ~URLImpl(); - const bool toAbsoluteCwd(); - const bool toAbsolute(const std::string& baseUrl); - const std::string asLocalFile(const std::string& suffix, bool reload = false); - - static boost::shared_ptr<URLImpl> toLocalFile(const std::string& content, const std::string& suffix); - - const bool isAbsolute() const { - return _uri.is_absolute(); - } - const std::string scheme() const { - return _uri.scheme(); - } - const std::string host() const { - return _uri.host(); - } - const std::string port() const { - return _uri.port(); - } - const std::string path() const { - return _uri.path(); - } - const std::string asString() const { - return _uri.as_string(); - } - -private: - std::string getLocalFilename(const std::string& suffix); - - Arabica::io::URI _uri; - std::string _localFile; -}; - -class URL { -public: - URL() : _impl() {} - URL(const std::string uri) : _impl(new URLImpl(uri)) {} - URL(boost::shared_ptr<URLImpl> const impl) : _impl(impl) { } - URL(const URL& other) : _impl(other._impl) { } - virtual ~URL() {}; - - static URL toLocalFile(const std::string& content, const std::string& suffix) { - boost::shared_ptr<URLImpl> impl = URLImpl::toLocalFile(content, suffix); - return URL(impl); - } - - operator bool() const { - return _impl; - } - bool operator< (const URL& other) const { - return _impl < other._impl; - } - bool operator==(const URL& other) const { - return _impl == other._impl; - } - bool operator!=(const URL& other) const { - return _impl != other._impl; - } - URL& operator= (const URL& other) { - _impl = other._impl; - return *this; - } - - const bool toAbsoluteCwd() { - return _impl->toAbsoluteCwd(); - } - const bool toAbsolute(const std::string& baseUrl) { - return _impl->toAbsolute(baseUrl); - } - const bool toAbsolute(const URL& baseUrl) { - return _impl->toAbsolute(baseUrl.asString()); - } - const std::string asLocalFile(const std::string& suffix, bool reload = false) { - return _impl->asLocalFile(suffix, reload); - } - - const bool isAbsolute() const { - return _impl->isAbsolute(); - } - const std::string scheme() const { - return _impl->scheme(); - } - const std::string host() const { - return _impl->host(); - } - const std::string port() const { - return _impl->port(); - } - const std::string path() const { - return _impl->path(); - } - const std::string asString() const { - return _impl->asString(); - } - - friend std::ostream & operator<<(std::ostream &stream, const URL& p); - -protected: - boost::shared_ptr<URLImpl> _impl; -}; - -enum fcurl_type_e { - CFTYPE_NONE=0, - CFTYPE_FILE=1, - CFTYPE_CURL=2 -}; - -struct fcurl_data { - enum fcurl_type_e type; /* type of handle */ - union { - CURL *curl; - FILE *file; - } handle; /* handle */ - - char *buffer; /* buffer to store cached data*/ - size_t buffer_len; /* currently allocated buffers length */ - size_t buffer_pos; /* end of data in buffer*/ - int still_running; /* Is background url fetch still in progress */ -}; - -typedef struct fcurl_data URL_FILE; - -URL_FILE *url_fopen(const char *url,const char *operation); -int url_fclose(URL_FILE *file); -int url_feof(URL_FILE *file); -size_t url_fread(void *ptr, size_t size, size_t nmemb, URL_FILE *file); -char * url_fgets(char *ptr, size_t size, URL_FILE *file); -void url_rewind(URL_FILE *file); - -std::ostream & operator<<(std::ostream &stream, const URL& url); - -} - - -#endif /* end of include guard: URL_H_27HPRH76 */ diff --git a/src/uscxml/concurrency/BlockingQueue.h b/src/uscxml/concurrency/BlockingQueue.h index f318ccf..0f4c965 100644 --- a/src/uscxml/concurrency/BlockingQueue.h +++ b/src/uscxml/concurrency/BlockingQueue.h @@ -14,12 +14,18 @@ public: virtual ~BlockingQueue() { } - virtual void push(T& elem) { + virtual void push(const T& elem) { tthread::lock_guard<tthread::mutex> lock(_mutex); _queue.push_back(elem); _cond.notify_all(); } + virtual void push_front(const T& elem) { + tthread::lock_guard<tthread::mutex> lock(_mutex); + _queue.push_front(elem); + _cond.notify_all(); + } + virtual T pop() { tthread::lock_guard<tthread::mutex> lock(_mutex); while (_queue.empty()) { diff --git a/src/uscxml/concurrency/tinythread.cpp b/src/uscxml/concurrency/tinythread.cpp index 66e73c1..6167545 100644 --- a/src/uscxml/concurrency/tinythread.cpp +++ b/src/uscxml/concurrency/tinythread.cpp @@ -295,4 +295,24 @@ thread::id this_thread::get_id() { #endif } +namespace chrono { +namespace system_clock { +uint64_t now() { + uint64_t time = 0; +#ifdef _WIN32 + FILETIME tv; + GetSystemTimeAsFileTime(&tv); + time = (((uint64_t) tv.dwHighDateTime) << 32) + tv.dwLowDateTime; + time /= 10000; +#else + struct timeval tv; + gettimeofday(&tv, NULL); + time += tv.tv_sec * 1000; + time += tv.tv_usec / 1000; +#endif + return time; +} +} +} + } diff --git a/src/uscxml/concurrency/tinythread.h b/src/uscxml/concurrency/tinythread.h index d824067..9dd2fcb 100644 --- a/src/uscxml/concurrency/tinythread.h +++ b/src/uscxml/concurrency/tinythread.h @@ -646,6 +646,11 @@ typedef duration<__intmax_t, ratio<1, 1000> > milliseconds; ///< Duration w typedef duration<__intmax_t> seconds; ///< Duration with the unit seconds. typedef duration<__intmax_t, ratio<60> > minutes; ///< Duration with the unit minutes. typedef duration<__intmax_t, ratio<3600> > hours; ///< Duration with the unit hours. + +namespace system_clock { +uint64_t now(); +} + } /// The namespace @c this_thread provides methods for dealing with the diff --git a/src/uscxml/plugins/element/postpone/PostponeElement.cpp b/src/uscxml/plugins/element/postpone/PostponeElement.cpp new file mode 100644 index 0000000..644cb1d --- /dev/null +++ b/src/uscxml/plugins/element/postpone/PostponeElement.cpp @@ -0,0 +1,97 @@ +#include "PostponeElement.h" +#include "uscxml/plugins/invoker/http/HTTPServletInvoker.h" +#include <glog/logging.h> + +#ifdef BUILD_AS_PLUGINS +#include <Pluma/Connector.hpp> +#endif + +namespace uscxml { + +#ifdef BUILD_AS_PLUGINS +PLUMA_CONNECTOR +bool connect(pluma::Host& host) { + host.add( new PostponeElementProvider() ); + return true; +} +#endif + +boost::shared_ptr<ExecutableContentImpl> PostponeElement::create(Interpreter* interpreter) { + boost::shared_ptr<PostponeElement> invoker = boost::shared_ptr<PostponeElement>(new PostponeElement()); + invoker->_interpreter = interpreter; + return invoker; +} + +void PostponeElement::enterElement(const Arabica::DOM::Node<std::string>& node) { + if (!_interpreter->getDataModel()) { + LOG(ERROR) << "Postpone element requires a datamodel"; + return; + } + + // under which condition will we postpone the current event? + if (HAS_ATTR(node, "cond")) { + std::string cond = ATTR(node, "cond"); + try { + if (!_interpreter->getDataModel().evalAsBool(cond)) + return; + } catch (Event e) { + LOG(ERROR) << "Syntax error in cond attribute of postpone element:" << std::endl << e << std::endl; + return; + } + } + + // when will we refire the event? + if (!HAS_ATTR(node, "until")) { + LOG(ERROR) << "Postpone element requires until attribute "; + return; + } + std::string until = ATTR(node, "until"); + + Event currEvent = _interpreter->getCurrentEvent(); + Resubmitter::postpone(currEvent, until, _interpreter); +} + +void PostponeElement::exitElement(const Arabica::DOM::Node<std::string>& node) { +} + +void PostponeElement::Resubmitter::postpone(const Event& event, std::string until, Interpreter* interpreter) { + Resubmitter* resubmitter = getInstance(interpreter); + resubmitter->_postponedEvents.push_back(std::make_pair(until, event)); +} + +void PostponeElement::Resubmitter::onStableConfiguration(Interpreter* interpreter) { + std::list<std::pair<std::string, Event> >::iterator eventIter = _postponedEvents.begin(); + while(eventIter != _postponedEvents.end()) { + try { + if (interpreter->getDataModel().evalAsBool(eventIter->first)) { + interpreter->receive(eventIter->second, true); + _postponedEvents.erase(eventIter); + break; + } + } catch (Event e) { + LOG(ERROR) << "Syntax error in until attribute of postpone element:" << std::endl << e << std::endl; + _postponedEvents.erase(eventIter++); + continue; + } + eventIter++; + } +} + +void PostponeElement::Resubmitter::afterCompletion(Interpreter* interpreter) { + tthread::lock_guard<tthread::recursive_mutex> lock(PostponeElement::Resubmitter::_accessLock); + _instances.erase(interpreter); + delete this; // committing suicide is ok if we are careful +} + +std::map<Interpreter*, PostponeElement::Resubmitter*> PostponeElement::Resubmitter::_instances; +tthread::recursive_mutex PostponeElement::Resubmitter::_accessLock; + +PostponeElement::Resubmitter* PostponeElement::Resubmitter::getInstance(Interpreter* interpreter) { + tthread::lock_guard<tthread::recursive_mutex> lock(PostponeElement::Resubmitter::_accessLock); + if (_instances.find(interpreter) == _instances.end()) { + _instances[interpreter] = new Resubmitter(interpreter); + } + return _instances[interpreter]; +} + +}
\ No newline at end of file diff --git a/src/uscxml/plugins/element/postpone/PostponeElement.h b/src/uscxml/plugins/element/postpone/PostponeElement.h new file mode 100644 index 0000000..03aafde --- /dev/null +++ b/src/uscxml/plugins/element/postpone/PostponeElement.h @@ -0,0 +1,62 @@ +#ifndef POSTPONEELEMENT_H_WN8EIYYI +#define POSTPONEELEMENT_H_WN8EIYYI + +#include <uscxml/Interpreter.h> +#include <list> + +#ifdef BUILD_AS_PLUGINS +#include "uscxml/plugins/Plugins.h" +#endif + +namespace uscxml { + +class PostponeElement : public ExecutableContentImpl { +public: + PostponeElement() {} + virtual ~PostponeElement() {} + boost::shared_ptr<ExecutableContentImpl> create(Interpreter* interpreter); + + std::string getLocalName() { + return "postpone"; + } + + std::string getNamespace() { + return "http://www.w3.org/2005/07/scxml"; + } + + bool processChildren() { + return false; + } + + void enterElement(const Arabica::DOM::Node<std::string>& node); + void exitElement(const Arabica::DOM::Node<std::string>& node); + +protected: + // once per interpreter + class Resubmitter : public InterpreterMonitor { + public: + Resubmitter(Interpreter* interpreter) { + interpreter->addMonitor(this); + } + + static Resubmitter* getInstance(Interpreter* interpreter); + static void postpone(const Event& event, std::string until, Interpreter* interpreter); + + // InterpreterMonitor + void onStableConfiguration(Interpreter* interpreter); + void afterCompletion(Interpreter* interpreter); + + std::list<std::pair<std::string, Event> > _postponedEvents; + static std::map<Interpreter*, Resubmitter*> _instances; + static tthread::recursive_mutex _accessLock; + + }; +}; + +#ifdef BUILD_AS_PLUGINS +PLUMA_INHERIT_PROVIDER(PostponeElement, Element); +#endif + +} + +#endif /* end of include guard: POSTPONEELEMENT_H_WN8EIYYI */ diff --git a/src/uscxml/plugins/element/response/ResponseElement.cpp b/src/uscxml/plugins/element/response/ResponseElement.cpp index 814f726..7f1a479 100644 --- a/src/uscxml/plugins/element/response/ResponseElement.cpp +++ b/src/uscxml/plugins/element/response/ResponseElement.cpp @@ -23,6 +23,7 @@ boost::shared_ptr<ExecutableContentImpl> ResponseElement::create(Interpreter* in } void ResponseElement::enterElement(const Arabica::DOM::Node<std::string>& node) { + // try to get the request id if (!HAS_ATTR(node, "request") && !HAS_ATTR(node, "requestexpr")) { LOG(ERROR) << "Response element requires request or requestexpr"; return; @@ -31,12 +32,9 @@ void ResponseElement::enterElement(const Arabica::DOM::Node<std::string>& node) LOG(ERROR) << "Response element with requestexpr requires datamodel"; return; } - if (HAS_ATTR(node, "close")) { - - } - std::string requestId = (HAS_ATTR(node, "request") ? ATTR(node, "request") : _interpreter->getDataModel().evalAsString(ATTR(node, "requestexpr"))); + // try to get the request object HTTPServletInvoker* servlet = _interpreter->getHTTPServlet(); tthread::lock_guard<tthread::recursive_mutex> lock(servlet->getMutex()); @@ -44,19 +42,39 @@ void ResponseElement::enterElement(const Arabica::DOM::Node<std::string>& node) LOG(ERROR) << "No matching HTTP request for response element"; return; } + HTTPServer::Request httpReq = servlet->getRequests()[requestId]; + HTTPServer::Reply httpReply(httpReq); + // get the status or default to 200 std::string statusStr = (HAS_ATTR(node, "status") ? ATTR(node, "status") : "200"); if (!isNumeric(statusStr.c_str(), 10)) { LOG(ERROR) << "Response element with non-numeric status " << statusStr; return; } - int status = strTo<int>(statusStr); - - HTTPServer::Request httpReq = servlet->getRequests()[requestId]; - - HTTPServer::Reply httpReply(httpReq); - httpReply.status = status; + httpReply.status = strTo<int>(statusStr);; + + // extract the content + Arabica::XPath::NodeSet<std::string> contents = Interpreter::filterChildElements(_interpreter->getXMLPrefixForNS(getNamespace()) + "content", node); + if (contents.size() > 0) { + if (HAS_ATTR(contents[0], "expr")) { + if (_interpreter->getDataModel()) { + try { + std::string contentValue = _interpreter->getDataModel().evalAsString(ATTR(contents[0], "expr")); + httpReply.content = contentValue; + } catch (Event e) { + LOG(ERROR) << "Syntax error with expr in content child of response element:" << std::endl << e << std::endl; + } + } else { + LOG(ERROR) << "content element has expr attribute but no datamodel is specified."; + } + } else if (contents[0].hasChildNodes()) { + httpReply.content = contents[0].getFirstChild().getNodeValue(); + } else { + LOG(ERROR) << "content element does not specify any content."; + } + } + // send the reply HTTPServer::reply(httpReply); servlet->getRequests().erase(requestId); } diff --git a/src/uscxml/plugins/invoker/ffmpeg/FFMPEGInvoker.cpp b/src/uscxml/plugins/invoker/ffmpeg/FFMPEGInvoker.cpp new file mode 100644 index 0000000..05af363 --- /dev/null +++ b/src/uscxml/plugins/invoker/ffmpeg/FFMPEGInvoker.cpp @@ -0,0 +1,48 @@ +#include "FFMPEGInvoker.h" +#include <glog/logging.h> + +#include <libavcodec/avcodec.h> +#include <libavformat/avformat.h> + +#ifdef BUILD_AS_PLUGINS +#include <Pluma/Connector.hpp> +#endif + +namespace uscxml { + +#ifdef BUILD_AS_PLUGINS +PLUMA_CONNECTOR +bool connect(pluma::Host& host) { + host.add( new FFMPEGInvokerProvider() ); + return true; +} +#endif + +FFMPEGInvoker::FFMPEGInvoker() { +} + +FFMPEGInvoker::~FFMPEGInvoker() { +}; + +boost::shared_ptr<IOProcessorImpl> FFMPEGInvoker::create(Interpreter* interpreter) { + boost::shared_ptr<FFMPEGInvoker> invoker = boost::shared_ptr<FFMPEGInvoker>(new FFMPEGInvoker()); + invoker->_interpreter = interpreter; + return invoker; +} + +Data FFMPEGInvoker::getDataModelVariables() { + Data data; + return data; +} + +void FFMPEGInvoker::send(const SendRequest& req) { +} + +void FFMPEGInvoker::cancel(const std::string sendId) { +} + +void FFMPEGInvoker::invoke(const InvokeRequest& req) { +// AVIOContext* avCtx = avio_alloc_context(); +} + +}
\ No newline at end of file diff --git a/src/uscxml/plugins/invoker/ffmpeg/FFMPEGInvoker.h b/src/uscxml/plugins/invoker/ffmpeg/FFMPEGInvoker.h new file mode 100644 index 0000000..9b1b0ca --- /dev/null +++ b/src/uscxml/plugins/invoker/ffmpeg/FFMPEGInvoker.h @@ -0,0 +1,40 @@ +#ifndef FFMPEGINVOKER_H_VQD1V1C2 +#define FFMPEGINVOKER_H_VQD1V1C2 + +#include <uscxml/Interpreter.h> + +#ifdef BUILD_AS_PLUGINS +#include "uscxml/plugins/Plugins.h" +#endif + +namespace uscxml { + +class FFMPEGInvoker : public InvokerImpl { +public: + FFMPEGInvoker(); + virtual ~FFMPEGInvoker(); + virtual boost::shared_ptr<IOProcessorImpl> create(Interpreter* interpreter); + + virtual std::set<std::string> getNames() { + std::set<std::string> names; + names.insert("ffmpeg"); + names.insert("http://uscxml.tk.informatik.tu-darmstadt.de/#ffmpeg"); + return names; + } + + virtual Data getDataModelVariables(); + virtual void send(const SendRequest& req); + virtual void cancel(const std::string sendId); + virtual void invoke(const InvokeRequest& req); + +protected: +}; + +#ifdef BUILD_AS_PLUGINS +PLUMA_INHERIT_PROVIDER(FFMPEGInvoker, Invoker); +#endif + +} + + +#endif /* end of include guard: FFMPEGINVOKER_H_VQD1V1C2 */ diff --git a/src/uscxml/plugins/invoker/filesystem/dirmon/DirMonInvoker.cpp b/src/uscxml/plugins/invoker/filesystem/dirmon/DirMonInvoker.cpp index 9486de3..e34517d 100644 --- a/src/uscxml/plugins/invoker/filesystem/dirmon/DirMonInvoker.cpp +++ b/src/uscxml/plugins/invoker/filesystem/dirmon/DirMonInvoker.cpp @@ -1,6 +1,8 @@ #include "DirMonInvoker.h" #include <glog/logging.h> +#include "uscxml/config.h" + #ifdef BUILD_AS_PLUGINS #include <Pluma/Connector.hpp> #endif @@ -111,13 +113,38 @@ void DirMonInvoker::handleFileAction(FW::WatchID watchid, const FW::String& dir, case FW::Actions::Modified: event.name = "file.modified"; break; - default: break; } - event.data.compound["file"].compound["name"] = Data(filename, Data::VERBATIM); - event.data.compound["file"].compound["dir"] = Data(dir, Data::VERBATIM); + std::string basename; + size_t lastSep; + if ((lastSep = filename.find_last_of(PATH_SEPERATOR)) != std::string::npos) { + lastSep++; + basename = filename.substr(lastSep, filename.length() - lastSep); + } else { + basename = filename; + } + + std::string extension; + size_t lastDot; + if ((lastDot = basename.find_last_of(".")) != std::string::npos) { + lastDot++; + extension = basename.substr(lastDot, basename.length() - lastDot); + } + + std::string relPath; + if (boost::algorithm::starts_with(filename, dir)) { + relPath = filename.substr(dir.length()); + } else { + relPath = filename; + } + + event.data.compound["file"].compound["name"] = Data(basename, Data::VERBATIM); + event.data.compound["file"].compound["path"] = Data(filename, Data::VERBATIM); + event.data.compound["file"].compound["relPath"] = Data(relPath, Data::VERBATIM); + event.data.compound["file"].compound["dir"] = Data(dir, Data::VERBATIM); + event.data.compound["file"].compound["extension"] = Data(extension, Data::VERBATIM); event.data.compound["file"].compound["mtime"] = toStr(fileStat.st_mtime); event.data.compound["file"].compound["ctime"] = toStr(fileStat.st_ctime); diff --git a/src/uscxml/plugins/invoker/graphics/openscenegraph/OSGConverter.cpp b/src/uscxml/plugins/invoker/graphics/openscenegraph/OSGConverter.cpp index 49c9ccb..726cd08 100644 --- a/src/uscxml/plugins/invoker/graphics/openscenegraph/OSGConverter.cpp +++ b/src/uscxml/plugins/invoker/graphics/openscenegraph/OSGConverter.cpp @@ -1,5 +1,12 @@ #include "OSGConverter.h" #include <glog/logging.h> +#include "uscxml/config.h" + +#include <osg/MatrixTransform> +#include <osgDB/ReadFile> +#include <osgDB/WriteFile> +#include <osgDB/Registry> +#include <osgGA/TrackballManipulator> #ifdef BUILD_AS_PLUGINS #include <Pluma/Connector.hpp> @@ -15,10 +22,15 @@ bool connect(pluma::Host& host) { } #endif -OSGConverter::OSGConverter() { +OSGConverter::OSGConverter() : _isRunning(false) { } OSGConverter::~OSGConverter() { + _isRunning = false; + std::set<tthread::thread*>::iterator threadIter = _threads.begin(); + while(threadIter != _threads.end()) { + (*threadIter)->join(); + } }; boost::shared_ptr<IOProcessorImpl> OSGConverter::create(Interpreter* interpreter) { @@ -33,16 +45,294 @@ Data OSGConverter::getDataModelVariables() { } void OSGConverter::send(const SendRequest& req) { - std::cout << req << std::endl; - Event event; - event.name = "error"; - returnEvent(event); + + /** + * we have to resolve all datamodel dependent strings first as + * we cannot access the datamodel from within another thread without locking + */ + + // make a copy + SendRequest actualReq(req); + + if (actualReq.params.find("source") == actualReq.params.end()) { + // no explicit source + if (actualReq.params.find("sourceexpr") != actualReq.params.end() && _interpreter->getDataModel()) { + actualReq.params.insert(std::make_pair("source", _interpreter->getDataModel().evalAsString(actualReq.params.find("sourceexpr")->second))); + } else { + LOG(ERROR) << "SendRequests for osginvoker missing source or sourceExpr and datamodel"; + return; + } + } + + if (actualReq.params.find("dest") == actualReq.params.end()) { + // no explicit destination + if (actualReq.params.find("destexpr") != actualReq.params.end() && _interpreter->getDataModel()) { + actualReq.params.insert(std::make_pair("dest", _interpreter->getDataModel().evalAsString(actualReq.params.find("destexpr")->second))); + } else { + LOG(ERROR) << "SendRequests for osginvoker missing dest or destExpr and datamodel"; + return; + } + } + + if (actualReq.params.find("format") == actualReq.params.end()) { + // no explicit format + if (actualReq.params.find("formatexpr") != actualReq.params.end() && _interpreter->getDataModel()) { + actualReq.params.insert(std::make_pair("format", _interpreter->getDataModel().evalAsString(actualReq.params.find("formatexpr")->second))); + } else { + std::string format; + size_t lastDot; + std::string dest = req.params.find("dest")->second; + if ((lastDot = dest.find_last_of(".")) != std::string::npos) { + lastDot++; + format = dest.substr(lastDot, dest.length() - lastDot); + } + if (format.length() == 0 || format.find_last_of(PATH_SEPERATOR) != std::string::npos) { + // empty format or pathseperator in format + format = "png"; + } + actualReq.params.insert(std::make_pair("format", format)); + } + } + + if (actualReq.params.find("height") == actualReq.params.end()) { + // no explicit height + if (actualReq.params.find("heightexpr") != actualReq.params.end() && _interpreter->getDataModel()) { + actualReq.params.insert(std::make_pair("height", _interpreter->getDataModel().evalAsString(actualReq.params.find("heightexpr")->second))); + } + } + + if (actualReq.params.find("width") == actualReq.params.end()) { + // no explicit width + if (actualReq.params.find("widthexpr") != actualReq.params.end() && _interpreter->getDataModel()) { + actualReq.params.insert(std::make_pair("width", _interpreter->getDataModel().evalAsString(actualReq.params.find("widthexpr")->second))); + } + } + + _workQueue.push(actualReq); } void OSGConverter::cancel(const std::string sendId) { } void OSGConverter::invoke(const InvokeRequest& req) { + int nrThreads = 1; + if (req.params.find("threads") != req.params.end() && isNumeric(req.params.find("threads")->second.c_str(), 10)) { + nrThreads = strTo<int>(req.params.find("threads")->second); + } + + _isRunning = true; + for (int i = 0; i < nrThreads; i++) { + _threads.insert(new tthread::thread(OSGConverter::run, this)); + } +} + +void OSGConverter::run(void* instance) { + OSGConverter* INSTANCE = (OSGConverter*)instance; + while(true) { + SendRequest req = INSTANCE->_workQueue.pop(); + if (INSTANCE->_isRunning) { + INSTANCE->process(req); + } else { + return; + } + } +} + +void OSGConverter::process(const SendRequest& req) { + + int width = (req.params.find("width") != req.params.end() ? strTo<int>(req.params.find("width")->second) : 640); + int height = (req.params.find("height") != req.params.end() ? strTo<int>(req.params.find("height")->second) : 480); + + assert(req.params.find("source") != req.params.end()); + assert(req.params.find("dest") != req.params.end()); + assert(req.params.find("format") != req.params.end()); + + std::string source = req.params.find("source")->second; + std::string dest = req.params.find("dest")->second; + std::string format = req.params.find("format")->second; + + osg::ref_ptr<osg::Node> sceneGraph = setupGraph(source); + + osgDB::ReaderWriter::WriteResult result; + if (osgDB::Registry::instance()->getReaderWriterForExtension(format) != NULL) { + // write as another 3D file + result = osgDB::Registry::instance()->writeNode(*sceneGraph, dest, osgDB::Registry::instance()->getOptions()); + } + + if (result.error()) { + // make a screenshot + osgViewer::ScreenCaptureHandler::CaptureOperation* cOp = new NameRespectingWriteToFile(dest, + format, + osgViewer::ScreenCaptureHandler::WriteToFile::OVERWRITE + ); + + osgViewer::ScreenCaptureHandler* captureHandler = new osgViewer::ScreenCaptureHandler(cOp, -1); + + osgViewer::Viewer viewer; + viewer.setSceneData(sceneGraph); + viewer.setCameraManipulator(new osgGA::TrackballManipulator()); + viewer.addEventHandler(captureHandler); + captureHandler->startCapture(); + + osg::DisplaySettings* ds = osg::DisplaySettings::instance().get(); + osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits(ds); + traits->width = width; + traits->height = height; + traits->pbuffer = true; + osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get()); + GLenum pbuffer = gc->getTraits()->doubleBuffer ? GL_BACK : GL_FRONT; + + viewer.getCamera()->setGraphicsContext(gc.get()); + viewer.getCamera()->setViewport(new osg::Viewport(0,0,traits->width,traits->height)); + viewer.getCamera()->setDrawBuffer(pbuffer); + viewer.getCamera()->setReadBuffer(pbuffer); + + // set background color + viewer.getCamera()->setClearColor(osg::Vec4f(1.0f,1.0f,1.0f,1.0f)); + viewer.getCamera()->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + ((osg::MatrixTransform*)sceneGraph.get())->setMatrix(requestToModelPose(req)); + viewer.getCamera()->setViewMatrix(requestToCamPose(req)); + +// viewer.home(); + + // perform one viewer iteration + viewer.realize(); + viewer.frame(); + } +} + +osg::Matrix OSGConverter::requestToModelPose(const SendRequest& req) { + double pitch = (req.params.find("pitch") != req.params.end() ? strTo<int>(req.params.find("pitch")->second) : 0); + double roll = (req.params.find("roll") != req.params.end() ? strTo<int>(req.params.find("roll")->second) : 0); + double yaw = (req.params.find("yaw") != req.params.end() ? strTo<int>(req.params.find("yaw")->second) : 0); + + return eulerToMatrix(pitch, roll, yaw); +// osg::Matrix m; +// m.makeIdentity(); +// return m; +} + +osg::Matrix OSGConverter::requestToCamPose(const SendRequest& req) { + return eulerToMatrix(0, 0, 0); +} + +osg::ref_ptr<osg::Node> OSGConverter::setupGraph(const std::string filename) { + + /** + * root (model pose) + * - modelCenter (center model) + * - model (actual model) + */ + + long now = tthread::chrono::system_clock::now(); + + { + // get some privacy + tthread::lock_guard<tthread::recursive_mutex> lock(_cacheMutex); + + // do we have it in the cache? + if (_models.find(filename) == _models.end()) { + osg::ref_ptr<osg::Node> model = osgDB::readNodeFile(filename); + if (!model.valid()) { + LOG(ERROR) << "Cannot load model from " << filename; + return new osg::MatrixTransform(); + } + _models[filename] = std::make_pair(now, model); + } + + // remove old models from cache + std::map<std::string, std::pair<long, osg::ref_ptr<osg::Node> > >::iterator modelIter = _models.begin(); + while(modelIter != _models.end()) { + // delete every model unused for 1 minutes + if (now - modelIter->second.first > 60000) { + _models.erase(modelIter++); + } else { + modelIter++; + } + } + } + + osg::ref_ptr<osg::MatrixTransform> root = new osg::MatrixTransform(); + + osg::ref_ptr<osg::Node> model = _models[filename].second; + _models[filename].first = now; + + // translation matrix to move model into center + osg::ref_ptr<osg::MatrixTransform> modelCenter = new osg::MatrixTransform(); + modelCenter->addChild(model); + + // move bounding sphere center into origin + osg::BoundingSphere bs = model->getBound(); + modelCenter->setMatrix(osg::Matrix::translate(bs.center() *= -1)); + + // add to model pose matrix + root->addChild(modelCenter); + + return root; +} + +osg::Matrix OSGConverter::eulerToMatrix(double pitch, double roll, double yaw) { + // see http://www.flipcode.com/documents/matrfaq.html#Q36 + osg::Matrix m; + m.makeIdentity(); + + double A = cos(pitch); + double B = sin(pitch); + double C = cos(roll); + double D = sin(roll); + double E = cos(yaw); + double F = sin(yaw); + + double AD = A * D; + double BD = B * D; + + m(0,0) = C * E; + m(0,1) = -C * F; + m(0,2) = -D; + m(1,0) = -BD * E + A * F; + m(1,1) = BD * F + A * E; + m(1,2) = -B * C; + m(2,0) = AD * E + B * F; + m(2,1) = -AD * F + B * E; + m(2,2) = A * C; + + m(0,3) = m(1,3) = m(2,3) = m(3,0) = m(3,1) = m(3,2) = 0; + m(3,3) = 1; + + return m; +} + +void OSGConverter::matrixToEuler(const osg::Matrix& m, double& pitch, double& roll, double& yaw) { + // see: http://www.flipcode.com/documents/matrfaq.html#Q37 + double angle_x, angle_z; + double D = -1 * asin(m(0,2)); /* Calculate Y-axis angle */ + double angle_y = D; + double C = cos(angle_y); + + /* Gimball lock? */ + if ( fabs( C ) > 0.005 ) { + double tr_x = m(2,2) / C; /* No, so get X-axis angle */ + double tr_y = -1 * m(1,2) / C; + angle_x = atan2( tr_y, tr_x ); + tr_x = m(0,0) / C; /* Get Z-axis angle */ + tr_y = -1 * m(0,1) / C; + angle_z = atan2( tr_y, tr_x ); + } else { + /* Gimball lock has occurred */ + angle_x = 0; /* Set X-axis angle to zero */ + double tr_x = m(1,1); /* And calculate Z-axis angle */ + double tr_y = m(1,0); + angle_z = atan2( tr_y, tr_x ); + } + + pitch = fmod(angle_x, 2 * M_PI ); /* Clamp all angles to range */ + roll = fmod( angle_y, 2 * M_PI ); + yaw = fmod( angle_z, 2 * M_PI ); +} + +void OSGConverter::NameRespectingWriteToFile::operator()(const osg::Image& image, const unsigned int context_id) { + osgDB::writeImageFile(image, _filename); } }
\ No newline at end of file diff --git a/src/uscxml/plugins/invoker/graphics/openscenegraph/OSGConverter.h b/src/uscxml/plugins/invoker/graphics/openscenegraph/OSGConverter.h index c52b1ee..e96a4e9 100644 --- a/src/uscxml/plugins/invoker/graphics/openscenegraph/OSGConverter.h +++ b/src/uscxml/plugins/invoker/graphics/openscenegraph/OSGConverter.h @@ -2,6 +2,8 @@ #define OSGCONVERTER_H_W09J90F0 #include <uscxml/Interpreter.h> +#include <osg/Node> +#include <osgViewer/ViewerEventHandlers> #ifdef BUILD_AS_PLUGINS #include "uscxml/plugins/Plugins.h" @@ -29,7 +31,35 @@ public: virtual void cancel(const std::string sendId); virtual void invoke(const InvokeRequest& req); + osg::Matrix requestToModelPose(const SendRequest& req); + osg::Matrix requestToCamPose(const SendRequest& req); + + static osg::Matrix eulerToMatrix(double pitch, double roll, double yaw); + static void matrixToEuler(const osg::Matrix& m, double& pitch, double& roll, double& yaw); + protected: + class NameRespectingWriteToFile : public osgViewer::ScreenCaptureHandler::WriteToFile { + public: + NameRespectingWriteToFile(const std::string& filename, + const std::string& extension, + SavePolicy savePolicy) : osgViewer::ScreenCaptureHandler::WriteToFile(filename, extension, savePolicy) { + } + + virtual void operator()(const osg::Image& image, const unsigned int context_id); + }; + + uscxml::concurrency::BlockingQueue<SendRequest> _workQueue; + osg::ref_ptr<osg::Node> setupGraph(const std::string filename); + + std::map<std::string, std::pair<long, osg::ref_ptr<osg::Node> > > _models; + tthread::recursive_mutex _cacheMutex; + + std::set<tthread::thread*> _threads; + + static void run(void*); + void process(const SendRequest& req); + + bool _isRunning; }; #ifdef BUILD_AS_PLUGINS diff --git a/src/uscxml/plugins/invoker/http/HTTPServletInvoker.cpp b/src/uscxml/plugins/invoker/http/HTTPServletInvoker.cpp index 78e3bea..753877c 100644 --- a/src/uscxml/plugins/invoker/http/HTTPServletInvoker.cpp +++ b/src/uscxml/plugins/invoker/http/HTTPServletInvoker.cpp @@ -117,25 +117,16 @@ void HTTPServletInvoker::httpRecvRequest(const HTTPServer::Request& req) { _requests[toStr((uintptr_t)req.curlReq)] = req; - Event event; + Event event = req; if (_isInterpreterGlobal) { - event.name = "http." + req.type; + event.name = "http." + event.data.compound["type"].atom; event.origin = toStr((uintptr_t)req.curlReq); } else { event.name = _callback; event.data.compound["reqId"] = Data(toStr((uintptr_t)req.curlReq), Data::VERBATIM); } - std::map<std::string, std::string>::const_iterator headerIter = req.headers.begin(); - while(headerIter != req.headers.end()) { - event.data.compound["headers"].compound[headerIter->first] = Data(headerIter->second, Data::VERBATIM); - headerIter++; - } - - event.data.compound["content"] = Data(req.content, Data::VERBATIM); - event.data.compound["type"] = Data(req.type, Data::VERBATIM); - returnEvent(event); } diff --git a/src/uscxml/plugins/invoker/http/HTTPServletInvoker.h b/src/uscxml/plugins/invoker/http/HTTPServletInvoker.h index 4ac87e0..c9fe844 100644 --- a/src/uscxml/plugins/invoker/http/HTTPServletInvoker.h +++ b/src/uscxml/plugins/invoker/http/HTTPServletInvoker.h @@ -19,7 +19,7 @@ public: virtual std::set<std::string> getNames() { std::set<std::string> names; - names.insert("httpserver"); + names.insert("httpservlet"); names.insert("http://uscxml.tk.informatik.tu-darmstadt.de/#httpserver"); return names; } diff --git a/src/uscxml/plugins/invoker/scxml/USCXMLInvoker.cpp b/src/uscxml/plugins/invoker/scxml/USCXMLInvoker.cpp index 4f7d61d..b7d08c4 100644 --- a/src/uscxml/plugins/invoker/scxml/USCXMLInvoker.cpp +++ b/src/uscxml/plugins/invoker/scxml/USCXMLInvoker.cpp @@ -54,9 +54,10 @@ void USCXMLInvoker::invoke(const InvokeRequest& req) { } } -void USCXMLInvoker::push(Event& event) { - event.invokeid = _invokeId; - _parentInterpreter->receive(event); +void USCXMLInvoker::push(const Event& event) { + Event copyEvent(event); + copyEvent.invokeid = _invokeId; + _parentInterpreter->receive(copyEvent); } }
\ No newline at end of file diff --git a/src/uscxml/plugins/invoker/scxml/USCXMLInvoker.h b/src/uscxml/plugins/invoker/scxml/USCXMLInvoker.h index 792cc5d..aedef32 100644 --- a/src/uscxml/plugins/invoker/scxml/USCXMLInvoker.h +++ b/src/uscxml/plugins/invoker/scxml/USCXMLInvoker.h @@ -23,6 +23,7 @@ public: virtual boost::shared_ptr<IOProcessorImpl> create(Interpreter* interpreter); virtual std::set<std::string> getNames() { std::set<std::string> names; + names.insert("scxml"); names.insert("uscxml"); names.insert("http://www.w3.org/TR/scxml"); names.insert("http://www.w3.org/TR/scxml/"); @@ -34,7 +35,7 @@ public: virtual void cancel(const std::string sendId); virtual void invoke(const InvokeRequest& req); - virtual void push(Event& event); + virtual void push(const Event& event); protected: Interpreter* _invokedInterpreter; diff --git a/src/uscxml/plugins/ioprocessor/basichttp/libevent/EventIOProcessor.cpp b/src/uscxml/plugins/ioprocessor/basichttp/libevent/EventIOProcessor.cpp index 1b58785..6c7a8fc 100644 --- a/src/uscxml/plugins/ioprocessor/basichttp/libevent/EventIOProcessor.cpp +++ b/src/uscxml/plugins/ioprocessor/basichttp/libevent/EventIOProcessor.cpp @@ -67,10 +67,26 @@ Data EventIOProcessor::getDataModelVariables() { } void EventIOProcessor::httpRecvRequest(const HTTPServer::Request& req) { - Event reqEvent; + Event reqEvent = req; reqEvent.type = Event::EXTERNAL; bool scxmlStructFound = false; + if (reqEvent.data.compound["header"].compound.find("_scxmleventstruct") != reqEvent.data.compound["header"].compound.end()) { + // TODO: this looses all other information + reqEvent = Event::fromXML(evhttp_decode_uri(reqEvent.data.compound["header"].compound["_scxmleventstruct"].atom.c_str())); + scxmlStructFound = true; + } + if (reqEvent.data.compound["header"].compound.find("_scxmleventname") != reqEvent.data.compound["header"].compound.end()) { + reqEvent.name = evhttp_decode_uri(reqEvent.data.compound["header"].compound["_scxmleventname"].atom.c_str()); + } + + std::map<std::string, Data>::iterator headerIter = reqEvent.data.compound["header"].compound.begin(); + while(headerIter != reqEvent.data.compound["header"].compound.end()) { + reqEvent.data.compound[headerIter->first] = Data(evhttp_decode_uri(headerIter->second.atom.c_str()), Data::VERBATIM); + headerIter++; + } + +#if 0 std::map<std::string, std::string>::const_iterator headerIter = req.headers.begin(); while(headerIter != req.headers.end()) { if (boost::iequals("_scxmleventstruct", headerIter->first)) { @@ -84,6 +100,7 @@ void EventIOProcessor::httpRecvRequest(const HTTPServer::Request& req) { } headerIter++; } +#endif if (reqEvent.name.length() == 0) reqEvent.name = req.type; diff --git a/src/uscxml/server/HTTPServer.cpp b/src/uscxml/server/HTTPServer.cpp index fccc8a9..d8a6474 100644 --- a/src/uscxml/server/HTTPServer.cpp +++ b/src/uscxml/server/HTTPServer.cpp @@ -5,6 +5,7 @@ #include "uscxml/server/HTTPServer.h" #include "uscxml/Message.h" +#include "uscxml/Factory.h" #include <iostream> #include <event2/dns.h> #include <event2/event.h> @@ -14,7 +15,8 @@ #include <event2/http_struct.h> #include <event2/thread.h> -#include <string.h> +#include <string> +#include <iostream> #include <glog/logging.h> #include <boost/algorithm/string.hpp> @@ -39,6 +41,9 @@ HTTPServer::HTTPServer(unsigned short port) { _port++; } determineAddress(); + + // generic callback + evhttp_set_gencb(_http, HTTPServer::httpRecvReqCallback, NULL); } HTTPServer::~HTTPServer() { @@ -71,34 +76,34 @@ void HTTPServer::httpRecvReqCallback(struct evhttp_request *req, void *callbackD switch (evhttp_request_get_command(req)) { case EVHTTP_REQ_GET: - request.type = "GET"; + request.data.compound["type"] = Data("get", Data::VERBATIM); break; case EVHTTP_REQ_POST: - request.type = "POST"; + request.data.compound["type"] = Data("post", Data::VERBATIM); break; case EVHTTP_REQ_HEAD: - request.type = "HEAD"; + request.data.compound["type"] = Data("head", Data::VERBATIM); break; case EVHTTP_REQ_PUT: - request.type = "PUT"; + request.data.compound["type"] = Data("put", Data::VERBATIM); break; case EVHTTP_REQ_DELETE: - request.type = "DELETE"; + request.data.compound["type"] = Data("delete", Data::VERBATIM); break; case EVHTTP_REQ_OPTIONS: - request.type = "OPTIONS"; + request.data.compound["type"] = Data("options", Data::VERBATIM); break; case EVHTTP_REQ_TRACE: - request.type = "TRACE"; + request.data.compound["type"] = Data("trace", Data::VERBATIM); break; case EVHTTP_REQ_CONNECT: - request.type = "CONNECT"; + request.data.compound["type"] = Data("connect", Data::VERBATIM); break; case EVHTTP_REQ_PATCH: - request.type = "PATCH"; + request.data.compound["type"] = Data("patch", Data::VERBATIM); break; default: - request.type = "unknown"; + request.data.compound["type"] = Data("unknown", Data::VERBATIM); break; } @@ -106,17 +111,38 @@ void HTTPServer::httpRecvReqCallback(struct evhttp_request *req, void *callbackD struct evkeyval *header; struct evbuffer *buf; - // map headers to event structure + // insert headers to event data headers = evhttp_request_get_input_headers(req); for (header = headers->tqh_first; header; header = header->next.tqe_next) { - request.headers[header->key] = header->value; + request.data.compound["header"].compound[header->key] = Data(header->value, Data::VERBATIM); } - request.remoteHost = req->remote_host; - request.remotePort = req->remote_port; - request.httpMajor = req->major; - request.httpMinor = req->minor; - request.uri = req->uri; + request.data.compound["remoteHost"] = Data(req->remote_host, Data::VERBATIM); + request.data.compound["remotePort"] = Data(toStr(req->remote_port), Data::VERBATIM); + request.data.compound["httpMajor"] = Data(toStr((unsigned short)req->major), Data::VERBATIM); + request.data.compound["httpMinor"] = Data(toStr((unsigned short)req->minor), Data::VERBATIM); + request.data.compound["uri"] = Data(HTTPServer::getBaseURL() + req->uri, Data::VERBATIM); + request.data.compound["path"] = Data(evhttp_uri_get_path(evhttp_request_get_evhttp_uri(req)), Data::VERBATIM); + + // seperate path into components + std::stringstream ss(request.data.compound["path"].atom); + std::string item; + while(std::getline(ss, item, '/')) { + if (item.length() == 0) + continue; + request.data.compound["pathComponent"].array.push_back(Data(item, Data::VERBATIM)); + } + + // parse query string + struct evkeyvalq params; + struct evkeyval *param; + const char* query = evhttp_uri_get_query(evhttp_request_get_evhttp_uri(req)); + + evhttp_parse_query_str(query, ¶ms); + for (param = params.tqh_first; param; param = param->next.tqe_next) { + request.data.compound["query"].compound[param->key] = Data(param->value, Data::VERBATIM); + } + evhttp_clear_headers(¶ms); // get content buf = evhttp_request_get_input_buffer(req); @@ -129,7 +155,40 @@ void HTTPServer::httpRecvReqCallback(struct evhttp_request *req, void *callbackD } } - ((HTTPServlet*)callbackData)->httpRecvRequest(request); + if (callbackData == NULL) { + HTTPServer::getInstance()->processByMatchingServlet(request); + } else { + ((HTTPServlet*)callbackData)->httpRecvRequest(request); + } +} + +void HTTPServer::processByMatchingServlet(const Request& request) { + servlet_iter_t servletIter = _servlets.begin(); + + std::string actualPath = request.data.compound.at("path").atom; + HTTPServlet* bestMatch = NULL; + std::string bestPath; + + while(servletIter != _servlets.end()) { + // is the servlet path a prefix of the actual path? + std::string servletPath = "/" + servletIter->first; + if (boost::iequals(actualPath.substr(0, servletPath.length()), servletPath) && // actual path is a prefix + boost::iequals(actualPath.substr(servletPath.length(), 1), "/")) { // and next character is a '/' + if (bestPath.length() < servletPath.length()) { + // this servlet is a better match + bestPath = servletPath; + bestMatch = servletIter->second; + } + } + servletIter++; + } + + if (bestMatch != NULL) { + bestMatch->httpRecvRequest(request); + } else { + LOG(INFO) << "Got an HTTP request at " << actualPath << " but no servlet is registered there or at a prefix"; + evhttp_send_error(request.curlReq, 404, NULL); + } } void HTTPServer::reply(const Reply& reply) { @@ -154,18 +213,6 @@ bool HTTPServer::registerServlet(const std::string& path, HTTPServlet* servlet) HTTPServer* INSTANCE = getInstance(); tthread::lock_guard<tthread::recursive_mutex> lock(INSTANCE->_mutex); - /** - * Determine path for interpreter. - * - * If the interpreter has a name and it is not yet taken, choose it as the path - * for requests. If the interpreters name path is already taken, append digits - * until we have an available path. - * - * If the interpreter does not specify a name, take its sessionid. - * - * Responsibility moved to individual servlets. - */ - if(INSTANCE->_servlets.find(path) != INSTANCE->_servlets.end()) { return false; } @@ -182,9 +229,13 @@ bool HTTPServer::registerServlet(const std::string& path, HTTPServlet* servlet) evhttp_set_cb(INSTANCE->_http, ("/" + path).c_str(), HTTPServer::httpRecvReqCallback, servlet); return true; - // generic callback -// evhttp_set_cb(THIS->_http, "/", EventIOProcessor::httpRecvReq, processor); -// evhttp_set_gencb(THIS->_http, EventIOProcessor::httpRecvReq, NULL); +} + +std::string HTTPServer::getBaseURL() { + HTTPServer* INSTANCE = getInstance(); + std::stringstream servletURL; + servletURL << "http://" << INSTANCE->_address << ":" << INSTANCE->_port; + return servletURL.str(); } void HTTPServer::unregisterServlet(HTTPServlet* servlet) { diff --git a/src/uscxml/server/HTTPServer.h b/src/uscxml/server/HTTPServer.h index a387e1f..1ec28c7 100644 --- a/src/uscxml/server/HTTPServer.h +++ b/src/uscxml/server/HTTPServer.h @@ -7,6 +7,7 @@ #include <event2/http.h> #include "uscxml/concurrency/tinythread.h" +#include "uscxml/Message.h" namespace uscxml { @@ -14,23 +15,16 @@ class HTTPServlet; class HTTPServer { public: - class Request { + class Request : public Event { public: Request() : curlReq(NULL) {} - std::string type; - std::map<std::string, std::string> headers; std::string content; - std::string remoteHost; - unsigned short remotePort; - std::string httpMajor; - std::string httpMinor; - std::string uri; struct evhttp_request* curlReq; }; class Reply { public: - Reply(Request req) : status(200), type(req.type), curlReq(req.curlReq) {} + Reply(Request req) : status(200), type(req.data.compound["type"].atom), curlReq(req.curlReq) {} int status; std::string type; std::map<std::string, std::string> headers; @@ -44,6 +38,8 @@ public: }; static HTTPServer* getInstance(int port = 8080); + static std::string getBaseURL(); + static void reply(const Reply& reply); static bool registerServlet(const std::string& path, HTTPServlet* servlet); ///< Register a servlet, returns false if path is already taken @@ -60,6 +56,8 @@ private: void determineAddress(); static void httpRecvReqCallback(struct evhttp_request *req, void *callbackData); + void processByMatchingServlet(const Request& request); + std::map<std::string, HTTPServlet*> _servlets; typedef std::map<std::string, HTTPServlet*>::iterator servlet_iter_t; |