| 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 | * AsyncOperation.cpp
|
|---|
| 10 | *
|
|---|
| 11 | * Created on: Nov 11, 2011
|
|---|
| 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 |
|
|---|
| 24 | #include "CodePatterns/MemDebug.hpp"
|
|---|
| 25 |
|
|---|
| 26 | #include <boost/bind.hpp>
|
|---|
| 27 | #include <vector>
|
|---|
| 28 | #include "Connection.hpp" // Must come before boost/serialization headers.
|
|---|
| 29 | #include "CodePatterns/Info.hpp"
|
|---|
| 30 | #include "CodePatterns/Log.hpp"
|
|---|
| 31 |
|
|---|
| 32 | #include "Controller/Commands/AsyncOperation.hpp"
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 | /** Constructor for class AsyncOperation.
|
|---|
| 36 | *
|
|---|
| 37 | * \param _name name for this Operation for retrieval from a registry
|
|---|
| 38 | * \param _connection connection to operate on
|
|---|
| 39 | */
|
|---|
| 40 | AsyncOperation::AsyncOperation(const std::string &_name, Connection &_connection) :
|
|---|
| 41 | connection_(_connection),
|
|---|
| 42 | name(_name),
|
|---|
| 43 | Exitflag(OkFlag)
|
|---|
| 44 | {}
|
|---|
| 45 |
|
|---|
| 46 | /** Destructor for class AsyncOperation.
|
|---|
| 47 | *
|
|---|
| 48 | */
|
|---|
| 49 | AsyncOperation::~AsyncOperation()
|
|---|
| 50 | {}
|
|---|
| 51 |
|
|---|
| 52 | /** Callback function when an operation has been completed.
|
|---|
| 53 | *
|
|---|
| 54 | * \param e error code if something went wrong
|
|---|
| 55 | */
|
|---|
| 56 | void AsyncOperation::handle_FinishOperation(const boost::system::error_code& e)
|
|---|
| 57 | {
|
|---|
| 58 | Info info(__FUNCTION__);
|
|---|
| 59 | if (!e)
|
|---|
| 60 | {
|
|---|
| 61 | LOG(1, "INFO: AsyncOperation completed.");
|
|---|
| 62 | }
|
|---|
| 63 | else
|
|---|
| 64 | {
|
|---|
| 65 | // An error occurred.
|
|---|
| 66 | Exitflag = ErrorFlag;
|
|---|
| 67 | ELOG(1, e.message());
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | // Since we are not starting a new operation the io_service will run out of
|
|---|
| 71 | // work to do and the client will exit.
|
|---|
| 72 | disconnect();
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | /** Internal function to disconnect connection_ correctly.
|
|---|
| 76 | *
|
|---|
| 77 | */
|
|---|
| 78 | void AsyncOperation::disconnect()
|
|---|
| 79 | {
|
|---|
| 80 | connection_.socket().close();
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | void AsyncOperation::operator()(const std::string& _host, const std::string& _service)
|
|---|
| 84 | {
|
|---|
| 85 | Info info(__FUNCTION__);
|
|---|
| 86 | // Resolve the host name into an IP address.
|
|---|
| 87 | boost::asio::ip::tcp::resolver resolver(connection_.socket().get_io_service());
|
|---|
| 88 | boost::asio::ip::tcp::resolver::query query(_host, _service);
|
|---|
| 89 | boost::asio::ip::tcp::resolver::iterator endpoint_iterator =
|
|---|
| 90 | resolver.resolve(query);
|
|---|
| 91 | boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
|
|---|
| 92 |
|
|---|
| 93 | // Start an asynchronous connect operation.
|
|---|
| 94 | std::cout << "Connecting to endpoint " << endpoint << " ..." << std::endl;
|
|---|
| 95 | connection_.socket().async_connect(endpoint,
|
|---|
| 96 | boost::bind(&AsyncOperation::handle_connect, this,
|
|---|
| 97 | boost::asio::placeholders::error, ++endpoint_iterator));
|
|---|
| 98 | }
|
|---|