2 * sockethandlerTest.cpp
4 * Created on: Dec 19, 2011
8 #include "sockethandlerTest.h"
10 #include <sys/socket.h> /* for socket(), connect(), (), and recv() */
11 #include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
12 #include <sys/ioctl.h>
19 #define SOCK_PATH "/tmp/mysock"
21 using namespace testing;
24 DLT_DECLARE_CONTEXT(AudioManager)
26 static volatile sig_atomic_t gDispatch = 1; //this global is used to stop the mainloop
28 sockethandlerTest::sockethandlerTest()
32 sockethandlerTest::~sockethandlerTest()
36 timerCallBack::timerCallBack(SocketHandler *myHandler) :
37 pTimer1Callback(this, &timerCallBack::timer1Callback), //
38 pTimer2Callback(this, &timerCallBack::timer2Callback), //
39 pTimer3Callback(this, &timerCallBack::timer3Callback), //
40 pTimer4Callback(this, &timerCallBack::timer4Callback), //
41 mSocketHandler(myHandler)
46 am::timerCallBack::~timerCallBack()
50 void am::timerCallBack::timer1Callback(sh_timerHandle_t handle, void* userData)
52 std::cout << "callback1 called" << std::endl;
56 shTimerCallBack *buf = &pTimer1Callback;
57 sh_timerHandle_t handle_;
58 mSocketHandler->addTimer(timeout, buf, handle_, NULL);
61 void am::timerCallBack::timer2Callback(sh_timerHandle_t handle, void* userData)
63 std::cout << "callback2 called" << std::endl;
67 shTimerCallBack *buf = &pTimer2Callback;
68 sh_timerHandle_t handle_;
69 mSocketHandler->addTimer(timeout, buf, handle_, NULL);
72 void am::timerCallBack::timer3Callback(sh_timerHandle_t, void* userData)
74 std::cout << "callback3 called" << std::endl;
77 void am::timerCallBack::timer4Callback(sh_timerHandle_t, void* userData)
79 std::cout << "callback4 called" << std::endl;
80 mSocketHandler->stop_listening();
83 void* playWithSocketServer(void* data)
85 SocketHandler myHandler;
86 SamplePlugin::sockType_e type = SamplePlugin::INET;
87 SamplePlugin myplugin(&myHandler, type);
88 myHandler.start_listenting();
91 void* playWithUnixSocketServer(void* data)
93 SocketHandler myHandler;
94 SamplePlugin::sockType_e type = SamplePlugin::UNIX;
95 SamplePlugin myplugin(&myHandler, type);
96 myHandler.start_listenting();
99 TEST(sockethandlerTest,playWithUNIXSockets)
101 pthread_t serverThread;
103 struct sockaddr_un servAddr;
106 //creates a thread that handles the serverpart
107 pthread_create(&serverThread, NULL, playWithUnixSocketServer, NULL);
109 sleep(1); //we need that here because the port needs to be opened
110 if ((socket_ = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
112 std::cout << "socket problem" << std::endl;
116 memset(&servAddr, 0, sizeof(servAddr));
117 strcpy(servAddr.sun_path, SOCK_PATH);
118 servAddr.sun_family = AF_UNIX;
119 if (connect(socket_, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
121 std::cout << "ERROR: connect() failed\n" << std::endl;
124 for (int i = 0; i <= 1000; i++)
126 std::string string("Got It?");
127 send(socket_, string.c_str(), string.size(), 0);
129 std::string string("finish!");
130 send(socket_, string.c_str(), string.size(), 0);
132 pthread_join(serverThread, NULL);
135 TEST(sockethandlerTest,playWithSockets)
137 pthread_t serverThread;
139 struct sockaddr_in servAddr;
140 unsigned short servPort = 6060;
141 struct hostent *host;
144 //creates a thread that handles the serverpart
145 pthread_create(&serverThread, NULL, playWithSocketServer, NULL);
147 sleep(1); //we need that here because the port needs to be opened
148 if ((socket_ = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
150 std::cout << "socket problem" << std::endl;
154 if ((host = (struct hostent*) gethostbyname("localhost")) == 0)
156 std::cout << "ERROR: gethostbyname() failed\n" << std::endl;
160 memset(&servAddr, 0, sizeof(servAddr));
161 servAddr.sin_family = AF_INET;
162 servAddr.sin_addr.s_addr = inet_addr(inet_ntoa(*(struct in_addr*) (host->h_addr_list[0])));
163 servAddr.sin_port = htons(servPort);
165 if (connect(socket_, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
167 std::cout << "ERROR: connect() failed\n" << std::endl;
170 for (int i = 0; i <= 1000; i++)
172 std::string string("Got It?");
173 send(socket_, string.c_str(), string.size(), 0);
175 std::string string("finish!");
176 send(socket_, string.c_str(), string.size(), 0);
178 pthread_join(serverThread, NULL);
181 TEST(sockethandlerTest,playWithTimers)
184 SocketHandler myHandler;
185 timerCallBack testCallback(&myHandler);
186 timespec timeoutTime, timeout2, timeout3, timeout4;
187 timeoutTime.tv_sec = 3;
188 timeoutTime.tv_nsec = 0;
189 timeout2.tv_nsec = 0;
191 timeout3.tv_nsec = 000000000;
193 timeout4.tv_nsec = 0;
194 timeout4.tv_sec = 13;
195 shTimerCallBack* buf = &testCallback.pTimer1Callback;
196 shTimerCallBack* buf2 = &testCallback.pTimer2Callback;
197 shTimerCallBack* buf3 = &testCallback.pTimer3Callback;
198 shTimerCallBack* buf4 = &testCallback.pTimer4Callback;
199 sh_timerHandle_t handle;
200 myHandler.addTimer(timeoutTime, buf, handle, NULL);
201 myHandler.addTimer(timeout2, buf2, handle, NULL);
202 myHandler.addTimer(timeout3, buf3, handle, NULL);
203 myHandler.addTimer(timeout4, buf4, handle, NULL);
204 myHandler.start_listenting();
208 void sockethandlerTest::SetUp()
212 void sockethandlerTest::TearDown()
216 int main(int argc, char **argv)
218 ::testing::InitGoogleTest(&argc, argv);
219 return RUN_ALL_TESTS();
222 am::SamplePlugin::SamplePlugin(SocketHandler *mySocketHandler, sockType_e socketType) :
223 connectFiredCB(this, &SamplePlugin::connectSocket), //
224 receiveFiredCB(this, &SamplePlugin::receiveData), //
225 sampleDispatchCB(this, &SamplePlugin::dispatchData), //
226 sampleCheckCB(this, &SamplePlugin::check), //
227 mSocketHandler(mySocketHandler), //
236 struct sockaddr_in servAddr;
237 struct sockaddr_un unixAddr;
238 unsigned int servPort = 6060;
243 socketHandle = socket(AF_UNIX, SOCK_STREAM, 0);
244 unixAddr.sun_family = AF_UNIX;
245 strcpy(unixAddr.sun_path, SOCK_PATH);
246 unlink(unixAddr.sun_path);
247 bind(socketHandle, (struct sockaddr *) &unixAddr, strlen(unixAddr.sun_path) + sizeof(unixAddr.sun_family));
250 socketHandle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
251 setsockopt(socketHandle, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
252 memset(&servAddr, 0, sizeof(servAddr));
253 servAddr.sin_family = AF_INET;
254 servAddr.sin_addr.s_addr = INADDR_ANY;
255 servAddr.sin_port = htons(servPort);
256 bind(socketHandle, (struct sockaddr *) &servAddr, sizeof(servAddr));
262 if (listen(socketHandle, 3) < 0)
264 std::cout << "listen ok" << std::endl;
268 ioctl(socketHandle, FIONBIO, (char *) &a);
269 setsockopt(socketHandle, SOL_SOCKET, SO_KEEPALIVE, (char *) &a, sizeof(a));
273 mySocketHandler->addFDPoll(socketHandle, events, NULL, &connectFiredCB, NULL, NULL, NULL, mConnecthandle);
274 std::cout << "setup server - listening" << std::endl;
277 void am::SamplePlugin::connectSocket(const pollfd pollfd1, const sh_pollHandle_t handle, void *userData)
279 //first, accept the connection, create a new filedescriptor
280 std::cout << "Got a connection request !" << std::endl;
281 struct sockaddr answer;
282 socklen_t len = sizeof(answer);
283 int receiveFD = accept(pollfd1.fd, (struct sockaddr*) &answer, &len);
285 //set the correct event:
289 //aded the filedescriptor to the sockethandler and register the callbacks for receiving the data
290 mSocketHandler->addFDPoll(receiveFD, event, NULL, &receiveFiredCB, &sampleCheckCB, &sampleDispatchCB, NULL, mReceiveHandle);
294 void am::SamplePlugin::receiveData(const pollfd pollfd, const sh_pollHandle_t handle, void *userData)
298 //read until buffer is full or no more data is there
299 int read = recv(pollfd.fd, buffer, 7, NULL);
302 //read the message and store it in a queue
303 std::string msg = std::string(buffer, read);
305 std::cout << "Got a message !" << std::endl;
309 bool am::SamplePlugin::dispatchData(const sh_pollHandle_t handle, void *userData)
311 //read data from the queue
312 std::cout << "Data:" << msgList.front() << std::endl;
314 //if the message was our finish message, we quit the poll loop
315 if (msgList.front().compare("finish!") == 0)
317 mSocketHandler->stop_listening();
319 //remove the message from the queue and return false if there is no more message to read.
321 if (msgList.size() != 0) return true;
325 bool am::SamplePlugin::check(const sh_pollHandle_t handle, void *userData)
327 //checks if there is data to dispatch
328 std::cout << "check!:" << std::endl;
329 if (msgList.size() != 0) return true;