| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
| 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 | 
 | 
|---|
| 24 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 25 | 
 | 
|---|
| 26 | #include "SyncOperation.hpp"
 | 
|---|
| 27 | 
 | 
|---|
| 28 | #include <boost/bind.hpp>
 | 
|---|
| 29 | #include <vector>
 | 
|---|
| 30 | #include "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);
 | 
|---|
| 44 |   boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
 | 
|---|
| 45 | 
 | 
|---|
| 46 |   // Start an asynchronous connect operation.
 | 
|---|
| 47 |   std::cout << "Connecting to endpoint " << endpoint << " ..." << std::endl;
 | 
|---|
| 48 |   connection_.socket().connect(endpoint);
 | 
|---|
| 49 | }
 | 
|---|
| 50 | 
 | 
|---|
| 51 | 
 | 
|---|
| 52 | /** Internal function to disconnect connection_ correctly.
 | 
|---|
| 53 |  *
 | 
|---|
| 54 |  */
 | 
|---|
| 55 | void SyncOperation::disconnect()
 | 
|---|
| 56 | {
 | 
|---|
| 57 |   connection_.socket().close();
 | 
|---|
| 58 | }
 | 
|---|
| 59 | 
 | 
|---|
| 60 | /** Wrapper function for the virtual call to internal() that connects and disconnects.
 | 
|---|
| 61 |  *
 | 
|---|
| 62 |  * @param _host host address to connect to
 | 
|---|
| 63 |  * @param _service service to connect to
 | 
|---|
| 64 |  */
 | 
|---|
| 65 | void SyncOperation::operator()(const std::string& _host, const std::string& _service)
 | 
|---|
| 66 | {
 | 
|---|
| 67 |   LOG(1, "INFO: Commencing operation ...");
 | 
|---|
| 68 | 
 | 
|---|
| 69 |   // connect
 | 
|---|
| 70 |   connect(_host, _service);
 | 
|---|
| 71 | 
 | 
|---|
| 72 |   // call virtual function to continue
 | 
|---|
| 73 |   internal();
 | 
|---|
| 74 | 
 | 
|---|
| 75 |   // disconnect
 | 
|---|
| 76 |   disconnect();
 | 
|---|
| 77 | 
 | 
|---|
| 78 |   LOG(1, "INFO: Operation completed.");
 | 
|---|
| 79 | }
 | 
|---|