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>
17 using namespace testing;
20 sockethandlerTest::sockethandlerTest()
24 sockethandlerTest::~sockethandlerTest()
28 void fdCallBack::connectSocket(int fd, const int16_t events,void * userData)
30 std::cout<<"Socket connection received and open"<<std::endl;
32 //accept the connection
33 mSocketConnection = accept(fd, NULL, NULL);
37 shPollCallBack* buf=&pSocketDataCallback;
38 //add new socketConnection to the handler
39 mSocketHandler->addFDPoll(mSocketConnection,event,buf,NULL);
45 fdCallBack::fdCallBack(SocketHandler *SocketHandler)
46 :mSocketConnection(0),
47 mSocketHandler(SocketHandler),
48 pSocketDataCallback(this, &fdCallBack::handleSocketData),
49 pSocketConnectionCallback(this, &fdCallBack::connectSocket)
55 void am::fdCallBack::handleSocketData(int fd, const int16_t events, void* userdata)
60 //there is something for us, read it
61 int read=recv(mSocketConnection,buffer,sizeof(buffer),NULL);
62 msg=std::string(buffer,read);
63 if (msg.compare("stopit")==0)
65 mSocketHandler->stop_listening();
67 else if (msg.compare("answer")==0)
69 std::string answer="myAnswer";
70 send(mSocketConnection,answer.c_str(),answer.size(),NULL);
74 fdCallBack::~fdCallBack()
78 am::timerCallBack::timerCallBack(SocketHandler *myHandler)
79 :pTimer1Callback(this, &timerCallBack::timer1Callback),
80 pTimer2Callback(this, &timerCallBack::timer2Callback),
81 pTimer3Callback(this, &timerCallBack::timer3Callback),
82 pTimer4Callback(this, &timerCallBack::timer4Callback),
83 mSocketHandler(myHandler)
90 am::timerCallBack::~timerCallBack()
96 void am::timerCallBack::timer1Callback(SocketHandler::sh_timerHandle_t handle, void* userData)
98 std::cout<<"callback1 called"<<std::endl;
102 TBasicTimerCallback *buf=&pTimer1Callback;
103 SocketHandler::sh_timerHandle_t handle_;
104 mSocketHandler->addTimer(timeout,buf,handle_,NULL);
109 void am::timerCallBack::timer2Callback(SocketHandler::sh_timerHandle_t handle, void* userData)
111 std::cout<<"callback2 called"<<std::endl;
115 TBasicTimerCallback *buf=&pTimer2Callback;
116 SocketHandler::sh_timerHandle_t handle_;
117 mSocketHandler->addTimer(timeout,buf,handle_,NULL);
120 void am::timerCallBack::timer3Callback(SocketHandler::sh_timerHandle_t handle, void* userData)
122 std::cout<<"callback3 called"<<std::endl;
125 void am::timerCallBack::timer4Callback(SocketHandler::sh_timerHandle_t handle, void* userData)
127 std::cout<<"callback4 called"<<std::endl;
130 void* playWithSocketServer(void* data)
134 //get a SocketHandler
135 SocketHandler myHandler;
137 //get a class that handles the callbacks from the handler
138 fdCallBack testCallback(&myHandler);
140 //prepare the socket, bind etc...
141 struct sockaddr_in servAddr;
142 int socketHandle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
143 setsockopt(socketHandle, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
144 servAddr.sin_family = AF_INET;
145 servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
146 servAddr.sin_port = htons(6060);
147 bind(socketHandle, (struct sockaddr*)(&servAddr), sizeof (servAddr));
148 listen(socketHandle, 3);
150 //prepare the event (we want POLLIN because we need to listen)
154 shPollCallBack* buf=&testCallback.pSocketConnectionCallback;
155 //add the callback to the Sockethandler
156 myHandler.addFDPoll(socketHandle, event, buf, NULL);
159 myHandler.start_listenting();
165 TEST(sockethandlerTest,playWithSockets)
167 pthread_t serverThread;
170 //creates a thread that handles the serverpart
171 pthread_create(&serverThread,NULL,playWithSocketServer,NULL);
173 sleep(1); //give a little time to settle everything
174 //make everything ready to send data
175 struct sockaddr_in servAddr;
176 int socketHandle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
177 struct hostent *host;
178 host = (struct hostent*) gethostbyname("localhost");
180 memset(&servAddr, 0, sizeof(servAddr));
181 memcpy(&servAddr.sin_addr, host->h_addr_list[0], host->h_length);
182 servAddr.sin_family = AF_INET;
183 servAddr.sin_port = htons(6060);
185 //connect to the socket
186 int k =connect(socketHandle,(struct sockaddr *) &servAddr, sizeof(servAddr));
188 std::string msg="answer";
190 //send first the answer message and wait for the reply
191 int p=send(socketHandle,msg.c_str(),msg.size(),NULL);
192 int read=recv(socketHandle,buffer,sizeof(buffer),NULL);
193 msg=std::string(buffer,read);
194 ASSERT_TRUE(msg.compare("myAnswer")==0);
197 //now send a message causing the handler to stop and end the loop
198 p=send(socketHandle,msg.c_str(),msg.size(),NULL);
199 pthread_join(serverThread,NULL);
202 TEST(sockethandlerTest,playWithTimers)
204 SocketHandler myHandler;
205 timerCallBack testCallback(&myHandler);
206 timespec timeoutTime, timeout2, timeout3, timeout4;
207 timeoutTime.tv_sec=3;
208 timeoutTime.tv_nsec=0;
211 timeout3.tv_nsec=000000000;
215 TBasicTimerCallback* buf=&testCallback.pTimer1Callback;
216 TBasicTimerCallback* buf2=&testCallback.pTimer2Callback;
217 TBasicTimerCallback* buf3=&testCallback.pTimer3Callback;
218 TBasicTimerCallback* buf4=&testCallback.pTimer4Callback;
219 SocketHandler::sh_timerHandle_t handle;
220 myHandler.addTimer(timeoutTime,buf,handle,NULL);
221 myHandler.addTimer(timeout2,buf2,handle,NULL);
222 myHandler.addTimer(timeout3,buf3,handle,NULL);
223 myHandler.addTimer(timeout4,buf4,handle,NULL);
224 myHandler.start_listenting();
228 void sockethandlerTest::SetUp()
232 void sockethandlerTest::TearDown()
236 int main(int argc, char **argv)
238 ::testing::InitGoogleTest(&argc, argv);
239 return RUN_ALL_TESTS();