#include <astxx/manager/connection.h>

This class is the main point of access to the manager API. Once connected you can send commands to and read events from Asterisk.
Commands can be sent using the connection::send_action() or connection::send_action_async() function. The connection::send_action() function blocks and waits for asterisk to send a response before returning. The '()' operator is overloaded to behave as both of these functions depending on which arguments are passed to it.
action::logoff logoff; message::response r = logoff(connection); // action::logoff will throw // exceptions for 'Error' // responses from Asterisk
message::response r = connection(action::logoff()); // no processing // of the response // will be done
async_response_handler handler(); action::logoff logoff; connection(logoff, handler);
To recieve events you must register an event handler (via connection::register_event()) then trigger the handler by calling connection::process_events() (optionally after connection::wait_event() which will wait for an event and put it in the event queue if the queue is empty). The handler can be a function, functor, or member function that takes message::event object as a parameter. To unregister an event use the boost::signals::connection::disconnect() method from the boost::signals::connection object returned by connection::register_event().
Your main application loop could look something like this:
connection.wait_event(); connection.pump_messages(); connection.process_events(); connection.process_responses(); // if you are sending actions asychronously.
Definition at line 99 of file connection.h.
Public Types | |
| typedef boost::function< void(message::event)> | event_handler_t |
|
typedef std::map< std::string, boost::shared_ptr < boost::signal< void(message::event)> > > | event_handlers_t |
|
typedef std::queue < message::event > | events_t |
| typedef boost::function< void(message::response)> | response_handler_t |
|
typedef std::queue < response_handler_t > | response_handlers_t |
|
typedef std::queue < message::response > | responses_t |
Public Member Functions | |
| void | connect (const std::string &host="", unsigned short port=0) |
| Connect to the given host on the given port. | |
| connection (const std::string &host, unsigned short port=5038) | |
| Initilize a connection to the given host on the given port. | |
| void | disconnect () |
| Close our connection to Asterisk. | |
| bool | is_connected () const |
| Check if we are connected. | |
| std::string | name () const |
| Get the name of the manager we are connected to. | |
| void | operator() (const manager::basic_action &command, response_handler_t handler) |
| Send a command to Asterisk and recieve the response asynchronously. | |
| message::response | operator() (const manager::basic_action &command) |
| Send a command to Asterisk. | |
| void | process_events () |
| Process all the events in the queue. | |
| void | process_responses () |
| Process asynchronous responses. | |
| void | pump_messages () |
| Read messages from the network until there is no more data waiting. | |
| boost::signals::connection | register_event (const std::string &e, boost::function< void(message::event)> f) |
| Register an event handler. | |
| message::response | send_action (const manager::basic_action &command) |
| Send a command to Asterisk. | |
| void | send_action_async (const manager::basic_action &command, response_handler_t handler) |
| Send a command to Asterisk and recieve the response asynchronously. | |
| std::string | version () const |
| Get the version string for the manager we are connected to. | |
| void | wait_event () |
| Wait for an event and put it in the queue. | |
| void | wait_response () |
| Wait for a response and puts it in the queue. | |
| astxx::manager::connection::connection | ( | const std::string & | host, | |
| unsigned short | port = 5038 | |||
| ) |
Initilize a connection to the given host on the given port.
| host | the host to connect to | |
| port | the port to connect on |
| boost::system::system_error | if there is a problem connecting to or resolving a host |
Definition at line 81 of file connection.cc.
References connect().
| void astxx::manager::connection::connect | ( | const std::string & | host = "", |
|
| unsigned short | port = 0 | |||
| ) |
Connect to the given host on the given port.
| host | the host to connect to | |
| port | the port to connect on |
| boost::system::system_error | if there is a problem connecting to or resolving a host |
Definition at line 99 of file connection.cc.
Referenced by connection().
| void astxx::manager::connection::disconnect | ( | ) |
Close our connection to Asterisk.
Definition at line 142 of file connection.cc.
| bool astxx::manager::connection::is_connected | ( | ) | const |
Check if we are connected.
Definition at line 150 of file connection.cc.
| void astxx::manager::connection::operator() | ( | const manager::basic_action & | command, | |
| response_handler_t | handler | |||
| ) |
Send a command to Asterisk and recieve the response asynchronously.
| command | the command to send | |
| handler | the handler function/functor |
Definition at line 238 of file connection.cc.
References send_action_async().
| message::response astxx::manager::connection::operator() | ( | const manager::basic_action & | command | ) |
Send a command to Asterisk.
| command | the command to send |
Definition at line 228 of file connection.cc.
References send_action().
| void astxx::manager::connection::process_events | ( | ) |
Process all the events in the queue.
This function executes all the registered event handlers that match events in the queue.
Definition at line 386 of file connection.cc.
References astxx::manager::message::basic_message< message_traits >::main_header().
| void astxx::manager::connection::process_responses | ( | ) |
Process asynchronous responses.
This function executes handlers for responses waiting.
This function is called by connection::send_action() while waiting for a response.
Definition at line 427 of file connection.cc.
| void astxx::manager::connection::pump_messages | ( | ) |
Read messages from the network until there is no more data waiting.
This function does not block. It reads messages from the network until there are no more waiting and places them in the proper queues. If there are no messages waiting it will simply return.
Definition at line 457 of file connection.cc.
| boost::signals::connection astxx::manager::connection::register_event | ( | const std::string & | e, | |
| boost::function< void(message::event)> | f | |||
| ) |
Register an event handler.
| e | the name of the event (case sensitive, use a blank string to match all events) | |
| f | the callback function pointer or functor |
The event handler can be unregistered using the boost::signals::connection object that is returned.
The signature of the handler should be:
void event_handler(message::event e); // e is the event
Definition at line 487 of file connection.cc.
| message::response astxx::manager::connection::send_action | ( | const manager::basic_action & | command | ) |
Send a command to Asterisk.
| command | the command to send |
If error handling is desired, pass the result of this function to basic_action::handle_response() which will throw exceptions for most errors the manager returns. The basic_action::operator()() function does this automatically.
action::example example; message::response r = example(connection); // send this action over // "connection" and handle // errors before returning // the response
Definition at line 180 of file connection.cc.
References send_action_async(), and astxx::manager::response_waiter::wait().
Referenced by operator()().
| void astxx::manager::connection::send_action_async | ( | const manager::basic_action & | command, | |
| response_handler_t | handler | |||
| ) |
Send a command to Asterisk and recieve the response asynchronously.
| command | the command to send | |
| handler | the handler function/functor |
The handler function pointer/functor will be copied unless you use boost::ref to prevent it. You can use boost::bind to pass member functions as response handlers.
The signature of the handler should be:
void response_handler(message::response r); // r is the response
The connection::process_responses() function triggers response handler execution. See its documentation for circumstances when it will be called.
Definition at line 212 of file connection.cc.
References astxx::manager::basic_action::action(), astxx::manager::basic_action::action_id(), and astxx::manager::message::basic_message< message_traits >::format().
Referenced by operator()(), and send_action().
| void astxx::manager::connection::wait_event | ( | ) |
Wait for an event and put it in the queue.
This function waits for an event.
Definition at line 415 of file connection.cc.
| void astxx::manager::connection::wait_response | ( | ) |
Wait for a response and puts it in the queue.
This function waits for a response.
Definition at line 446 of file connection.cc.
1.5.6