Asterisk-CoroManager
view release on metacpan or search on metacpan
lib/Asterisk/CoroManager.pm view on Meta::CPAN
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' ) {
$actionid = 'Ping';
}
$command->{ActionID} ||= $actionid;
$astman->trace("Sending command: ". Dumper($command));
my $fh = $astman->{fh};
my $cstring = make_packet(%$command);
eval { # Send command to Asterisk
$fh->send("$cstring$EOL");
} or $astman->check_connection;
if (defined wantarray) {
$astman->debug("Waiting for response of command here.");
$astman->trace(longmess());
$astman->trace("-------------------------------------");
$args ||= {};
my $timeout = $args->{Timeout} || DEFAULT_TIMEOUT;
my $response = new Coro::Channel;
$astman->{action_cb}{$actionid} = sub{ $response->put(@_) };
my $resp = $response->get; # Cede's until a response is gotten
return unless( $resp );
return wantarray ? %{$resp} : $resp;
# TODO: Timeout!
}
return;
}
##############################################################################
=head2 check_connection
$astman->check_connection();
Checks if the connection is still alive. Tries to reconnect if it
isn't.
=cut
sub check_connection {
my( $astman ) = @_;
if( $astman->connected ) {
$astman->debug("...connection appears to be fine...");
}
else {
$astman->error("Lost connection to server!");
$astman->error("Trying to reconnect...");
$astman->{fh}->close if $astman->{fh}->connected;
undef $astman->{fh};
if( $astman->connect ) {
$astman->error("Succeeded in reconnect! "
."Continuing as if nothing happened.");
}
else {
$astman->fatal("Couldn't reconnect. "
."Dying here. Good bye cruel world.");
croak "Couldn't reconnect. Dying here. "
."Good bye cruel world.";
}
}
return;
}
##############################################################################
=head2 eventloop
$astman->eventloop();
Will wait for events, until shut down.
=cut
sub eventloop {
my ($astman) = @_;
# Coro debug shell, if socket is available
lib/Asterisk/CoroManager.pm view on Meta::CPAN
##############################################################################
=head2 host
$astman->host('localhost')
Set host for manager connection.
=cut
sub host {
my ($astman, $host) = @_;
if ($host) {
$astman->{host} = $host;
}
return $astman->{host};
}
##############################################################################
=head2 port
$astman->port(5038)
Set port for manager connection; defaults to 5038.
=cut
sub port {
my ($astman, $port) = @_;
if ($port) {
$astman->{port} = $port;
}
return $astman->{port};
}
##############################################################################
=head2 connected
croak "Not connected!" unless($astman->connected($timeout))
Checks if manager is connected (for timeout seconds).
Returns 1 if conencted, 0 if not.
=cut
sub connected {
my ($astman, $timeout) = @_;
return ($astman->{fh}->connected and
$astman->sendcommand({ Action => 'Ping' },
{ Timeout => $timeout || DEFAULT_TIMEOUT })
);
}
##############################################################################
##############################################################################
=head2 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.
=cut
sub add_event_callback {
my ($astman, $event, $function) = @_;
if (defined($function) && ref($function) eq 'CODE') {
$astman->{event_cb}{$event} = []
unless $astman->{event_cb}{$event};
push @{$astman->{event_cb}{$event}}, $function;
}
else
{
$astman->error("add_event_callback called without CODE ref.");
return 0;
}
return 1;
}
##############################################################################
=head2 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.
=cut
sub add_default_event_callback {
my ($astman, $function) = @_;
if (defined($function) && ref($function) eq 'CODE') {
$astman->{event_dcb} = []
unless $astman->{event_dcb};
push @{$astman->{event_dcb}}, $function;
}
else
{
$astman->error("add_default_event_callback called without "
."CODE ref."
);
( run in 1.634 second using v1.01-cache-2.11-cpan-98e64b0badf )