<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][17262] </title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=17262">17262</a></dd>
<dt>Author</dt> <dd>math.parent</dd>
<dt>Date</dt> <dd>2010-04-09 07:40:33 -0500 (Fri, 09 Apr 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>perl-skinny: enhance test
- Use thread for: keepalive, receive and send
- Run indefinitevly</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcmodendpointsmod_skinnyNetSkinnyMessagepm">freeswitch/trunk/src/mod/endpoints/mod_skinny/Net/Skinny/Message.pm</a></li>
<li><a href="#freeswitchtrunksrcmodendpointsmod_skinnyNetSkinnyProtocolpm">freeswitch/trunk/src/mod/endpoints/mod_skinny/Net/Skinny/Protocol.pm</a></li>
<li><a href="#freeswitchtrunksrcmodendpointsmod_skinnyNetSkinnypm">freeswitch/trunk/src/mod/endpoints/mod_skinny/Net/Skinny.pm</a></li>
<li><a href="#freeswitchtrunksrcmodendpointsmod_skinnytestskinnypl">freeswitch/trunk/src/mod/endpoints/mod_skinny/test-skinny.pl</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcmodendpointsmod_skinnyNetSkinnyClientpm">freeswitch/trunk/src/mod/endpoints/mod_skinny/Net/Skinny/Client.pm</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunksrcmodendpointsmod_skinnyNetSkinnyClientpm"></a>
<div class="addfile"><h4>Added: freeswitch/trunk/src/mod/endpoints/mod_skinny/Net/Skinny/Client.pm (0 => 17262)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_skinny/Net/Skinny/Client.pm         (rev 0)
+++ freeswitch/trunk/src/mod/endpoints/mod_skinny/Net/Skinny/Client.pm        2010-04-09 12:40:33 UTC (rev 17262)
</span><span class="lines">@@ -0,0 +1,94 @@
</span><ins>+# Copyright (c) 2010 Mathieu Parent <math.parent@gmail.com>.
+# 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::Client;
+
+use strict;
+use warnings;
+
+use Config;
+use threads;
+use threads::shared;
+use Thread::Queue;
+
+require Net::Skinny;
+use Net::Skinny::Protocol qw/:all/;
+use Net::Skinny::Message;
+
+our(@ISA);
+@ISA = qw(Net::Skinny);
+
+my $keep_alive_thread;
+my $keep_alives :shared;
+our $kept_self;
+my $messages_send_queue;
+my $messages_receive_queue;
+
+$Config{useithreads} or die('Recompile Perl with threads to run this program.');
+
+sub new {
+ $kept_self = shift->SUPER::new(@_);
+        $messages_send_queue = Thread::Queue->new();
+        $messages_receive_queue = Thread::Queue->new();
+ threads->create(\&send_messages_thread_func);
+ threads->create(\&receive_messages_thread_func);
+ return $kept_self;
+}
+
+sub send_message {
+        my $self = shift;
+        $messages_send_queue->enqueue(\@_);
+}
+
+sub receive_message {
+        my $self = shift;
+        my $message = $messages_receive_queue->dequeue();
+        if($message->type() == 0x100) {#keepaliveack
+                if(1) {
+                        lock($keep_alives);
+                        $keep_alives--;
+                }
+                $message = $messages_receive_queue->dequeue();
+        }
+        return $message;
+}
+
+sub launch_keep_alive_thread
+{
+        if(!$keep_alive_thread) {
+                $keep_alive_thread = threads->create(\&keep_alive_thread_func);
+        } else {
+                print "keep-alive thread is already running\n";
+        }
+        return $keep_alive_thread;
+}
+
+sub keep_alive_thread_func
+{
+        while($kept_self) {
+                if(1) {
+                        lock($keep_alives);
+                        $keep_alives++;
+                        $kept_self->send_message(KEEP_ALIVE_MESSAGE);
+                } #mutex unlocked
+                $kept_self->sleep(30, quiet => 0);
+        }
+}
+
+sub send_messages_thread_func
+{
+        while(my $message = $messages_send_queue->dequeue()) {
+                my $type = shift @$message;
+                $kept_self->SUPER::send_message($type, @$message);
+        }
+}
+
+sub receive_messages_thread_func
+{
+        while(1) {
+         $messages_receive_queue->enqueue($kept_self->SUPER::receive_message());
+        }
+}
+
+1;
</ins></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_skinnyNetSkinnyMessagepm"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_skinny/Net/Skinny/Message.pm (17261 => 17262)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_skinny/Net/Skinny/Message.pm        2010-04-09 09:13:22 UTC (rev 17261)
+++ freeswitch/trunk/src/mod/endpoints/mod_skinny/Net/Skinny/Message.pm        2010-04-09 12:40:33 UTC (rev 17262)
</span><span class="lines">@@ -7,54 +7,96 @@
</span><span class="cx"> use strict;
</span><span class="cx"> use warnings;
</span><span class="cx">
</span><ins>+use threads;
+use threads::shared;
+
</ins><span class="cx"> use Net::Skinny::Protocol qw/:all/;
</span><span class="cx">
</span><del>-use Data::Dumper;
-
-require Exporter;
-our @ISA = qw(Exporter);
-our @EXPORT = qw(send);
-
-sub new {
</del><ins>+sub new_empty {
</ins><span class="cx"> my $class = shift;
</span><span class="cx"> my $self = {};
</span><span class="cx"> bless $self, $class;
</span><del>- $self->{'socket'} = shift;
- $self->{'type'} = shift;
- %{$self->{'data'}} = @_;
- return $ self;
</del><ins>+ $self->{'type'} = undef;
+ $self->{'data'} = undef;
+ $self->{'raw'} = undef;
+ return $self;        
</ins><span class="cx"> }
</span><span class="cx">
</span><del>-sub send {
</del><ins>+sub new {
+        my $self = shift->new_empty();
+        $self->type(shift);
+        $self->data(@_) if @_;
+ return $self;
+}
+
+sub new_raw {
+        my $self = shift->new_empty();
+        $self->type(shift);
+        $self->raw(shift);
+ return $self;
+}
+
+sub type
+{
</ins><span class="cx"> my $self = shift;
</span><del>- my $struct = Net::Skinny::Protocol::skinny_message_struct($self->{'type'});
- my $raw = '';
- my $parsed_count = 0;
- for my $info ( @$struct) {
- last if !defined($self->{'data'}{@$info[1]});
- if(@$info[0] eq 'char') {
- $raw .= pack("a".@$info[2], $self->{'data'}{@$info[1]});
- } elsif(@$info[0] eq 'uint32_t') {
- $raw .= pack("V".@$info[2], $self->{'data'}{@$info[1]});
- } elsif(@$info[0] eq 'uint16_t') {
- $raw .= pack("n".@$info[2], $self->{'data'}{@$info[1]});
- } elsif(@$info[0] eq 'struct in_addr') {
- $raw .= pack("V".@$info[2], $self->{'data'}{@$info[1]});
- } elsif(@$info[0] eq 'struct station_capabilities') {
- $raw .= $self->{'data'}{@$info[1]};
- } else {
- printf "Unknown type: %s\n", @$info[0];
- return;
- }
- $parsed_count++;
</del><ins>+ my $type = @_ ? shift : undef;
+ if(defined($type)) {
+         $self->{'type'} = $type;
</ins><span class="cx"> }
</span><del>- if($parsed_count != scalar(keys %{$self->{'data'}})) {
-         printf "Incomplete message (type=%s (%X)) %d out of %d\n", Net::Skinny::Protocol::skinny_message_type2str($self->{'type'}), $self->{'type'},
- $parsed_count, scalar(keys %{$self->{'data'}});
- print Dumper(@$struct);
- return;
</del><ins>+ return $self->{'type'};
+}
+
+sub data
+{
+ my $self = shift;
+ my @data = @_;
+ if(@data) {
+         %{$self->{'data'}} = @data;
+         $self->{'raw'} = undef;
+ } elsif(!defined($self->{'data'})) {
+         printf "Conversion from raw to data not implemented\n";
</ins><span class="cx"> }
</span><del>- $self->{'socket'}->send_data($self->{'type'}, $raw);
</del><ins>+ return $self->{'data'};
</ins><span class="cx"> }
</span><span class="cx">
</span><ins>+sub raw
+{
+ my $self = shift;
+ my $raw = shift || undef;
+ if(defined($raw)) {
+         $self->{'raw'} = $raw;
+         $self->{'data'} = undef;
+ }
+ if(!defined($self->{'raw'})) {
+                my $struct = Net::Skinny::Protocol::skinny_message_struct($self->{'type'});
+                my $raw = '';
+                my $parsed_count = 0;
+                for my $info ( @$struct) {
+                 last if !defined($self->{'data'}{@$info[1]});
+                 if(@$info[0] eq 'char') {
+                 $raw .= pack("a".@$info[2], $self->{'data'}{@$info[1]});
+                 } elsif(@$info[0] eq 'uint32_t') {
+                 $raw .= pack("V".@$info[2], $self->{'data'}{@$info[1]});
+                 } elsif(@$info[0] eq 'uint16_t') {
+                 $raw .= pack("n".@$info[2], $self->{'data'}{@$info[1]});
+                 } elsif(@$info[0] eq 'struct in_addr') {
+                 $raw .= pack("V".@$info[2], $self->{'data'}{@$info[1]});
+                 } elsif(@$info[0] eq 'struct station_capabilities') {
+                 $raw .= $self->{'data'}{@$info[1]};
+                 } else {
+                 printf "Unknown type: %s\n", @$info[0];
+                 return;
+                 }
+                 $parsed_count++;
+                }
+                if($parsed_count != scalar(keys %{$self->{'data'}})) {
+                        printf "Incomplete message (type=%s (%X)) %d out of %d\n", Net::Skinny::Protocol::skinny_message_type2str($self->{'type'}), $self->{'type'},
+                 $parsed_count, scalar(keys %{$self->{'data'}});
+                 return;
+                }
+                $self->{'raw'} = $raw;
+ }
+ return $self->{'raw'};
+}
+
</ins><span class="cx"> 1;
</span></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_skinnyNetSkinnyProtocolpm"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_skinny/Net/Skinny/Protocol.pm (17261 => 17262)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_skinny/Net/Skinny/Protocol.pm        2010-04-09 09:13:22 UTC (rev 17261)
+++ freeswitch/trunk/src/mod/endpoints/mod_skinny/Net/Skinny/Protocol.pm        2010-04-09 12:40:33 UTC (rev 17262)
</span><span class="lines">@@ -8,7 +8,6 @@
</span><span class="cx"> no strict "refs";
</span><span class="cx"> use warnings;
</span><span class="cx"> use Carp;
</span><del>-use Data::Dumper;
</del><span class="cx">
</span><span class="cx"> require Exporter;
</span><span class="cx"> our @ISA = qw(Exporter);
</span><span class="lines">@@ -69,7 +68,6 @@
</span><span class="cx"> printf "Unparsed line '%s' in %s\n", $_, $struct_name;
</span><span class="cx"> }
</span><span class="cx"> }
</span><del>- #print "$name: ".Dumper($struct{$name});
</del><span class="cx"> }
</span><span class="cx"> }
</span><span class="cx"> @sub{@_};
</span><span class="lines">@@ -77,6 +75,7 @@
</span><span class="cx">
</span><span class="cx"> sub skinny_message_type2str {
</span><span class="cx"> my $message_type = shift;
</span><ins>+ return "UndefinedTypeMessage" if !defined($message_type);
</ins><span class="cx">
</span><span class="cx"> keys %const;
</span><span class="cx"> while (my ($key, $value) = each %const) {
</span></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_skinnyNetSkinnypm"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_skinny/Net/Skinny.pm (17261 => 17262)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_skinny/Net/Skinny.pm        2010-04-09 09:13:22 UTC (rev 17261)
+++ freeswitch/trunk/src/mod/endpoints/mod_skinny/Net/Skinny.pm        2010-04-09 12:40:33 UTC (rev 17262)
</span><span class="lines">@@ -6,26 +6,25 @@
</span><span class="cx">
</span><span class="cx"> use strict;
</span><span class="cx"> use warnings;
</span><del>-use IO::Socket;
</del><ins>+
+require IO::Socket;
+
</ins><span class="cx"> use Net::Skinny::Protocol qw/:all/;
</span><span class="cx">
</span><del>-our(@ISA);
-@ISA = qw(IO::Socket::INET);
</del><ins>+our @ISA = qw(IO::Socket::INET);
</ins><span class="cx">
</span><span class="cx"> sub new {
</span><span class="cx"> shift->SUPER::new(PeerPort => 2000, @_);
</span><span class="cx"> }
</span><span class="cx">
</span><del>-sub send_data
</del><ins>+sub send_raw
</ins><span class="cx"> {
</span><span class="cx"> my $self = shift;
</span><span class="cx">         my $type = shift;
</span><del>-        my $data = shift;
-        my $len = length($data)+4;
</del><ins>+        my $raw = shift;
+        my $len = length($raw)+4;
</ins><span class="cx">         printf "Sending message (length=%d, type=%s (%X))", $len, Net::Skinny::Protocol::skinny_message_type2str($type), $type;
</span><del>-        $self->send(
-                pack("VVV", $len, 0, $type).
-                $data);
</del><ins>+        $self->send(pack("VVV", $len, 0, $type).$raw);
</ins><span class="cx">         printf ".\n";
</span><span class="cx"> }
</span><span class="cx">
</span><span class="lines">@@ -33,11 +32,8 @@
</span><span class="cx"> {
</span><span class="cx"> my $self = shift;
</span><span class="cx"> my $type = shift;
</span><del>- return Net::Skinny::Message->new(
- $self,
- $type,
- @_
- )->send();
</del><ins>+ my $message = Net::Skinny::Message->new($type, @_);
+ return $self->send_raw($message->type(), $message->raw());
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> sub receive_message
</span><span class="lines">@@ -58,20 +54,26 @@
</span><span class="cx">         printf "type=%s (%X))", Net::Skinny::Protocol::skinny_message_type2str($type), $type;
</span><span class="cx">         if($len > 4) {
</span><span class="cx">                 $self->recv($buf, $len-4);
</span><ins>+        } else {
+                $buf = '';
</ins><span class="cx">         }
</span><span class="cx">         printf ".\n";
</span><ins>+        return Net::Skinny::Message->new_raw($type, $buf);
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> sub sleep
</span><span class="cx"> {
</span><span class="cx"> my $self = shift;
</span><span class="cx">         my $t = shift;
</span><del>-        
</del><ins>+        my %args = @_;
+        $args{'quiet'} = 0 if not $args{'quiet'};
</ins><span class="cx">         printf "Sleeping %d seconds", $t;
</span><span class="cx">         while(--$t){
</span><span class="cx">                 sleep(1);
</span><del>-                printf "." if $t % 10;
-                printf "_" unless $t % 10;
</del><ins>+                if(!$args{'quiet'}) {
+                        printf "." if $t % 10;
+                        printf "_" unless $t % 10;
+                }
</ins><span class="cx">         }
</span><span class="cx">         printf ".\n";
</span><span class="cx"> }
</span></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_skinnytestskinnypl"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_skinny/test-skinny.pl (17261 => 17262)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_skinny/test-skinny.pl        2010-04-09 09:13:22 UTC (rev 17261)
+++ freeswitch/trunk/src/mod/endpoints/mod_skinny/test-skinny.pl        2010-04-09 12:40:33 UTC (rev 17262)
</span><span class="lines">@@ -15,6 +15,7 @@
</span><span class="cx"> use Net::Skinny;
</span><span class="cx"> use Net::Skinny::Protocol qw/:all/;
</span><span class="cx"> use Net::Skinny::Message;
</span><ins>+use Net::Skinny::Client;
</ins><span class="cx">
</span><span class="cx"> #Config
</span><span class="cx"> my $skinny_server = hostname;
</span><span class="lines">@@ -23,13 +24,13 @@
</span><span class="cx"> #======
</span><span class="cx"> $| = 1;
</span><span class="cx">
</span><del>-my $socket = Net::Skinny->new(
</del><ins>+my $socket = Net::Skinny::Client->new(
</ins><span class="cx">                 PeerAddr => $skinny_server,
</span><span class="cx">                 PeerPort => 2000,
</span><span class="cx">                 );
</span><span class="cx">
</span><span class="cx"> if(!$socket) {
</span><del>- print "Unable to connect to server\n";
</del><ins>+ printf "Unable to connect to server %s\n", $skinny_server;
</ins><span class="cx"> exit 1;
</span><span class="cx"> }
</span><span class="cx"> # =============================================================================
</span><span class="lines">@@ -84,11 +85,8 @@
</span><span class="cx"> count => 2
</span><span class="cx"> );
</span><span class="cx">
</span><del>-for(my $i = 0; $i < 1; $i++) {
-        $socket->sleep(5);
-        $socket->send_message(KEEP_ALIVE_MESSAGE);
-        $socket->receive_message(); # keepaliveack
-}
</del><ins>+$socket->launch_keep_alive_thread();
+
</ins><span class="cx"> $socket->sleep(5);
</span><span class="cx">
</span><span class="cx"> #NewCall
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>
</body>
</html>