X-Git-Url: http://gb7djk.dxcluster.net/gitweb/gitweb.cgi?a=blobdiff_plain;f=perl%2FAGWMsg.pm;h=dbb8d8d2dfeeb4410a54aa73d4a42446f5b8de32;hb=a46e7e063c95e0dc3a470f91568cedb58290769c;hp=9a39874cc7d4c6030547b35134861f3dc20623e6;hpb=e9a4c82122720cd66da440e4ff6f88aaded8e146;p=spider.git diff --git a/perl/AGWMsg.pm b/perl/AGWMsg.pm index 9a39874c..dbb8d8d2 100644 --- a/perl/AGWMsg.pm +++ b/perl/AGWMsg.pm @@ -41,19 +41,25 @@ $rproc = undef; $noports = 0; $lastytime = $lasthtime = time; $ypolltime = 10 unless defined $ypolltime; -$hpolltime = 120 unless defined $hpolltime; +$hpolltime = 300 unless defined $hpolltime; %circuit = (); +use vars qw($VERSION $BRANCH); +$VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ ); +$BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0; +$main::build += $VERSION; +$main::branch += $BRANCH; + sub init { return unless $enable; $rproc = shift; finish(); - dbg('err', "AGW initialising and connecting to $addr/$port ..."); - $sock = IO::Socket::INET->new(PeerAddr => $addr, PeerPort => $port, Proto=>'tcp', Timeout => 30); + dbg("AGW initialising and connecting to $addr/$port ..."); + $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 $!"); + dbg("Cannot connect to AGW Engine at $addr/$port $!"); return; } Msg::blocking($sock, 0); @@ -76,11 +82,16 @@ sub init _sendf('m') if $monitor; } +my $finishing = 0; + sub finish { + return if $finishing; if ($sock) { - dbg('err', "AGW ending..."); + $finishing = 1; + dbg("AGW ending..."); for (values %circuit) { + &{$_->{eproc}}() if $_->{eproc}; $_->disconnect; } # say we are going @@ -92,6 +103,11 @@ sub finish } } +sub active +{ + return $sock; +} + sub _sendf { my $sort = shift || confess "need a valid AGW command letter"; @@ -104,15 +120,15 @@ sub _sendf $len = length $data; if ($sort eq 'y' || $sort eq 'H') { - dbg('agwpoll', "AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$data\""); + dbg("AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$data\"") if isdbg('agwpoll'); } elsif ($sort eq 'D') { if (isdbg('agw')) { my $d = $data; $d =~ s/\cM$//; - dbg('agw', "AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$d\""); + dbg("AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$d\"") if isdbg('agw'); } } else { - dbg('agw', "AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$data\""); + dbg("AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$data\"") if isdbg('agw'); } push @outqueue, pack('C x3 a1 x1 C x1 a10 a10 V x4 a*', $port, $sort, $pid, $from, $to, $len, $data); Msg::set_event_handler($sock, write=>\&_send); @@ -152,6 +168,9 @@ sub _send return 0; # fail. Message remains in queue .. } } + if (isdbg('raw')) { + dbgdump('raw', "AGW send $bytes_written: ", $msg); + } $offset += $bytes_written; $bytes_to_write -= $bytes_written; } @@ -178,6 +197,9 @@ sub _rcv { # Complement to _send if (defined ($bytes_read)) { if ($bytes_read > 0) { $inmsg .= $msg; + if (isdbg('raw')) { + dbgdump('raw', "AGW read $bytes_read: ", $msg); + } } } else { if (Msg::_err_will_block($!)) { @@ -191,17 +213,17 @@ FINISH: if (defined $bytes_read && $bytes_read == 0) { finish(); } else { - _decode() if length $inmsg > 36; + _decode() if length $inmsg >= 36; } } sub _error { - dbg('agw', "error on AGW connection $addr/$port $!"); + dbg("error on AGW connection $addr/$port $!"); Msg::set_event_handler($sock, read=>undef, write=>undef, error=>undef); $sock = undef; for (%circuit) { - next unless $_->isa('AGWMsg'); + &{$_->{eproc}}() if $_->{eproc}; $_->disconnect; } } @@ -211,150 +233,160 @@ sub _decode return unless $sock; # we have at least 36 bytes of data (ugh!) - my ($port, $sort, $pid, $from, $to, $len) = unpack('C x3 a1 x1 C x1 Z10 Z10 V x4', $inmsg); - my $data; - - # do a sanity check on the length - if ($len > 2000) { - dbg('err', "AGW: invalid length $len > 2000 received ($sort $port $pid '$from'->'$to')"); - finish(); - return; - } - if ($len == 0){ - if (length $inmsg > 36) { - $inmsg = substr($inmsg, 36); - } else { - $inmsg = ''; + while (length $inmsg >= 36) { + my ($port, $sort, $pid, $from, $to, $len) = unpack('C x3 a1 x1 C x1 Z10 Z10 V x4', $inmsg); + my $data; + + # do a sanity check on the length + if ($len > 2000) { + dbg("AGW: invalid length $len > 2000 received ($sort $port $pid '$from'->'$to')"); + finish(); + return; } - } elsif (length $inmsg > $len + 36) { - $data = substr($inmsg, 36, $len); - $inmsg = substr($inmsg, $len + 36); - } elsif (length $inmsg == $len + 36) { - $data = substr($inmsg, 36); - $inmsg = ''; - } else { - # we don't have enough data or something - # or we have screwed up - return; - } - - $data = '' unless defined $data; - if ($sort eq 'D') { - my $d = unpack "Z*", $data; - $d =~ s/\cM$//; - dbg('agw', "AGW Data In port: $port pid: $pid '$from'->'$to' length: $len \"$d\""); - my $conn = _find($from eq $main::mycall ? $to : $from); - if ($conn) { - if ($conn->{state} eq 'WC') { - if (exists $conn->{cmd}) { - if (@{$conn->{cmd}}) { - dbg('connect', $d); - $conn->_docmd($d); - } - } - if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) { - $conn->to_connected($conn->{call}, 'O', $conn->{csort}); - } + if ($len == 0){ + if (length $inmsg > 36) { + $inmsg = substr($inmsg, 36); } else { - my @lines = split /\cM/, $data; - if (@lines) { - for (@lines) { - &{$conn->{rproc}}($conn, "I$conn->{call}|$_"); - } - } else { - &{$conn->{rproc}}($conn, "I$conn->{call}|"); - } + $inmsg = ''; } + } elsif (length $inmsg > $len + 36) { + $data = substr($inmsg, 36, $len); + $inmsg = substr($inmsg, $len + 36); + } elsif (length $inmsg == $len + 36) { + $data = substr($inmsg, 36); + $inmsg = ''; } else { - dbg('err', "AGW error Unsolicited Data!"); + # + # we don't have enough data or something + # or we have screwed up + # + return; } - } elsif ($sort eq 'I' || $sort eq 'S' || $sort eq 'U' || $sort eq 'M' || $sort eq 'T') { - my $d = unpack "Z*", $data; - $d =~ s/\cM$//; - my @lines = split /\cM/, $d; - - for (@lines) { - s/([\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg; - dbg('agw', "AGW Monitor port: $port \"$_\""); - } - } elsif ($sort eq 'C') { - my $d = unpack "Z*", $data; - $d =~ s/\cM$//; - dbg('agw', "AGW Connect port: $port pid: $pid '$from'->'$to' \"$d\""); - my $call = $from eq $main::mycall ? $to : $from; - my $conn = _find($call); - if ($conn) { - if ($conn->{state} eq 'WC') { - if (exists $conn->{cmd} && @{$conn->{cmd}}) { - $conn->_docmd($d); - if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) { + + $data = '' unless defined $data; + if ($sort eq 'D') { + my $d = unpack "Z*", $data; + $d =~ s/\cM$//; + dbg("AGW Data In port: $port pid: $pid '$from'->'$to' length: $len \"$d\"") if isdbg('agw'); + my $conn = _find($from eq $main::mycall ? $to : $from); + if ($conn) { + if ($conn->{state} eq 'WC') { + if (exists $conn->{cmd}) { + if (@{$conn->{cmd}}) { + dbg($d) if isdbg('connect'); + $conn->_docmd($d); + } + } + if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) { $conn->to_connected($conn->{call}, 'O', $conn->{csort}); } + } else { + my @lines = split /\cM/, $data; + if (@lines) { + for (@lines) { + &{$conn->{rproc}}($conn, "I$conn->{call}|$_"); + } + } else { + &{$conn->{rproc}}($conn, "I$conn->{call}|"); + } } + } else { + dbg("AGW error Unsolicited Data!"); } - } else { - $conn = AGWMsg->new($rproc); - $conn->{agwpid} = $pid; - $conn->{agwport} = $port; - $conn->{lineend} = "\cM"; - $conn->{incoming} = 1; - $conn->{agwcall} = $call; - $circuit{$call} = $conn; - if ($call =~ /^(\w+)-(\d\d?)$/) { - my $c = $1; - my $s = $2; - $s = 15 - $s; - if ($s <= 8 && $s > 0) { - $call = "${c}-${s}"; - } else { - $call = $c; + } elsif ($sort eq 'I' || $sort eq 'S' || $sort eq 'U' || $sort eq 'M' || $sort eq 'T') { + my $d = unpack "Z*", $data; + $d =~ s/\cM$//; + my @lines = split /\cM/, $d; + + for (@lines) { + s/([\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg; + dbg("AGW Monitor port: $port \"$_\"") if isdbg('agw'); + } + } elsif ($sort eq 'C') { + my $d = unpack "Z*", $data; + $d =~ s/\cM$//; + dbg("AGW Connect port: $port pid: $pid '$from'->'$to' \"$d\"") if isdbg('agw'); + my $call = $from eq $main::mycall ? $to : $from; + my $conn = _find($call); + if ($conn) { + if ($conn->{state} eq 'WC') { + if (exists $conn->{cmd} && @{$conn->{cmd}}) { + $conn->_docmd($d); + if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) { + $conn->to_connected($conn->{call}, 'O', $conn->{csort}); + } + } + } + } else { + $conn = AGWMsg->new($rproc); + $conn->{agwpid} = $pid; + $conn->{agwport} = $port; + $conn->{lineend} = "\cM"; + $conn->{incoming} = 1; + $conn->{agwcall} = $call; + $circuit{$call} = $conn; + if ($call =~ /^(\w+)-(\d\d?)$/) { + my $c = $1; + my $s = $2; + $s = 15 - $s; + if ($s <= 8 && $s > 0) { + $call = "${c}-${s}"; + } else { + $call = $c; + } } + $conn->to_connected($call, 'A', $conn->{csort} = 'ax25'); } - $conn->to_connected($call, 'A', $conn->{csort} = 'ax25'); - } - } 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; - } elsif ($sort eq 'y') { - my ($frames) = unpack "V", $data; - dbg('agwpollans', "AGW Frames Outstanding on port $port = $frames"); - my $conn = _find($from); - $conn->{oframes} = $frames if $conn; - } elsif ($sort eq 'Y') { - my ($frames) = unpack "V", $data; - dbg('agw', "AGW Frames Outstanding on circuit '$from'->'$to' = $frames"); - my $conn = _find($from eq $main::mycall ? $to : $from); - $conn->{oframes} = $frames if $conn; - } elsif ($sort eq 'H') { - unless ($from =~ /^\s+$/) { + } elsif ($sort eq 'd') { my $d = unpack "Z*", $data; $d =~ s/\cM$//; - dbg('agw', "AGW Heard port: $port \"$d\""); - } - } elsif ($sort eq 'X') { - my ($r) = unpack "C", $data; - $r = $r ? "Successful" : "Failed"; - dbg('err', "AGW Register $from $r"); - finish() unless $r; - } elsif ($sort eq 'R') { - my ($major, $minor) = unpack "v x2 v x2", $data; - dbg('agw', "AGW Version $major.$minor"); - } elsif ($sort eq 'G') { - my @ports = split /;/, $data; - $noports = shift @ports || '0'; - dbg('agw', "AGW $noports Ports available"); - pop @ports while @ports > $noports; - for (@ports) { - next unless $_; - dbg('agw', "AGW Port: $_"); - } - for (my $i = 0; $i < $noports; $i++) { - _sendf('y', undef, undef, $i ); + dbg("AGW '$from'->'$to' port: $port Disconnected ($d)") if isdbg('agw'); + my $conn = _find($from eq $main::mycall ? $to : $from); + if ($conn) { + &{$conn->{eproc}}() if $conn->{eproc}; + $conn->in_disconnect; + } + } elsif ($sort eq 'y') { + my ($frames) = unpack "V", $data; + dbg("AGW Frames Outstanding on port $port = $frames") if isdbg('agwpollans'); + my $conn = _find($from); + $conn->{oframes} = $frames if $conn; + } elsif ($sort eq 'Y') { + my ($frames) = unpack "V", $data; + dbg("AGW Frames Outstanding on circuit '$from'->'$to' = $frames") if isdbg('agw'); + my $conn = _find($from eq $main::mycall ? $to : $from); + $conn->{oframes} = $frames if $conn; + } elsif ($sort eq 'H') { + unless ($from =~ /^\s+$/) { + my $d = unpack "Z*", $data; + $d =~ s/\cM$//; + dbg("AGW Heard port: $port \"$d\"") if isdbg('agw'); + } + } elsif ($sort eq 'X') { + my ($r) = unpack "C", $data; + $r = $r ? "Successful" : "Failed"; + dbg("AGW Register $from $r"); + finish() unless $r; + } elsif ($sort eq 'R') { + my ($major, $minor) = unpack "v x2 v x2", $data; + dbg("AGW Version $major.$minor") if isdbg('agw'); + } elsif ($sort eq 'G') { + my @ports = split /;/, $data; + $noports = shift @ports || '0'; + dbg("AGW $noports Ports available") if isdbg('agw'); + pop @ports while @ports > $noports; + for (@ports) { + next unless $_; + dbg("AGW Port: $_") if isdbg('agw'); + } + for (my $i = 0; $i < $noports; $i++) { + _sendf('y', undef, undef, $i); + _sendf('g', undef, undef, $i); + } + } else { + my $d = unpack "Z*", $data; + dbg("AGW decode $sort port: $port pid: $pid '$from'->'$to' length: $len \"$d\"") if isdbg('agw'); } - } else { - my $d = unpack "Z*", $data; - dbg('agw', "AGW decode $sort port: $port pid: $pid '$from'->'$to' length: $len \"$d\""); } } @@ -367,7 +399,7 @@ sub _find sub connect { my ($conn, $line) = @_; - + my ($port, $call) = split /\s+/, $line; $conn->{agwpid} = ord "\xF0"; $conn->{agwport} = $port - 1; @@ -387,7 +419,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 +441,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 Data Out port: $conn->{agwport} pid: $conn->{agwpid} '$main::mycall'->'$conn->{agwcall}' length: $len \"$msg\"") if isdbg('agw'); } }