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 |
|
---|
29 | #include <iostream>
|
---|
30 | #include <vector>
|
---|
31 | #include <map>
|
---|
32 |
|
---|
33 | #include "atexit.hpp"
|
---|
34 | #include "CodePatterns/Info.hpp"
|
---|
35 | #include "CodePatterns/Log.hpp"
|
---|
36 | #include "Controller/FragmentController.hpp"
|
---|
37 | #include "Controller/Commands/CheckResultsOperation.hpp"
|
---|
38 | #include "Controller/Commands/ReceiveJobsOperation.hpp"
|
---|
39 | #include "Controller/Commands/SendResultsOperation.hpp"
|
---|
40 | #include "Controller/Commands/ShutdownOperation.hpp"
|
---|
41 | #include "FragmentJob.hpp"
|
---|
42 | #include "FragmentResult.hpp"
|
---|
43 |
|
---|
44 | enum CommandIndices {
|
---|
45 | UnknownCommandIndex = 0,
|
---|
46 | SendJobsIndex = 1,
|
---|
47 | CheckResultsIndex = 2,
|
---|
48 | ReceiveResultsIndex = 3,
|
---|
49 | ShutdownIndex = 4
|
---|
50 | };
|
---|
51 |
|
---|
52 | void createjobs(std::vector<FragmentJob> &jobs)
|
---|
53 | {
|
---|
54 | FragmentJob testJob(std::string("do something"), 1);
|
---|
55 | FragmentJob othertestJob(std::string("do something else"), 2);
|
---|
56 | jobs.push_back(testJob);
|
---|
57 | jobs.push_back(othertestJob);
|
---|
58 | }
|
---|
59 |
|
---|
60 | void addjobs(FragmentController &controller, std::vector<FragmentJob> &jobs)
|
---|
61 | {
|
---|
62 | ReceiveJobsOperation *recjobs = static_cast<ReceiveJobsOperation *>(
|
---|
63 | controller.Commands.getByName("receivejobs"));
|
---|
64 | recjobs->addJobs(jobs);
|
---|
65 | }
|
---|
66 |
|
---|
67 | void sendjobs(FragmentController &controller, const std::string &host, const std::string &service)
|
---|
68 | {
|
---|
69 | ReceiveJobsOperation *recjobs = static_cast<ReceiveJobsOperation *>(
|
---|
70 | controller.Commands.getByName("receivejobs"));
|
---|
71 | (*recjobs)(host, service);
|
---|
72 | }
|
---|
73 |
|
---|
74 | void checkresults(FragmentController &controller, const std::string &host, const std::string &service)
|
---|
75 | {
|
---|
76 | CheckResultsOperation *checkres = static_cast<CheckResultsOperation *>(
|
---|
77 | controller.Commands.getByName("checkresults"));
|
---|
78 | (*checkres)(host, service);
|
---|
79 | }
|
---|
80 |
|
---|
81 | void printdonejobs(FragmentController &controller)
|
---|
82 | {
|
---|
83 | CheckResultsOperation *checkres = static_cast<CheckResultsOperation *>(
|
---|
84 | controller.Commands.getByName("checkresults"));
|
---|
85 | const size_t doneJobs = checkres->getDoneJobs();
|
---|
86 | LOG(1, "INFO: " << doneJobs << " jobs are calculated so far.");
|
---|
87 | }
|
---|
88 |
|
---|
89 | void receiveresults(FragmentController &controller, const std::string &host, const std::string &service)
|
---|
90 | {
|
---|
91 | SendResultsOperation *sendres = static_cast<SendResultsOperation *>(
|
---|
92 | controller.Commands.getByName("sendresults"));
|
---|
93 | (*sendres)(host, service);
|
---|
94 | }
|
---|
95 |
|
---|
96 | void printreceivedresults(FragmentController &controller)
|
---|
97 | {
|
---|
98 | SendResultsOperation *sendres = static_cast<SendResultsOperation *>(
|
---|
99 | controller.Commands.getByName("sendresults"));
|
---|
100 | std::vector<FragmentResult> results = sendres->getResults();
|
---|
101 | for (std::vector<FragmentResult>::const_iterator iter = results.begin();
|
---|
102 | iter != results.end(); ++iter)
|
---|
103 | LOG(1, "RESULT: job #"+toString((*iter).getId())+": "+toString((*iter).result));
|
---|
104 | }
|
---|
105 |
|
---|
106 | void shutdown(FragmentController &controller, const std::string &host, const std::string &service)
|
---|
107 | {
|
---|
108 | ShutdownOperation *shutdown = static_cast<ShutdownOperation *>(
|
---|
109 | controller.Commands.getByName("shutdown"));
|
---|
110 | (*shutdown)(host, service);
|
---|
111 | }
|
---|
112 |
|
---|
113 | /** Returns a unique index for every command to allow switching over it.
|
---|
114 | *
|
---|
115 | * \param &commandmap map with command strings
|
---|
116 | * \param &cmd command string
|
---|
117 | * \return index from CommandIndices: UnkownCommandIndex - unknown command, else - command index
|
---|
118 | */
|
---|
119 | CommandIndices getCommandIndex(std::map<std::string, CommandIndices> &commandmap, const std::string &cmd)
|
---|
120 | {
|
---|
121 | std::map<std::string, CommandIndices>::const_iterator iter = commandmap.find(cmd);
|
---|
122 | if (iter != commandmap.end())
|
---|
123 | return iter->second;
|
---|
124 | else
|
---|
125 | return UnknownCommandIndex;
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | int main(int argc, char* argv[])
|
---|
130 | {
|
---|
131 | // from this moment on, we need to be sure to deeinitialize in the correct order
|
---|
132 | // this is handled by the cleanup function
|
---|
133 | atexit(cleanUp);
|
---|
134 |
|
---|
135 | setVerbosity(3);
|
---|
136 |
|
---|
137 | size_t Exitflag = 0;
|
---|
138 | std::map<std::string, CommandIndices> CommandsMap;
|
---|
139 | CommandsMap.insert( std::make_pair("sendjobs", SendJobsIndex) );
|
---|
140 | CommandsMap.insert( std::make_pair("checkresults", CheckResultsIndex) );
|
---|
141 | CommandsMap.insert( std::make_pair("receiveresults", ReceiveResultsIndex) );
|
---|
142 | CommandsMap.insert( std::make_pair("shutdown", ShutdownIndex) );
|
---|
143 | try
|
---|
144 | {
|
---|
145 | // Check command line arguments.
|
---|
146 | if (argc != 4)
|
---|
147 | {
|
---|
148 | std::cerr << "Usage: " << argv[0] << " <host> <port> <command>" << std::endl;
|
---|
149 | return 1;
|
---|
150 | }
|
---|
151 |
|
---|
152 | boost::asio::io_service io_service;
|
---|
153 | FragmentController controller(io_service);
|
---|
154 |
|
---|
155 | switch(getCommandIndex(CommandsMap, argv[3])) {
|
---|
156 | case SendJobsIndex:
|
---|
157 | {
|
---|
158 | std::vector<FragmentJob> jobs;
|
---|
159 | createjobs(jobs);
|
---|
160 | addjobs(controller, jobs);
|
---|
161 | sendjobs(controller, argv[1], argv[2]);
|
---|
162 | break;
|
---|
163 | }
|
---|
164 | case CheckResultsIndex:
|
---|
165 | {
|
---|
166 | checkresults(controller, argv[1], argv[2]);
|
---|
167 | break;
|
---|
168 | }
|
---|
169 | case ReceiveResultsIndex:
|
---|
170 | {
|
---|
171 | receiveresults(controller, argv[1], argv[2]);
|
---|
172 | break;
|
---|
173 | }
|
---|
174 | case ShutdownIndex:
|
---|
175 | {
|
---|
176 | shutdown(controller, argv[1], argv[2]);
|
---|
177 | break;
|
---|
178 | }
|
---|
179 | case UnknownCommandIndex:
|
---|
180 | default:
|
---|
181 | ELOG(0, "Unrecognized command '"+toString(argv[3])+"'.");
|
---|
182 | break;
|
---|
183 | }
|
---|
184 |
|
---|
185 | {
|
---|
186 | Info info("io_service");
|
---|
187 | io_service.run();
|
---|
188 | }
|
---|
189 |
|
---|
190 | switch(getCommandIndex(CommandsMap, argv[3])) {
|
---|
191 | case SendJobsIndex:
|
---|
192 | break;
|
---|
193 | case CheckResultsIndex:
|
---|
194 | {
|
---|
195 | printdonejobs(controller);
|
---|
196 | break;
|
---|
197 | }
|
---|
198 | case ReceiveResultsIndex:
|
---|
199 | {
|
---|
200 | printreceivedresults(controller);
|
---|
201 | break;
|
---|
202 | }
|
---|
203 | case ShutdownIndex:
|
---|
204 | break;
|
---|
205 | case UnknownCommandIndex:
|
---|
206 | default:
|
---|
207 | ELOG(0, "Unrecognized command '"+toString(argv[3])+"'.");
|
---|
208 | break;
|
---|
209 | }
|
---|
210 | Exitflag = controller.getExitflag();
|
---|
211 | }
|
---|
212 | catch (std::exception& e)
|
---|
213 | {
|
---|
214 | std::cerr << e.what() << std::endl;
|
---|
215 | }
|
---|
216 |
|
---|
217 | return Exitflag;
|
---|
218 | }
|
---|