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