From fe941ab36b44f083886c07a72d371b2cfba55fe3 Mon Sep 17 00:00:00 2001 From: Stefan Radomski Date: Wed, 13 Aug 2014 15:29:38 +0200 Subject: Build on Linux and Windows again --- .../plugins/ioprocessor/modality/MMIMessages.cpp | 2 +- src/uscxml/server/Socket.cpp | 47 ++++++++++++++-------- src/uscxml/server/Socket.h | 4 +- test/CMakeLists.txt | 19 ++++----- test/src/test-sockets.cpp | 4 +- test/src/test-vxml-mmi-http.cpp | 25 ++++++++---- 6 files changed, 63 insertions(+), 38 deletions(-) diff --git a/src/uscxml/plugins/ioprocessor/modality/MMIMessages.cpp b/src/uscxml/plugins/ioprocessor/modality/MMIMessages.cpp index 67a2371..1c5b98a 100644 --- a/src/uscxml/plugins/ioprocessor/modality/MMIMessages.cpp +++ b/src/uscxml/plugins/ioprocessor/modality/MMIMessages.cpp @@ -160,7 +160,7 @@ Arabica::DOM::Document ContentRequest::toXML(bool encapsulateInMMI) Element msgElem = Element(doc.getDocumentElement().getFirstChild()); if (contentURL.href.size() > 0) { - Element contentURLElem = doc.createElementNS(nameSpace, "contentURL"); + Element contentURLElem = doc.createElementNS(nameSpace, "ContentURL"); contentURLElem.setAttributeNS(nameSpace, "href", contentURL.href); contentURLElem.setAttributeNS(nameSpace, "fetchtimeout", contentURL.fetchTimeout); contentURLElem.setAttributeNS(nameSpace, "max-age", contentURL.maxAge); diff --git a/src/uscxml/server/Socket.cpp b/src/uscxml/server/Socket.cpp index 2d474ea..fc79357 100644 --- a/src/uscxml/server/Socket.cpp +++ b/src/uscxml/server/Socket.cpp @@ -19,6 +19,7 @@ #include "Socket.h" +#include "uscxml/Common.h" // for Data, Event #include "uscxml/Message.h" // for Data, Event #include "uscxml/config.h" // for OPENSSL_FOUND @@ -46,14 +47,6 @@ Socket::Socket(int domain, int type, int protocol) { if (_socketFD == -1) throw std::runtime_error(std::string("socket: ") + strerror(errno)); -#ifndef WIN32 - { - int one = 1; - if (setsockopt(_socketFD, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) != 0) { - throw std::runtime_error(std::string("setsockopt: ") + strerror(errno)); - } - } -#endif } @@ -300,16 +293,25 @@ void ServerSocket::listen(const std::string& address, int port) { setupSockAddr(address, port); bind(); - int reuseaddr_on = 1; - setsockopt(_socketFD, SOL_SOCKET, SO_REUSEADDR, &reuseaddr_on, sizeof(reuseaddr_on)); + int one = 1; + if (setsockopt(_socketFD, SOL_SOCKET, SO_REUSEADDR, (const char*)&one, sizeof(one)) != 0) { + throw std::runtime_error(std::string("setsockopt: ") + strerror(errno)); + } +#ifndef _WIN32 int flags = fcntl(_socketFD, F_GETFL); if (flags >= 0) { flags |= O_NONBLOCK; if (fcntl(_socketFD, F_SETFL, flags) < 0) { - // could not set to non-blocj + // could not set to non-block } } +#else + unsigned long on = 1; + if (ioctlsocket(_socketFD, FIONBIO, &on) != 0) { + // could not set to non-block + } +#endif _listenerEvent = event_new(_base->base, _socketFD, EV_READ|EV_PERSIST, acceptCallback, (void*)this); /*XXX check it */ @@ -353,13 +355,24 @@ void ServerSocket::Connection::reply(const char* data, size_t size) { bufferevent_write(bufferEvent, data, size); } +PacketServerSocket::~PacketServerSocket() { + for(std::map::iterator fragIter = _fragments.begin(); + fragIter != _fragments.end(); + fragIter++) { + delete fragIter->second; + } +} + void PacketServerSocket::readCallback(const char* data, size_t size, Connection& conn) { - std::stringstream& fragment = _fragments[conn]; - fragment << std::string(data, size); + if (_fragments.find(conn) == _fragments.end()) + _fragments[conn] = new std::stringstream(); + + std::stringstream* fragment = _fragments[conn]; + *fragment << std::string(data, size); size_t startPos = 0; size_t endPos; - const std::string& buffer = fragment.str(); + const std::string& buffer = fragment->str(); while((endPos = buffer.find(_sep, startPos)) != std::string::npos) { // std::cout << ">" << buffer.substr(startPos, endPos - startPos) << "<" << std::endl; readCallback(buffer.substr(startPos, endPos - startPos), conn); @@ -367,9 +380,9 @@ void PacketServerSocket::readCallback(const char* data, size_t size, Connection& } if (startPos != 0 && startPos < buffer.size() + 1) { std::string rest = buffer.substr(startPos); - fragment.str(std::string()); - fragment.clear(); - fragment << rest; + fragment->str(std::string()); + fragment->clear(); + *fragment << rest; } } diff --git a/src/uscxml/server/Socket.h b/src/uscxml/server/Socket.h index 9330c4b..7bb8ebe 100644 --- a/src/uscxml/server/Socket.h +++ b/src/uscxml/server/Socket.h @@ -103,14 +103,14 @@ protected: class USCXML_API PacketServerSocket : public ServerSocket { public: PacketServerSocket(int domain, int type, int protocol, const std::string& sep) : ServerSocket(domain, type, protocol), _sep(sep) {} - virtual ~PacketServerSocket() {} + virtual ~PacketServerSocket(); void readCallback(const char* data, size_t size, Connection& conn); virtual void readCallback(const std::string& packet, Connection& conn) = 0; protected: std::string _sep; - std::map _fragments; + std::map _fragments; }; class USCXML_API ClientSocket : public Socket { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index dbc5a15..b879401 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -62,6 +62,16 @@ add_test(test-lifecycle ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test-lifecycle) set_target_properties(test-lifecycle PROPERTIES FOLDER "Tests") if (NOT WIN32) + add_executable(test-vxml-mmi-socket src/test-vxml-mmi-socket.cpp) + target_link_libraries(test-vxml-mmi-socket uscxml) + # add_test(test-datamodel ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test-sockets) + set_target_properties(test-vxml-mmi-socket PROPERTIES FOLDER "Tests") + + add_executable(test-vxml-mmi-http src/test-vxml-mmi-http.cpp) + target_link_libraries(test-vxml-mmi-http uscxml) + # add_test(test-datamodel ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test-sockets) + set_target_properties(test-vxml-mmi-http PROPERTIES FOLDER "Tests") + add_executable(test-trie src/test-trie.cpp) target_link_libraries(test-trie uscxml) add_test(test-trie ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test-trie) @@ -141,15 +151,6 @@ target_link_libraries(test-sockets uscxml) # add_test(test-datamodel ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test-sockets) set_target_properties(test-sockets PROPERTIES FOLDER "Tests") -add_executable(test-vxml-mmi-socket src/test-vxml-mmi-socket.cpp) -target_link_libraries(test-vxml-mmi-socket uscxml) -# add_test(test-datamodel ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test-sockets) -set_target_properties(test-vxml-mmi-socket PROPERTIES FOLDER "Tests") - -add_executable(test-vxml-mmi-http src/test-vxml-mmi-http.cpp) -target_link_libraries(test-vxml-mmi-http uscxml) -# add_test(test-datamodel ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test-sockets) -set_target_properties(test-vxml-mmi-http PROPERTIES FOLDER "Tests") # if (NOT WIN32) # add_executable(test-mmi src/test-mmi.cpp) diff --git a/test/src/test-sockets.cpp b/test/src/test-sockets.cpp index ad567f7..993aaff 100644 --- a/test/src/test-sockets.cpp +++ b/test/src/test-sockets.cpp @@ -87,7 +87,7 @@ int main(int argc, char** argv) { client.write(contentSS.str()); while(packetSeq != iterations) - usleep(10000); + tthread::this_thread::sleep_for(tthread::chrono::milliseconds(20)); } if (1) { @@ -105,7 +105,7 @@ int main(int argc, char** argv) { } while(packetSeq != iterations) - usleep(10000); + tthread::this_thread::sleep_for(tthread::chrono::milliseconds(20)); } exit(0); diff --git a/test/src/test-vxml-mmi-http.cpp b/test/src/test-vxml-mmi-http.cpp index 50a8dd0..130441c 100644 --- a/test/src/test-vxml-mmi-http.cpp +++ b/test/src/test-vxml-mmi-http.cpp @@ -31,9 +31,10 @@ std::stringstream name##XMLSS;\ name##XMLSS << name##XML;\ URL name##URL(target);\ + std::cout << name##XMLSS.str();\ name##URL.setOutContent(name##XMLSS.str());\ name##URL.addOutHeader("Content-type", "application/xml");\ - name##URL.download(true);\ + name##URL.download(false);\ } using namespace uscxml; @@ -44,25 +45,34 @@ std::map Replies; tthread::condition_variable Cond; tthread::mutex Mutex; +std::string context; + class MMIServlet : public HTTPServlet { public: bool httpRecvRequest(const HTTPServer::Request& request) { tthread::lock_guard lock(Mutex); - NameSpacingParser parser = NameSpacingParser::fromXML(request.content); - switch(MMIEvent::getType(parser.getDocument().getDocumentElement())) { + const Arabica::DOM::Document& doc = request.data.at("content").node.getOwnerDocument(); +// NameSpacingParser parser = NameSpacingParser::fromXML(request.content); + switch(MMIEvent::getType(doc.getDocumentElement())) { case MMIEvent::NEWCONTEXTRESPONSE: { - NewContextResponse* resp = new NewContextResponse(NewContextResponse::fromXML(parser.getDocument().getDocumentElement())); + NewContextResponse* resp = new NewContextResponse(NewContextResponse::fromXML(doc.getDocumentElement())); + context = resp->context; Replies[resp->requestId] = resp; + break; } case MMIEvent::STARTRESPONSE: { - StartResponse* resp = new StartResponse(StartResponse::fromXML(parser.getDocument().getDocumentElement())); + StartResponse* resp = new StartResponse(StartResponse::fromXML(doc.getDocumentElement())); Replies[resp->requestId] = resp; } default: ; } Cond.notify_all(); + + HTTPServer::Reply reply(request); + HTTPServer::reply(reply); + return true; } void setURL(const std::string& url) { @@ -135,7 +145,7 @@ int main(int argc, char** argv) { NewContextRequest newCtxReq; newCtxReq.source = source; newCtxReq.target = target; - newCtxReq.requestId = UUID::getUUID(); + newCtxReq.requestId = uscxml::UUID::getUUID(); Requests[newCtxReq.requestId] = &newCtxReq; ISSUE_REQUEST(newCtxReq); @@ -144,9 +154,10 @@ int main(int argc, char** argv) { Cond.wait(Mutex); StartRequest startReq; + startReq.context = context; startReq.source = source; startReq.target = target; - startReq.requestId = UUID::getUUID(); + startReq.requestId = uscxml::UUID::getUUID(); startReq.contentURL.href = document; //"https://raw.githubusercontent.com/Roland-Taizun-Azhar/TaskAssistance-Project/master/WebContent/hello.vxml"; -- cgit v0.12