Asterisk-AMI

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN


	- Fixed a bug with older version of ExtUtils::MakeMaker

	- Fix to properly us \v when perl >=5.10, we were only doing it when perl >5.10. This only affects performance.

	- 'Keepalive' was not being set if Blocking was set to 1. Now 'Keepalive' is always set if configured,
	it just may not be helpful if you are not running an event loop.

	- Updated Dependancy to AnyEvent version 5.0 or greater

	- Switch to AE interface for AnyEvent for timers and condvars. Lowers overhead when using alot of timers.

	- Some general code cleanups

0.1.6 	- Fixes calling wrong modules (leftover from name change)

	- Adds backwards compatability for perl 5.8

0.1.5	- Bad test, which broken installing from cpan

0.1.4	- Changed namespace from AMI to Asterisk::AMI

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

                }
        }

        #Initialize the seq number
        $self->{idseq} = 1;

        #Weaken reference for use in anonsub
        weaken($self);

        #Set keepalive
        $self->{CONFIG}->{KEEPALIVE} = AE::timer($self->{CONFIG}->{KEEPALIVE}, $self->{CONFIG}->{KEEPALIVE}, sub { $self->_send_keepalive }) if ($self->{CONFIG}->{KEEPALIVE});
        
        return 1;
}

#Handles connection failures (includes login failure);
sub _on_connect_err {

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

        warnings::warnif('Asterisk::AMI', "Failed to connect to asterisk - $self->{CONFIG}->{PEERADDR}:$self->{CONFIG}->{PEERPORT}");

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

                                my $response = $self->{'RESPONSEBUFFER'}->{$id};
                                delete $self->{RESPONSEBUFFER}->{$id};
                                delete $self->{CALLBACKS}->{$id};
                                delete $self->{EXPECTED}->{$id};
                                $process->send($response);
                        };

                #Make sure event loop is up to date in case of sleeps
                AE::now_update;

                $self->{CALLBACKS}->{$id}->{'timer'} = AE::timer $timeout, 0, $self->{CALLBACKS}->{$id}->{'timeout'};
        }

        return $process->recv;
}

sub _build_action {
        my ($actionhash, $id) = @_;

        my $action;
        my $async;

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

        $timeout = $self->{CONFIG}->{TIMEOUT} unless (defined $timeout);

        #Setup callback
        if (defined $callback) {
                #Set callback if defined
                $self->{CALLBACKS}->{$id}->{'cb'} = $callback;
                #Variable to return with Callback
                $self->{CALLBACKS}->{$id}->{'store'} = $store;
        }

        #Start timer for timeouts
        if ($timeout && defined $self->{CALLBACKS}->{$id}) {
                $self->{CALLBACKS}->{$id}->{'timeout'} = sub {
                                my $response = $self->{RESPONSEBUFFER}->{$id};
                                my $cb = $self->{CALLBACKS}->{$id}->{'cb'};
                                my $st = $self->{CALLBACKS}->{$id}->{'store'};
                                delete $self->{RESPONSEBUFFER}->{$id};
                                delete $self->{CALLBACKS}->{$id};
                                delete $self->{EXPECTED}->{$id};
                                delete $self->{PRELOGIN}->{$id};
                                $cb->($self, $response, $st);;
                        };
                $self->{CALLBACKS}->{$id}->{'timer'} = AE::timer $timeout, 0, $self->{CALLBACKS}->{$id}->{'timeout'};
        }

        return $id;
}

#Checks for a response to an action If no actionid is given uses last actionid sent Returns 1 if action success, 0 if 
#failure
sub check_response {
        my ($self, $actionid, $timeout) = @_;

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

                $self->{CALLBACKS}->{'EVENT'}->{'cb'} = sub { $process->send($_[0]) };
                $self->{CALLBACKS}->{'EVENT'}->{'timeout'} = sub { warnings::warnif('Asterisk::AMI', "Timed out waiting for event"); $process->send(undef); };

                $timeout = $self->{CONFIG}->{TIMEOUT} unless (defined $timeout);

                if ($timeout) {

                        #Make sure event loop is up to date in case of sleeps
                        AE::now_update;

                        $self->{CALLBACKS}->{'EVENT'}->{'timer'} = AE::timer $timeout, 0, $self->{CALLBACKS}->{'EVENT'}->{'timeout'};
                }

                return $process->recv;
        }

        return shift @{$self->{EVENTBUFFER}};
}

#Returns server AMI version
sub amiver {



( run in 0.632 second using v1.01-cache-2.11-cpan-49f99fa48dc )