/* * AsyncOperation.hpp * * Created on: Nov 11, 2011 * Author: heber */ #ifndef FRAGMENTCONTROLLER_ASYNCOPERATION_HPP_ #define FRAGMENTCONTROLLER_ASYNCOPERATION_HPP_ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include "CodePatterns/Observer/Observable.hpp" #include "Operation.hpp" /** This class represents an asynchronous operation that is initiated by * calling AsyncOperation::operator() and gives an update with the Observer * construct when the operation has finished. * * \note Make sure that deriving classes still end the asynchronous operation * chain by calling AsyncOperation::handle_FinishOperation(). Otherwise the * update signal at operation's end is not triggered. */ class AsyncOperation : public Operation, public Observable { public: //!> static function as default callback on success/failure of operation that does nothing static const boost::function NoOpCallback; public: /** Constructor for class AsyncOperation. * * \param _name name for this AsyncOperation for retrieval from a registry * \param _connection connection to operate on * \param callback_on_success function to call when operation ends successfully * \param callback_on_failure function to call when operation fails */ AsyncOperation( const std::string &_name, Connection &_connection, const boost::function &_callback_on_success = NoOpCallback, const boost::function &_callback_on_failure = NoOpCallback) : Operation(_name, _connection), Observable(_name), callback_on_success(_callback_on_success), callback_on_failure(_callback_on_failure) {} /** Destructor for class AsyncOperation. * */ virtual ~AsyncOperation() {} public: // virtual function pointer to the operation to do void operator()(const std::string& _host, const std::string& _service); protected: // virtual function pointer to the connection handler virtual void handle_connect(const boost::system::error_code& e, boost::asio::ip::tcp::resolver::iterator endpoint_iterator) = 0; /// Handle completion of an operation. void handle_FinishOperation(const boost::system::error_code& e); protected: /// internal function to disconnect from server void disconnect(); private: //!> Callback function on successful end of operation boost::function callback_on_success; //!> Callback function on end of operation with failure boost::function callback_on_failure; }; #endif /* FRAGMENTCONTROLLER_ASYNCOPERATION_HPP_ */