Add mod_event_socket remote client module and sample client.

To Test:

uncomment or add from modules.conf
make installall again to compile it
uncomment the load line from freeswitch.xml

the default values are to bind to 127.0.0.1 port 8021

telnet to port 8021
enter "auth ClueCon" to authenticate

from here you can do the following:
*) events [xml|plain] <list of events to log or all for all>
*) noevents 
*) log <level> // same as the console.conf values
*) nolog
*) api <command> <arg>
*) exit

there is a perl client in scripts/socket called fs.pl

with the module up and loaded:
cd scripts/socket
perl fs.pl <optional log level>

you can enter a few api commands like "show or status"




git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@2047 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Anthony Minessale
2006-07-22 21:49:52 +00:00
parent 7a5bc3b711
commit 9c79c2a3fb
9 changed files with 1130 additions and 10 deletions

View File

@@ -0,0 +1,134 @@
package FreeSWITCH::Client;
$|=1;
use IO::Socket::INET;
use IO::Select;
use Data::Dumper;
sub init($;$) {
my $proto = shift;
my $args = shift;
my $class = ref($proto) || $proto;
$self->{_host} = $args->{-host} || "localhost";
$self->{_port} = $args->{-port} || 8021;
$self->{_password} = $args->{-password} || undef;
my $me = bless $self,$class;
if ($me->connect()) {
return $me;
} else {
return undef;
}
}
sub input($;$) {
my ($self,$to) = @_;
my $i;
my @r;
my $s = $self->{_sock};
my $x = 0;
my $done = 0;
my $start = time;
while(!$done) {
if ($to and time - $start > $to) {
last;
}
@ready = $self->{_sel}->can_read($to);
if (@ready) {
$x=0;
foreach my $s (@ready) {
while ($i = <$s>) {
$x++;
return @r if($i eq "\n");
$i =~ s/[\n]+$//g;
push @r,$i;
}
unless($x) {
return ("SocketError: yes");
}
}
}
}
return @r;
}
sub readhash($$) {
my $self = shift;
my $arg = shift;
my @r = $self->input($arg);
my $data = join "\n", @r;
my %h = $data =~ /^([^:]+)\s*:\s*([^\n]*)/mg;
foreach (keys %h) {
my $new = lc $_;
$h{$new} = $h{$_};
delete $h{$_};
}
if ($h{'content-length'}) {
my $s = $self->{_sock};
read $s, $h{body}, $h{'content-length'};
}
return \%h;
}
sub error($$) {
my($self,$error) = @_;
die $error;
}
sub output($$) {
my ($self,$data) = @_;
my $s = $self->{_sock};
print $s $data;
}
sub cmd($$$) {
my $self = shift;
my $cmd = shift;
my $to = shift;
$self->output($cmd);
my $h = $self->readhash($to);
$h;
}
sub connect($) {
my $self = shift;
$self->{_sock} = new IO::Socket::INET( Proto => 'tcp',
PeerAddr => $self->{_host},
PeerPort => $self->{_port}
) or return $self->error("Connection refused $self->{_host} port $self->{_port}");
$self->{_sock}->autoflush(1);
#$self->{_sock}->blocking(0);
$self->{_sel} = new IO::Select( $self->{_sock} );
my $h = $self->readhash(undef);
if ($h->{"content-type"} eq "auth/request") {
my $pass = $self->{"_password"};
$self->output("auth $pass");
$h = $self->readhash(undef);
}
if ($h->{'reply-text'} =~ "OK") {
return 1;
}
return 0;
}
1;

55
scripts/socket/fs.pl Normal file
View File

@@ -0,0 +1,55 @@
use FreeSWITCH::Client;
use Data::Dumper;
use Term::ReadLine;
my $password = "ClueCon";
my $fs = init FreeSWITCH::Client {-password => $password} or die "Error $@";
my $term = new Term::ReadLine ?FreeSWITCH CLI?;
my $prompt = "FS>";
my $OUT = $term->OUT .. \*STDOUT;
my $log = shift;
if ($log) {
$pid = fork;
if (!$pid) {
my $fs2 = init FreeSWITCH::Client {-password => $password} or die "Error $@";
$fs2->cmd("log $log");
while (1) {
my $reply = $fs2->readhash(undef);
if ($reply->{body}) {
print $reply->{body} . "\n";
} elsif ($reply->{'reply-text'}) {
print $reply->{'reply-text'} . "\n";
}
}
}
}
while ( defined ($_ = $term->readline($prompt)) ) {
my $reply;
if ($_) {
if ($_ =~ /^alog|^anolog/) {
$reply = $fs2->cmd($_);
} else {
$reply = $fs->cmd("api $_");
}
if ($reply->{body}) {
print $reply->{body};
} elsif ($reply->{'reply-text'}) {
print $reply->{'reply-text'};
}
print "\n";
if ($_ =~ /exit/) {
last;
}
}
$term->addhistory($_) if /\S/;
}