| [7ca772e] | 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 |  | 
|---|
| [986885] | 29 | #include <boost/archive/text_oarchive.hpp> | 
|---|
|  | 30 | #include <boost/archive/text_iarchive.hpp> | 
|---|
| [ff60cfa] | 31 | #include <fstream> | 
|---|
| [7ca772e] | 32 | #include <iostream> | 
|---|
|  | 33 | #include <map> | 
|---|
| [ff60cfa] | 34 | #include <sstream> | 
|---|
|  | 35 | #include <streambuf> | 
|---|
|  | 36 | #include <vector> | 
|---|
| [7ca772e] | 37 |  | 
|---|
|  | 38 | #include "atexit.hpp" | 
|---|
|  | 39 | #include "CodePatterns/Info.hpp" | 
|---|
|  | 40 | #include "CodePatterns/Log.hpp" | 
|---|
| [d4885d] | 41 | #include "FragmentController.hpp" | 
|---|
| [ff60cfa] | 42 | #include "Jobs/MPQCCommandJob.hpp" | 
|---|
| [d920b9] | 43 | #include "Jobs/SystemCommandJob.hpp" | 
|---|
| [7670865] | 44 | #include "Results/FragmentResult.hpp" | 
|---|
| [7ca772e] | 45 |  | 
|---|
|  | 46 | enum CommandIndices { | 
|---|
|  | 47 | UnknownCommandIndex = 0, | 
|---|
| [ff60cfa] | 48 | AddJobsIndex = 1, | 
|---|
|  | 49 | CreateJobsIndex = 2, | 
|---|
|  | 50 | CheckResultsIndex = 3, | 
|---|
|  | 51 | ReceiveResultsIndex = 4, | 
|---|
| [986885] | 52 | ReceiveMPQCIndex = 5, | 
|---|
|  | 53 | ShutdownIndex = 6, | 
|---|
| [7ca772e] | 54 | }; | 
|---|
|  | 55 |  | 
|---|
| [ff60cfa] | 56 |  | 
|---|
| [4dd16e] | 57 | /** Creates a SystemCommandJob out of give \a command with \a argument. | 
|---|
| [d1dbfc] | 58 | * | 
|---|
| [4dd16e] | 59 | * @param jobs created job is added to this vector | 
|---|
|  | 60 | * @param command command to execute for SystemCommandJob | 
|---|
|  | 61 | * @param argument argument for command to execute | 
|---|
|  | 62 | * @param nextid id for this job | 
|---|
| [d1dbfc] | 63 | */ | 
|---|
| [554809] | 64 | void createjobs( | 
|---|
|  | 65 | std::vector<FragmentJob::ptr> &jobs, | 
|---|
|  | 66 | const std::string &command, | 
|---|
|  | 67 | const std::string &argument, | 
|---|
|  | 68 | const JobId_t nextid) | 
|---|
| [d1dbfc] | 69 | { | 
|---|
| [4dd16e] | 70 |  | 
|---|
| [554809] | 71 | FragmentJob::ptr testJob( new SystemCommandJob(command, argument, nextid) ); | 
|---|
| [7ca772e] | 72 | jobs.push_back(testJob); | 
|---|
| [554809] | 73 | LOG(1, "INFO: Added one SystemCommandJob."); | 
|---|
| [7ca772e] | 74 | } | 
|---|
|  | 75 |  | 
|---|
| [d1dbfc] | 76 | /** Creates a MPQCCommandJob with argument \a filename. | 
|---|
|  | 77 | * | 
|---|
|  | 78 | * @param jobs created job is added to this vector | 
|---|
|  | 79 | * @param filename filename being argument to job | 
|---|
|  | 80 | * @param nextid id for this job | 
|---|
|  | 81 | */ | 
|---|
|  | 82 | void parsejob( | 
|---|
|  | 83 | std::vector<FragmentJob::ptr> &jobs, | 
|---|
|  | 84 | const std::string &filename, | 
|---|
|  | 85 | const JobId_t nextid) | 
|---|
| [ff60cfa] | 86 | { | 
|---|
|  | 87 | std::ifstream file; | 
|---|
|  | 88 | file.open(filename.c_str()); | 
|---|
|  | 89 | ASSERT( file.good(), "parsejob() - file "+filename+" does not exist."); | 
|---|
|  | 90 | std::string output((std::istreambuf_iterator<char>(file)), | 
|---|
|  | 91 | std::istreambuf_iterator<char>()); | 
|---|
| [d1dbfc] | 92 | FragmentJob::ptr testJob( new MPQCCommandJob(output, nextid) ); | 
|---|
| [ff60cfa] | 93 | jobs.push_back(testJob); | 
|---|
|  | 94 | file.close(); | 
|---|
|  | 95 | LOG(1, "INFO: Added MPQCCommandJob from file "+filename+"."); | 
|---|
|  | 96 | } | 
|---|
|  | 97 |  | 
|---|
| [d1dbfc] | 98 | /** Print received results. | 
|---|
|  | 99 | * | 
|---|
| [4dd16e] | 100 | * @param results received results to print | 
|---|
| [d1dbfc] | 101 | */ | 
|---|
| [4dd16e] | 102 | void printReceivedResults(std::vector<FragmentResult::ptr> &results) | 
|---|
| [7ca772e] | 103 | { | 
|---|
| [35f587] | 104 | for (std::vector<FragmentResult::ptr>::const_iterator iter = results.begin(); | 
|---|
| [7ca772e] | 105 | iter != results.end(); ++iter) | 
|---|
| [35f587] | 106 | LOG(1, "RESULT: job #"+toString((*iter)->getId())+": "+toString((*iter)->result)); | 
|---|
| [7ca772e] | 107 | } | 
|---|
|  | 108 |  | 
|---|
| [986885] | 109 | /** Print received results. | 
|---|
|  | 110 | * | 
|---|
| [4dd16e] | 111 | * @param results vector of all received FragmentResult's | 
|---|
|  | 112 | * @param KeySetFilename filename containing keysets | 
|---|
| [986885] | 113 | */ | 
|---|
| [4dd16e] | 114 | void printreceivedmpqcresults(std::vector<FragmentResult::ptr> &results, const std::string &KeySetFilename) | 
|---|
| [986885] | 115 | { | 
|---|
|  | 116 | // parse in KeySetfile | 
|---|
|  | 117 | //  const size_t MAXBUFFER = 256; | 
|---|
|  | 118 | std::ifstream inputfile; | 
|---|
|  | 119 | inputfile.open(KeySetFilename.c_str()); | 
|---|
|  | 120 | //  while (inputfile.getline(buffer, MAXBUFFER)) { | 
|---|
|  | 121 | // | 
|---|
|  | 122 | //  } | 
|---|
|  | 123 | inputfile.close(); | 
|---|
|  | 124 |  | 
|---|
|  | 125 | // combine all found data | 
|---|
|  | 126 | std::vector<MPQCData> fragmentData(results.size()); | 
|---|
|  | 127 | MPQCData combinedData; | 
|---|
|  | 128 |  | 
|---|
|  | 129 | LOG(2, "DEBUG: Parsing now through " << results.size() << " results."); | 
|---|
|  | 130 | for (std::vector<FragmentResult::ptr>::const_iterator iter = results.begin(); | 
|---|
|  | 131 | iter != results.end(); ++iter) { | 
|---|
|  | 132 | LOG(1, "RESULT: job #"+toString((*iter)->getId())+": "+toString((*iter)->result)); | 
|---|
|  | 133 | MPQCData extractedData; | 
|---|
|  | 134 | std::stringstream inputstream((*iter)->result); | 
|---|
|  | 135 | boost::archive::text_iarchive ia(inputstream); | 
|---|
|  | 136 | ia >> extractedData; | 
|---|
|  | 137 | LOG(1, "INFO: extracted data is " << extractedData << "."); | 
|---|
|  | 138 | } | 
|---|
|  | 139 | } | 
|---|
|  | 140 |  | 
|---|
| [7ca772e] | 141 | /** Returns a unique index for every command to allow switching over it. | 
|---|
|  | 142 | * | 
|---|
|  | 143 | * \param &commandmap map with command strings | 
|---|
|  | 144 | * \param &cmd command string | 
|---|
|  | 145 | * \return index from CommandIndices: UnkownCommandIndex - unknown command, else - command index | 
|---|
|  | 146 | */ | 
|---|
|  | 147 | CommandIndices getCommandIndex(std::map<std::string, CommandIndices> &commandmap, const std::string &cmd) | 
|---|
|  | 148 | { | 
|---|
|  | 149 | std::map<std::string, CommandIndices>::const_iterator iter = commandmap.find(cmd); | 
|---|
|  | 150 | if (iter != commandmap.end()) | 
|---|
|  | 151 | return iter->second; | 
|---|
|  | 152 | else | 
|---|
|  | 153 | return UnknownCommandIndex; | 
|---|
|  | 154 | } | 
|---|
|  | 155 |  | 
|---|
|  | 156 |  | 
|---|
|  | 157 | int main(int argc, char* argv[]) | 
|---|
|  | 158 | { | 
|---|
|  | 159 | // from this moment on, we need to be sure to deeinitialize in the correct order | 
|---|
|  | 160 | // this is handled by the cleanup function | 
|---|
|  | 161 | atexit(cleanUp); | 
|---|
|  | 162 |  | 
|---|
|  | 163 | setVerbosity(3); | 
|---|
|  | 164 |  | 
|---|
|  | 165 | size_t Exitflag = 0; | 
|---|
| [ff60cfa] | 166 | typedef std::map<std::string, CommandIndices> CommandsMap_t; | 
|---|
|  | 167 | CommandsMap_t CommandsMap; | 
|---|
|  | 168 | CommandsMap.insert( std::make_pair("addjobs", AddJobsIndex) ); | 
|---|
|  | 169 | CommandsMap.insert( std::make_pair("createjobs", CreateJobsIndex) ); | 
|---|
| [7ca772e] | 170 | CommandsMap.insert( std::make_pair("checkresults", CheckResultsIndex) ); | 
|---|
|  | 171 | CommandsMap.insert( std::make_pair("receiveresults", ReceiveResultsIndex) ); | 
|---|
| [986885] | 172 | CommandsMap.insert( std::make_pair("receivempqc", ReceiveMPQCIndex) ); | 
|---|
| [7ca772e] | 173 | CommandsMap.insert( std::make_pair("shutdown", ShutdownIndex) ); | 
|---|
|  | 174 | try | 
|---|
|  | 175 | { | 
|---|
|  | 176 | // Check command line arguments. | 
|---|
| [ff60cfa] | 177 | if (argc < 4) | 
|---|
| [7ca772e] | 178 | { | 
|---|
| [ff60cfa] | 179 | std::cerr << "Usage: " << argv[0] << " <host> <port> <command> [options to command]" << std::endl; | 
|---|
|  | 180 | std::cerr << "List of available commands:" << std::endl; | 
|---|
|  | 181 | for(CommandsMap_t::const_iterator iter = CommandsMap.begin(); | 
|---|
|  | 182 | iter != CommandsMap.end(); ++iter) { | 
|---|
|  | 183 | std::cerr << "\t" << iter->first << std::endl; | 
|---|
|  | 184 | } | 
|---|
| [7ca772e] | 185 | return 1; | 
|---|
|  | 186 | } | 
|---|
|  | 187 |  | 
|---|
|  | 188 | boost::asio::io_service io_service; | 
|---|
|  | 189 | FragmentController controller(io_service); | 
|---|
|  | 190 |  | 
|---|
| [d1dbfc] | 191 | // Initial phase: information gathering from server | 
|---|
|  | 192 |  | 
|---|
|  | 193 | switch(getCommandIndex(CommandsMap, argv[3])) { | 
|---|
|  | 194 | case AddJobsIndex: | 
|---|
|  | 195 | { | 
|---|
|  | 196 | if (argc < 5) { | 
|---|
|  | 197 | ELOG(1, "Please add a filename for the MPQCCommandJob."); | 
|---|
|  | 198 | } else { | 
|---|
|  | 199 | // get an id for every filename | 
|---|
|  | 200 | for (int argcount = 4; argcount < argc; ++argcount) { | 
|---|
| [4dd16e] | 201 | controller.requestId(argv[1], argv[2]); | 
|---|
| [d1dbfc] | 202 | } | 
|---|
|  | 203 | } | 
|---|
|  | 204 | break; | 
|---|
|  | 205 | } | 
|---|
|  | 206 | case CreateJobsIndex: | 
|---|
|  | 207 | { | 
|---|
| [554809] | 208 | std::vector<FragmentJob::ptr> jobs; | 
|---|
|  | 209 | if (argc < 6) { | 
|---|
|  | 210 | ELOG(1, "'createjobs' requires two options: [command] [argument]."); | 
|---|
|  | 211 | } else { | 
|---|
| [4dd16e] | 212 | controller.requestId(argv[1], argv[2]); | 
|---|
| [554809] | 213 | } | 
|---|
| [d1dbfc] | 214 | break; | 
|---|
|  | 215 | } | 
|---|
|  | 216 | case CheckResultsIndex: | 
|---|
|  | 217 | break; | 
|---|
|  | 218 | case ReceiveResultsIndex: | 
|---|
|  | 219 | break; | 
|---|
| [986885] | 220 | case ReceiveMPQCIndex: | 
|---|
|  | 221 | break; | 
|---|
| [d1dbfc] | 222 | case ShutdownIndex: | 
|---|
|  | 223 | break; | 
|---|
|  | 224 | case UnknownCommandIndex: | 
|---|
|  | 225 | default: | 
|---|
|  | 226 | ELOG(1, "Unrecognized command '"+toString(argv[3])+"'."); | 
|---|
|  | 227 | break; | 
|---|
|  | 228 | } | 
|---|
|  | 229 |  | 
|---|
|  | 230 | { | 
|---|
|  | 231 | io_service.reset(); | 
|---|
|  | 232 | Info info("io_service: Phase One"); | 
|---|
|  | 233 | io_service.run(); | 
|---|
|  | 234 | } | 
|---|
|  | 235 |  | 
|---|
|  | 236 | // Second phase: Building jobs and sending information to server | 
|---|
|  | 237 |  | 
|---|
| [7ca772e] | 238 | switch(getCommandIndex(CommandsMap, argv[3])) { | 
|---|
| [ff60cfa] | 239 | case AddJobsIndex: | 
|---|
|  | 240 | { | 
|---|
|  | 241 | std::vector<FragmentJob::ptr> jobs; | 
|---|
| [d1dbfc] | 242 | if (argc < 5) { | 
|---|
| [ff60cfa] | 243 | ELOG(1, "Please add a filename for the MPQCCommandJob."); | 
|---|
|  | 244 | } else { | 
|---|
|  | 245 | for (int argcount = 4; argcount < argc; ++argcount) { | 
|---|
| [4dd16e] | 246 | const JobId_t next_id = controller.getAvailableId(); | 
|---|
| [d1dbfc] | 247 | const std::string filename(argv[argcount]); | 
|---|
|  | 248 | LOG(1, "INFO: Creating MPQCCommandJob with filename'" | 
|---|
|  | 249 | +filename+"', and id "+toString(next_id)+"."); | 
|---|
|  | 250 | parsejob(jobs, filename, next_id); | 
|---|
| [ff60cfa] | 251 | } | 
|---|
| [4dd16e] | 252 | controller.addJobs(jobs); | 
|---|
|  | 253 | controller.sendJobs(argv[1], argv[2]); | 
|---|
| [ff60cfa] | 254 | } | 
|---|
|  | 255 | break; | 
|---|
|  | 256 | } | 
|---|
|  | 257 | case CreateJobsIndex: | 
|---|
| [7ca772e] | 258 | { | 
|---|
| [78ad7d] | 259 | std::vector<FragmentJob::ptr> jobs; | 
|---|
| [554809] | 260 | if (argc < 6) { | 
|---|
| [4dd16e] | 261 | ELOG(1, "'createjobs' requires two options: [command] [argument]."); | 
|---|
|  | 262 | } else { | 
|---|
|  | 263 | const JobId_t next_id = controller.getAvailableId(); | 
|---|
|  | 264 | createjobs(jobs, argv[4], argv[5], next_id); | 
|---|
|  | 265 | controller.addJobs(jobs); | 
|---|
|  | 266 | controller.sendJobs(argv[1], argv[2]); | 
|---|
| [554809] | 267 | } | 
|---|
| [7ca772e] | 268 | break; | 
|---|
|  | 269 | } | 
|---|
|  | 270 | case CheckResultsIndex: | 
|---|
|  | 271 | { | 
|---|
| [4dd16e] | 272 | controller.checkResults(argv[1], argv[2]); | 
|---|
| [7ca772e] | 273 | break; | 
|---|
|  | 274 | } | 
|---|
|  | 275 | case ReceiveResultsIndex: | 
|---|
|  | 276 | { | 
|---|
| [4dd16e] | 277 | controller.receiveResults(argv[1], argv[2]); | 
|---|
| [7ca772e] | 278 | break; | 
|---|
|  | 279 | } | 
|---|
| [986885] | 280 | case ReceiveMPQCIndex: | 
|---|
|  | 281 | { | 
|---|
| [4dd16e] | 282 | controller.receiveResults(argv[1], argv[2]); | 
|---|
| [986885] | 283 | break; | 
|---|
|  | 284 | } | 
|---|
| [7ca772e] | 285 | case ShutdownIndex: | 
|---|
|  | 286 | { | 
|---|
| [4dd16e] | 287 | controller.shutdown(argv[1], argv[2]); | 
|---|
| [7ca772e] | 288 | break; | 
|---|
|  | 289 | } | 
|---|
|  | 290 | case UnknownCommandIndex: | 
|---|
|  | 291 | default: | 
|---|
|  | 292 | ELOG(0, "Unrecognized command '"+toString(argv[3])+"'."); | 
|---|
|  | 293 | break; | 
|---|
|  | 294 | } | 
|---|
|  | 295 |  | 
|---|
|  | 296 | { | 
|---|
| [d1dbfc] | 297 | io_service.reset(); | 
|---|
|  | 298 | Info info("io_service: Phase Two"); | 
|---|
| [7ca772e] | 299 | io_service.run(); | 
|---|
|  | 300 | } | 
|---|
|  | 301 |  | 
|---|
| [d1dbfc] | 302 | // Final phase: Print result of command | 
|---|
|  | 303 |  | 
|---|
| [7ca772e] | 304 | switch(getCommandIndex(CommandsMap, argv[3])) { | 
|---|
| [ff60cfa] | 305 | case AddJobsIndex: | 
|---|
|  | 306 | case CreateJobsIndex: | 
|---|
| [7ca772e] | 307 | break; | 
|---|
|  | 308 | case CheckResultsIndex: | 
|---|
|  | 309 | { | 
|---|
| [4dd16e] | 310 | controller.printDoneJobs(); | 
|---|
| [7ca772e] | 311 | break; | 
|---|
|  | 312 | } | 
|---|
|  | 313 | case ReceiveResultsIndex: | 
|---|
|  | 314 | { | 
|---|
| [4dd16e] | 315 | std::vector<FragmentResult::ptr> results = controller.getReceivedResults(); | 
|---|
|  | 316 | printReceivedResults(results); | 
|---|
| [7ca772e] | 317 | break; | 
|---|
|  | 318 | } | 
|---|
| [986885] | 319 | case ReceiveMPQCIndex: | 
|---|
|  | 320 | { | 
|---|
|  | 321 | if (argc == 4) { | 
|---|
|  | 322 | ELOG(1, "'receivempqc' requires one option: [KeySetFilename]."); | 
|---|
|  | 323 | } else { | 
|---|
|  | 324 | const std::string KeySetFilename = argv[4]; | 
|---|
|  | 325 | LOG(1, "INFO: Parsing id associations from " << KeySetFilename << "."); | 
|---|
| [4dd16e] | 326 | std::vector<FragmentResult::ptr> results = controller.getReceivedResults(); | 
|---|
|  | 327 | printreceivedmpqcresults(results, KeySetFilename); | 
|---|
| [986885] | 328 | } | 
|---|
|  | 329 | break; | 
|---|
|  | 330 | } | 
|---|
| [7ca772e] | 331 | case ShutdownIndex: | 
|---|
|  | 332 | break; | 
|---|
|  | 333 | case UnknownCommandIndex: | 
|---|
|  | 334 | default: | 
|---|
|  | 335 | ELOG(0, "Unrecognized command '"+toString(argv[3])+"'."); | 
|---|
|  | 336 | break; | 
|---|
|  | 337 | } | 
|---|
|  | 338 | Exitflag = controller.getExitflag(); | 
|---|
|  | 339 | } | 
|---|
|  | 340 | catch (std::exception& e) | 
|---|
|  | 341 | { | 
|---|
|  | 342 | std::cerr << e.what() << std::endl; | 
|---|
|  | 343 | } | 
|---|
|  | 344 |  | 
|---|
|  | 345 | return Exitflag; | 
|---|
|  | 346 | } | 
|---|
| [4dd16e] | 347 |  | 
|---|