[404d2b] | 1 | /*
|
---|
| 2 | * Project: JobMarket
|
---|
| 3 | * Description: asynchronous Server/Controller/Client-approach to parallel computing, based on boost::asio
|
---|
| 4 | * Copyright (C) 2010 Frederik Heber. All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | /*
|
---|
| 9 | * SyncOperation.cpp
|
---|
| 10 | *
|
---|
| 11 | * Created on: Mar 04, 2012
|
---|
| 12 | * Author: heber
|
---|
| 13 | */
|
---|
| 14 |
|
---|
| 15 |
|
---|
| 16 | // include config.h
|
---|
| 17 | #ifdef HAVE_CONFIG_H
|
---|
| 18 | #include <config.h>
|
---|
| 19 | #endif
|
---|
| 20 |
|
---|
| 21 | // boost asio needs specific operator new
|
---|
| 22 | #include <boost/asio.hpp>
|
---|
| 23 |
|
---|
[9eb71b3] | 24 | //#include "CodePatterns/MemDebug.hpp"
|
---|
[404d2b] | 25 |
|
---|
| 26 | #include "JobMarket/Operations/SyncOperation.hpp"
|
---|
| 27 |
|
---|
| 28 | #include <boost/bind.hpp>
|
---|
| 29 | #include <vector>
|
---|
| 30 | #include "JobMarket/Connection.hpp" // Must come before boost/serialization headers.
|
---|
| 31 | #include "CodePatterns/Info.hpp"
|
---|
| 32 | #include "CodePatterns/Log.hpp"
|
---|
| 33 |
|
---|
| 34 | /** Internal function to connect connection_.
|
---|
| 35 | *
|
---|
| 36 | */
|
---|
| 37 | void SyncOperation::connect(const std::string& _host, const std::string& _service)
|
---|
| 38 | {
|
---|
| 39 | // Resolve the host name into an IP address.
|
---|
| 40 | boost::asio::ip::tcp::resolver resolver(connection_.socket().get_io_service());
|
---|
| 41 | boost::asio::ip::tcp::resolver::query query(_host, _service);
|
---|
| 42 | boost::asio::ip::tcp::resolver::iterator endpoint_iterator =
|
---|
| 43 | resolver.resolve(query);
|
---|
[8e7a1b] | 44 | // check whether host could be resolved
|
---|
| 45 | if (endpoint_iterator != boost::asio::ip::tcp::resolver::iterator()) {
|
---|
| 46 | boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
|
---|
[404d2b] | 47 |
|
---|
[8e7a1b] | 48 | // Start an asynchronous connect operation.
|
---|
| 49 | LOG(3, "DEBUG: Connecting synchronously to endpoint " << endpoint << " ...");
|
---|
| 50 | connection_.socket().connect(endpoint);
|
---|
| 51 | } else {
|
---|
| 52 | status = Operation::error;
|
---|
| 53 | }
|
---|
[404d2b] | 54 | }
|
---|
| 55 |
|
---|
| 56 |
|
---|
| 57 | /** Internal function to disconnect connection_ correctly.
|
---|
| 58 | *
|
---|
| 59 | */
|
---|
| 60 | void SyncOperation::disconnect()
|
---|
| 61 | {
|
---|
| 62 | connection_.socket().close();
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | /** Wrapper function for the virtual call to internal() that connects and disconnects.
|
---|
| 66 | *
|
---|
| 67 | * @param _host host address to connect to
|
---|
| 68 | * @param _service service to connect to
|
---|
| 69 | */
|
---|
| 70 | void SyncOperation::operator()(const std::string& _host, const std::string& _service)
|
---|
| 71 | {
|
---|
| 72 | DEBUG_FUNCTION_ENTRYEXIT
|
---|
| 73 |
|
---|
[8e7a1b] | 74 | status = Operation::running;
|
---|
| 75 |
|
---|
[404d2b] | 76 | // connect
|
---|
| 77 | connect(_host, _service);
|
---|
| 78 |
|
---|
| 79 | // call virtual function to continue
|
---|
| 80 | internal();
|
---|
| 81 |
|
---|
| 82 | // disconnect
|
---|
| 83 | disconnect();
|
---|
[8e7a1b] | 84 |
|
---|
| 85 | if (status == Operation::running)
|
---|
| 86 | status = Operation::success;
|
---|
[404d2b] | 87 | }
|
---|