Created
July 4, 2016 18:59
-
-
Save Prajithp/43b96a3f5e7ddb9444597c3838afb753 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package AMI; | |
use strict; | |
use warnings; | |
use base 'Mojo::Base'; | |
use Data::Dumper; | |
use Mojo::IOLoop; | |
use Scalar::Util (); | |
my $EOL = "\015\012"; | |
my $BLANK = $EOL x 2; | |
my $ACTIONID_SEQ = 1; | |
my $RESULT_SEQ = 1; | |
__PACKAGE__->attr( host => '127.0.0.1:5038' ); | |
__PACKAGE__->attr( user => 'admin' ); | |
__PACKAGE__->attr( secret => 'redhat' ); | |
__PACKAGE__->attr( timeout => '30' ); | |
__PACKAGE__->attr( ioloop => sub { Mojo::IOLoop->singleton } ); | |
__PACKAGE__->attr( error => undef ); | |
__PACKAGE__->attr( | |
on_error => sub { | |
sub { | |
my $self = shift; | |
warn "Asterisk error: ", $self->error, "\n"; | |
} | |
} | |
); | |
sub DESTROY { | |
my $self = shift; | |
# Loop | |
return unless my $loop = $self->ioloop; | |
# Cleanup connection | |
$loop->remove( $self->{_connection} ) | |
if $self->{_connection}; | |
} | |
sub connect { | |
my $self = shift; | |
if ( $self->connected ) { | |
$self->ioloop->remove( $self->{_connection} ); | |
} | |
my $port; | |
my $address; | |
if ( $self->host =~ m{^([^:]+)(:(\d+))?} ) { | |
$address = $1; | |
$port = $3; | |
} | |
Scalar::Util::weaken $self; | |
$self->{_connection} = $self->ioloop->client( | |
{ address => $address, | |
port => $port | |
}, | |
sub { | |
my ( $loop, $err, $stream ) = @_; | |
if ($err) { | |
warn $err; | |
$self->error($err); | |
$self->on_error->($self); | |
return; | |
} | |
$stream->timeout($self->timeout); | |
$stream->on( | |
read => sub { | |
my ( $stream, $chunk ) = @_; | |
print $chunk . "\n"; | |
} | |
); | |
$stream->on( | |
close => sub { | |
my $str = shift; | |
$self->{error} ||= 'disconnected'; | |
warn 'disconnected'; | |
delete $self->{_connection}; | |
} | |
); | |
$stream->on( | |
error => sub { | |
my ( $str, $error ) = @_; | |
$self->error($error); | |
$self->on_error->($self); | |
$self->ioloop->remove( $self->{_connection} ); | |
} | |
); | |
} | |
); | |
return $self; | |
} | |
sub make_packet { | |
my ( %thash ) = @_; | |
my $tstring = ''; | |
if ( $thash{ActionID} ) { # ActionID must be first | |
$tstring .= 'ActionID: ' . $thash{ActionID} . ${EOL}; | |
} | |
foreach my $key ( keys %thash ) { | |
next if $key eq 'ActionID'; | |
$tstring .= $key . ': ' . $thash{$key} . ${EOL}; | |
} | |
return $tstring; | |
} | |
sub login { | |
my $self = shift; | |
my $login_hash = { Action => 'Login', Username => $self->user, Secret => $self->secret }; | |
unless ( $self->{_connection} ) { | |
$self->connect; | |
} | |
my $login = make_packet(%$login_hash); | |
my $id = $self->{_connection}; | |
$self->ioloop->stream($id)->write($login); | |
return $self; | |
} | |
sub start { | |
my ($self) = @_; | |
$self->ioloop->start; | |
return $self; | |
} | |
sub stop { | |
my ($self) = @_; | |
$self->ioloop->stop; | |
return $self; | |
} | |
sub connected { | |
my $self = shift; | |
return $self->{_connection}; | |
} | |
sub disconnect { | |
my $self = shift; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment