Asterisk-AMI

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN


	(If you get undef you should check the connection.)

	- check_connection changed to connected()

	- New Method process_packet(). Tells AMI to process input from asterisk.

	- 'EventPreempt' changed to 'FastEvents', behavior changed to immeditly proccess any events
	after processing a packet.

	- Added 'Timeout' option to constructor. Allows setting a default timeout on function calls.

	- Removed AutoAge and AutoClear settings and added 'BufferSize', which is the max number of Actions to hold 
	in memory. We basically use a ring buffer now for actions. Default is 30000.

	- Fixed a bug in AMI::Common where we were accidnetly using the channel name for positions for Queue Entries

	- Added AMI::Common::play_digits() which plays a series of DTMF tones on a channel.
	
	- Restructued the logic for sorting events and responses

examples/cmd_server.pl  view on Meta::CPAN

$list .= 'quit - Disconnects from server' . $EOL;
$list .= 'list - Displays this list' . $EOL . $EOR;

#Keep a list of clients
my %clients;

#Connect to asterisk
my $astman = Asterisk::AMI->new(PeerAddr => '127.0.0.1',
				Username => 'test',
				Secret	=> 'supersecret',
				Timeout => 3, #Default timeout for all operations, 3 seconds
				Keepalive => 60, #Send a keepalive every minute
				on_error => sub { print "Error occured on socket\r\n"; exit; },
				on_timeout => sub { print "Connection to asterisk timed out\r\n"; exit; },
                                Blocking => 0
			);

die "Unable to connect to asterisk" unless ($astman);

#Callback on mailbox command
sub mailboxcb {

examples/event_proxy.pl  view on Meta::CPAN

my $EOR = $EOL;

#Keep a list of clients
my %clients;

#Connect to asterisk
my $astman = Asterisk::AMI->new(PeerAddr => '127.0.0.1',
				Username => 'admin',
				Secret	=> 'supersecret',
				Events	=> 'on', #Give us something to proxy
				Timeout => 3, #Default timeout for all operations, 3 seconds
				Handlers => { default => \&proxy_event }, #Install default handler
				Keepalive => 60, #Send a keepalive every minute
				on_error => sub { print "Error occured on socket\r\n"; exit; },
				on_timeout => sub { print "Connection to asterisk timed out\r\n"; exit; }
			);

die "Unable to connect to asterisk" unless ($astman);

#Handler for events
sub proxy_event {

lib/Asterisk/AMI.pm  view on Meta::CPAN


        Key-Value Pairs accepted:
        PeerAddr        Remote host address        <hostname>
        PeerPort        Remote host port        <service>
        Events Enable/Disable Events 'on'|'off'
        Username        Username to access the AMI
        Secret Secret used to connect to AMI
        AuthType        Authentication type to use for login        'plaintext'|'MD5'
        UseSSL Enables/Disables SSL for the connection 0|1
        BufferSize        Maximum size of buffer, in number of actions
        Timeout Default timeout of all actions in seconds
        Handlers        Hash reference of Handlers for events        { 'EVENT' => \&somesub };
        Keepalive        Interval (in seconds) to periodically send 'Ping' actions to asterisk
        TCP_Keepalive        Enables/Disables SO_KEEPALIVE option on the socket        0|1
        Blocking        Enable/Disable blocking connects        0|1
        on_connect        A subroutine to run after we connect
        on_connect_err        A subroutine to call if we have an error while connecting
        on_error        A subroutine to call when an error occurs on the socket
        on_disconnect        A subroutine to call when the remote end disconnects
        on_timeout        A subroutine to call if our Keepalive times out
        OriginateHack        Changes settings to allow Async Originates to work 0|1

lib/Asterisk/AMI.pm  view on Meta::CPAN

        'PeerAddr' defaults to 127.0.0.1.
        'PeerPort' defaults to 5038.
        'Events' default is 'off'. May be anything that the AMI will accept as a part of the 'Events' parameter for the
        login action.
        'Username' has no default and must be supplied.
        'Secret' has no default and must be supplied.
        'AuthType' sets the authentication type to use for login. Default is 'plaintext'.  Use 'MD5' for MD5 challenge
        authentication.
        'UseSSL' defaults to 0 (no ssl). When SSL is enabled the default PeerPort changes to 5039.
        'BufferSize' has a default of 30000. It also acts as our max actionid before we reset the counter.
        'Timeout' has a default of 0, which means no timeout on blocking.
        'Handlers' accepts a hash reference setting a callback handler for the specified event. EVENT should match
        the contents of the {'Event'} key of the event object will be. The handler should be a subroutine reference that
        will be passed the a copy of the AMI object and the event object. The 'default' keyword can be used to set
        a default event handler. If handlers are installed we do not buffer events and instead immediately dispatch them.
        If no handler is specified for an event type and a 'default' was not set the event is discarded.
        'Keepalive' only works when running with an event loop. Used with on_timeout, this can be used to detect if
        asterisk has become un-responsive.
        'TCP_Keepalive' default is disabled. Activates the tcp keep-alive at the socket layer. This does not require
        an event-loop and is lightweight. Useful for applications that use long-lived connections to Asterisk but
        do not run an event loop.

lib/Asterisk/AMI.pm  view on Meta::CPAN

        $astman->send_action({ Action => 'Ping' }, sub { return }, 7);

        

=head3 Callback Caveats

Callbacks only work if we are processing packets, therefore you must be running an event loop. Alternatively, we run 
mini-event loops for our blocking calls (e.g. action(), get_action()), so in theory if you set callbacks and then 
issue a blocking call those callbacks should also get triggered. However this is an unsupported scenario.

Timeouts are done using timers and they are set as soon as you send the object. Therefore if you send an action with a 
timeout and then monkey around for a long time before getting back to your event loop (to process input) you can time 
out before ever even attempting to receive the response.

        A very contrived example:
        $astman->send_action({ Action => 'Ping' }, \&somemethod, 3);

        sleep(4);

        #Start loop
        $astman->loop;

lib/Asterisk/AMI.pm  view on Meta::CPAN


                my $lkey = lc($key);

                #Callbacks
                if ($key eq 'CALLBACK') {
                        carp "Use of the CALLBACK key in an action is deprecated and will be removed in a future release.\n",
                        "Please use the syntax that is available." if warnings::enabled('Asterisk::AMI');

                        $callback = $actionhash->{$key} unless (defined $callback);
                        next;
                #Timeout
                } elsif ($key eq 'TIMEOUT') {
                        carp "Use of the TIMEOUT key in an action is deprecated and will be removed in a future release\n",
                        "Please use the syntax that is available." if warnings::enabled('Asterisk::AMI');

                        $timeout = $actionhash->{$key} unless (defined $timeout);
                        next;
                #Exception of Orignate Async
                } elsif ($lkey eq 'async' && $value == 1) {
                        $async = 1;
                #Clean out user ActionIDs

lib/Asterisk/AMI.pm  view on Meta::CPAN

        return $md5->hexdigest;
}

#Logs into the AMI
sub _login {
        my ($self) = @_;

        #Auth challenge
        my %challenge;

        #Timeout to use
        my $timeout;
        $timeout = 5 unless ($self->{CONFIG}->{TIMEOUT});
        
        #Build login action
        my %action = (  Action => 'login',
                        Username => $self->{CONFIG}->{USERNAME},
                        Events => $self->{CONFIG}->{EVENTS} );

        #Actions to take for different authtypes
        if (lc($self->{CONFIG}->{AUTHTYPE}) eq 'md5') {

lib/Asterisk/AMI/Common.pm  view on Meta::CPAN

        Returns 1 if the call was parked, or 0 if it failed, or undef on error and timeout.

parked_calls ( [ TIMEOUT ] )

        Returns a hash reference containing parking lots and their members, or undef if an error/timeout or if no calls
        were parked. TIMEOUT is optional.

        Hash reference structure:

        $hashref->{lotnumber}->{'Channel'}
                               {'Timeout'}
                               {'CallerID'}
                               {'CallerIDName'}

sip_peers ( [ TIMEOUT ] )

        Returns a hash reference containing all SIP peers, or undef on error or timeout. TIMEOUT is optional.

        Hash reference structure:

        $hashref->{peername}->{'Channeltype'}

lib/Asterisk/AMI/Common.pm  view on Meta::CPAN

        return;
}

sub park {
        my ($self, $chan1, $chan2, $parktime, $timeout) = @_;

        my %action = (  Action  => 'Park',
                        Channel => $chan1,
                        Channel2 => $chan2 );

        $action{'Timeout'} = $parktime if (defined $parktime);

        return $self->simple_action(\%action, $timeout);
}

sub parked_calls {

        my ($self, $timeout) = @_;

        my $action = $self->action({ Action => 'ParkedCalls' }, $timeout);

lib/Asterisk/AMI/Common.pm  view on Meta::CPAN

                return $action->{'PARSED'}->{'Waiting'};
        }

        return;
}

sub chan_timeout {

        my ($self, $channel, $chantimeout, $timeout) = @_;

        return $self->simple_action({   Action => 'AbsoluteTimeout',
                                        Channel => $channel,
                                        Timeout => $chantimeout }, $timeout);
}

sub queues {
        
        my ($self, $timeout) = @_;

        my $action = $self->action({ Action => 'QueueStatus' }, $timeout);

        return unless ($action->{'GOOD'});

lib/Asterisk/AMI/Common.pm  view on Meta::CPAN

        my %action = (  Action => 'Originate',
                        Channel => $chan,
                        Context => $context,
                        Exten => $exten,
                        Priority => 1,
                        );

        $action{'CallerID'} = $callerid if (defined $callerid);

        if (defined $ctime) {
                $action{'Timeout'} = $ctime * 1000;

                if ($timeout) {
                        $timeout = $ctime + $timeout;
                }
        }

        return $self->simple_action(\%action, $timeout);
}

sub originate_async {

lib/Asterisk/AMI/Common.pm  view on Meta::CPAN


        my %action = (  Action => 'Originate',
                        Channel => $chan,
                        Context => $context,
                        Exten => $exten,
                        Priority => 1,
                        Async => 1
                        );

        $action{'CallerID'} = $callerid if (defined $callerid);
        $action{'Timeout'} = $ctime * 1000 if (defined $ctime);

        my $actionid = $self->send_action(\%action);

        #Bypass async wait, bit hacky
        #allows us to get the intial response
        delete $self->{RESPONSEBUFFER}->{$actionid}->{'ASYNC'};

        return $self->check_response($actionid, $timeout);
}



( run in 0.380 second using v1.01-cache-2.11-cpan-4d50c553e7e )