Asterisk-CoroManager
view release on metacpan or search on metacpan
lib/Asterisk/CoroManager.pm view on Meta::CPAN
return $astman->error("Unknown Protocol\n");
}
$astman->{ami_version} = $version;
# Prepare normal recieving watcher-event
$astman->{watcher}
= AnyEvent->io( fh => $fh,
poll => 'r',
cb => sub { $astman->read_incoming( $fh ) },
);
$astman->{finished} = AnyEvent->condvar;
# Check if the remote host supports MD5 Challenge authentication
my $authresp = $astman->sendcommand({
Action => 'Challenge',
AuthType => 'MD5',
});
if ($authresp->{Response} eq 'Success') {
# Do md5 login
my $md5 = new Digest::MD5;
$md5->add($authresp->{Challenge});
$md5->add($secret);
my $digest = $md5->hexdigest;
my $resp = $astman->sendcommand({
Action => 'Login',
AuthType => 'MD5',
Username => $user,
Key => $digest,
});
$astman->debug("Login(MD5) response: ". Dumper($resp));
unless( $resp->{Response} eq 'Success' ) {
$astman->fatal("Login failed.");
return;
}
}
else {
# Do plain text login
my $resp = $astman->sendcommand({
Action => 'Login',
Username => $user,
Secret => $secret
});
$astman->debug("Login(plain) response: ". Dumper($resp));
unless( $resp->{Response} eq 'Success' ) {
$astman->fatal("Login failed.");
return;
}
}
# Add watcher for restart, to reconnect after 3 seconds
$astman->add_event_callback('Shutdown',
sub{ sleep 3;
$astman->{fh}->close;
$astman->check_connection;
}
);
# Check connection every 5th second..
$astman->{clock_check}
= AnyEvent->timer( after => 5,
interval => 5,
cb => sub {
async{ $astman->check_connection }
},
);
return $version;
}
##############################################################################
=head2 disconnect
$astman->disconnect;
Disconnects from asterisk manager interface and ends eventloop.
=cut
sub disconnect
{
my ($astman) = @_;
$astman->{finished}->send;
return;
}
##############################################################################
=head2 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.
=cut
sub sendcommand {
my( $astman, $command, $args ) = @_;
my $actionid = $command->{ActionID} || $ACTIONID_SEQ++;
# Ping must be handled specially, since it doesn't return ActionID
if( $command->{Action} and
$command->{Action} eq 'Ping' ) {
( run in 3.007 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )