| 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 <vector> | 
|---|
| 33 |  | 
|---|
| 34 | #include "Fragmentation/Automation/atexit.hpp" | 
|---|
| 35 | #include "CodePatterns/Info.hpp" | 
|---|
| 36 | #include "CodePatterns/Log.hpp" | 
|---|
| 37 | #include "Pool/PoolWorker.hpp" | 
|---|
| 38 | #include "atexit.hpp" | 
|---|
| 39 | #include "Jobs/SystemCommandJob.hpp" | 
|---|
| 40 | #include "SignalHandler.hpp" | 
|---|
| 41 | #include "WorkerOptions.hpp" | 
|---|
| 42 |  | 
|---|
| 43 |  | 
|---|
| 44 | int main(int argc, char* argv[]) | 
|---|
| 45 | { | 
|---|
| 46 | // from this moment on, we need to be sure to deeinitialize in the correct order | 
|---|
| 47 | // this is handled by the cleanup function | 
|---|
| 48 | atexit(cleanUp); | 
|---|
| 49 |  | 
|---|
| 50 | WorkerOptions WorkerOpts; | 
|---|
| 51 | boost::program_options::variables_map vm; | 
|---|
| 52 |  | 
|---|
| 53 | // Declare the supported options. | 
|---|
| 54 | boost::program_options::options_description desc("Allowed options"); | 
|---|
| 55 | desc.add_options() | 
|---|
| 56 | ("help,h", "produce help message") | 
|---|
| 57 | ("verbosity,v", boost::program_options::value<size_t>(), "set verbosity level") | 
|---|
| 58 | ("signal", boost::program_options::value< std::vector<size_t> >(), "set signal to catch (can be given multiple times)") | 
|---|
| 59 | ("server", boost::program_options::value< std::string>(), "connect to server at this address (host:port)") | 
|---|
| 60 | ("listen", boost::program_options::value< std::string >(), "listen on this port") | 
|---|
| 61 | ("hostname", boost::program_options::value< std::string>(), "name of host on which this codes runs and which server can resolve") | 
|---|
| 62 | ; | 
|---|
| 63 |  | 
|---|
| 64 | boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm); | 
|---|
| 65 | boost::program_options::notify(vm); | 
|---|
| 66 |  | 
|---|
| 67 | int status = 0; | 
|---|
| 68 | if (status) return status; | 
|---|
| 69 | status = WorkerOpts.parseHelp(vm, desc); | 
|---|
| 70 | if (status) return status; | 
|---|
| 71 | status = WorkerOpts.parseVerbosity(vm); | 
|---|
| 72 | if (status) return status; | 
|---|
| 73 | status = WorkerOpts.parseServer(vm); | 
|---|
| 74 | if (status) return status; | 
|---|
| 75 | status = WorkerOpts.parseListenPort(vm); | 
|---|
| 76 | if (status) return status; | 
|---|
| 77 | status = WorkerOpts.parseLocalhost(vm); | 
|---|
| 78 | if (status) return status; | 
|---|
| 79 | status = WorkerOpts.parseSignals(vm); | 
|---|
| 80 | if (status) return status; | 
|---|
| 81 |  | 
|---|
| 82 | size_t exitflag = 0; | 
|---|
| 83 | try | 
|---|
| 84 | { | 
|---|
| 85 | boost::asio::io_service io_service; | 
|---|
| 86 | PoolWorker client(io_service, WorkerOpts.server, WorkerOpts.serverport, WorkerOpts.hostname, WorkerOpts.listenport); | 
|---|
| 87 |  | 
|---|
| 88 | // catch ctrl-c and shutdown worker properly | 
|---|
| 89 | boost::function<void (int)> shutdownfunction = boost::bind(&PoolWorker::shutdown, boost::ref(client), _1); | 
|---|
| 90 | SignalHandler signalhandler(shutdownfunction, WorkerOpts.signals); | 
|---|
| 91 |  | 
|---|
| 92 | // process io requests | 
|---|
| 93 | { | 
|---|
| 94 | Info info("io_service"); | 
|---|
| 95 | io_service.run(); | 
|---|
| 96 | } | 
|---|
| 97 | exitflag = client.getFlag(); | 
|---|
| 98 | } | 
|---|
| 99 | catch (std::exception& e) | 
|---|
| 100 | { | 
|---|
| 101 | std::cerr << e.what() << std::endl; | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 |  | 
|---|
| 105 | return exitflag; | 
|---|
| 106 | } | 
|---|