Added: freeswitch/trunk/libs/esl/perl/ESL/Dispatch.pm (0 => 16922)
--- freeswitch/trunk/libs/esl/perl/ESL/Dispatch.pm (rev 0)
+++ freeswitch/trunk/libs/esl/perl/ESL/Dispatch.pm 2010-03-05 23:46:37 UTC (rev 16922)
@@ -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;
Added: freeswitch/trunk/libs/esl/perl/dispatch.pl (0 => 16922)
--- freeswitch/trunk/libs/esl/perl/dispatch.pl (rev 0)
+++ freeswitch/trunk/libs/esl/perl/dispatch.pl 2010-03-05 23:46:37 UTC (rev 16922)
@@ -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;