diff options
Diffstat (limited to 'src/bitz/manager.cpp')
-rw-r--r-- | src/bitz/manager.cpp | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/src/bitz/manager.cpp b/src/bitz/manager.cpp index ffa2dae..14033a5 100644 --- a/src/bitz/manager.cpp +++ b/src/bitz/manager.cpp @@ -31,8 +31,11 @@ namespace bitz { Manager::Manager( unsigned short port, const std::string &address, int backlog ) throw( ManagerException ) { // initialise manager - _manager.worker = false; - _manager.max_workers = 0; + _manager.worker = false; + _manager.max_workers = 0; + _manager.max_worker_requests = 0; + _manager.comm_timeout = 0; + _manager.workers_count = 0; _manager.worker_id = 0; _manager.socket = NULL; @@ -40,13 +43,16 @@ namespace bitz { // initialise listening socket try { - if ( address.empty() ) { - _manager.socket = new socketlibrary::TCPServerSocket( port, backlog ); - } else { - _manager.socket = new socketlibrary::TCPServerSocket( address, port, backlog ); - } - } catch ( socketlibrary::SocketException &sex ) { - throw ManagerException( "failed to initialise socket" ); + + // network socket address + psocksxx::nsockaddr naddr( address.c_str(), port ); + + _manager.socket = new psocksxx::tcpnsockstream(); + _manager.socket->bind( &naddr, true ); + _manager.socket->listen( backlog ); + + } catch ( psocksxx::sockexception &e ) { + throw ManagerException( std::string( "failed to initialise socket, " ).append( e.what() ) ); } Logger &logger = Logger::instance(); @@ -71,10 +77,11 @@ namespace bitz { } - void Manager::spawn( unsigned int max_workers, unsigned int max_worker_requests ) throw( ManagerException ) { + void Manager::spawn( unsigned int max_workers, unsigned int max_worker_requests, unsigned int comm_timeout ) throw( ManagerException ) { _manager.max_workers = max_workers; _manager.max_worker_requests = max_worker_requests; + _manager.comm_timeout = comm_timeout; _manager.worker_pool = new worker_pool_t[max_workers]; // pre-fork workers @@ -114,7 +121,7 @@ namespace bitz { _manager.worker_pool[worker_id].worker_id = worker_id; _manager.worker_pool[worker_id].worker_pid = worker_pid; - _manager.worker_pool[worker_id].worker->run( _manager.socket, _manager.max_worker_requests ); + _manager.worker_pool[worker_id].worker->run( _manager.socket, _manager.max_worker_requests, _manager.comm_timeout ); logger.info( std::string( "end of cycle, worker[" ).append( util::itoa( worker_id ) ).append( "]" ) ); delete _manager.worker_pool[worker_id].worker; @@ -200,6 +207,9 @@ namespace bitz { void Manager::manager_workers() throw() { + // logger + Logger &logger = Logger::instance(); + if (! _manager.worker ) { // check the worker count @@ -208,8 +218,12 @@ namespace bitz { // we are missing workers, find out who for (unsigned int i = 0; i < _manager.max_workers; i++ ) { if ( _manager.worker_pool[i].worker_pid == 0 ) { - // spawn a worker for the missing - spawn_worker( i ); + try { + // spawn a worker for the missing + spawn_worker( i ); + } catch ( ManagerException &mex ) { + logger.warn( std::string( "[manager] failed to spawn worker[" ).append( util::itoa( i ) ).append( "], exception: ").append( mex.what() ) ); + } } } |