mirror of
https://github.com/davisking/dlib.git
synced 2024-11-01 10:14:53 +08:00
cleaned up the sockets test
--HG-- extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402497
This commit is contained in:
parent
aa34d39f27
commit
b5578687c8
@ -12,8 +12,7 @@
|
||||
|
||||
#include "tester.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
|
||||
using namespace test;
|
||||
using namespace dlib;
|
||||
@ -23,8 +22,6 @@ namespace
|
||||
dlib::signaler s(m);
|
||||
const char magic_num = 42;
|
||||
const int min_bytes_sent = 10000;
|
||||
int thread_count;
|
||||
int thread_count2;
|
||||
int assigned_port;
|
||||
|
||||
logger dlog("test.sockets");
|
||||
@ -36,7 +33,8 @@ namespace
|
||||
public:
|
||||
serv (
|
||||
) :
|
||||
error_occurred (false)
|
||||
error_occurred (false),
|
||||
got_connections(false)
|
||||
{}
|
||||
|
||||
void on_listening_port_assigned (
|
||||
@ -73,93 +71,116 @@ namespace
|
||||
tag = 5.0;
|
||||
error_occurred = true;
|
||||
}
|
||||
got_connections = true;
|
||||
dlog << LINFO << "in serv::on_connect(): on_connect ending";
|
||||
}
|
||||
|
||||
bool error_occurred;
|
||||
bool got_connections;
|
||||
double tag;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
void thread_proc (
|
||||
void* param
|
||||
)
|
||||
class thread_container : public multithreaded_object
|
||||
{
|
||||
serv& srv = *reinterpret_cast<serv*>(param);
|
||||
try
|
||||
public:
|
||||
|
||||
serv& srv;
|
||||
|
||||
thread_container (
|
||||
serv& srv_
|
||||
) : srv(srv_)
|
||||
{
|
||||
dlog << LTRACE << "enter thread";
|
||||
{
|
||||
auto_mutex M(m);
|
||||
while (assigned_port == 0)
|
||||
s.wait();
|
||||
}
|
||||
for (int i = 0; i < 10; ++i)
|
||||
register_thread(*this, &thread_container::thread_proc);
|
||||
|
||||
int status;
|
||||
connection* con;
|
||||
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);
|
||||
if (status)
|
||||
{
|
||||
srv.tag = 2.0;
|
||||
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();
|
||||
status = create_connection(con,srv.get_listening_port(),ip);
|
||||
if (status)
|
||||
{
|
||||
srv.tag = 3.0;
|
||||
srv.error_occurred = true;
|
||||
srv.clear();
|
||||
dlog << LWARN << "leaving thread, line: " << __LINE__;
|
||||
return;
|
||||
}
|
||||
|
||||
dlog << LTRACE << "sending magic_num to server";
|
||||
int i;
|
||||
for (i = 0; i < min_bytes_sent; ++i)
|
||||
{
|
||||
con->write(&magic_num,1);
|
||||
}
|
||||
|
||||
dlog << LTRACE << "shutting down connection to server";
|
||||
close_gracefully(con);
|
||||
dlog << LTRACE << "finished calling close_gracefully() on the connection";
|
||||
|
||||
auto_mutex M(m);
|
||||
--thread_count;
|
||||
s.broadcast();
|
||||
while (thread_count > 0)
|
||||
s.wait();
|
||||
|
||||
srv.clear();
|
||||
|
||||
--thread_count2;
|
||||
s.broadcast();
|
||||
// start up the threads
|
||||
start();
|
||||
}
|
||||
catch (exception& e)
|
||||
|
||||
~thread_container ()
|
||||
{
|
||||
srv.error_occurred = true;
|
||||
dlog << LERROR << "exception thrown in thread_proc(): " << e.what();
|
||||
cout << "exception thrown in thread_proc(): " << e.what();
|
||||
// wait for all threads to terminate
|
||||
wait();
|
||||
}
|
||||
dlog << LTRACE << "exit thread";
|
||||
|
||||
void thread_proc (
|
||||
)
|
||||
{
|
||||
try
|
||||
{
|
||||
dlog << LTRACE << "enter thread";
|
||||
{
|
||||
auto_mutex M(::m);
|
||||
while (assigned_port == 0)
|
||||
::s.wait();
|
||||
}
|
||||
|
||||
int status;
|
||||
scoped_ptr<connection> con;
|
||||
string hostname;
|
||||
string ip;
|
||||
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;
|
||||
}
|
||||
|
||||
status = hostname_to_ip(hostname,ip);
|
||||
if (status)
|
||||
{
|
||||
srv.tag = 2.0;
|
||||
srv.error_occurred = true;
|
||||
srv.clear();
|
||||
dlog << LERROR << "leaving thread, line: " << __LINE__;
|
||||
dlog << LERROR << "hostname_to_ip() failed";
|
||||
return;
|
||||
}
|
||||
|
||||
dlog << LTRACE << "try to connect to the server at port " << srv.get_listening_port();
|
||||
status = create_connection(con,srv.get_listening_port(),ip);
|
||||
if (status)
|
||||
{
|
||||
srv.tag = 3.0;
|
||||
srv.error_occurred = true;
|
||||
srv.clear();
|
||||
dlog << LERROR << "leaving thread, line: " << __LINE__;
|
||||
dlog << LERROR << "create_connection() failed";
|
||||
return;
|
||||
}
|
||||
|
||||
dlog << LTRACE << "sending magic_num to server";
|
||||
int i;
|
||||
for (i = 0; i < min_bytes_sent; ++i)
|
||||
{
|
||||
con->write(&magic_num,1);
|
||||
}
|
||||
|
||||
dlog << LTRACE << "shutting down connection to server";
|
||||
close_gracefully(con);
|
||||
dlog << LTRACE << "finished calling close_gracefully() on the connection";
|
||||
}
|
||||
catch (exception& e)
|
||||
{
|
||||
srv.error_occurred = true;
|
||||
dlog << LERROR << "exception thrown in thread_proc(): " << e.what();
|
||||
cout << "exception thrown in thread_proc(): " << e.what();
|
||||
}
|
||||
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 (
|
||||
@ -176,36 +197,29 @@ namespace
|
||||
dlog << LTRACE << "starting test";
|
||||
serv srv;
|
||||
|
||||
thread_count = 10;
|
||||
assigned_port = 0;
|
||||
thread_count2 = thread_count;
|
||||
|
||||
|
||||
dlog << LTRACE << "spawning threads";
|
||||
int num = thread_count;
|
||||
for (int i = 0; i < num; ++i)
|
||||
{
|
||||
create_new_thread(thread_proc,&srv);
|
||||
}
|
||||
thread_container stuff(srv);
|
||||
|
||||
|
||||
|
||||
dlog << LTRACE << "calling srv.start()";
|
||||
srv.start();
|
||||
dlog << LTRACE << "srv.start() just ended.";
|
||||
thread_function thread2(run_server, &srv);
|
||||
|
||||
// wait until all the sending threads have ended
|
||||
stuff.wait();
|
||||
|
||||
{
|
||||
auto_mutex M(m);
|
||||
while (thread_count2 > 0)
|
||||
s.wait();
|
||||
}
|
||||
if (srv.error_occurred)
|
||||
{
|
||||
dlog << LDEBUG << "tag: " << srv.tag;
|
||||
}
|
||||
|
||||
srv.clear();
|
||||
|
||||
dlog << LTRACE << "ending successful test";
|
||||
DLIB_CASSERT( !srv.error_occurred,"");
|
||||
DLIB_CASSERT( srv.got_connections,"");
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
Loading…
Reference in New Issue
Block a user