From 58f4ba6c8a4f9662dad9f5ddb3106c2e2c34daa6 Mon Sep 17 00:00:00 2001 From: Pixel Date: Mon, 17 Oct 2011 21:19:58 -0700 Subject: Adding the Socket class, and a few tools that comes with it. --- tests/test-Sockets.cc | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 tests/test-Sockets.cc (limited to 'tests') diff --git a/tests/test-Sockets.cc b/tests/test-Sockets.cc new file mode 100644 index 0000000..fd6eaa0 --- /dev/null +++ b/tests/test-Sockets.cc @@ -0,0 +1,86 @@ +#include +#include + +BALAU_STARTUP; + +using namespace Balau; + +class Worker : public Task { + public: + Worker(IO io); + virtual const char * getName(); + virtual void Do(); + IO m_io; + String m_name; +}; + +Worker::Worker(IO io) : m_io(io) { + m_name = m_io->getName(); + Printer::log(M_STATUS, "Got connection: %s", m_name.to_charp()); +} + +const char * Worker::getName() { + return m_name.to_charp(); +} + +void Worker::Do() { + char x, y; + + int r; + r = m_io->read(&x, 1); + Assert(x == 'x'); + Assert(r == 1); + y = 'y'; + r = m_io->write(&y, 1); + Assert(r == 1); +} + +Listener * listener; + +class Client : public Task { + public: + virtual const char * getName() { return "Test client"; } + virtual void Do() { + Events::Timeout evt(0.1); + waitFor(&evt); + yield(); + + char x, y; + IO s(new Socket()); + bool c = s->connect("localhost", 1234); + Assert(c); + x = 'x'; + int r; + r = s->write(&x, 1); + Assert(r == 1); + r = s->read(&y, 1); + Assert(y == 'y'); + Assert(r == 1); + listener->stop(); + } +}; + +void MainTask::Do() { + Printer::enable(M_ALL); + Printer::log(M_STATUS, "Test::Sockets running."); + + Events::TaskEvent evtSvr(listener = new Listener(1234)); + Events::TaskEvent evtCln(new Client); + Printer::log(M_STATUS, "Created %s", listener->getName()); + waitFor(&evtSvr); + waitFor(&evtCln); + bool svrDone = false, clnDone = false; + while (!svrDone || !clnDone) { + yield(); + if (evtSvr.gotSignal()) { + evtSvr.ack(); + svrDone = true; + } + if (evtCln.gotSignal()) { + evtCln.ack(); + clnDone = true; + } + } + + Printer::log(M_STATUS, "Test::Sockets passed."); +} -- cgit v1.2.3