SIP Multiplex repairs
authorBill Erickson <berick@esilibrary.com>
Thu, 12 Sep 2013 20:15:54 +0000 (16:15 -0400)
committerBill Erickson <berick@esilibrary.com>
Fri, 8 Aug 2014 14:08:36 +0000 (10:08 -0400)
 * mux_input params / string handling - no reading from $fh
 * until proven otherwise, capture mux $fh for later writing -- may be
   unnecessary.
 * minor thinkos

Signed-off-by: Bill Erickson <berick@esilibrary.com>
SIPServer.pm
Sip.pm
Sip/MsgType.pm

index 1572895..0118cf3 100644 (file)
@@ -29,9 +29,11 @@ use Net::Server::Multiplex;
 use Net::Server::PreFork;
 use Net::Server::Proto;
 use IO::Socket::INET;
+use IO::String;
 use Socket qw(:crlf);
 use Data::Dumper;              # For debugging
 require UNIVERSAL::require;
+use POSIX qw/:sys_wait_h :errno_h/;
 
 #use Sip qw(readline);
 use Sip::Constants qw(:all);
@@ -168,23 +170,30 @@ my $kid_count;
 
 sub REAPER {
   for (keys(%kid_hash)) {
-    if ( my $reaped = waitpid($_, &WNOHANG) > 0 ) {
+    if ( my $reaped = waitpid($_, WNOHANG) > 0 ) {
       # Mourning... done.
       $kid_count--;
       delete $kid_hash{$_};
     }
   }
-  $SIG{CHLD} = &REAPER();
+  $SIG{CHLD} = sub { REAPER() };
 }
 
 my %active_connections;
 sub mux_input {
     my $mself = shift;
     my $mux = shift;
-    my $fh = shift;
+    my $mux_fh = shift;
+    my $str_ref = shift;
+
+    # clone the mux string into a file handle
+    my $str_fh = IO::String->new(''.$$str_ref);
+
+    # clear read data from the mux string ref
+    $$str_ref = '';
 
     my $self;
-    my $conn_id = fileno($fh);
+    my $conn_id = fileno($mux_fh);
 
     # check for kids that went away
     REAPER();
@@ -218,7 +227,7 @@ sub mux_input {
             return;
         }
 
-        &$transport($self, $fh);
+        &$transport($self, $str_fh);
 
         $active_connections{$conn_id} =
             { id         => $conn_id,
@@ -237,6 +246,7 @@ sub mux_input {
     }
 
     $self = $active_connections{$conn_id}->{net_server};
+    $self->{output_fh} = $mux_fh;
 
     my $pid = fork();
     die "Cannot fork: $!" unless (defined($pid) && $pid > -1);
@@ -246,7 +256,8 @@ sub mux_input {
         # build the connection we deleted after logging in
         $self->{ils} = $active_connections{$conn_id}->{ils}->new($self->{institution}, $self->{account});
 
-        my $input = Sip::read_SIP_packet($fh);
+        # build the connection we deleted after logging in
+        my $input = Sip::read_SIP_packet($str_fh);
         $input =~ s/[\r\n]+$//sm;    # Strip off any trailing line ends
 
         my $status = Sip::MsgType::handle($input, $self, '');
@@ -271,7 +282,8 @@ sub mux_input {
 
 sub raw_transport {
     my $self = shift;
-    my $fh ||= *STDIN;
+    my $fh = shift || *STDIN;
+
     my ($uid, $pwd);
     my $input;
     my $service = $self->{service};
@@ -319,12 +331,12 @@ sub raw_transport {
     syslog("LOG_DEBUG", "raw_transport: uname/inst: '%s/%s'",
         $self->{account}->{id},
         $self->{account}->{institution});
-
 }
 
 sub telnet_transport {
     my $self = shift;
-    my $fh ||= *STDIN;
+    my $fh = shift || *STDIN;
+
     my ($uid, $pwd);
     my $strikes = 3;
     my $account = undef;
diff --git a/Sip.pm b/Sip.pm
index a687cd7..8a59c10 100644 (file)
--- a/Sip.pm
+++ b/Sip.pm
@@ -233,6 +233,8 @@ sub read_SIP_packet {
 sub write_msg {
     my ($self, $msg, $file) = @_;
 
+    $file ||= $self->{output_fh};
+
     if ($error_detection) {
         if (defined($self->{seqno})) {
             $msg .= 'AY' . $self->{seqno};
@@ -246,9 +248,9 @@ sub write_msg {
         print $file "$msg\r";
     } else {
         print "$msg\r";
-        syslog("LOG_INFO", "OUTPUT MSG: '$msg'");
     }
 
+    syslog("LOG_INFO", "OUTPUT MSG: '$msg'");
     $last_response = $msg;
 }
 
index 543838e..b744ef6 100644 (file)
@@ -423,6 +423,7 @@ sub handle {
        $req && (substr($msg, 0, 2) ne $req)) {
        return substr($msg, 0, 2);
     }
+    $self->{output_fh} = $server->{output_fh};
     return($self->{handler}->($self, $server));
 }