1 | /*
|
---|
2 | * Project: JobMarket
|
---|
3 | * Description: asynchronous Server/Controller/Client-approach to parallel computing, based on boost::asio
|
---|
4 | * Copyright (C) 2012 Frederik Heber. All rights reserved.
|
---|
5 | *
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * controller_SystemCommandJob.cpp
|
---|
10 | *
|
---|
11 | * Created on: 01.06.2012
|
---|
12 | * Author: heber
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | // boost asio needs specific operator new
|
---|
21 | #include <boost/asio.hpp>
|
---|
22 |
|
---|
23 | #include "CodePatterns/MemDebug.hpp"
|
---|
24 |
|
---|
25 | #include "JobMarket/Controller/controller_SystemCommandJob.hpp"
|
---|
26 |
|
---|
27 | #include <boost/assign.hpp>
|
---|
28 | #include <boost/bind.hpp>
|
---|
29 | #include "CodePatterns/Log.hpp"
|
---|
30 | #include <vector>
|
---|
31 |
|
---|
32 | #include "JobMarket/Controller/ControllerCommand.hpp"
|
---|
33 | #include "JobMarket/Controller/ControllerCommandRegistry.hpp"
|
---|
34 | #include "JobMarket/Controller/ControllerOptions_SystemCommandJob.hpp"
|
---|
35 | #include "JobMarket/Controller/FragmentController.hpp"
|
---|
36 | #include "JobMarket/Jobs/NoOpJob.hpp"
|
---|
37 | #include "JobMarket/Jobs/SystemCommandJob.hpp"
|
---|
38 | #include "JobMarket/Results/FragmentResult.hpp"
|
---|
39 | #include "JobMarket/types.hpp"
|
---|
40 |
|
---|
41 |
|
---|
42 | /** Print received results.
|
---|
43 | *
|
---|
44 | * @param results received results to print
|
---|
45 | */
|
---|
46 | void printReceivedResults(const std::vector<FragmentResult::ptr> &results)
|
---|
47 | {
|
---|
48 | for (std::vector<FragmentResult::ptr>::const_iterator iter = results.begin();
|
---|
49 | iter != results.end(); ++iter)
|
---|
50 | LOG(1, "RESULT: job #"+toString((*iter)->getId())+": "+toString((*iter)->result));
|
---|
51 | }
|
---|
52 |
|
---|
53 | /** Creates a SystemCommandJob out of give \a command with \a argument.
|
---|
54 | *
|
---|
55 | * @param controller reference to controller to add jobs
|
---|
56 | * @param ControllerInfo information on the job
|
---|
57 | */
|
---|
58 | void createJobs(FragmentController &controller, const ControllerOptions_SystemCommandJob &ControllerInfo)
|
---|
59 | {
|
---|
60 | if ((!ControllerInfo.jobcommand.empty()) && (!ControllerInfo.executable.empty())) {
|
---|
61 | LOG(2, "DEBUG: Creating " << ControllerInfo.times << " new SystemCommandJob(s) with '"
|
---|
62 | +toString(ControllerInfo.executable)+"' and argument '"
|
---|
63 | +toString(ControllerInfo.jobcommand)+"'.");
|
---|
64 | std::vector<FragmentJob::ptr> jobs;
|
---|
65 | for (size_t times=ControllerInfo.times; times--;) {
|
---|
66 | const JobId_t next_id = controller.getAvailableId();
|
---|
67 | FragmentJob::ptr testJob(
|
---|
68 | new SystemCommandJob(ControllerInfo.executable, ControllerInfo.jobcommand, next_id) );
|
---|
69 | jobs.push_back(testJob);
|
---|
70 | }
|
---|
71 | controller.addJobs(jobs);
|
---|
72 | controller.sendJobs(ControllerInfo.server, ControllerInfo.serverport);
|
---|
73 | LOG(1, "INFO: Added SystemCommandJobs.");
|
---|
74 | } else {
|
---|
75 | LOG(2, "DEBUG: Creating " << ControllerInfo.times << " new NoOpJob(s).");
|
---|
76 | std::vector<FragmentJob::ptr> jobs;
|
---|
77 | for (size_t times=ControllerInfo.times; times--;) {
|
---|
78 | const JobId_t next_id = controller.getAvailableId();
|
---|
79 | FragmentJob::ptr testJob( new NoOpJob(next_id) );
|
---|
80 | jobs.push_back(testJob);
|
---|
81 | }
|
---|
82 | controller.addJobs(jobs);
|
---|
83 | controller.sendJobs(ControllerInfo.server, ControllerInfo.serverport);
|
---|
84 | LOG(1, "INFO: Added NoOpJobs.");
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | inline std::vector<std::string> getListOfCommands(const ControllerCommandRegistry &ControllerCommands)
|
---|
89 | {
|
---|
90 | std::vector<std::string> Commands;
|
---|
91 | for (ControllerCommandRegistry::const_iterator iter = ControllerCommands.getBeginIter();
|
---|
92 | iter != ControllerCommands.getEndIter(); ++iter)
|
---|
93 | Commands.push_back(iter->first);
|
---|
94 |
|
---|
95 | return Commands;
|
---|
96 | }
|
---|
97 |
|
---|
98 | ControllerOptions *controller_SystemCommandJob::allocateControllerInfo()
|
---|
99 | {
|
---|
100 | return new ControllerOptions_SystemCommandJob();
|
---|
101 | }
|
---|
102 |
|
---|
103 | void controller_SystemCommandJob::addSpecificCommands(
|
---|
104 | boost::function<void (ControllerCommand *)> ®istrator,
|
---|
105 | FragmentController &controller,
|
---|
106 | ControllerOptions &ControllerInfo)
|
---|
107 | {
|
---|
108 | ControllerOptions_SystemCommandJob &CI =
|
---|
109 | reinterpret_cast<ControllerOptions_SystemCommandJob &>(ControllerInfo);
|
---|
110 | registrator(new ControllerCommand("createjobs",
|
---|
111 | boost::assign::list_of< ControllerCommand::commands_t >
|
---|
112 | (boost::bind(&FragmentController::requestIds,
|
---|
113 | boost::ref(controller),
|
---|
114 | boost::cref(ControllerInfo.server), boost::cref(ControllerInfo.serverport), boost::cref(CI.times)))
|
---|
115 | (boost::bind(&createJobs, boost::ref(controller), boost::cref(CI)))
|
---|
116 | ));
|
---|
117 | registrator(new ControllerCommand("receiveresults",
|
---|
118 | boost::assign::list_of< ControllerCommand::commands_t >
|
---|
119 | (boost::bind(&FragmentController_JobIdProxy::setJobids,
|
---|
120 | boost::ref(controller), boost::cref(CI.ids)))
|
---|
121 | (boost::bind(&FragmentController::receiveResults,
|
---|
122 | boost::ref(controller), boost::cref(ControllerInfo.server), boost::cref(ControllerInfo.serverport)))
|
---|
123 | (boost::bind(&printReceivedResults,
|
---|
124 | boost::bind(&FragmentController::getReceivedResults, boost::ref(controller))))
|
---|
125 | ));
|
---|
126 | }
|
---|
127 |
|
---|
128 | void controller_SystemCommandJob::addSpecificOptions(
|
---|
129 | boost::program_options::options_description_easy_init option)
|
---|
130 | {
|
---|
131 | option
|
---|
132 | ("jobcommand", boost::program_options::value< std::string >(), "command argument for executable for 'createjobs'")
|
---|
133 | ("executable", boost::program_options::value< std::string >(), "executable for commands 'createjobs'")
|
---|
134 | ("ids", boost::program_options::value< std::vector<JobId_t> >()->multitoken(), "ids to receive for 'receiveresults'")
|
---|
135 | ("times", boost::program_options::value< size_t >(), "sets the number of times the 'createjobs' job is added")
|
---|
136 | ;
|
---|
137 | }
|
---|
138 |
|
---|
139 | int controller_SystemCommandJob::addOtherParsings(
|
---|
140 | ControllerOptions &ControllerInfo,
|
---|
141 | boost::program_options::variables_map &vm)
|
---|
142 | {
|
---|
143 | ControllerOptions_SystemCommandJob &CI =
|
---|
144 | reinterpret_cast<ControllerOptions_SystemCommandJob &>(ControllerInfo);
|
---|
145 | int status = 0;
|
---|
146 | status = CI.parseExecutable(vm);
|
---|
147 | if (status) return status;
|
---|
148 | status = CI.parseJobCommand(vm);
|
---|
149 | if (status) return status;
|
---|
150 | status = CI.parseTimes(vm);
|
---|
151 | if (status) return status;
|
---|
152 | status = CI.parseIds(vm);
|
---|
153 | return status;
|
---|
154 | }
|
---|