| 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 FragmentScheduler.cpp
|
|---|
| 10 | *
|
|---|
| 11 | * This file strongly follows the Serialization example from the boost::asio
|
|---|
| 12 | * library (see server.cpp)
|
|---|
| 13 | *
|
|---|
| 14 | * Created on: Oct 19, 2011
|
|---|
| 15 | * Author: heber
|
|---|
| 16 | */
|
|---|
| 17 |
|
|---|
| 18 | // include config.h
|
|---|
| 19 | #ifdef HAVE_CONFIG_H
|
|---|
| 20 | #include <config.h>
|
|---|
| 21 | #endif
|
|---|
| 22 |
|
|---|
| 23 | // boost asio needs specific operator new
|
|---|
| 24 | #include <boost/asio.hpp>
|
|---|
| 25 |
|
|---|
| 26 | #include "CodePatterns/MemDebug.hpp"
|
|---|
| 27 |
|
|---|
| 28 | #include <boost/bind.hpp>
|
|---|
| 29 | #include <boost/lexical_cast.hpp>
|
|---|
| 30 | #include <iostream>
|
|---|
| 31 | #include <vector>
|
|---|
| 32 | #include "Connection.hpp" // Must come before boost/serialization headers.
|
|---|
| 33 | #include <boost/serialization/vector.hpp>
|
|---|
| 34 | #include "CodePatterns/Info.hpp"
|
|---|
| 35 | #include "CodePatterns/Log.hpp"
|
|---|
| 36 | #include "FragmentJob.hpp"
|
|---|
| 37 | #include "JobId.hpp"
|
|---|
| 38 |
|
|---|
| 39 | #include "FragmentScheduler.hpp"
|
|---|
| 40 |
|
|---|
| 41 | FragmentJob FragmentScheduler::NoJob(std::string("NoJob"), JobId::NoJob);
|
|---|
| 42 |
|
|---|
| 43 | /** Constructor of class FragmentScheduler.
|
|---|
| 44 | *
|
|---|
| 45 | * We setup both acceptors to accept connections from workers and Controller.
|
|---|
| 46 | *
|
|---|
| 47 | * \param io_service io_service of the asynchronous communications
|
|---|
| 48 | * \param workerport port to listen for worker connections
|
|---|
| 49 | * \param controllerport port to listen for controller connections.
|
|---|
| 50 | */
|
|---|
| 51 | FragmentScheduler::FragmentScheduler(boost::asio::io_service& io_service, unsigned short workerport, unsigned short controllerport) :
|
|---|
| 52 | worker_acceptor_(io_service,
|
|---|
| 53 | boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), workerport)
|
|---|
| 54 | ),
|
|---|
| 55 | controller_acceptor_(io_service,
|
|---|
| 56 | boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), controllerport)
|
|---|
| 57 | ),
|
|---|
| 58 | result(JobId::NoJob),
|
|---|
| 59 | Exitflag(OkFlag)
|
|---|
| 60 | {
|
|---|
| 61 | Info info(__FUNCTION__);
|
|---|
| 62 | {
|
|---|
| 63 | // Start an accept operation for worker connections.
|
|---|
| 64 | connection_ptr new_conn(new Connection(worker_acceptor_.get_io_service()));
|
|---|
| 65 | worker_acceptor_.async_accept(new_conn->socket(),
|
|---|
| 66 | boost::bind(&FragmentScheduler::handle_AcceptWorker, this,
|
|---|
| 67 | boost::asio::placeholders::error, new_conn));
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | {
|
|---|
| 71 | // Start an accept operation for controller connection.
|
|---|
| 72 | connection_ptr new_conn(new Connection(controller_acceptor_.get_io_service()));
|
|---|
| 73 | controller_acceptor_.async_accept(new_conn->socket(),
|
|---|
| 74 | boost::bind(&FragmentScheduler::handle_AcceptController, this,
|
|---|
| 75 | boost::asio::placeholders::error, new_conn));
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /** Handle a new worker connection.
|
|---|
| 80 | *
|
|---|
| 81 | * We check whether jobs are in the JobsQueue. If present, job is sent.
|
|---|
| 82 | *
|
|---|
| 83 | * \sa handle_SendJobtoWorker()
|
|---|
| 84 | *
|
|---|
| 85 | * \param e error code if something went wrong
|
|---|
| 86 | * \param conn reference with the connection
|
|---|
| 87 | */
|
|---|
| 88 | void FragmentScheduler::handle_AcceptWorker(const boost::system::error_code& e, connection_ptr conn)
|
|---|
| 89 | {
|
|---|
| 90 | Info info(__FUNCTION__);
|
|---|
| 91 | if (!e)
|
|---|
| 92 | {
|
|---|
| 93 | // Successfully accepted a new connection.
|
|---|
| 94 | // Check whether there are jobs in the queue
|
|---|
| 95 | if (JobsQueue.isJobPresent()) {
|
|---|
| 96 | // pop a job and send it to the client.
|
|---|
| 97 | FragmentJob job(JobsQueue.popJob());
|
|---|
| 98 | // The connection::async_write() function will automatically
|
|---|
| 99 | // serialize the data structure for us.
|
|---|
| 100 | LOG(1, "INFO: Sending job #" << job.getId() << ".");
|
|---|
| 101 | conn->async_write(job,
|
|---|
| 102 | boost::bind(&FragmentScheduler::handle_SendJobtoWorker, this,
|
|---|
| 103 | boost::asio::placeholders::error, conn));
|
|---|
| 104 |
|
|---|
| 105 | // Start an accept operation for a new Connection only when there
|
|---|
| 106 | // are still jobs present otherwise we quit.
|
|---|
| 107 | connection_ptr new_conn(new Connection(worker_acceptor_.get_io_service()));
|
|---|
| 108 | worker_acceptor_.async_accept(new_conn->socket(),
|
|---|
| 109 | boost::bind(&FragmentScheduler::handle_AcceptWorker, this,
|
|---|
| 110 | boost::asio::placeholders::error, new_conn));
|
|---|
| 111 | } else {
|
|---|
| 112 | // send the static NoJob
|
|---|
| 113 | conn->async_write(NoJob,
|
|---|
| 114 | boost::bind(&FragmentScheduler::handle_SendJobtoWorker, this,
|
|---|
| 115 | boost::asio::placeholders::error, conn));
|
|---|
| 116 |
|
|---|
| 117 | // then there must be no read necesary
|
|---|
| 118 |
|
|---|
| 119 | ELOG(2, "There is currently no job present in the queue.");
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 | else
|
|---|
| 123 | {
|
|---|
| 124 | // An error occurred. Log it and return. Since we are not starting a new
|
|---|
| 125 | // accept operation the io_service will run out of work to do and the
|
|---|
| 126 | // server will exit.
|
|---|
| 127 | ELOG(0, e.message());
|
|---|
| 128 | }
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /** Callback function when job has been sent.
|
|---|
| 132 | *
|
|---|
| 133 | * After job has been sent we start async_read() for the result.
|
|---|
| 134 | *
|
|---|
| 135 | * \sa handle_ReceiveResultFromWorker()
|
|---|
| 136 | *
|
|---|
| 137 | * \param e error code if something went wrong
|
|---|
| 138 | * \param conn reference with the connection
|
|---|
| 139 | */
|
|---|
| 140 | void FragmentScheduler::handle_SendJobtoWorker(const boost::system::error_code& e, connection_ptr conn)
|
|---|
| 141 | {
|
|---|
| 142 | Info info(__FUNCTION__);
|
|---|
| 143 | LOG(1, "INFO: Job sent.");
|
|---|
| 144 | // obtain result
|
|---|
| 145 | LOG(1, "INFO: Receiving result for a job ...");
|
|---|
| 146 | conn->async_read(result,
|
|---|
| 147 | boost::bind(&FragmentScheduler::handle_ReceiveResultFromWorker, this,
|
|---|
| 148 | boost::asio::placeholders::error, conn));
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | /** Callback function when result has been received.
|
|---|
| 152 | *
|
|---|
| 153 | * \param e error code if something went wrong
|
|---|
| 154 | * \param conn reference with the connection
|
|---|
| 155 | */
|
|---|
| 156 | void FragmentScheduler::handle_ReceiveResultFromWorker(const boost::system::error_code& e, connection_ptr conn)
|
|---|
| 157 | {
|
|---|
| 158 | Info info(__FUNCTION__);
|
|---|
| 159 | // nothing to do
|
|---|
| 160 | LOG(1, "INFO: Received result for job #" << result.getId() << " ...");
|
|---|
| 161 | // and push into queue
|
|---|
| 162 | ASSERT(result.getId() != (JobId_t)JobId::NoJob,
|
|---|
| 163 | "FragmentScheduler::handle_ReceiveResultFromWorker() - result received has NoJob id.");
|
|---|
| 164 | ASSERT(result.getId() != (JobId_t)JobId::IllegalJob,
|
|---|
| 165 | "FragmentScheduler::handle_ReceiveResultFromWorker() - result received has IllegalJob id.");
|
|---|
| 166 | if ((result.getId() != (JobId_t)JobId::NoJob) && (result.getId() != (JobId_t)JobId::IllegalJob))
|
|---|
| 167 | JobsQueue.pushResult(result);
|
|---|
| 168 | // erase result
|
|---|
| 169 | result = FragmentResult(JobId::NoJob);
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | /** Handle a new controller connection.
|
|---|
| 173 | *
|
|---|
| 174 | * \sa handle_ReceiveJobs()
|
|---|
| 175 | * \sa handle_CheckResultState()
|
|---|
| 176 | * \sa handle_SendResults()
|
|---|
| 177 | *
|
|---|
| 178 | * \param e error code if something went wrong
|
|---|
| 179 | * \param conn reference with the connection
|
|---|
| 180 | */
|
|---|
| 181 | void FragmentScheduler::handle_AcceptController(const boost::system::error_code& e, connection_ptr conn)
|
|---|
| 182 | {
|
|---|
| 183 | Info info(__FUNCTION__);
|
|---|
| 184 | if (!e)
|
|---|
| 185 | {
|
|---|
| 186 | if (jobs.empty()) {
|
|---|
| 187 | // The connection::async_write() function will automatically
|
|---|
| 188 | // serialize the data structure for us.
|
|---|
| 189 | LOG(1, "INFO: Receiving bunch of jobs from a controller ...");
|
|---|
| 190 | conn->async_read(jobs,
|
|---|
| 191 | boost::bind(&FragmentScheduler::handle_ReceiveJobs, this,
|
|---|
| 192 | boost::asio::placeholders::error, conn));
|
|---|
| 193 | }
|
|---|
| 194 | }
|
|---|
| 195 | else
|
|---|
| 196 | {
|
|---|
| 197 | // An error occurred. Log it and return. Since we are not starting a new
|
|---|
| 198 | // accept operation the io_service will run out of work to do and the
|
|---|
| 199 | // server will exit.
|
|---|
| 200 | Exitflag = ErrorFlag;
|
|---|
| 201 | ELOG(0, e.message());
|
|---|
| 202 | }
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | /** Controller callback function when job has been sent.
|
|---|
| 206 | *
|
|---|
| 207 | * \param e error code if something went wrong
|
|---|
| 208 | * \param conn reference with the connection
|
|---|
| 209 | */
|
|---|
| 210 | void FragmentScheduler::handle_ReceiveJobs(const boost::system::error_code& e, connection_ptr conn)
|
|---|
| 211 | {
|
|---|
| 212 | Info info(__FUNCTION__);
|
|---|
| 213 | // jobs are received, hence place in JobsQueue
|
|---|
| 214 | if (!jobs.empty()) {
|
|---|
| 215 | LOG(1, "INFO: Pushing " << jobs.size() << " jobs into queue.");
|
|---|
| 216 | JobsQueue.pushJobs(jobs);
|
|---|
| 217 | }
|
|---|
| 218 | // launch new acceptor of queue has been filled/is full
|
|---|
| 219 | if (JobsQueue.isJobPresent()) {
|
|---|
| 220 | // Start an accept operation for a new Connection.
|
|---|
| 221 | connection_ptr new_conn(new Connection(controller_acceptor_.get_io_service()));
|
|---|
| 222 | controller_acceptor_.async_accept(new_conn->socket(),
|
|---|
| 223 | boost::bind(&FragmentScheduler::handle_AcceptController, this,
|
|---|
| 224 | boost::asio::placeholders::error, new_conn));
|
|---|
| 225 | } else {
|
|---|
| 226 | LOG(1, "INFO: Shutting down controller socket.");
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | jobs.clear();
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|