From 868530fc05a0c33fc02fe86f7ac5d46c646e4362 Mon Sep 17 00:00:00 2001 From: Mathieu Parent Date: Sat, 27 Mar 2010 22:43:18 +0000 Subject: [PATCH] Skinny: split the test into perl lib + bin git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@17124 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/mod/endpoints/mod_skinny/Net/Skinny.pm | 68 ++++++++++ .../mod_skinny/Net/Skinny/Message.pm | 10 ++ .../mod_skinny/Net/Skinny/Protocol.pm | 64 ++++++++++ src/mod/endpoints/mod_skinny/test-skinny.pl | 116 +++++------------- 4 files changed, 176 insertions(+), 82 deletions(-) create mode 100644 src/mod/endpoints/mod_skinny/Net/Skinny.pm create mode 100644 src/mod/endpoints/mod_skinny/Net/Skinny/Message.pm create mode 100644 src/mod/endpoints/mod_skinny/Net/Skinny/Protocol.pm diff --git a/src/mod/endpoints/mod_skinny/Net/Skinny.pm b/src/mod/endpoints/mod_skinny/Net/Skinny.pm new file mode 100644 index 0000000000..93b0f3056d --- /dev/null +++ b/src/mod/endpoints/mod_skinny/Net/Skinny.pm @@ -0,0 +1,68 @@ +# Copyright (c) 2010 Mathieu Parent . +# All rights reserved. This program is free software; you can redistribute it +# and/or modify it under the same terms as Perl itself. + +package Net::Skinny; + +use strict; +use warnings; +use IO::Socket; +use Net::Skinny::Protocol qw/:all/; + +our(@ISA); +@ISA = qw(IO::Socket::INET); + +sub new { + shift->SUPER::new(PeerPort => 2000, @_); +} + +sub send_message +{ + my $self = shift; + my $type = shift; + my $data = shift; + my $len = length($data)+4; + printf "Sending message (length=%d, type=%s (%X))", $len, Net::Skinny::Protocol::skinny_message_type2str($type), $type; + $self->send( + pack("VVV", $len, 0, $type). + $data); + printf ".\n"; +} + +sub receive_message +{ + my $self = shift; + my $buf; + $self->recv($buf, 4); + my $len = unpack("V", $buf); + printf "Receiving message (length=%d,", $len; + if($len < 4) { + printf "type=?).\n"; + printf "Problem! Length is < 4.\n"; + exit 1; + } + $self->recv($buf, 4); #reserved + $self->recv($buf, 4); #type + my $type = unpack("V", $buf); + printf "type=%s (%X))", Net::Skinny::Protocol::skinny_message_type2str($type), $type; + if($len > 4) { + $self->recv($buf, $len-4); + } + printf ".\n"; +} + +sub sleep +{ + my $self = shift; + my $t = shift; + + printf "Sleeping %d seconds", $t; + while(--$t){ + sleep(1); + printf "." if $t % 10; + printf "_" unless $t % 10; + } + printf ".\n"; +} + +1; diff --git a/src/mod/endpoints/mod_skinny/Net/Skinny/Message.pm b/src/mod/endpoints/mod_skinny/Net/Skinny/Message.pm new file mode 100644 index 0000000000..e16e39aeb8 --- /dev/null +++ b/src/mod/endpoints/mod_skinny/Net/Skinny/Message.pm @@ -0,0 +1,10 @@ +# Copyright (c) 2010 Mathieu Parent . +# All rights reserved. This program is free software; you can redistribute it +# and/or modify it under the same terms as Perl itself. + +package Net::Skinny::Message; + +use strict; +use warnings; + + diff --git a/src/mod/endpoints/mod_skinny/Net/Skinny/Protocol.pm b/src/mod/endpoints/mod_skinny/Net/Skinny/Protocol.pm new file mode 100644 index 0000000000..a8b502e816 --- /dev/null +++ b/src/mod/endpoints/mod_skinny/Net/Skinny/Protocol.pm @@ -0,0 +1,64 @@ +# Copyright (c) 2010 Mathieu Parent . +# All rights reserved. This program is free software; you can redistribute it +# and/or modify it under the same terms as Perl itself. + +package Net::Skinny::Protocol; + +use strict; +no strict "refs"; +use warnings; +use Carp; + +require Exporter; +our @ISA = qw(Exporter); +our @EXPORT = qw(skinny_message_type2str); + +my %const; +my %sub; +my $skinny_protocol_h = 'src/mod/endpoints/mod_skinny/skinny_protocol.h'; + +sub import { + shift; + my $callpkg = caller(0); + _find(@_); + my $oops; + my $all = grep /:all/, @_; + foreach my $sym ($all ? keys %sub : @_) { + if (my $sub = $sub{$sym}) { + *{$callpkg . "::$sym"} = $sub; + } + else { + ++$oops; + carp(qq["$sym" is not exported by the Net::Skinny::Protocol module]); + } + } + croak("Can't continue after import errors") if $oops; +} + +sub _find { + my $fh; + unless (open($fh, $skinny_protocol_h)) { + print STDERR "Can't open $skinny_protocol_h: $!\n"; + return; + } + while(<$fh>) { + if( /^#define\s+([\w_]+)\s+(0x[0-9a-f]+)\s*$/i) { + my ($name, $value) = ($1,hex($2)); + $sub{$name} = sub () { $value }; + $const{$name} = $value; + } + } + @sub{@_}; +} + +sub skinny_message_type2str { + my $message_type = shift; + + keys %const; + while (my ($key, $value) = each %const) { + if($value == $message_type) { + return $key; + } + } + return "UnknownMessage"; +} diff --git a/src/mod/endpoints/mod_skinny/test-skinny.pl b/src/mod/endpoints/mod_skinny/test-skinny.pl index deb60f410c..3cf207e1dc 100644 --- a/src/mod/endpoints/mod_skinny/test-skinny.pl +++ b/src/mod/endpoints/mod_skinny/test-skinny.pl @@ -1,8 +1,19 @@ #!/usr/bin/perl +# Copyright (c) 2010 Mathieu Parent . +# All rights reserved. This program is free software; you can redistribute it +# and/or modify it under the same terms as Perl itself. + +BEGIN { + push @INC, 'src/mod/endpoints/mod_skinny'; +} + use strict; use warnings; -use IO::Socket; + +use Data::Dumper; +use Net::Skinny; +use Net::Skinny::Protocol qw/:all/; #Config my $skinny_server = '127.0.0.1'; @@ -11,69 +22,17 @@ my $device_ip = 10+256*(11+256*(12+256*13)); # 10.11.12.13 #====== $| = 1; -my $socket; - -sub skinny_connect -{ - $socket = IO::Socket::INET->new( +my $socket = Net::Skinny->new( PeerAddr => $skinny_server, PeerPort => 2000, ); -} -sub skinny_send -{ - my $type = shift; - my $data = shift; - my $len = length($data)+4; - printf "Sending message (length=%d, type=%X)", $len, $type; - $socket->send( - pack("VVV", $len, 0, $type). - $data); - printf ".\n"; +if(!$socket) { + print "Unable to connect to server\n"; + exit 1; } - -sub skinny_recv -{ - my $buf; - $socket->recv($buf, 4); - my $len = unpack("V", $buf); - printf "Receiving message (length=%d,", $len; - if($len < 4) { - printf "type=?).\n"; - printf "Problem! Length is < 4.\n"; - exit 1; - } - $socket->recv($buf, 4); #reserved - $socket->recv($buf, 4); #type - my $type = unpack("V", $buf); - printf "type=%X)", $type; - if($len > 4) { - $socket->recv($buf, $len-4); - } - printf ".\n"; -} - -sub skinny_sleep -{ - my $t = shift; - - printf "Sleeping %d seconds", $t; - while(--$t){ - sleep(1); - printf "." if $t % 10; - printf "_" unless $t % 10; - } - printf ".\n"; -} - # ============================================================================= -# -# ============================================================================= -skinny_connect(); - -# ============================================================================= -skinny_send(0x0001, # Register +$socket->send_message(REGISTER_MESSAGE, # Register pack("a16VVVVV", $device_name, 0, # userId; @@ -82,48 +41,41 @@ skinny_send(0x0001, # Register 7, # deviceType; 0, # maxStreams; )); -skinny_recv(); # RegisterAck +$socket->receive_message(); # RegisterAck -skinny_send(0x0002, # Port +$socket->send_message(0x0002, # Port pack("n", 2000 )); -skinny_send(0x002b, # HeadSetStatus +$socket->send_message(HEADSET_STATUS_MESSAGE, pack("V", 2, # Off )); -skinny_recv(); # CapabilitiesReq -skinny_send(0x0010, # CapabilitiesRes +$socket->receive_message(); # CapabilitiesReq +$socket->send_message(CAPABILITIES_RES_MESSAGE, pack("V"."Vva10"."Vva10", 2, # count 2, 8, "", # codec, frames, res 4, 16, "", # codec, frames, res )); -skinny_send(0x000e, # ButtonTemplateReqMessage - ""); -skinny_recv(); # ButtonTemplateMessage +$socket->send_message(BUTTON_TEMPLATE_REQ_MESSAGE, ""); +$socket->receive_message(); # ButtonTemplateMessage -skinny_send(0x0028, # SoftKeyTemplateReq - ""); -skinny_recv(); # SoftKeyTemplateRes +$socket->send_message(SOFT_KEY_TEMPLATE_REQ_MESSAGE, ""); +$socket->receive_message(); # SoftKeyTemplateRes -skinny_send(0x0025, # SoftKeySetReq - ""); -skinny_recv(); # SoftKeySetRes +$socket->send_message(SOFT_KEY_SET_REQ_MESSAGE, ""); +$socket->receive_message(); # SoftKeySetRes -skinny_send(0x000B, # LineStatReq - pack("V", 1)); -skinny_recv(); # LineStat +$socket->send_message(LINE_STAT_REQ_MESSAGE, pack("V", 1)); +$socket->receive_message(); # LineStat -skinny_send(0x002D, # RegisterAvailableLines - pack("V", 2 - )); +$socket->send_message(REGISTER_AVAILABLE_LINES_MESSAGE, pack("V", 2)); while(1) { - skinny_sleep(20); - skinny_send(0x0000, # keepalive - ""); - skinny_recv(); # keepaliveack + $socket->sleep(20); + $socket->send_message(KEEP_ALIVE_MESSAGE, ""); + $socket->receive_message(); # keepaliveack }