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