diff options
author | Alexander Chemeris <Alexander.Chemeris@gmail.com> | 2013-07-14 13:22:31 +0400 |
---|---|---|
committer | Alexander Chemeris <Alexander.Chemeris@gmail.com> | 2013-07-14 23:11:54 +0400 |
commit | 604f65e69f2317dd5dc1aeb8faf3c107cd8a2231 (patch) | |
tree | ea6a5a3f4b3576e0e933a0725c2c095896a2235e /CommonLibs/SocketsTest.cpp | |
parent | 129ad76b15f1fdf0588d5a24fe2d0f7ef3df274e (diff) |
CommonLibs: Port InterthreadTest and SocketsTest to the new Thread API.
Diffstat (limited to 'CommonLibs/SocketsTest.cpp')
-rw-r--r-- | CommonLibs/SocketsTest.cpp | 91 |
1 files changed, 52 insertions, 39 deletions
diff --git a/CommonLibs/SocketsTest.cpp b/CommonLibs/SocketsTest.cpp index 9a4997b..5aab615 100644 --- a/CommonLibs/SocketsTest.cpp +++ b/CommonLibs/SocketsTest.cpp @@ -1,5 +1,6 @@ /* * Copyright 2008 Free Software Foundation, Inc. +* Copyright 2013 Alexander Chemeris <Alexander.Chemeris@fairwaves.ru> * * * This software is distributed under the terms of the GNU Affero Public License. @@ -28,59 +29,73 @@ #include "Sockets.h" #include "Threads.h" -#include <stdio.h> +#include "Configuration.h" +#include "Timeval.h" #include <stdlib.h> +ConfigurationTable gConfig; static const int gNumToSend = 10; -void *testReaderIP(void *) +class TestReaderIP : public Thread { - UDPSocket readSocket(5934, "localhost", 5061); - readSocket.nonblocking(); - int rc = 0; - while (rc<gNumToSend) { - char buf[MAX_UDP_LENGTH]; - int count = readSocket.read(buf); - if (count>0) { - COUT("read: " << buf); - rc++; - } else { - sleep(2); +public: + TestReaderIP() : Thread("TestReaderIP") {} + +protected: + virtual void runThread() + { + UDPSocket readSocket(5934, "localhost", 5061); + readSocket.nonblocking(); + int rc = 0; + while (rc<gNumToSend) { + char buf[MAX_UDP_LENGTH]; + int count = readSocket.read(buf); + if (count>0) { + COUT("IP read: " << buf); + rc++; + } else { + COUT("IP sleeping..."); + sleep(2); + } } } - return NULL; -} - +}; - -void *testReaderUnix(void *) +class TestReaderUnix : public Thread { - UDDSocket readSocket("testDestination"); - readSocket.nonblocking(); - int rc = 0; - while (rc<gNumToSend) { - char buf[MAX_UDP_LENGTH]; - int count = readSocket.read(buf); - if (count>0) { - COUT("read: " << buf); - rc++; - } else { - sleep(2); +public: + TestReaderUnix() : Thread("TestReaderUnix") {} + +protected: + virtual void runThread() + { + UDDSocket readSocket("testDestination"); + readSocket.nonblocking(); + int rc = 0; + while (rc<gNumToSend) { + char buf[MAX_UDP_LENGTH]; + int count = readSocket.read(buf); + if (count>0) { + COUT("UNIX read: " << buf); + rc++; + } else { + COUT("UNIX sleeping..."); + sleep(2); + } } } - return NULL; -} +}; int main(int argc, char * argv[] ) { - Thread readerThreadIP; - readerThreadIP.start(testReaderIP,NULL); - Thread readerThreadUnix; - readerThreadUnix.start(testReaderUnix,NULL); + TestReaderIP readerThreadIP; + TestReaderUnix readerThreadUnix; + readerThreadIP.startThread(); + readerThreadUnix.startThread(); UDPSocket socket1(5061, "127.0.0.1",5934); UDDSocket socket1U("testSource","testDestination"); @@ -92,12 +107,10 @@ int main(int argc, char * argv[] ) for (int i=0; i<gNumToSend; i++) { socket1.write("Hello IP land"); - socket1U.write("Hello Unix domain"); - sleep(1); + socket1U.write("Hello Unix domain"); + msleep(1); } - readerThreadIP.join(); - readerThreadUnix.join(); } // vim: ts=4 sw=4 |