make oneself broadcast
[spider.git] / perl / Aranea.pm
index 087eefc3d2f16c639f01384a77107648d95f5771..475fdd05d12f71c1527c9477fb6d2e1393549ad5 100644 (file)
@@ -36,12 +36,14 @@ use vars qw($VERSION $BRANCH);
 
 main::mkver($VERSION = q$Revision$);
 
-use vars qw(@ISA $ntpflag $dupeage);
+use vars qw(@ISA $ntpflag $dupeage $cf_interval $hello_interval);
 
 @ISA = qw(DXChannel);
 
 $ntpflag = 0;                                  # should be set in startup if NTP in use
 $dupeage = 12*60*60;                   # duplicates stored half a day 
+$cf_interval = 30*60;                  # interval between config broadcasts
+$hello_interval = 3*60*60;             # interval between hello broadcasts for me and local users
 
 my $seqno = 0;
 my $dayno = 0;
@@ -59,7 +61,6 @@ sub new
        # add this node to the table, the values get filled in later
        my $pkg = shift;
        my $call = shift;
-       $main::routeroot->add($call, '5251', Route::here(1)) if $call ne $main::mycall;
        $self->{'sort'} = 'W';
        return $self;
 }
@@ -73,7 +74,7 @@ sub start
        # log it
        my $host = $self->{conn}->{peerhost} || "unknown";
        Log('Aranea', "$call connected from $host");
-       
+
        # remember type of connection
        $self->{consort} = $line;
        $self->{outbound} = $sort eq 'O';
@@ -123,8 +124,9 @@ sub start
 
        # broadcast our configuration to the world
        unless ($self->{outbound}) {
-               my $thing = Thingy::Rt->new_lcf;
+               my $thing = Thingy::Rt->new_cf;
                $thing->broadcast;
+               $self->lastcf($main::systime);
        }
        
        # run a script send the output to the debug file
@@ -143,9 +145,11 @@ sub normal
 }
 
 #
-# periodic processing
+# periodic processing (every second)
 #
 
+my $lastmin = time;
+
 sub process
 {
 
@@ -155,6 +159,46 @@ sub process
                $dayno = $d;
                $daystart = $main::systime - ($main::systime % 86400);
        }
+       if ($main::systime >= $lastmin + 60) {
+               per_minute();
+               $lastmin = $main::systime;
+       }
+}
+
+sub per_minute
+{
+       # send hello and cf packages periodically
+       foreach my $dxchan (DXChannel::get_all()) {
+               next if $dxchan->is_aranea;
+               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->lastcf + $cf_interval) {
+                               my $call = $dxchan->call;
+                               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;
+                               }
+                       }
+               }
+       }
 }
 
 sub disconnect
@@ -248,7 +292,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;
@@ -269,7 +313,7 @@ sub tencode
 {
        my $s = shift;
        $s =~ s/([\%=|,\'\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg; 
-       $s = "'$s'" if $s =~ / /;
+#      $s = "'$s'" if $s =~ / /;
        return $s;
 }
 
@@ -278,7 +322,7 @@ sub tdecode
        my $s = shift;
        $s =~ s/^'(.*)'$/$1/;
        $s =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
-       return $s;
+       return length $s ? $s : '';
 }
 
 sub genmsg
@@ -336,6 +380,7 @@ sub input
        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) {