[7ca772] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2011 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 controller.cpp
|
---|
| 10 | *
|
---|
| 11 | * This file strongly follows the Serialization example from the boost::asio
|
---|
| 12 | * library (see client.cpp)
|
---|
| 13 | *
|
---|
| 14 | * Created on: Nov 27, 2011
|
---|
| 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 |
|
---|
[685e28] | 29 | #include <boost/assign.hpp>
|
---|
[af9b9ff] | 30 | #include <boost/program_options.hpp>
|
---|
[685e28] | 31 | #include <deque>
|
---|
[ff60cfa] | 32 | #include <fstream>
|
---|
[7ca772] | 33 | #include <iostream>
|
---|
| 34 | #include <map>
|
---|
[ff60cfa] | 35 | #include <sstream>
|
---|
| 36 | #include <streambuf>
|
---|
| 37 | #include <vector>
|
---|
[7ca772] | 38 |
|
---|
[cc5db5] | 39 | #include "Fragmentation/Automation/atexit.hpp"
|
---|
[7ca772] | 40 | #include "CodePatterns/Info.hpp"
|
---|
| 41 | #include "CodePatterns/Log.hpp"
|
---|
[cbcbbd] | 42 |
|
---|
[d4885d] | 43 | #include "FragmentController.hpp"
|
---|
[5fa1f0] | 44 | #include "ControllerOptions_MPQCCommandJob.hpp"
|
---|
[ee61ce] | 45 | #include "ControllerCommandRegistry.hpp"
|
---|
[7ca772] | 46 |
|
---|
[cbcbbd] | 47 | #include "controller_MPQCCommandJob.hpp"
|
---|
| 48 |
|
---|
[685e28] | 49 | /** Print the status of scheduled and done jobs.
|
---|
| 50 | *
|
---|
| 51 | * @param status pair of number of schedule and done jobs
|
---|
| 52 | */
|
---|
| 53 | void printJobStatus(const std::pair<size_t, size_t> &JobStatus)
|
---|
| 54 | {
|
---|
| 55 | LOG(1, "INFO: #" << JobStatus.first << " are waiting in the queue and #"
|
---|
| 56 | << JobStatus.second << " jobs are calculated so far.");
|
---|
| 57 | }
|
---|
[ff60cfa] | 58 |
|
---|
[cbcbbd] | 59 | inline std::vector<std::string> getListOfCommands(const ControllerCommandRegistry &ControllerCommands)
|
---|
[5a8512] | 60 | {
|
---|
[cbcbbd] | 61 | std::vector<std::string> Commands;
|
---|
| 62 | for (ControllerCommandRegistry::const_iterator iter = ControllerCommands.getBeginIter();
|
---|
| 63 | iter != ControllerCommands.getEndIter(); ++iter)
|
---|
| 64 | Commands.push_back(iter->first);
|
---|
[5a8512] | 65 |
|
---|
[cbcbbd] | 66 | return Commands;
|
---|
[685e28] | 67 | }
|
---|
| 68 |
|
---|
[7ca772] | 69 | int main(int argc, char* argv[])
|
---|
| 70 | {
|
---|
| 71 | // from this moment on, we need to be sure to deeinitialize in the correct order
|
---|
| 72 | // this is handled by the cleanup function
|
---|
| 73 | atexit(cleanUp);
|
---|
| 74 |
|
---|
| 75 | size_t Exitflag = 0;
|
---|
[685e28] | 76 |
|
---|
[cbcbbd] | 77 | ControllerOptions *ControllerInfo = allocateControllerInfo();
|
---|
[685e28] | 78 | boost::asio::io_service io_service;
|
---|
| 79 | FragmentController controller(io_service);
|
---|
| 80 | boost::program_options::variables_map vm;
|
---|
| 81 |
|
---|
[ee61ce] | 82 | // prepare ControllerCommand
|
---|
| 83 | // note: we need "< ControllerCommand::commands_t >" because parseExecutable(),... return int
|
---|
[685e28] | 84 | // in contrast to other functions that return void
|
---|
[ee61ce] | 85 | ControllerCommandRegistry ControllerCommands;
|
---|
[cbcbbd] | 86 | boost::function<void (ControllerCommand *)> registrator =
|
---|
| 87 | (boost::bind(&Registry<ControllerCommand>::registerInstance, boost::ref(ControllerCommands), _1));
|
---|
| 88 | registrator(new ControllerCommand("checkresults",
|
---|
[ee61ce] | 89 | boost::assign::list_of< ControllerCommand::commands_t >
|
---|
| 90 | (boost::bind(&FragmentController::checkResults,
|
---|
[cbcbbd] | 91 | boost::ref(controller), boost::cref(ControllerInfo->server), boost::cref(ControllerInfo->serverport)))
|
---|
[ee61ce] | 92 | (boost::bind(&printJobStatus,
|
---|
| 93 | boost::bind(&FragmentController::getJobStatus, boost::ref(controller))))
|
---|
| 94 | ));
|
---|
[cbcbbd] | 95 | registrator(new ControllerCommand("removeall",
|
---|
[ee61ce] | 96 | boost::assign::list_of< ControllerCommand::commands_t >
|
---|
| 97 | (boost::bind(&FragmentController::removeall,
|
---|
[cbcbbd] | 98 | boost::ref(controller), boost::cref(ControllerInfo->server), boost::cref(ControllerInfo->serverport)))
|
---|
[ee61ce] | 99 | ));
|
---|
[cbcbbd] | 100 | registrator(new ControllerCommand("shutdown",
|
---|
[ee61ce] | 101 | boost::assign::list_of< ControllerCommand::commands_t >
|
---|
| 102 | (boost::bind(&FragmentController::shutdown,
|
---|
[cbcbbd] | 103 | boost::ref(controller), boost::cref(ControllerInfo->server), boost::cref(ControllerInfo->serverport)))
|
---|
[ee61ce] | 104 | ));
|
---|
[cbcbbd] | 105 | addSpecificCommands(registrator, controller, *ControllerInfo, vm);
|
---|
[af9b9ff] | 106 |
|
---|
| 107 | // Declare the supported options.
|
---|
| 108 | boost::program_options::options_description desc("Allowed options");
|
---|
| 109 | desc.add_options()
|
---|
| 110 | ("help,h", "produce help message")
|
---|
| 111 | ("verbosity,v", boost::program_options::value<size_t>(), "set verbosity level")
|
---|
| 112 | ("server", boost::program_options::value< std::string >(), "connect to server at this address (host:port)")
|
---|
[cbcbbd] | 113 | ("command", boost::program_options::value< std::string >(), (std::string("command to send to server: ")+toString(getListOfCommands(ControllerCommands))).c_str())
|
---|
[af9b9ff] | 114 | ;
|
---|
[cbcbbd] | 115 | addSpecificOptions(desc.add_options());
|
---|
[af9b9ff] | 116 |
|
---|
[685e28] | 117 | // parse command line
|
---|
[af9b9ff] | 118 | boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
|
---|
| 119 | boost::program_options::notify(vm);
|
---|
| 120 |
|
---|
[685e28] | 121 | // set controller information
|
---|
[5fa1f0] | 122 | int status = 0;
|
---|
[cbcbbd] | 123 | status = ControllerInfo->parseHelp(vm, desc);
|
---|
[5fa1f0] | 124 | if (status) return status;
|
---|
[cbcbbd] | 125 | status = ControllerInfo->parseVerbosity(vm);
|
---|
[5fa1f0] | 126 | if (status) return status;
|
---|
[cbcbbd] | 127 | status = ControllerInfo->parseServerPort(vm);
|
---|
[5fa1f0] | 128 | if (status) return status;
|
---|
[cbcbbd] | 129 | status = ControllerInfo->parseCommand(vm, getListOfCommands(ControllerCommands));
|
---|
[5fa1f0] | 130 | if (status) return status;
|
---|
[9dcce3] | 131 |
|
---|
[685e28] | 132 | // all later parse functions depend on parsed command
|
---|
[cbcbbd] | 133 | status = addOtherParsings(*ControllerInfo, vm, ControllerCommands);
|
---|
[5fa1f0] | 134 | if (status) return status;
|
---|
[685e28] | 135 |
|
---|
| 136 | // parse given ControllerCommand
|
---|
[cbcbbd] | 137 | if(!ControllerCommands.isPresentByName(ControllerInfo->command)) {
|
---|
| 138 | ELOG(1, "Unrecognized command '"+toString(ControllerInfo->command)+"'.");
|
---|
[685e28] | 139 | return 255;
|
---|
| 140 | }
|
---|
[cbcbbd] | 141 | const ControllerCommand *commands = ControllerCommands.getByName(ControllerInfo->command);
|
---|
[7ca772] | 142 | try
|
---|
| 143 | {
|
---|
[685e28] | 144 | // execute each command in the queue synchronously
|
---|
| 145 | size_t phase = 1;
|
---|
[ee61ce] | 146 | for (ControllerCommand::const_iterator iter = commands->begin();
|
---|
| 147 | iter != commands->end(); ++iter, ++phase) {
|
---|
| 148 | (*iter)();
|
---|
[7ca772] | 149 | {
|
---|
[685e28] | 150 | io_service.reset();
|
---|
| 151 | Info info((std::string("io_service: ")+toString(phase)).c_str());
|
---|
| 152 | io_service.run();
|
---|
[7ca772] | 153 | }
|
---|
| 154 | }
|
---|
| 155 | Exitflag = controller.getExitflag();
|
---|
| 156 | }
|
---|
| 157 | catch (std::exception& e)
|
---|
| 158 | {
|
---|
| 159 | std::cerr << e.what() << std::endl;
|
---|
| 160 | }
|
---|
[cbcbbd] | 161 | delete ControllerInfo;
|
---|
[7ca772] | 162 |
|
---|
| 163 | return Exitflag;
|
---|
| 164 | }
|
---|
[4dd16e] | 165 |
|
---|