1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2012 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * \file poolworker.cpp
|
---|
10 | *
|
---|
11 | * This file strongly follows the Serialization example from the boost::asio
|
---|
12 | * library (see client.cpp)
|
---|
13 | *
|
---|
14 | * Created on: Feb 28, 2012
|
---|
15 | * Author: heber
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | // include config.h
|
---|
20 | #ifdef HAVE_CONFIG_H
|
---|
21 | #include <config.h>
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | // boost asio needs specific operator new
|
---|
25 | #include <boost/asio.hpp>
|
---|
26 |
|
---|
27 | #include "CodePatterns/MemDebug.hpp"
|
---|
28 |
|
---|
29 | #include <iostream>
|
---|
30 | #include <boost/program_options.hpp>
|
---|
31 | #include <boost/lexical_cast.hpp>
|
---|
32 | #include <signal.h>
|
---|
33 | #include <vector>
|
---|
34 |
|
---|
35 | #include "Fragmentation/Automation/atexit.hpp"
|
---|
36 | #include "CodePatterns/Info.hpp"
|
---|
37 | #include "CodePatterns/Log.hpp"
|
---|
38 | #include "Pool/PoolWorker.hpp"
|
---|
39 | #include "atexit.hpp"
|
---|
40 | #include "Jobs/SystemCommandJob.hpp"
|
---|
41 | #include "WorkerOptions.hpp"
|
---|
42 |
|
---|
43 | //!> global shutdown function
|
---|
44 | boost::function<void (int)> shutdownfunction;
|
---|
45 | //!> global signal vector for later releasing
|
---|
46 | std::vector<size_t> signals;
|
---|
47 | //!> counter for the number of received signals
|
---|
48 | size_t NoSignalsReceived = 0;
|
---|
49 | //!> maximum number of received signals after which the handlers are released
|
---|
50 | const size_t MAX_NOSIGNALSRECEIVED = 3;
|
---|
51 |
|
---|
52 | void signalhandler(int sig)
|
---|
53 | {
|
---|
54 | // increment received signal counter
|
---|
55 | ++NoSignalsReceived;
|
---|
56 |
|
---|
57 | // shutdown if we have handler
|
---|
58 | if (shutdownfunction)
|
---|
59 | shutdownfunction(sig);
|
---|
60 |
|
---|
61 | if (NoSignalsReceived >= MAX_NOSIGNALSRECEIVED) {
|
---|
62 | // release signal hook again
|
---|
63 | for (std::vector<size_t>::const_iterator iter = signals.begin();
|
---|
64 | iter != signals.end(); ++iter)
|
---|
65 | signal(*iter, NULL);
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | int main(int argc, char* argv[])
|
---|
71 | {
|
---|
72 | // from this moment on, we need to be sure to deeinitialize in the correct order
|
---|
73 | // this is handled by the cleanup function
|
---|
74 | atexit(cleanUp);
|
---|
75 |
|
---|
76 | WorkerOptions WorkerOpts;
|
---|
77 | boost::program_options::variables_map vm;
|
---|
78 |
|
---|
79 | // Declare the supported options.
|
---|
80 | boost::program_options::options_description desc("Allowed options");
|
---|
81 | desc.add_options()
|
---|
82 | ("help,h", "produce help message")
|
---|
83 | ("verbosity,v", boost::program_options::value<size_t>(), "set verbosity level")
|
---|
84 | ("signal", boost::program_options::value< std::vector<size_t> >(), "set signal to catch (can be given multiple times)")
|
---|
85 | ("server", boost::program_options::value< std::string>(), "connect to server at this address (host:port)")
|
---|
86 | ("listen", boost::program_options::value< std::string >(), "listen on this port")
|
---|
87 | ("hostname", boost::program_options::value< std::string>(), "name of host on which this codes runs and which server can resolve")
|
---|
88 | ;
|
---|
89 |
|
---|
90 | boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
|
---|
91 | boost::program_options::notify(vm);
|
---|
92 |
|
---|
93 | int status = 0;
|
---|
94 | if (status) return status;
|
---|
95 | status = WorkerOpts.parseHelp(vm, desc);
|
---|
96 | if (status) return status;
|
---|
97 | status = WorkerOpts.parseVerbosity(vm);
|
---|
98 | if (status) return status;
|
---|
99 | status = WorkerOpts.parseServer(vm);
|
---|
100 | if (status) return status;
|
---|
101 | status = WorkerOpts.parseListenPort(vm);
|
---|
102 | if (status) return status;
|
---|
103 | status = WorkerOpts.parseLocalhost(vm);
|
---|
104 | if (status) return status;
|
---|
105 | status = WorkerOpts.parseSignals(vm);
|
---|
106 | if (status) return status;
|
---|
107 |
|
---|
108 | size_t exitflag = 0;
|
---|
109 | try
|
---|
110 | {
|
---|
111 | boost::asio::io_service io_service;
|
---|
112 | PoolWorker client(io_service, WorkerOpts.server, WorkerOpts.serverport, WorkerOpts.hostname, WorkerOpts.listenport);
|
---|
113 |
|
---|
114 | // catch ctrl-c and shutdown worker properly
|
---|
115 | shutdownfunction = boost::bind(&PoolWorker::shutdown, boost::ref(client), _1);
|
---|
116 | for (std::vector<size_t>::const_iterator iter = WorkerOpts.signals.begin();
|
---|
117 | iter != WorkerOpts.signals.end(); ++iter) {
|
---|
118 | LOG(0, "STATUS: Catching signal " << *iter << " via signal handler.");
|
---|
119 | signal(*iter, &signalhandler);
|
---|
120 | }
|
---|
121 |
|
---|
122 | // process io requests
|
---|
123 | {
|
---|
124 | Info info("io_service");
|
---|
125 | io_service.run();
|
---|
126 | }
|
---|
127 | exitflag = client.getFlag();
|
---|
128 | }
|
---|
129 | catch (std::exception& e)
|
---|
130 | {
|
---|
131 | std::cerr << e.what() << std::endl;
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | return exitflag;
|
---|
136 | }
|
---|