-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
53 lines (43 loc) · 1.44 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <vector>
#include "network.h"
#include "threads.h"
#include "exceptions.h"
#include "signals/signals.h"
int main( int argc, char * argv[] ) {
try {
signalNS::blockSignals();
signalNS::setEmptyHandler( SIGUSR1 );
config.load( argc, argv );
Log.init( config.getLogFile() );
Log.welcome();
Log.options();
CThreadSafeQueue<CRequest *, nullptr> queue( config.getPortCount() );
CRunnable::ptr_t signalHandler( new CSignalThread( &queue ) );
vector<CRunnable::ptr_t> ports;
for ( unsigned int i = 0; i < config.getPortCount(); i++ ) {
try {
ports.push_back( CRunnable::ptr_t( new CPortThread( i, config.getPorts()[i].c_str(), 10, &queue ) ) );
Log.ger<>( NETWORK, "Port", config.getPorts()[i], "obtained" );
}
catch ( PortInitializationException& e ) {
queue.notifyProducerStopped();
Log.ger<>( CRITICAL, e );
}
}
vector<CRunnable::ptr_t> workers;
for ( unsigned int i = 0; i < config.getWorkerCount(); i++ )
workers.push_back( CRunnable::ptr_t( new CWorkerThread( i, config.getWorkDir().c_str(), &queue ) ) );
CThreadPool sigThread( signalHandler );
CThreadPool portThreads( ports.begin(), ports.end() );
CThreadPool workerThreads( workers.begin(), workers.end() );
Log.ger<>( CRITICAL, "Server started" );
workerThreads.join();
portThreads.stop();
sigThread.stop();
Log.ger<>( CRITICAL, "Server stoped" );
}
catch ( GeneralException& e ) {
Log.ger<>( CRITICAL, e );
}
return 0;
}