Commit b5578687 authored by Davis King's avatar Davis King

cleaned up the sockets test

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402497
parent aa34d39f
...@@ -12,8 +12,7 @@ ...@@ -12,8 +12,7 @@
#include "tester.h" #include "tester.h"
namespace namespace {
{
using namespace test; using namespace test;
using namespace dlib; using namespace dlib;
...@@ -23,8 +22,6 @@ namespace ...@@ -23,8 +22,6 @@ namespace
dlib::signaler s(m); dlib::signaler s(m);
const char magic_num = 42; const char magic_num = 42;
const int min_bytes_sent = 10000; const int min_bytes_sent = 10000;
int thread_count;
int thread_count2;
int assigned_port; int assigned_port;
logger dlog("test.sockets"); logger dlog("test.sockets");
...@@ -36,7 +33,8 @@ namespace ...@@ -36,7 +33,8 @@ namespace
public: public:
serv ( serv (
) : ) :
error_occurred (false) error_occurred (false),
got_connections(false)
{} {}
void on_listening_port_assigned ( void on_listening_port_assigned (
...@@ -73,93 +71,116 @@ namespace ...@@ -73,93 +71,116 @@ namespace
tag = 5.0; tag = 5.0;
error_occurred = true; error_occurred = true;
} }
got_connections = true;
dlog << LINFO << "in serv::on_connect(): on_connect ending"; dlog << LINFO << "in serv::on_connect(): on_connect ending";
} }
bool error_occurred; bool error_occurred;
bool got_connections;
double tag; double tag;
}; };
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
void thread_proc ( class thread_container : public multithreaded_object
void* param
)
{ {
serv& srv = *reinterpret_cast<serv*>(param); public:
try
serv& srv;
thread_container (
serv& srv_
) : srv(srv_)
{ {
dlog << LTRACE << "enter thread"; for (int i = 0; i < 10; ++i)
{ register_thread(*this, &thread_container::thread_proc);
auto_mutex M(m);
while (assigned_port == 0)
s.wait();
}
int status; // start up the threads
connection* con; start();
string hostname; }
string ip;
status = get_local_hostname(hostname);
if (status)
{
srv.tag = 1.0;
srv.error_occurred = true;
srv.clear();
dlog << LWARN << "leaving thread, line: " << __LINE__;
return;
}
status = hostname_to_ip(hostname,ip); ~thread_container ()
if (status) {
{ // wait for all threads to terminate
srv.tag = 2.0; wait();
srv.error_occurred = true; }
srv.clear();
dlog << LWARN << "leaving thread, line: " << __LINE__;
return;
}
dlog << LTRACE << "try to connect to the server at port " << srv.get_listening_port(); void thread_proc (
status = create_connection(con,srv.get_listening_port(),ip); )
if (status) {
try
{ {
srv.tag = 3.0; dlog << LTRACE << "enter thread";
srv.error_occurred = true; {
srv.clear(); auto_mutex M(::m);
dlog << LWARN << "leaving thread, line: " << __LINE__; while (assigned_port == 0)
return; ::s.wait();
} }
dlog << LTRACE << "sending magic_num to server"; int status;
int i; scoped_ptr<connection> con;
for (i = 0; i < min_bytes_sent; ++i) string hostname;
{ string ip;
con->write(&magic_num,1); status = get_local_hostname(hostname);
} if (status)
{
srv.tag = 1.0;
srv.error_occurred = true;
srv.clear();
dlog << LERROR << "leaving thread, line: " << __LINE__;
dlog << LERROR << "get_local_hostname() failed";
return;
}
dlog << LTRACE << "shutting down connection to server"; status = hostname_to_ip(hostname,ip);
close_gracefully(con); if (status)
dlog << LTRACE << "finished calling close_gracefully() on the connection"; {
srv.tag = 2.0;
srv.error_occurred = true;
srv.clear();
dlog << LERROR << "leaving thread, line: " << __LINE__;
dlog << LERROR << "hostname_to_ip() failed";
return;
}
auto_mutex M(m); dlog << LTRACE << "try to connect to the server at port " << srv.get_listening_port();
--thread_count; status = create_connection(con,srv.get_listening_port(),ip);
s.broadcast(); if (status)
while (thread_count > 0) {
s.wait(); srv.tag = 3.0;
srv.error_occurred = true;
srv.clear();
dlog << LERROR << "leaving thread, line: " << __LINE__;
dlog << LERROR << "create_connection() failed";
return;
}
srv.clear(); dlog << LTRACE << "sending magic_num to server";
int i;
for (i = 0; i < min_bytes_sent; ++i)
{
con->write(&magic_num,1);
}
--thread_count2; dlog << LTRACE << "shutting down connection to server";
s.broadcast(); close_gracefully(con);
} dlog << LTRACE << "finished calling close_gracefully() on the connection";
catch (exception& e) }
{ catch (exception& e)
srv.error_occurred = true; {
dlog << LERROR << "exception thrown in thread_proc(): " << e.what(); srv.error_occurred = true;
cout << "exception thrown in thread_proc(): " << e.what(); dlog << LERROR << "exception thrown in thread_proc(): " << e.what();
cout << "exception thrown in thread_proc(): " << e.what();
}
dlog << LTRACE << "exit thread";
} }
dlog << LTRACE << "exit thread"; };
void run_server(serv* srv)
{
dlog << LTRACE << "calling srv.start()";
srv->start();
dlog << LTRACE << "srv.start() just ended.";
} }
void sockets_test ( void sockets_test (
...@@ -176,36 +197,29 @@ namespace ...@@ -176,36 +197,29 @@ namespace
dlog << LTRACE << "starting test"; dlog << LTRACE << "starting test";
serv srv; serv srv;
thread_count = 10;
assigned_port = 0; assigned_port = 0;
thread_count2 = thread_count;
dlog << LTRACE << "spawning threads"; dlog << LTRACE << "spawning threads";
int num = thread_count; thread_container stuff(srv);
for (int i = 0; i < num; ++i)
{
create_new_thread(thread_proc,&srv);
}
dlog << LTRACE << "calling srv.start()"; thread_function thread2(run_server, &srv);
srv.start();
dlog << LTRACE << "srv.start() just ended."; // wait until all the sending threads have ended
stuff.wait();
{
auto_mutex M(m);
while (thread_count2 > 0)
s.wait();
}
if (srv.error_occurred) if (srv.error_occurred)
{ {
dlog << LDEBUG << "tag: " << srv.tag; dlog << LDEBUG << "tag: " << srv.tag;
} }
srv.clear();
dlog << LTRACE << "ending successful test"; dlog << LTRACE << "ending successful test";
DLIB_CASSERT( !srv.error_occurred,""); DLIB_CASSERT( !srv.error_occurred,"");
DLIB_CASSERT( srv.got_connections,"");
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment