NAME

Asterisk::CoroManager - Asterisk Manager Interface, Coro version

SYNOPSIS

use Asterisk::CoroManager;

my $astman = new Asterisk::CoroManager({
                                        user   => 'username',
                                        secret => 'test',
                                        host   => 'localhost',
                                      });

$astman->connect || die "Could not connect to " . $astman->host . "!\n";

my $ping = $astman->sendcommand({ Action => 'Ping' }, { timeout => '2' });

if( $ping ) {
    # $ping->{Response} should be 'Pong'
    print "Yay, we're alive! We got ". $ping->{Response} ."\n";
}
else {
    print "Got no pong in 2 seconds :-(\n";
}

$astman->disconnect;

DESCRIPTION

This module provides a dependable, event-based interface to the asterisk manager interface. http://www.voip-info.org/wiki/view/Asterisk+manager+API

This is done with Coro, and continuations. If you are unfamiliar with Coro, go read up on it! Your program should 'use Coro' quite at the beginning, and be aware of that it is asynchronous. If you wait for an answer to a sendcommand, other events will probably be triggered in the meanwhile.

Logging / Error handling

Asterisk::CoroManager uses Log::Log4perl if it is installed. Read Log::Log4perl, or initialize a simple logger like this:

use Log::Log4perl qw(:easy);
Log::Log4perl->easy_init( { level => $DEBUG,
                            file => ">>test.log" } );

Constructor

new

my $astman = new Asterisk::CoroManager({
                                        host   => 'localhost',
                                        user   => 'username',
                                        secret => 'test',
                                       });

Supported args are:

host       Asterisk host.  Defaults to 'localhost'.
port       Manager port.  Defaults to 5038,
user       Manager user.
secret     Manager secret.

Actions

connect

$astman->connect or croak "Could not connect to ". $astman->host ."!\n";

Connects the manager to asterisk. User, secret and host should be set before calling this.

Returns Asterisk Manager Interface version on success; otherwise undef.

disconnect

$astman->disconnect;

Disconnects from asterisk manager interface and ends eventloop.

sendcommand

my $resp = $astman->sendcommand({
                                 Action    => 'QueuePause',
                                 Interface => 'SIP/1234',
                                 Paused    => 'true',
                                },
                                { Timeout => 2 });

Sends a command to asterisk.

If you are looking for a response, the command will wait for the specific response from asterisk (identified with an ActionID). Otherwise it returns immediately.

TODO: Implement timeout: Timeout is how long to wait for the response. Defaults to 3 seconds.

Returns a hash or hash-ref on success (depending on wantarray), undef on timeout.

check_connection

$astman->check_connection();

Checks if the connection is still alive. Tries to reconnect if it isn't.

eventloop

$astman->eventloop();

Will wait for events, until shut down.

Accessors

user

$astman->user('user')

Set user for manager connection.

secret

$astman->secret('secret')

Set secret for manager connection.

host

$astman->host('localhost')

Set host for manager connection.

port

$astman->port(5038)

Set port for manager connection; defaults to 5038.

connected

croak "Not connected!" unless($astman->connected($timeout))

Checks if manager is connected (for timeout seconds).

Returns 1 if conencted, 0 if not.

add_event_callback

$astman->add_event_callback('Join', \&update_queue_status)

Add a callback for a specific event.

Returns 1 on success, 0 on error.

add_default_event_callback

$astman->add_default_event_callback(\&debug_events)

Add a callback for all events that don't have an callback set.

Returns 1 on success, undef on error.

add_uevent_callback

$astman->add_uevent_callback('MyUserEvent', \&myfunction)

Add a callback for a specific user event.

Returns 1 on success, undef on error.

add_default_uevent_callback

$astman->add_default_uevent_callback(\&debug_events)

Add a callback for all user events that don't have an callback set.

Returns 1 on success, undef on error.

Private methods & functions

read_incoming

Called for incoming data on fh. Calls handle_packet on complete packets.

handle_packet

handle_packet is called when incoming on fh has gotten a full packet.

handle_event

handle_event is called if an incoming packet is an event. Falls back to default event handler.

handle_uevent

handle_uevent is called if an incoming packet is a user event. Falls back to default user event handler and ultimately to default event handler.

handle_actionresponse

handle_actionresponse is called if an incoming packet is a response with an ActionID.

parse_packet

Parses a packet as array-ref and returns it as hash-ref.

Puts unmatched lines in an array in $pack->{RestResult}.

make_packet

Converting a hash-ref to packet-string for manager connection.