[2eff32] | 1 | /*
|
---|
[cd4a6e] | 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 FragmentWorker.cpp
|
---|
| 10 | *
|
---|
| 11 | * This file strongly follows the Serialization example from the boost::asio
|
---|
| 12 | * library (see client.cpp).
|
---|
[2eff32] | 13 | *
|
---|
| 14 | * Created on: Nov 18, 2011
|
---|
| 15 | * Author: heber
|
---|
| 16 | */
|
---|
| 17 |
|
---|
[f93842] | 18 | // include config.h
|
---|
| 19 | #ifdef HAVE_CONFIG_H
|
---|
| 20 | #include <config.h>
|
---|
| 21 | #endif
|
---|
| 22 |
|
---|
[00d4fb] | 23 | // boost asio needs specific operator new
|
---|
[2eff32] | 24 | #include <boost/asio.hpp>
|
---|
[00d4fb] | 25 |
|
---|
| 26 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 27 |
|
---|
[2eff32] | 28 | #include <boost/bind.hpp>
|
---|
| 29 | #include <iostream>
|
---|
| 30 | #include <vector>
|
---|
[af3aed] | 31 | #include "Connection.hpp" // Must come before boost/serialization headers.
|
---|
[2eff32] | 32 | #include <boost/serialization/vector.hpp>
|
---|
[af3aed] | 33 | #include "CodePatterns/Info.hpp"
|
---|
[b0b64c] | 34 | #include "CodePatterns/Log.hpp"
|
---|
[f0834d] | 35 | #include "Jobs/FragmentJob.hpp"
|
---|
[306f60] | 36 | #include "Jobs/MPQCCommandJob.hpp"
|
---|
[d920b9] | 37 | #include "Jobs/SystemCommandJob.hpp"
|
---|
[7670865] | 38 | #include "Results/FragmentResult.hpp"
|
---|
[cd4a6e] | 39 | #include "FragmentWorker.hpp"
|
---|
[2eff32] | 40 |
|
---|
[35f587] | 41 | FragmentResult::ptr FragmentWorker::EmptyResult( new FragmentResult(JobId::NoJob, std::string("EmptyResult")) );
|
---|
[ef2767] | 42 |
|
---|
[d920b9] | 43 | /** Helper function to enforce binding of FragmentWorker to possible derived
|
---|
| 44 | * FragmentJob classes.
|
---|
| 45 | */
|
---|
| 46 | void dummyInit() {
|
---|
| 47 | SystemCommandJob("/bin/false", "something", JobId::IllegalJob);
|
---|
[306f60] | 48 | MPQCCommandJob("nofile", JobId::IllegalJob);
|
---|
[d920b9] | 49 | }
|
---|
| 50 |
|
---|
[a636f8] | 51 | /// Constructor starts the asynchronous connect operation.
|
---|
[8c9596] | 52 | FragmentWorker::FragmentWorker(
|
---|
[a636f8] | 53 | boost::asio::io_service& io_service,
|
---|
| 54 | const std::string& host,
|
---|
| 55 | const std::string& service) :
|
---|
[a1b77d] | 56 | connection_(io_service),
|
---|
[0bdd51b] | 57 | result(JobId::IllegalJob),
|
---|
| 58 | Exitflag(OkFlag)
|
---|
[a636f8] | 59 | {
|
---|
[af3aed] | 60 | Info info(__FUNCTION__);
|
---|
[a636f8] | 61 | // Resolve the host name into an IP address.
|
---|
| 62 | boost::asio::ip::tcp::resolver resolver(io_service);
|
---|
| 63 | boost::asio::ip::tcp::resolver::query query(host, service);
|
---|
| 64 | boost::asio::ip::tcp::resolver::iterator endpoint_iterator =
|
---|
| 65 | resolver.resolve(query);
|
---|
| 66 | boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
|
---|
[2eff32] | 67 |
|
---|
[a636f8] | 68 | // Start an asynchronous connect operation.
|
---|
[e3a9c9] | 69 | std::cout << "Connecting to endpoint " << endpoint << std::endl;
|
---|
[a636f8] | 70 | connection_.socket().async_connect(endpoint,
|
---|
[8c9596] | 71 | boost::bind(&FragmentWorker::handle_connect, this,
|
---|
[e3a9c9] | 72 | boost::asio::placeholders::error, ++endpoint_iterator));
|
---|
[a636f8] | 73 | }
|
---|
[2eff32] | 74 |
|
---|
[a636f8] | 75 | /// Handle completion of a connect operation.
|
---|
[e3a9c9] | 76 | void FragmentWorker::handle_connect(const boost::system::error_code& e,
|
---|
| 77 | boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
|
---|
[a636f8] | 78 | {
|
---|
[ef2767] | 79 | Info info(__FUNCTION__);
|
---|
[a636f8] | 80 | if (!e)
|
---|
[2eff32] | 81 | {
|
---|
[a636f8] | 82 | // Successfully established connection. Start operation to read the list
|
---|
[cd4a6e] | 83 | // of jobs. The connection::async_read() function will automatically
|
---|
[a636f8] | 84 | // decode the data that is read from the underlying socket.
|
---|
[ef2767] | 85 | LOG(1, "INFO: Receiving a job ...");
|
---|
[b0b64c] | 86 | connection_.async_read(job,
|
---|
[083490] | 87 | boost::bind(&FragmentWorker::handle_ReceiveJob, this,
|
---|
[a636f8] | 88 | boost::asio::placeholders::error));
|
---|
[e3a9c9] | 89 | } else if (endpoint_iterator != boost::asio::ip::tcp::resolver::iterator()) {
|
---|
| 90 | // Try the next endpoint.
|
---|
| 91 | connection_.socket().close();
|
---|
| 92 | boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
|
---|
| 93 | connection_.socket().async_connect(endpoint,
|
---|
| 94 | boost::bind(&FragmentWorker::handle_connect, this,
|
---|
| 95 | boost::asio::placeholders::error, ++endpoint_iterator));
|
---|
| 96 | } else {
|
---|
[a636f8] | 97 | // An error occurred. Log it and return. Since we are not starting a new
|
---|
| 98 | // operation the io_service will run out of work to do and the client will
|
---|
| 99 | // exit.
|
---|
[0bdd51b] | 100 | Exitflag = ErrorFlag;
|
---|
[b0b64c] | 101 | ELOG(1, e.message());
|
---|
[2eff32] | 102 | }
|
---|
[a636f8] | 103 | }
|
---|
[2eff32] | 104 |
|
---|
[083490] | 105 | /// Callback function when job has been received.
|
---|
| 106 | void FragmentWorker::handle_ReceiveJob(const boost::system::error_code& e)
|
---|
[a636f8] | 107 | {
|
---|
[b05673] | 108 | Info info(__FUNCTION__);
|
---|
[a636f8] | 109 | if (!e)
|
---|
[2eff32] | 110 | {
|
---|
[78ad7d] | 111 | LOG(1, "INFO: Received job #" << job->getId() << ".");
|
---|
| 112 | if (job->getId() != JobId::NoJob) {
|
---|
[c7deca] | 113 | // Print out the data that was received.
|
---|
[78ad7d] | 114 | std::cout << "Job id: " << job->getId() << "\n";
|
---|
[ef2767] | 115 |
|
---|
[a1b77d] | 116 | // do something .. right now: wait
|
---|
[78ad7d] | 117 | LOG(1, "INFO: Calculating job #" << job->getId() << " ...");
|
---|
[35f587] | 118 | FragmentResult::ptr result(job->Work());
|
---|
[ef2767] | 119 |
|
---|
| 120 | // write the result to the server
|
---|
[78ad7d] | 121 | LOG(1, "INFO: Sending result for job #" << job->getId() << " ...");
|
---|
[ef2767] | 122 | connection_.async_write(result,
|
---|
[083490] | 123 | boost::bind(&FragmentWorker::handle_SendResult, this,
|
---|
[ef2767] | 124 | boost::asio::placeholders::error));
|
---|
| 125 |
|
---|
[c7deca] | 126 | } else {
|
---|
| 127 | std::cout << "The server has no job for me." << std::endl;
|
---|
[ef2767] | 128 | // send out empty result
|
---|
| 129 | LOG(1, "INFO: Sending empty result ...");
|
---|
| 130 | connection_.async_write(EmptyResult,
|
---|
[083490] | 131 | boost::bind(&FragmentWorker::handle_SendResult, this,
|
---|
[ef2767] | 132 | boost::asio::placeholders::error));
|
---|
[0bdd51b] | 133 | Exitflag = NoJobFlag;
|
---|
[c7deca] | 134 | }
|
---|
[2eff32] | 135 | }
|
---|
[a636f8] | 136 | else
|
---|
| 137 | {
|
---|
| 138 | // An error occurred.
|
---|
[0bdd51b] | 139 | Exitflag = ErrorFlag;
|
---|
[b0b64c] | 140 | ELOG(1, e.message());
|
---|
[a636f8] | 141 | }
|
---|
| 142 |
|
---|
| 143 | // Since we are not starting a new operation the io_service will run out of
|
---|
| 144 | // work to do and the client will exit.
|
---|
| 145 | }
|
---|
[0bdd51b] | 146 |
|
---|
[083490] | 147 | /// Callback function when result has been sent.
|
---|
| 148 | void FragmentWorker::handle_SendResult(const boost::system::error_code& e)
|
---|
[ef2767] | 149 | {
|
---|
| 150 | Info info(__FUNCTION__);
|
---|
| 151 | // Nothing to do.
|
---|
[78ad7d] | 152 | LOG(1, "INFO: Job #" << job->getId() << " calculated and sent.");
|
---|
[ef2767] | 153 | // erase job
|
---|
[d920b9] | 154 | job.reset();
|
---|
[ef2767] | 155 | }
|
---|
| 156 |
|
---|