make sure touser is passed on
[spider.git] / perl / Aranea.pm
index 3a2190e7389ccabfe697172d615245594a4db180..7765af34fead427e39b4e7a12b2c95324994147d 100644 (file)
@@ -126,7 +126,7 @@ sub start
        unless ($self->{outbound}) {
                my $thing = Thingy::Rt->new_cf;
                $thing->broadcast;
-               $self->lastcf($main::systime);
+               $main::me->lastcf($main::systime);
        }
        
        # run a script send the output to the debug file
@@ -148,7 +148,7 @@ sub normal
 # periodic processing (every second)
 #
 
-my $lastmin = 0;
+my $lastmin = time;
 
 sub process
 {
@@ -160,10 +160,8 @@ sub process
                $daystart = $main::systime - ($main::systime % 86400);
        }
        if ($main::systime >= $lastmin + 60) {
-               if ($lastmin) {
-                       per_minute();
-                       $lastmin = $main::systime;
-               }
+               per_minute();
+               $lastmin = $main::systime;
        }
 }
 
@@ -171,24 +169,42 @@ sub per_minute
 {
        # send hello and cf packages periodically
        foreach my $dxchan (DXChannel::get_all()) {
-               next if $dxchan == $main::me;
                next if $dxchan->is_aranea;
-               if ($main::systime > $dxchan->lasthello + $hello_interval) {
-                       my $thing = Thingy::Hello->new(user => $dxchan->call, h => $dxchan->here);
+               if ($main::systime >= $dxchan->lasthello + $hello_interval) {
+                       my $thing = Thingy::Hello->new(h => $dxchan->here);
+                       $thing->{user} = $dxchan->{call} unless $dxchan == $main::me;
+                       if (my $v = $dxchan->{version}) {
+                               if ($dxchan->is_spider) {
+                                       $thing->{sw} = 'DXSp';
+                               }
+                               $thing->{v} = $v;
+                       }
+                       $thing->{b} = $dxchan->{build} if $dxchan->{build};
                        $thing->broadcast($dxchan);
                        $dxchan->lasthello($main::systime);
                }
                if ($dxchan->is_node) {
-                       if ($main::systime > $dxchan->lasthello + $hello_interval) {
+                       if ($main::systime >= $dxchan->lastcf + $cf_interval) {
                                my $call = $dxchan->call;
-                               my $thing = Thingy::Rt->new(user => $call);
-                               if (my $nref = Route::Node::get($call)) {
-                                       $thing->copy_pc16_data($nref);
-                                       $thing->broadcast($dxchan);
+                               if ($dxchan == $main::me) {
+
+                                       # i am special but, currently, still a node
+                                       my $thing = Thingy::Rt->new_cf;
+                                       $thing->broadcast;
                                        $dxchan->lastcf($main::systime);
                                } else {
-                                       dbg("Aranea::per_minute: Route::Node for $call disappeared");
-                                       $dxchan->disconnect;
+
+                                       # i am a pc protocol node connected directly
+                                       my $thing = Thingy::Rt->new();
+                                       $thing->{user} = $call unless $dxchan == $main::me;
+                                       if (my $nref = Route::Node::get($call)) {
+                                               $thing->copy_pc16_data($nref);
+                                               $thing->broadcast($dxchan);
+                                               $dxchan->lastcf($main::systime);
+                                       } else {
+                                               dbg("Aranea::per_minute: Route::Node for $call disappeared");
+                                               $dxchan->disconnect;
+                                       }
                                }
                        }
                }
@@ -231,7 +247,7 @@ sub disconnect
        DXProt::route_pc21($self, $main::mycall, undef, @rout) if @rout;
 
        # remove outstanding pings
-#      delete $pings{$call};
+       Thingy::Ping::forget($call);
        
        # I was the last node visited
     $self->user->node($main::mycall);
@@ -256,12 +272,14 @@ sub formathead
        my $hop = shift;
        my $user = shift;
        my $group = shift;
+       my $touser = shift;
        
        my $s = "$mycall,$dts,$hop";
        $s .= ",$user" if $user;
        if ($group) {
                $s .= "," unless $user;
                $s .= ",$group" if $group;
+               $s .= ",$touser" if $touser;
        } 
        return $s;
 }
@@ -271,9 +289,10 @@ sub genheader
        my $mycall = shift;
        my $to = shift;
        my $from = shift;
+       my $touser = shift;
        
        my $date = ((($dayno << 1) | $ntpflag) << 18) |  ($main::systime % 86400);
-       my $r = formathead($mycall, sprintf('%6X%04X', $date, $seqno), 0, $from, $to);
+       my $r = formathead($mycall, sprintf('%6X%04X', $date, $seqno), 0, $from, $to, $touser);
        $seqno++;
        $seqno = 0 if $seqno > 0x0ffff;
        return $r;
@@ -286,7 +305,7 @@ sub genheader
 sub decode_dts
 {
        my $dts = shift;
-       my ($dt, $seqno) = map {hex} unpack "H6H4", $dts;
+       my ($dt, $seqno) = map {hex} unpack "A6 A4", $dts;
        my $secs = $dt & 0x3FFFF;
        $dt >>= 18;
        my $day = $dt >> 1;
@@ -326,9 +345,11 @@ sub genmsg
        my ($name) = uc ref $thing;
        $name =~ /::(\w+)$/;
        $name = $1;
+       my $group = $thing->{group};
        my $head = genheader($thing->{origin}, 
-                                                ($thing->{group} || $thing->{touser} || $thing->{tonode}),
-                                                ($thing->{user} || $thing->{fromuser} || $thing->{fromnode})
+                                                $group,
+                                                ($thing->{user} || $thing->{fromuser} || $thing->{fromnode}),
+                                                $thing->{touser}
                                                );
         
        my $data = uc $name . ',';
@@ -355,7 +376,7 @@ sub input
        my ($head, $data) = split /\|/, $line, 2;
        return unless $head && $data;
 
-       my ($origin, $dts, $hop, $user, $group) = split /,/, $head;
+       my ($origin, $dts, $hop, $user, $group, $tocall) = split /,/, $head;
        return if DXDupe::check("Ara,$origin,$dts", $dupeage);
        my $err;
        $err .= "incomplete header," unless $origin && $dts && defined $hop;
@@ -364,16 +385,16 @@ sub input
        # validate it further
        $err .= "missing cmd or data," unless $cmd && $data;
        $err .= "invalid command ($cmd)," unless $cmd =~ /^[A-Z][A-Z0-9]*$/;
-       my ($gp, $tus) = split /:/, $group, 2 if $group;
 
        $err .= "from me," if $origin eq $main::mycall;
-       $err .= "invalid group ($gp)," if $gp && $gp !~ /^[A-Z0-9]{2,}$/;
-       $err .= "invalid tocall ($tus)," if $tus && !is_callsign($tus);
+       $err .= "invalid group ($group)," if $group && $group !~ /^[-A-Z0-9]{2,}$/;
+       $err .= "invalid tocall ($tocall)," if $tocall && !is_callsign($tocall);
        $err .= "invalid fromcall ($user)," if $user && !is_callsign($user);
 
        my $class = 'Thingy::' . ucfirst(lc $cmd);
        my $thing;
        my ($t, $seqno, $ntp) = decode_dts($dts) unless $err;
+       dbg("dts: $dts = $ntp $t($main::systime) $seqno") if isdbg('dts');
        $err .= "invalid date/seq," unless $t;
        
        if ($err) {
@@ -384,14 +405,14 @@ sub input
                $thing = $class->new();
 
                # reconstitute the header but wth hop increased by one
-               $head = formathead($origin, $dts, ++$hop, $user, $group);
+               $head = formathead($origin, $dts, ++$hop, $user, $group, $tocall);
                $thing->{Aranea} = "$head|$data";
 
                # store useful data
                $thing->{origin} = $origin;
                $thing->{time} = $t;
-               $thing->{group} = $gp if $gp;
-               $thing->{touser} = $tus if $tus;
+               $thing->{group} = $group if $group;
+               $thing->{touser} = $tocall if $tocall;
                $thing->{user} = $user if $user;
                $thing->{hopsaway} = $hop;