ESL::Dispatch example for Jaybinks to work on monitoring.
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@16922 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
parent
1baa8d5eee
commit
f32d1b1534
|
@ -0,0 +1,97 @@
|
|||
package ESL::Dispatch;
|
||||
use Data::Dumper;
|
||||
require ESL;
|
||||
require Exporter;
|
||||
use AutoLoader ();
|
||||
|
||||
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $AUTOLOAD);
|
||||
|
||||
$VERSION = "1.0";
|
||||
|
||||
@ISA = qw(Exporter DynaLoader);
|
||||
|
||||
|
||||
sub init($;$) {
|
||||
my $proto = shift;
|
||||
my $args = shift;
|
||||
my $class = ref($proto) || $proto;
|
||||
my $self = {};
|
||||
|
||||
$self->{_debug} = $args->{debug} ||= 0;
|
||||
|
||||
$self->{host} = $args->{host} ||= "localhost";
|
||||
$self->{port} = $args->{port} ||= "8021";
|
||||
$self->{pass} = $args->{pass} ||= "ClueCon";
|
||||
$self->{_esl} = new ESL::ESLconnection("$self->{host}", "$self->{port}", "$self->{pass}");
|
||||
$self->{_callback} = undef;
|
||||
|
||||
return bless($self, $class);
|
||||
}
|
||||
|
||||
sub set_worker($;$$) {
|
||||
my $self = shift;
|
||||
$self->{_worker} = shift;
|
||||
$self->{_timeout} = shift;
|
||||
}
|
||||
|
||||
sub set_callback($;$$) {
|
||||
my $self = shift;
|
||||
my $event = shift;
|
||||
$self->{_callback}->{$event} = shift;
|
||||
}
|
||||
|
||||
sub render_event($;$$) {
|
||||
my $self = shift;
|
||||
my $event = shift;
|
||||
my $h = $event->firstHeader();
|
||||
|
||||
while ($h) {
|
||||
$val = $event->getHeader($h);
|
||||
if($self->{_debug} > 3) {
|
||||
print "$h -> $val\n";
|
||||
}
|
||||
if ($self->{_decode}) {
|
||||
$val =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;
|
||||
}
|
||||
$self->{event_hash}->{lc($h)} = $val;
|
||||
$h = $event->nextHeader();
|
||||
}
|
||||
# Execute callback for this event
|
||||
eval {
|
||||
if($self->{_debug}) {
|
||||
$callback = lc($self->{event_hash}->{'event-name'});
|
||||
print "DEBUG: executing $callback callback\n";
|
||||
}
|
||||
&{$self->{_callback}->{lc($self->{event_hash}->{'event-name'})}}($self);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
sub run($;) {
|
||||
my $self = shift;
|
||||
my $event;
|
||||
|
||||
for(;;) {
|
||||
# Only register for events we have callbacks for.
|
||||
for my $key ( keys %{$self->{_callback}} ) {
|
||||
$self->{_esl}->events("plain", "$key");
|
||||
}
|
||||
|
||||
while ($self->{_esl}->connected()) {
|
||||
if($self->{_timeout} > 0) {
|
||||
$event = $self->{_esl}->recvEventTimed($self->{_timeout});
|
||||
if(!$event) {
|
||||
eval {&{$self->{_worker}}($self);};
|
||||
next;
|
||||
}
|
||||
} else {
|
||||
$event = $self->{_esl}->recvEvent();
|
||||
}
|
||||
$self->render_event($event,1);
|
||||
}
|
||||
sleep 1;
|
||||
$self->{_esl} = new ESL::ESLconnection("$self->{host}", "$self->{port}", "$self->{pass}");
|
||||
}
|
||||
}
|
||||
|
||||
1;
|
|
@ -0,0 +1,27 @@
|
|||
#!/usr/bin/perl
|
||||
|
||||
use ESL::Dispatch;
|
||||
use Data::Dumper;
|
||||
my $daemon = init ESL::Dispatch({});
|
||||
|
||||
$| = 1;
|
||||
|
||||
sub worker {
|
||||
my $self = shift;
|
||||
print "I'm a worker\n";
|
||||
}
|
||||
|
||||
sub heartbeat {
|
||||
my $self = shift;
|
||||
my $event = $self->{event_hash};
|
||||
print Dumper $event;
|
||||
}
|
||||
|
||||
|
||||
|
||||
$daemon->set_worker(\&worker, 2000);
|
||||
$daemon->set_callback("heartbeat", \&heartbeat);
|
||||
$daemon->set_callback("channel_hangup", \&heartbeat);
|
||||
|
||||
|
||||
$daemon->run;
|
Loading…
Reference in New Issue