Asterisk-CoroManager

 view release on metacpan or  search on metacpan

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

package Asterisk::CoroManager;

require 5.8.8;

use strict;
use warnings;
use warnings::register;
use utf8;

use Coro;
use Coro::AnyEvent;                # NB: I have only tested with Coro::EV
use Coro::Semaphore;
use Coro::Channel;
use Coro::Debug;

use Carp qw( croak longmess );
use IO::Socket;
use Digest::MD5;
use Data::Dumper::Simple;
use Time::HiRes qw ( time );

use constant DEFAULT_TIMEOUT => 3; # Default timeout to wait for
                                   # action response

=head1 NAME

Asterisk::CoroManager - Asterisk Manager Interface, Coro version

=head1 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;

=head1 DESCRIPTION

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

This is done with L<Coro>, and continuations.  If you are unfamiliar
with L<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.

=head2 Logging / Error handling

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

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

=cut

BEGIN {
    # Check for Log::Log4perl...
    eval { use Log::Log4perl qw(get_logger :nowarn); };
    if($@) {
	print "Log::Log4perl not installed - stubbing.\n";
	no strict qw(refs);
	*{__PACKAGE__."::$_"} = sub { } for qw(trace debug error fatal);
    }
    else {
	print "Log::Log4perl installed - logging enabled.\n";
	no strict qw(refs);
	foreach my $level (qw(trace debug error fatal)) {
	    *{__PACKAGE__."::$level"} =
	      sub {
		  $Log::Log4perl::caller_depth++;
		  my( $astman, $message ) = @_;
		  my $log = Log::Log4perl->get_logger();
		  $log->$level($astman->host .': '. $message);
		  $Log::Log4perl::caller_depth--;
		  return;
	      };
	}
    }
}


my $EOL = "\015\012";
my $BLANK = $EOL x 2;

use vars qw($VERSION); $VERSION = '0.11'; sub version { return $VERSION }

my $ACTIONID_SEQ = 1;
my $RESULT_SEQ = 1;



##############################################################################
##############################################################################

=head1 Constructor

=head2 new

  my $astman = new Asterisk::CoroManager({
                                          host   => 'localhost',
                                          user   => 'username',



( run in 0.873 second using v1.01-cache-2.11-cpan-5735350b133 )