1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
#include "uscxml/config.h"
#include "uscxml/server/Socket.h"
#include "uscxml/UUID.h"
#include <iostream>
#include <stdexcept>
#include <event2/event.h>
#include "event2/thread.h"
#ifdef HAS_SIGNAL_H
#include <signal.h>
#endif
#include "uscxml/concurrency/tinythread.h"
#include "uscxml/messages/MMIMessages.h"
#include <DOM/io/Stream.hpp>
using namespace uscxml;
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) : 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 {
public:
TestClient(int domain, int type, int protocol) : ClientSocket(domain, type, protocol) {}
virtual void readCallback(const char* data, size_t size) {
std::string content(data, size);
};
};
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)
signal(SIGPIPE, SIG_IGN);
#endif
#ifndef _WIN32
evthread_use_pthreads();
#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 (size_t 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 = "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());
}
|