aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexander Chemeris <Alexander.Chemeris@gmail.com>2013-07-14 13:22:31 +0400
committerAlexander Chemeris <Alexander.Chemeris@gmail.com>2013-07-14 23:11:54 +0400
commit604f65e69f2317dd5dc1aeb8faf3c107cd8a2231 (patch)
treeea6a5a3f4b3576e0e933a0725c2c095896a2235e
parent129ad76b15f1fdf0588d5a24fe2d0f7ef3df274e (diff)
CommonLibs: Port InterthreadTest and SocketsTest to the new Thread API.
-rw-r--r--CommonLibs/InterthreadTest.cpp135
-rw-r--r--CommonLibs/Makefile.am4
-rw-r--r--CommonLibs/SocketsTest.cpp91
3 files changed, 137 insertions, 93 deletions
diff --git a/CommonLibs/InterthreadTest.cpp b/CommonLibs/InterthreadTest.cpp
index 03445d9..f5a5fb3 100644
--- a/CommonLibs/InterthreadTest.cpp
+++ b/CommonLibs/InterthreadTest.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.
@@ -27,66 +28,91 @@
#include "Threads.h"
#include "Interthread.h"
+#include "Configuration.h"
#include <iostream>
using namespace std;
+ConfigurationTable gConfig;
InterthreadQueue<int> gQ;
InterthreadMap<int,int> gMap;
-void* qWriter(void*)
+class QueueWriter : public Thread
{
- int *p;
- for (int i=0; i<20; i++) {
+public:
+ QueueWriter() : Thread("QueueWriter") {}
+
+protected:
+ virtual void runThread()
+ {
+ int *p;
+ for (int i=0; i<20; i++) {
+ p = new int;
+ *p = i;
+ COUT("queue write " << *p);
+ gQ.write(p);
+ msleep(1);
+ }
p = new int;
- *p = i;
- COUT("queue write " << *p);
+ *p = -1;
gQ.write(p);
- if (random()%2) sleep(1);
}
- p = new int;
- *p = -1;
- gQ.write(p);
- return NULL;
-}
+};
-void* qReader(void*)
+class QueueReader : public Thread
{
- bool done = false;
- while (!done) {
- int *p = gQ.read();
- COUT("queue read " << *p);
- if (*p<0) done=true;
- delete p;
+public:
+ QueueReader() : Thread("QueueReader") {}
+
+protected:
+ virtual void runThread()
+ {
+ bool done = false;
+ while (!done) {
+ int *p = gQ.read();
+ COUT("queue read " << *p);
+ if (*p<0) done=true;
+ delete p;
+ }
}
- return NULL;
-}
+};
-void* mapWriter(void*)
+class MapWriter : public Thread
{
- int *p;
- for (int i=0; i<20; i++) {
- p = new int;
- *p = i;
- COUT("map write " << *p);
- gMap.write(i,p);
- if (random()%2) sleep(1);
+public:
+ MapWriter() : Thread("MapWriter") {}
+
+protected:
+ virtual void runThread()
+ {
+ int *p;
+ for (int i=0; i<20; i++) {
+ p = new int;
+ *p = i;
+ COUT("map write " << *p);
+ gMap.write(i,p);
+ msleep(1);
+ }
}
- return NULL;
-}
+};
-void* mapReader(void*)
+class MapReader : public Thread
{
- for (int i=0; i<20; i++) {
- int *p = gMap.read(i);
- COUT("map read " << *p);
- // InterthreadMap will delete the pointers
- // delete p;
+public:
+ MapReader() : Thread("MapReader") {}
+
+protected:
+ virtual void runThread()
+ {
+ for (int i=0; i<20; i++) {
+ int *p = gMap.read(i);
+ COUT("map read " << *p);
+ // InterthreadMap will delete the pointers
+ }
}
- return NULL;
-}
+};
@@ -95,20 +121,25 @@ void* mapReader(void*)
int main(int argc, char *argv[])
{
- Thread qReaderThread;
- qReaderThread.start(qReader,NULL);
- Thread mapReaderThread;
- mapReaderThread.start(mapReader,NULL);
-
- Thread qWriterThread;
- qWriterThread.start(qWriter,NULL);
- Thread mapWriterThread;
- mapWriterThread.start(mapWriter,NULL);
-
- qReaderThread.join();
- qWriterThread.join();
- mapReaderThread.join();
- mapWriterThread.join();
+ COUT("TEST 1: InterthreadQueue")
+ QueueReader qReaderThread;
+ QueueWriter qWriterThread;
+ qReaderThread.startThread();
+ qWriterThread.startThread();
+ // stopThread() will wait for a thread to stop for 5 seconds, which
+ // is more than enough for this test to finish.
+ qReaderThread.stopThread();
+ qWriterThread.stopThread();
+
+ COUT("TEST 2: InterthreadMap")
+ MapReader mapReaderThread;
+ mapReaderThread.startThread();
+ MapWriter mapWriterThread;
+ mapWriterThread.startThread();
+ // stopThread() will wait for a thread to stop for 5 seconds, which
+ // is more than enough for this test to finish.
+ mapReaderThread.stopThread();
+ mapWriterThread.stopThread();
}
diff --git a/CommonLibs/Makefile.am b/CommonLibs/Makefile.am
index 26a55ed..271a8ef 100644
--- a/CommonLibs/Makefile.am
+++ b/CommonLibs/Makefile.am
@@ -81,11 +81,11 @@ BitVectorTest_SOURCES = BitVectorTest.cpp
BitVectorTest_LDADD = libcommon.la $(SQLITE_LA)
InterthreadTest_SOURCES = InterthreadTest.cpp
-InterthreadTest_LDADD = libcommon.la
+InterthreadTest_LDADD = libcommon.la $(SQLITE_LA)
InterthreadTest_LDFLAGS = -lpthread
SocketsTest_SOURCES = SocketsTest.cpp
-SocketsTest_LDADD = libcommon.la
+SocketsTest_LDADD = libcommon.la $(SQLITE_LA)
SocketsTest_LDFLAGS = -lpthread
TimevalTest_SOURCES = TimevalTest.cpp
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