connection.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00029 #ifndef ASTXX_MANAGER_CONNECTION_H
00030 #define ASTXX_MANAGER_CONNECTION_H
00031
00032 #include <astxx/manager/message.h>
00033 #include <astxx/manager/basic_action.h>
00034
00035 #include <queue>
00036 #include <map>
00037 #include <string>
00038 #include <boost/function.hpp>
00039 #include <boost/asio.hpp>
00040 #include <boost/signal.hpp>
00041 #include <boost/shared_ptr.hpp>
00042
00043 namespace astxx {
00044 namespace manager {
00099 class connection {
00100 private:
00101 std::pair<std::string, std::string> parse_header(const std::string& header);
00102 std::string read_line();
00103 void read_message();
00104 message::response read_response();
00105 message::event read_event();
00106
00107 public:
00108 typedef boost::function<void (message::response)> response_handler_t;
00109 typedef boost::function<void (message::event)> event_handler_t;
00110 typedef std::queue<message::event> events_t;
00111 typedef std::queue<message::response> responses_t;
00112 typedef std::queue<response_handler_t> response_handlers_t;
00113 typedef std::map<std::string, boost::shared_ptr<boost::signal<void (message::event)> > > event_handlers_t;
00114
00115 connection(const std::string& host, unsigned short port = 5038);
00116
00117 void connect(const std::string& host = "", unsigned short port = 0);
00118 void disconnect();
00119 bool is_connected() const;
00120
00123 std::string name() const { return m_name; }
00126 std::string version() const { return m_version; }
00127
00128 message::response send_action(const manager::basic_action& command);
00129 void send_action_async(const manager::basic_action& command, response_handler_t handler);
00130
00131 message::response operator()(const manager::basic_action& command);
00132 void operator()(const manager::basic_action& command, response_handler_t handler);
00133
00134 void process_events();
00135 void wait_event();
00136
00137 void process_responses();
00138 void wait_response();
00139
00140 void pump_messages();
00141
00142 boost::signals::connection register_event(const std::string& e, boost::function<void (message::event)> f);
00143
00144 private:
00145 boost::asio::io_service io_service;
00146 boost::asio::ip::tcp::socket socket;
00147
00148 std::string m_name;
00149 std::string m_version;
00150
00151 std::string m_host;
00152 std::string m_port;
00153
00154 events_t events;
00155 responses_t responses;
00156 response_handlers_t response_handlers;
00157 event_handlers_t event_handlers;
00158 };
00159 }
00160 }
00161
00162 #endif