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_main.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 |
|
---|
29 | #include <boost/assign.hpp>
|
---|
30 | #include <boost/program_options.hpp>
|
---|
31 | #include <deque>
|
---|
32 | #include <fstream>
|
---|
33 | #include <iostream>
|
---|
34 | #include <map>
|
---|
35 | #include <sstream>
|
---|
36 | #include <streambuf>
|
---|
37 | #include <vector>
|
---|
38 |
|
---|
39 | #include "CodePatterns/Assert.hpp"
|
---|
40 | #include "CodePatterns/Info.hpp"
|
---|
41 | #include "CodePatterns/Log.hpp"
|
---|
42 | #include "atexit.hpp"
|
---|
43 | #include "FragmentController.hpp"
|
---|
44 |
|
---|
45 | #include "ControllerOptions_SystemCommandJob.hpp"
|
---|
46 | #include "ControllerCommandRegistry.hpp"
|
---|
47 |
|
---|
48 | #include "controller_AddOn.hpp"
|
---|
49 |
|
---|
50 | /** Print the status of scheduled and done jobs.
|
---|
51 | *
|
---|
52 | * @param status pair of number of schedule and done jobs
|
---|
53 | */
|
---|
54 | void printJobStatus(const std::pair<size_t, size_t> &JobStatus)
|
---|
55 | {
|
---|
56 | LOG(1, "INFO: #" << JobStatus.first << " are waiting in the queue and #"
|
---|
57 | << JobStatus.second << " jobs are calculated so far.");
|
---|
58 | }
|
---|
59 |
|
---|
60 | inline std::vector<std::string> getListOfCommands(const ControllerCommandRegistry &ControllerCommands)
|
---|
61 | {
|
---|
62 | std::vector<std::string> Commands;
|
---|
63 | for (ControllerCommandRegistry::const_iterator iter = ControllerCommands.getBeginIter();
|
---|
64 | iter != ControllerCommands.getEndIter(); ++iter)
|
---|
65 | Commands.push_back(iter->first);
|
---|
66 |
|
---|
67 | return Commands;
|
---|
68 | }
|
---|
69 |
|
---|
70 | int controller_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 | size_t Exitflag = 0;
|
---|
77 |
|
---|
78 | controller_AddOn *AddOn = getAddOn();
|
---|
79 | ASSERT(AddOn != NULL,
|
---|
80 | "main() - returned AddOn is NULL.");
|
---|
81 |
|
---|
82 | ControllerOptions *ControllerInfo = AddOn->allocateControllerInfo();
|
---|
83 | boost::asio::io_service io_service;
|
---|
84 | FragmentController controller(io_service);
|
---|
85 | boost::program_options::variables_map vm;
|
---|
86 |
|
---|
87 | // prepare ControllerCommand
|
---|
88 | // note: we need "< ControllerCommand::commands_t >" because parseExecutable(),... return int
|
---|
89 | // in contrast to other functions that return void
|
---|
90 | ControllerCommandRegistry ControllerCommands;
|
---|
91 | boost::function<void (ControllerCommand *)> registrator =
|
---|
92 | (boost::bind(&Registry<ControllerCommand>::registerInstance, boost::ref(ControllerCommands), _1));
|
---|
93 | registrator(new ControllerCommand("checkresults",
|
---|
94 | boost::assign::list_of< ControllerCommand::commands_t >
|
---|
95 | (boost::bind(&FragmentController::checkResults,
|
---|
96 | boost::ref(controller), boost::cref(ControllerInfo->server), boost::cref(ControllerInfo->serverport)))
|
---|
97 | (boost::bind(&printJobStatus,
|
---|
98 | boost::bind(&FragmentController::getJobStatus, boost::ref(controller))))
|
---|
99 | ));
|
---|
100 | registrator(new ControllerCommand("removeall",
|
---|
101 | boost::assign::list_of< ControllerCommand::commands_t >
|
---|
102 | (boost::bind(&FragmentController::removeall,
|
---|
103 | boost::ref(controller), boost::cref(ControllerInfo->server), boost::cref(ControllerInfo->serverport)))
|
---|
104 | ));
|
---|
105 | registrator(new ControllerCommand("shutdown",
|
---|
106 | boost::assign::list_of< ControllerCommand::commands_t >
|
---|
107 | (boost::bind(&FragmentController::shutdown,
|
---|
108 | boost::ref(controller), boost::cref(ControllerInfo->server), boost::cref(ControllerInfo->serverport)))
|
---|
109 | ));
|
---|
110 | AddOn->addSpecificCommands(registrator, controller, *ControllerInfo);
|
---|
111 |
|
---|
112 | // Declare the supported options.
|
---|
113 | boost::program_options::options_description desc("Allowed options");
|
---|
114 | desc.add_options()
|
---|
115 | ("help,h", "produce help message")
|
---|
116 | ("verbosity,v", boost::program_options::value<size_t>(), "set verbosity level")
|
---|
117 | ("server", boost::program_options::value< std::string >(), "connect to server at this address (host:port)")
|
---|
118 | ("command", boost::program_options::value< std::string >(), (std::string("command to send to server: ")+toString(getListOfCommands(ControllerCommands))).c_str())
|
---|
119 | ;
|
---|
120 | AddOn->addSpecificOptions(desc.add_options());
|
---|
121 |
|
---|
122 | // parse command line
|
---|
123 | boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
|
---|
124 | boost::program_options::notify(vm);
|
---|
125 |
|
---|
126 | // set controller information
|
---|
127 | int status = 0;
|
---|
128 | status = ControllerInfo->parseHelp(vm, desc);
|
---|
129 | if (status) return status;
|
---|
130 | status = ControllerInfo->parseVerbosity(vm);
|
---|
131 | if (status) return status;
|
---|
132 | status = ControllerInfo->parseServerPort(vm);
|
---|
133 | if (status) return status;
|
---|
134 | status = ControllerInfo->parseCommand(vm, getListOfCommands(ControllerCommands));
|
---|
135 | if (status) return status;
|
---|
136 |
|
---|
137 | // all later parse functions depend on parsed command
|
---|
138 | status = AddOn->addOtherParsings(*ControllerInfo, vm);
|
---|
139 | if (status) return status;
|
---|
140 |
|
---|
141 | // parse given ControllerCommand
|
---|
142 | if(!ControllerCommands.isPresentByName(ControllerInfo->command)) {
|
---|
143 | ELOG(1, "Unrecognized command '"+toString(ControllerInfo->command)+"'.");
|
---|
144 | return 255;
|
---|
145 | }
|
---|
146 | const ControllerCommand *commands = ControllerCommands.getByName(ControllerInfo->command);
|
---|
147 | try
|
---|
148 | {
|
---|
149 | // execute each command in the queue synchronously
|
---|
150 | size_t phase = 1;
|
---|
151 | for (ControllerCommand::const_iterator iter = commands->begin();
|
---|
152 | iter != commands->end(); ++iter) {
|
---|
153 | (*iter)();
|
---|
154 | {
|
---|
155 | io_service.reset();
|
---|
156 | Info info((std::string("io_service: ")+toString(phase)).c_str());
|
---|
157 | io_service.run();
|
---|
158 | }
|
---|
159 | }
|
---|
160 | Exitflag = controller.getExitflag();
|
---|
161 | }
|
---|
162 | catch (std::exception& e)
|
---|
163 | {
|
---|
164 | std::cerr << e.what() << std::endl;
|
---|
165 | }
|
---|
166 | delete AddOn;
|
---|
167 |
|
---|
168 | return Exitflag;
|
---|
169 | }
|
---|
170 |
|
---|