diff options
author | Stefan Radomski <radomski@tk.informatik.tu-darmstadt.de> | 2014-08-13 11:48:26 (GMT) |
---|---|---|
committer | Stefan Radomski <radomski@tk.informatik.tu-darmstadt.de> | 2014-08-13 11:48:26 (GMT) |
commit | 02f52d15e7df2500c0c6e96660a43a985add16e8 (patch) | |
tree | d4ccfdd7d1070201f39a9589b35e02a39ec179c8 /test | |
parent | c30b602cdb5ede809b960e35fc7e702b7f1f76e2 (diff) | |
download | uscxml-02f52d15e7df2500c0c6e96660a43a985add16e8.zip uscxml-02f52d15e7df2500c0c6e96660a43a985add16e8.tar.gz uscxml-02f52d15e7df2500c0c6e96660a43a985add16e8.tar.bz2 |
Builds for Raspberry and
started VoiceXML HTTP integration
Diffstat (limited to 'test')
-rw-r--r-- | test/CMakeLists.txt | 5 | ||||
-rw-r--r-- | test/src/test-sockets.cpp | 67 | ||||
-rw-r--r-- | test/src/test-vxml-mmi-http.cpp | 165 | ||||
-rw-r--r-- | test/src/test-vxml-mmi-socket.cpp | 133 |
4 files changed, 349 insertions, 21 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index dc827d3..dbc5a15 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -146,6 +146,11 @@ 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) # target_link_libraries(test-mmi uscxml) diff --git a/test/src/test-sockets.cpp b/test/src/test-sockets.cpp index a02eb9c..ad567f7 100644 --- a/test/src/test-sockets.cpp +++ b/test/src/test-sockets.cpp @@ -1,4 +1,5 @@ #include "uscxml/config.h" +#include "uscxml/Convenience.h" #include "uscxml/server/Socket.h" #include <iostream> #include <stdexcept> @@ -25,6 +26,28 @@ public: }; }; +int packetSeq = 0; + +class LogServer : public ServerSocket { +public: + LogServer(int domain, int type, int protocol) : ServerSocket(domain, type, protocol) {} + virtual void readCallback(const char* data, size_t size, Connection& conn) { + std::string content(data, size); + std::cout << "Server got: " << content << std::endl; + }; +}; + +class CountingPacketServer : public PacketServerSocket { +public: + CountingPacketServer(int domain, int type, int protocol, const std::string& sep) : PacketServerSocket(domain, type, protocol, sep) {} + virtual void readCallback(const std::string& packet, Connection& conn) { +// std::cout << "-- " << packet << std::endl; + size_t seq = strTo<size_t>(packet); + assert(seq == packetSeq); + packetSeq++; + }; +}; + class TestClient : public ClientSocket { public: TestClient(int domain, int type, int protocol) : ClientSocket(domain, type, protocol) {} @@ -45,7 +68,49 @@ int main(int argc, char** argv) { evthread_use_windows_threads(); #endif - if (0) { + if (1) { + packetSeq = 0; + CountingPacketServer server(PF_INET, SOCK_STREAM, 0, std::string("tadaa!")); +// LogServer server(PF_INET, SOCK_STREAM, 0); + server.listen("*", 1235); + server.setBlockSizeRead(1); + + TestClient client(PF_INET, SOCK_STREAM, 0); + client.connect("127.0.0.1", 1235); + + int iterations = 1000; + std::stringstream contentSS; + for (int i = 0; i < iterations; i++) { + contentSS << toStr(i); + contentSS << "tadaa!"; + } + client.write(contentSS.str()); + + while(packetSeq != iterations) + usleep(10000); + } + + if (1) { + packetSeq = 0; + CountingPacketServer server(PF_INET, SOCK_STREAM, 0, std::string("\0", 1)); + server.listen("*", 1235); + + TestClient client(PF_INET, SOCK_STREAM, 0); + client.connect("127.0.0.1", 1235); + + int iterations = 1000; + for (int i = 0; i < iterations; i++) { + client.write(toStr(i)); + client.write("\0", 1); + } + + while(packetSeq != iterations) + usleep(10000); + } + + exit(0); + + if (1) { // start server socket and connect int iterations = 100; diff --git a/test/src/test-vxml-mmi-http.cpp b/test/src/test-vxml-mmi-http.cpp new file mode 100644 index 0000000..50a8dd0 --- /dev/null +++ b/test/src/test-vxml-mmi-http.cpp @@ -0,0 +1,165 @@ +#include "uscxml/config.h" +#include "uscxml/server/Socket.h" +#include "uscxml/UUID.h" +#include <iostream> +#include <stdexcept> + +#include <boost/algorithm/string.hpp> + +#include <event2/event.h> +#include "event2/thread.h" + +#ifdef HAS_SIGNAL_H +#include <signal.h> +#endif + +#include "uscxml/server/HTTPServer.h" +#include "uscxml/URL.h" +#include "uscxml/concurrency/tinythread.h" +#include "uscxml/plugins/ioprocessor/modality/MMIMessages.h" +#include <DOM/io/Stream.hpp> + +#ifdef _WIN32 +#include "XGetopt.h" +#endif + +#include "uscxml/plugins/ioprocessor/modality/MMIMessages.cpp" + +#define ISSUE_REQUEST(name) {\ + Arabica::DOM::Document<std::string> name##XML = name.toXML(true);\ + name##XML.getDocumentElement().setPrefix("mmi");\ + std::stringstream name##XMLSS;\ + name##XMLSS << name##XML;\ + URL name##URL(target);\ + name##URL.setOutContent(name##XMLSS.str());\ + name##URL.addOutHeader("Content-type", "application/xml");\ + name##URL.download(true);\ +} + +using namespace uscxml; + +std::map<std::string, MMIEvent*> Requests; +std::map<std::string, MMIEvent*> Replies; + +tthread::condition_variable Cond; +tthread::mutex Mutex; + +class MMIServlet : public HTTPServlet { +public: + bool httpRecvRequest(const HTTPServer::Request& request) { + tthread::lock_guard<tthread::mutex> lock(Mutex); + + NameSpacingParser parser = NameSpacingParser::fromXML(request.content); + switch(MMIEvent::getType(parser.getDocument().getDocumentElement())) { + case MMIEvent::NEWCONTEXTRESPONSE: { + NewContextResponse* resp = new NewContextResponse(NewContextResponse::fromXML(parser.getDocument().getDocumentElement())); + Replies[resp->requestId] = resp; + } + case MMIEvent::STARTRESPONSE: { + StartResponse* resp = new StartResponse(StartResponse::fromXML(parser.getDocument().getDocumentElement())); + Replies[resp->requestId] = resp; + } + default: ; + } + + Cond.notify_all(); + return true; + } + void setURL(const std::string& url) { + this->url = url; + } + std::string url; +}; + +void printUsageAndExit(const char* progName) { + // remove path from program name + std::string progStr(progName); + if (progStr.find_last_of(PATH_SEPERATOR) != std::string::npos) { + progStr = progStr.substr(progStr.find_last_of(PATH_SEPERATOR) + 1, progStr.length() - (progStr.find_last_of(PATH_SEPERATOR) + 1)); + } + + printf("%s version " USCXML_VERSION " (" CMAKE_BUILD_TYPE " build - " CMAKE_COMPILER_STRING ")\n", progStr.c_str()); + printf("Usage\n"); + printf("\t%s", progStr.c_str()); + printf(" [-tURL] URL"); + printf("\n"); + printf("Options\n"); + printf("\t-tURL : URL of VoiceXML HTTP server\n"); + printf("\tURL : URL of a VoiceXML document\n"); + printf("\n"); + exit(1); +} + +int main(int argc, char** argv) { + try { + tthread::lock_guard<tthread::mutex> lock(Mutex); + + std::string target; + std::string document; + + if (argc < 2) + printUsageAndExit(argv[0]); + + int option; + while ((option = getopt(argc, argv, "t:")) != -1) { + switch(option) { + case 't': + target = optarg; + break; + default: + printUsageAndExit(argv[0]); + } + } + + if (argc < optind) + printUsageAndExit(argv[0]); + + document = argv[optind]; + + if (!boost::starts_with(document, "http")) + document = "http://" + document; + + if (!boost::starts_with(target, "http")) + document = "http://" + target; + + // target = "http://130.83.163.167:9090/mmi"; + // target = "http://localhost:9090/mmi"; + + + MMIServlet servlet; + HTTPServer::getInstance(4344, 0); + HTTPServer::getInstance()->registerServlet("/mmi", &servlet); + + std::string source = servlet.url; + + NewContextRequest newCtxReq; + newCtxReq.source = source; + newCtxReq.target = target; + newCtxReq.requestId = UUID::getUUID(); + + Requests[newCtxReq.requestId] = &newCtxReq; + ISSUE_REQUEST(newCtxReq); + + while(Replies.find(newCtxReq.requestId) == Replies.end()) + Cond.wait(Mutex); + + StartRequest startReq; + startReq.source = source; + startReq.target = target; + startReq.requestId = UUID::getUUID(); + startReq.contentURL.href = document; + //"https://raw.githubusercontent.com/Roland-Taizun-Azhar/TaskAssistance-Project/master/WebContent/hello.vxml"; + + Requests[startReq.requestId] = &startReq; + ISSUE_REQUEST(startReq); + + while(Replies.find(startReq.requestId) == Replies.end()) + Cond.wait(Mutex); + } catch (Event e) { + std::cout << e << std::endl; + } catch (std::exception e) { + std::cout << e.what() << std::endl; + } + + +}
\ No newline at end of file diff --git a/test/src/test-vxml-mmi-socket.cpp b/test/src/test-vxml-mmi-socket.cpp index a79cec7..8246c3b 100644 --- a/test/src/test-vxml-mmi-socket.cpp +++ b/test/src/test-vxml-mmi-socket.cpp @@ -1,5 +1,6 @@ #include "uscxml/config.h" #include "uscxml/server/Socket.h" +#include "uscxml/UUID.h" #include <iostream> #include <stdexcept> @@ -19,15 +20,70 @@ using namespace uscxml; -class TestServer : public ServerSocket { +bool testAddressParsing() { + std::string protocol; + std::string hostName; + uint16_t port; + + { + Socket::parseAddress("4343", protocol, hostName, port); + assert(protocol == "tcp"); + assert(hostName == "127.0.0.1"); + assert(port == 4343); + + Socket::parseAddress("localhost:4343", protocol, hostName, port); + assert(protocol == "tcp"); + assert(hostName == "localhost"); + assert(port == 4343); + + Socket::parseAddress("tcp://localhost:4343", protocol, hostName, port); + assert(protocol == "tcp"); + assert(hostName == "localhost"); + assert(port == 4343); + } + return true; +} + +bool testMMIEvents() { + { + NewContextRequest newCtxReq; + newCtxReq.source = "localhost:3434"; + newCtxReq.target = "localhost:1212"; + newCtxReq.requestId = "requestId"; + + Arabica::DOM::Document<std::string> newCtxReqXML1 = newCtxReq.toXML(); + Arabica::DOM::Document<std::string> newCtxReqXML2 = newCtxReq.toXML(true); + +// std::cout << newCtxReqXML1 << std::endl; +// std::cout << newCtxReqXML2 << std::endl; + + NewContextRequest newCtxReq1 = NewContextRequest::fromXML(newCtxReqXML1.getDocumentElement()); + NewContextRequest newCtxReq2 = NewContextRequest::fromXML(newCtxReqXML2.getDocumentElement()); + + assert(MMIEvent::getType(newCtxReqXML1.getDocumentElement()) == MMIEvent::NEWCONTEXTREQUEST); + assert(MMIEvent::getType(newCtxReqXML2.getDocumentElement()) == MMIEvent::NEWCONTEXTREQUEST); + + assert(newCtxReq1.source == "localhost:3434"); + assert(newCtxReq2.source == "localhost:3434"); + assert(newCtxReq1.target == "localhost:1212"); + assert(newCtxReq2.target == "localhost:1212"); + assert(newCtxReq1.requestId == "requestId"); + assert(newCtxReq2.requestId == "requestId"); + + } + return true; +} + +class TestServer : public PacketServerSocket { public: - TestServer(int domain, int type, int protocol) : ServerSocket(domain, type, protocol) {} - virtual void readCallback(const char* data, size_t size, Connection& conn) { - std::string content(data, size); -// std::cout << "Server got: " << content << std::endl; + TestServer(int domain, int type, int protocol) : PacketServerSocket(domain, type, protocol, std::string("\0", 1)) {} + virtual void readCallback(const std::string& packet, Connection& conn) { + std::cout << "Server got: " << packet << std::endl; std::string urghs("hi!"); conn.reply(urghs.data(), urghs.size()); }; + + std::stringstream fragment; }; class TestClient : public ClientSocket { @@ -38,6 +94,9 @@ public: }; }; +std::map<std::string, MMIEvent*> _requests; +std::map<std::string, MMIEvent*> _replies; + int main(int argc, char** argv) { #if defined(HAS_SIGNAL_H) && !defined(WIN32) @@ -49,24 +108,58 @@ int main(int argc, char** argv) { #else evthread_use_windows_threads(); #endif - + testAddressParsing(); + testMMIEvents(); + // TestClient client(PF_INET, SOCK_STREAM, 0); // client.connect("epikur.local", 4343); + std::string target = "localhost:4343"; + std::string source = "localhost:4344"; + + TestServer server(PF_INET, SOCK_STREAM, 0); + server.listen(source); + +// while(true) +// sleep(1000); + + TestClient client(PF_INET, SOCK_STREAM, 0); + client.connect(source); + + NewContextRequest newCtxReq; + newCtxReq.source = source; + newCtxReq.target = target; + newCtxReq.requestId = UUID::getUUID(); + + _requests[newCtxReq.requestId] = &newCtxReq; + + Arabica::DOM::Document<std::string> newCtxReqXML = newCtxReq.toXML(true); + std::stringstream newCtxReqXMLSS; + newCtxReqXMLSS << newCtxReqXML; + + for (int i = 0; i < 100000; i++) { + std::string index = toStr(i); + client.write(index.c_str(), index.size() + 1); +// client.write(newCtxReqXMLSS.str().data(), newCtxReqXMLSS.str().size()); +// client.write("\0", 1); + } + + while(true) + sleep(1000); - StartRequest startReq; - startReq.source = "undefined.source"; - startReq.target = "epikur.local:4343"; - startReq.requestId = "131234141234"; - startReq.data = - "<vxml xmlns=\"http://www.w3.org/2001/vxml\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" version=\"2.1\" xml:lang=\"en\"" - "xsi:schematicLocation=\"http://www.w3.org/2001/vxml http://www.w3.org/TR/voicexml20/vxml.xsd\">" - " <prompt>Goodbye!</prompt>" - "</vxml>"; - - Arabica::DOM::Document<std::string> reqXML = startReq.toXML(); - std::stringstream xmlSS; - xmlSS << reqXML; - std::cout << reqXML; +// StartRequest startReq; +// startReq.source = "localhost:4344"; +// startReq.target = "localhost:4343"; +// startReq.requestId = "131234141234"; +// startReq.data = +// "<vxml xmlns=\"http://www.w3.org/2001/vxml\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" version=\"2.1\" xml:lang=\"en\"" +// "xsi:schematicLocation=\"http://www.w3.org/2001/vxml http://www.w3.org/TR/voicexml20/vxml.xsd\">" +// " <prompt>Goodbye!</prompt>" +// "</vxml>"; +// +// Arabica::DOM::Document<std::string> reqXML = startReq.toXML(); +// std::stringstream xmlSS; +// xmlSS << reqXML; +// std::cout << reqXML; // client.write(xmlSS.str().data(), xmlSS.str().size()); }
\ No newline at end of file |