connection.h

Go to the documentation of this file.
00001 /* vim: set et sw=3 tw=0 fo=croqlaw cino=t0:
00002  *
00003  * Astxx, the Asterisk C++ API and Utility Library.
00004  * Copyright (C) 2005-2007  Matthew A. Nicholson
00005  * Copyright (C) 2005-2007  Digium, Inc.
00006  * 
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License version 2.1 as published by the Free Software Foundation.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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

Generated on Thu Jul 3 01:32:42 2008 for Astxx by  doxygen 1.5.6