1 | /*
|
---|
2 | * Project: JobMarket
|
---|
3 | * Description: asynchronous Server/Controller/Client-approach to parallel computing, based on boost::asio
|
---|
4 | * Copyright (C) 2011-2012 Frederik Heber. All rights reserved.
|
---|
5 | *
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * CheckResultsOperation.cpp
|
---|
10 | *
|
---|
11 | * Created on: Dec 11, 2011
|
---|
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/Operations/Controllers/CheckResultsOperation.hpp"
|
---|
26 |
|
---|
27 | #include <boost/bind.hpp>
|
---|
28 | #include <iostream>
|
---|
29 | #include "JobMarket/Connection.hpp" // Must come before boost/serialization headers.
|
---|
30 | #include <boost/serialization/set.hpp>
|
---|
31 | #include <boost/serialization/vector.hpp>
|
---|
32 | #include "CodePatterns/Info.hpp"
|
---|
33 | #include "CodePatterns/Log.hpp"
|
---|
34 | #include "JobMarket/ControllerChoices.hpp"
|
---|
35 |
|
---|
36 | // static entities
|
---|
37 | const std::set<JobId_t> CheckResultsOperation::emptyids;
|
---|
38 |
|
---|
39 | /** Handle connect operation to send number of done jobs.
|
---|
40 | *
|
---|
41 | * \param e error code if something went wrong
|
---|
42 | * \param endpoint_iterator endpoint of the connection
|
---|
43 | */
|
---|
44 | void CheckResultsOperation::handle_connect(const boost::system::error_code& e,
|
---|
45 | boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
|
---|
46 | {
|
---|
47 | DEBUG_FUNCTION_ENTRYEXIT
|
---|
48 | if (!e)
|
---|
49 | {
|
---|
50 | // Successfully established connection. Give choice.
|
---|
51 | enum ControllerChoices choice = CheckState;
|
---|
52 | connection_.async_write(choice,
|
---|
53 | boost::bind(&CheckResultsOperation::handle_SendJobIds, this,
|
---|
54 | boost::asio::placeholders::error));
|
---|
55 | } else if (endpoint_iterator != boost::asio::ip::tcp::resolver::iterator()) {
|
---|
56 | // Try the next endpoint.
|
---|
57 | connection_.socket().close();
|
---|
58 | boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
|
---|
59 | connection_.socket().async_connect(endpoint,
|
---|
60 | boost::bind(&CheckResultsOperation::handle_connect, this,
|
---|
61 | boost::asio::placeholders::error, ++endpoint_iterator));
|
---|
62 | } else {
|
---|
63 | // An error occurred. Log it and return. Since we are not starting a new
|
---|
64 | // operation the io_service will run out of work to do and the client will
|
---|
65 | // exit.
|
---|
66 | ELOG(1, e.message());
|
---|
67 | AsyncOperation::handle_FinishOperation(e);
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | /** Callback function when doneJobs have been received.
|
---|
72 | *
|
---|
73 | * \param e error code if something went wrong
|
---|
74 | */
|
---|
75 | void CheckResultsOperation::handle_SendJobIds(const boost::system::error_code& e)
|
---|
76 | {
|
---|
77 | DEBUG_FUNCTION_ENTRYEXIT
|
---|
78 | if (!e)
|
---|
79 | {
|
---|
80 | // The connection::async_write() function will automatically
|
---|
81 | // decode the data that is written to the underlying socket.
|
---|
82 | LOG(1, "INFO: Sending vector of desired " << jobids.size() << " jobids ...");
|
---|
83 | connection_.async_write(jobids,
|
---|
84 | boost::bind(&CheckResultsOperation::handle_ReceiveJobInfo, this,
|
---|
85 | boost::asio::placeholders::error));
|
---|
86 | }
|
---|
87 | else
|
---|
88 | {
|
---|
89 | // An error occurred.
|
---|
90 | ELOG(1, e.message());
|
---|
91 | AsyncOperation::handle_FinishOperation(e);
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | /** Callback function when doneJobs have been received.
|
---|
96 | *
|
---|
97 | * \param e error code if something went wrong
|
---|
98 | */
|
---|
99 | void CheckResultsOperation::handle_ReceiveJobInfo(const boost::system::error_code& e)
|
---|
100 | {
|
---|
101 | DEBUG_FUNCTION_ENTRYEXIT
|
---|
102 | if (!e)
|
---|
103 | {
|
---|
104 | // The connection::async_read() function will automatically
|
---|
105 | // decode the data that is written to the underlying socket.
|
---|
106 | LOG(1, "INFO: Obtaining number of present and done jobs ...");
|
---|
107 | connection_.async_read(jobInfo,
|
---|
108 | boost::bind(&CheckResultsOperation::handle_FinishOperation, this,
|
---|
109 | boost::asio::placeholders::error));
|
---|
110 | }
|
---|
111 | else
|
---|
112 | {
|
---|
113 | // An error occurred.
|
---|
114 | ELOG(1, e.message());
|
---|
115 | AsyncOperation::handle_FinishOperation(e);
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | /** Getter for jobInfo.
|
---|
120 | *
|
---|
121 | * \sa checkResults()
|
---|
122 | * \param number of present jobs
|
---|
123 | */
|
---|
124 | size_t CheckResultsOperation::getPresentJobs() const
|
---|
125 | {
|
---|
126 | ASSERT( jobInfo.size() == (size_t)2,
|
---|
127 | "CheckResultsOperation::getPresentJobs() - jobInfo does not contain present and done jobs.");
|
---|
128 | return jobInfo[0];
|
---|
129 | }
|
---|
130 |
|
---|
131 | /** Getter for jobInfo.
|
---|
132 | *
|
---|
133 | * \sa checkResults()
|
---|
134 | * \param number of done jobs
|
---|
135 | */
|
---|
136 | size_t CheckResultsOperation::getDoneJobs() const
|
---|
137 | {
|
---|
138 | ASSERT( jobInfo.size() == (size_t)2,
|
---|
139 | "CheckResultsOperation::getPresentJobs() - jobInfo does not contain present and done jobs.");
|
---|
140 | return jobInfo[1];
|
---|
141 | }
|
---|
142 |
|
---|