1 | /*
|
---|
2 | * Project: JobMarket
|
---|
3 | * Description: asynchronous Server/Controller/Client-approach to parallel computing, based on boost::asio
|
---|
4 | * Copyright (C) 2011 Frederik Heber. All rights reserved.
|
---|
5 | *
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * \file controller_main.cpp
|
---|
10 | *
|
---|
11 | * Created on: Nov 27, 2011
|
---|
12 | * Author: heber
|
---|
13 | */
|
---|
14 |
|
---|
15 |
|
---|
16 | // include config.h
|
---|
17 | #ifdef HAVE_CONFIG_H
|
---|
18 | #include <config.h>
|
---|
19 | #endif
|
---|
20 |
|
---|
21 | // boost asio needs specific operator new
|
---|
22 | #include <boost/asio.hpp>
|
---|
23 | // program_options must have some strange static stuff, causes double free or
|
---|
24 | // corruption if included after MemDebug
|
---|
25 | #include <boost/program_options.hpp>
|
---|
26 |
|
---|
27 | #include "CodePatterns/MemDebug.hpp"
|
---|
28 |
|
---|
29 | #include <boost/assign.hpp>
|
---|
30 | #include <deque>
|
---|
31 | #include <fstream>
|
---|
32 | #include <iostream>
|
---|
33 | #include <map>
|
---|
34 | #include <sstream>
|
---|
35 | #include <streambuf>
|
---|
36 | #include <vector>
|
---|
37 |
|
---|
38 | #include "CodePatterns/Assert.hpp"
|
---|
39 | #include "CodePatterns/Info.hpp"
|
---|
40 | #include "CodePatterns/Log.hpp"
|
---|
41 |
|
---|
42 | #include "JobMarket/atexit.hpp"
|
---|
43 | #include "JobMarket/Controller/controller_AddOn.hpp"
|
---|
44 | #include "JobMarket/Controller/ControllerCommandRegistry.hpp"
|
---|
45 | #include "JobMarket/Controller/ControllerOptions.hpp"
|
---|
46 | #include "JobMarket/Controller/FragmentController.hpp"
|
---|
47 |
|
---|
48 |
|
---|
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 | }
|
---|
58 |
|
---|
59 | inline std::vector<std::string> getListOfCommands(const ControllerCommandRegistry &ControllerCommands)
|
---|
60 | {
|
---|
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);
|
---|
65 |
|
---|
66 | return Commands;
|
---|
67 | }
|
---|
68 |
|
---|
69 | int controller_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;
|
---|
76 |
|
---|
77 | controller_AddOn *AddOn = getAddOn();
|
---|
78 | ASSERT(AddOn != NULL,
|
---|
79 | "main() - returned AddOn is NULL.");
|
---|
80 |
|
---|
81 | ControllerOptions *ControllerInfo = AddOn->allocateControllerInfo();
|
---|
82 | boost::asio::io_service io_service;
|
---|
83 | FragmentController controller(io_service);
|
---|
84 | boost::program_options::variables_map vm;
|
---|
85 |
|
---|
86 | // prepare ControllerCommand
|
---|
87 | // note: we need "< ControllerCommand::commands_t >" because parseExecutable(),... return int
|
---|
88 | // in contrast to other functions that return void
|
---|
89 | ControllerCommandRegistry ControllerCommands;
|
---|
90 | boost::function<void (ControllerCommand *)> registrator =
|
---|
91 | (boost::bind(&Registry<ControllerCommand>::registerInstance, boost::ref(ControllerCommands), _1));
|
---|
92 | registrator(new ControllerCommand("checkresults",
|
---|
93 | boost::assign::list_of< ControllerCommand::commands_t >
|
---|
94 | (boost::bind(&FragmentController::checkResults,
|
---|
95 | boost::ref(controller), boost::cref(ControllerInfo->server), boost::cref(ControllerInfo->serverport)))
|
---|
96 | (boost::bind(&printJobStatus,
|
---|
97 | boost::bind(&FragmentController::getJobStatus, boost::ref(controller))))
|
---|
98 | ));
|
---|
99 | registrator(new ControllerCommand("removeall",
|
---|
100 | boost::assign::list_of< ControllerCommand::commands_t >
|
---|
101 | (boost::bind(&FragmentController::removeall,
|
---|
102 | boost::ref(controller), boost::cref(ControllerInfo->server), boost::cref(ControllerInfo->serverport)))
|
---|
103 | ));
|
---|
104 | registrator(new ControllerCommand("removealljobs",
|
---|
105 | boost::assign::list_of< ControllerCommand::commands_t >
|
---|
106 | (boost::bind(&FragmentController::removeWaitingJobs,
|
---|
107 | boost::ref(controller), boost::cref(ControllerInfo->server), boost::cref(ControllerInfo->serverport)))
|
---|
108 | ));
|
---|
109 | registrator(new ControllerCommand("removeallresults",
|
---|
110 | boost::assign::list_of< ControllerCommand::commands_t >
|
---|
111 | (boost::bind(&FragmentController::removeWaitingResults,
|
---|
112 | boost::ref(controller), boost::cref(ControllerInfo->server), boost::cref(ControllerInfo->serverport)))
|
---|
113 | ));
|
---|
114 | registrator(new ControllerCommand("shutdown",
|
---|
115 | boost::assign::list_of< ControllerCommand::commands_t >
|
---|
116 | (boost::bind(&FragmentController::shutdown,
|
---|
117 | boost::ref(controller), boost::cref(ControllerInfo->server), boost::cref(ControllerInfo->serverport)))
|
---|
118 | ));
|
---|
119 | AddOn->addSpecificCommands(registrator, controller, *ControllerInfo);
|
---|
120 |
|
---|
121 | // Declare the supported options.
|
---|
122 | boost::program_options::options_description desc("Allowed options");
|
---|
123 | desc.add_options()
|
---|
124 | ("help,h", "produce help message")
|
---|
125 | ("verbosity,v", boost::program_options::value<size_t>(), "set verbosity level")
|
---|
126 | ("server", boost::program_options::value< std::string >(), "connect to server at this address (host:port)")
|
---|
127 | ("command", boost::program_options::value< std::string >(), (std::string("command to send to server: ")+toString(getListOfCommands(ControllerCommands))).c_str())
|
---|
128 | ;
|
---|
129 | AddOn->addSpecificOptions(desc.add_options());
|
---|
130 |
|
---|
131 | // parse command line
|
---|
132 | boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
|
---|
133 | boost::program_options::notify(vm);
|
---|
134 |
|
---|
135 | // set controller information
|
---|
136 | int status = 0;
|
---|
137 | status = ControllerInfo->parseHelp(vm, desc);
|
---|
138 | if (status) return status;
|
---|
139 | status = ControllerInfo->parseVerbosity(vm);
|
---|
140 | if (status) return status;
|
---|
141 | status = ControllerInfo->parseServerPort(vm);
|
---|
142 | if (status) return status;
|
---|
143 | status = ControllerInfo->parseCommand(vm, getListOfCommands(ControllerCommands));
|
---|
144 | if (status) return status;
|
---|
145 |
|
---|
146 | // all later parse functions depend on parsed command
|
---|
147 | status = AddOn->addOtherParsings(*ControllerInfo, vm);
|
---|
148 | if (status) return status;
|
---|
149 |
|
---|
150 | // parse given ControllerCommand
|
---|
151 | if(!ControllerCommands.isPresentByName(ControllerInfo->command)) {
|
---|
152 | ELOG(1, "Unrecognized command '"+toString(ControllerInfo->command)+"'.");
|
---|
153 | return 255;
|
---|
154 | }
|
---|
155 | const ControllerCommand *commands = ControllerCommands.getByName(ControllerInfo->command);
|
---|
156 | try
|
---|
157 | {
|
---|
158 | // execute each command in the queue synchronously
|
---|
159 | size_t phase = 1;
|
---|
160 | for (ControllerCommand::const_iterator iter = commands->begin();
|
---|
161 | iter != commands->end(); ++iter) {
|
---|
162 | (*iter)();
|
---|
163 | {
|
---|
164 | io_service.reset();
|
---|
165 | //Info info((std::string("io_service: ")+toString(phase)).c_str());
|
---|
166 | io_service.run();
|
---|
167 | }
|
---|
168 | }
|
---|
169 | Exitflag = controller.getExitflag();
|
---|
170 | }
|
---|
171 | catch (std::exception& e)
|
---|
172 | {
|
---|
173 | std::cerr << e.what() << std::endl;
|
---|
174 | }
|
---|
175 | delete AddOn;
|
---|
176 |
|
---|
177 | return Exitflag;
|
---|
178 | }
|
---|
179 |
|
---|