decode AGW messages when there 36 bytes available
[spider.git] / perl / AGWMsg.pm
index 854050ae7761ed534784b9882c81bc82d3bec28a..a2a353f42e47c394bc5cb128b6826cbd26dbfe5c 100644 (file)
@@ -51,7 +51,7 @@ sub init
        
        finish();
        dbg('err', "AGW initialising and connecting to $addr/$port ...");
-       $sock = IO::Socket::INET->new(PeerAddr => $addr, PeerPort => $port, Proto=>'tcp', Timeout => 30);
+       $sock = IO::Socket::INET->new(PeerAddr => $addr, PeerPort => $port, Proto=>'tcp', Timeout=>15);
        unless ($sock) {
                dbg('err', "Cannot connect to AGW Engine at $addr/$port $!");
                return;
@@ -76,11 +76,16 @@ sub init
        _sendf('m') if $monitor;
 }
 
+my $finishing = 0;
+
 sub finish
 {
+       return if $finishing;
        if ($sock) {
+               $finishing = 1;
                dbg('err', "AGW ending...");
                for (values %circuit) {
+                       &{$_->{eproc}}() if $_->{eproc};
                        $_->disconnect;
                }
                # say we are going
@@ -178,6 +183,8 @@ sub _rcv {                     # Complement to _send
        if (defined ($bytes_read)) {
                if ($bytes_read > 0) {
                        $inmsg .= $msg;
+#                      $msg =~ s/([\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg; 
+#                      dbg('connll', $msg);
                } 
        } else {
                if (Msg::_err_will_block($!)) {
@@ -191,7 +198,7 @@ FINISH:
     if (defined $bytes_read && $bytes_read == 0) {
                finish();
     } else {
-               _decode() if length $inmsg > 36;
+               _decode() if length $inmsg >= 36;
        }
 }
 
@@ -201,7 +208,7 @@ sub _error
        Msg::set_event_handler($sock, read=>undef, write=>undef, error=>undef);
        $sock = undef;
        for (%circuit) {
-               next unless $_->isa('AGWMsg');
+               &{$_->{eproc}}() if $_->{eproc};
                $_->disconnect;
        }
 }
@@ -315,7 +322,10 @@ sub _decode
        } elsif ($sort eq 'd') {
                dbg('agw', "AGW '$from'->'$to' port: $port Disconnected");
                my $conn = _find($from eq $main::mycall ? $to : $from);
-               $conn->in_disconnect if $conn;
+               if ($conn) {
+                       &{$conn->{eproc}}() if $conn->{eproc};
+                       $conn->in_disconnect;
+               }
        } elsif ($sort eq 'y') {
                my ($frames) = unpack "V", $data;
                dbg('agwpollans', "AGW Frames Outstanding on port $port = $frames");
@@ -350,7 +360,8 @@ sub _decode
                        dbg('agw', "AGW Port: $_");
                }
                for (my $i = 0; $i < $noports; $i++) {
-                       _sendf('y', undef, undef, $i );
+                       _sendf('y', undef, undef, $i);
+                       _sendf('g', undef, undef, $i);
                }
        } else {
                my $d = unpack "Z*", $data;
@@ -387,7 +398,6 @@ sub in_disconnect
 {
        my $conn = shift;
        delete $circuit{$conn->{agwcall}}; 
-       _sendf('d', $conn->{agwcall}, $main::mycall, $conn->{agwport}, $conn->{agwpid});
        $conn->SUPER::disconnect;
 }
 
@@ -410,6 +420,8 @@ sub enqueue
                $msg =~ s/^[-\w]+\|//;
 #              _sendf('Y', $main::mycall, $conn->{call}, $conn->{agwport}, $conn->{agwpid});
                _sendf('D', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid}, $msg . $conn->{lineend});
+               my $len = length($msg) + 1; 
+               dbg('agw', "AGW Data Out port: $conn->{agwport} pid: $conn->{agwpid} '$main::mycall'->'$conn->{agwcall}' length: $len \"$msg\"");
        }
 }