mirror of
https://github.com/signalwire/freeswitch.git
synced 2025-08-13 09:36:46 +00:00
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:
134
scripts/socket/FreeSWITCH/Client.pm
Normal file
134
scripts/socket/FreeSWITCH/Client.pm
Normal 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;
|
Reference in New Issue
Block a user