make just the time the default.
[spider.git] / perl / DXXml.pm
index 1bea12c784d38d9bda02e6cbfe1074aebb1a0312..9d0a59698170c153a5db6ec4ab77ac124012c878 100644 (file)
@@ -14,10 +14,12 @@ use IsoTime;
 use DXProt;
 use DXDebug;
 use DXLog;
+use DXUtil;
 use DXXml::Ping;
 use DXXml::Dx;
+use DXXml::IM;
 
-use vars qw($VERSION $BRANCH $xs $id);
+use vars qw($VERSION $BRANCH $xs $id $max_old_age $max_future_age $dupeage);
 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/  || (0,0));
 $main::build += $VERSION;
@@ -25,6 +27,10 @@ $main::branch += $BRANCH;
 
 $xs = undef;                                   # the XML::Simple parser instance
 $id = 0;                                               # the next ID to be used
+$max_old_age = 3600;                   # how old a sentence we will accept
+$max_future_age = 900;                 # how far into the future we will accept
+$dupeage = 12*60*60;                   # duplicates stored half a day 
+
 
 # generate a new XML sentence structure 
 sub new
@@ -41,11 +47,16 @@ sub init
 {
        return unless $main::do_xml;
        
-       eval { require XML::Simple; };
-       unless ($@) {
+       eval { require XML::Simple };
+       eval { require XML::Parser } unless $@;
+       if ($@) {
+               LogDbg('err', "do_xml was set to 1 and the XML routines failed to load ($@)");
+               $main::do_xml = 0;
+       } else {
+               $XML::Simple::PREFERRED_PARSER = 'XML::Parser';
                import XML::Simple;
                $DXProt::handle_xml = 1;
-               $xs = new XML::Simple();
+               $xs = new XML::Simple(Cache=>[]);
        }
        undef $@;
 }
@@ -77,11 +88,35 @@ sub normal
                undef $@;
                return;
        }
-       
+
+       # do some basic checks
+       my ($o, $t, $id);
+       unless (exists $xref->{o} && is_callsign($o = $xref->{o})) {
+               dbg("Invalid origin, not a callsign") if isdbg('chanerr');
+               return;
+       }
+       unless (exists $xref->{t} && ($t = IsoTime::unixtime($xref->{t}))) {
+               dbg("Invalid, non-existant or zero time") if isdbg('chanerr');
+               return;
+       }
+       unless ($t > $main::systime - $max_old_age && $t < $main::systime + $max_future_age) {
+               dbg("Too old or too far in the future") if isdbg('chanerr');
+               return;
+       }
+       unless (exists $xref->{id} && ($id = $xref->{id}) >= 0 && $id <= 999) {
+               dbg("Invalid or non-existant id") if isdbg('chanerr');
+               return;
+       }
+
        # mark the handle as accepting xml (but only if they 
        # have at least one right)
        $dxchan->handle_xml(1);
 
+       # now check that we have not seen this before 
+       # this is based on the tuple (o (origin), t (time, normalised to time_t), id)
+       $xref->{'-timet'} = $t;
+       return if DXDupe::check("xml,$o,$t,$id", $dupeage);
+               
        $xref = bless $xref, $pkg;
        $xref->{'-xml'} = $line; 
        $xref->handle_input($dxchan);
@@ -90,8 +125,44 @@ sub normal
 #
 # note that this a function not a method
 #
+
+my $last10;
+my $last_hour;
+
 sub process
 {
+       my $t = time;
+       my @dxchan = DXChannel::get_all();
+       my $dxchan;
+
+       foreach $dxchan (@dxchan) {
+               next unless $dxchan->is_node;
+               next unless $dxchan->handle_xml;
+               next if $dxchan == $main::me;
+
+               # send a ping out on this channel
+               if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
+                       if ($dxchan->{nopings} <= 0) {
+                               $dxchan->disconnect;
+                       } else {
+                               DXXml::Ping::add($main::me, $dxchan->call);
+                               $dxchan->{nopings} -= 1;
+                               $dxchan->{lastping} = $t;
+                               $dxchan->{lastping} += $dxchan->{pingint} / 2 unless @{$dxchan->{pingtime}};
+                       }
+               }
+       }
+
+
+       # every ten seconds
+       if (!$last10 || $t - $last10 >= 10) {   
+               $last10 = $t;
+       }
+
+       # every hour
+       if (!$last_hour || $main::systime - 3600 > $last_hour) {
+               $last_hour = $main::systime;
+       }
 
 }
 
@@ -119,10 +190,10 @@ sub toxml
 
        unless (exists $self->{'-xml'}) {
                $self->{o} ||= $main::mycall;
-               $self->{t} ||= IsoTime::dayms();
+               $self->{t} ||= IsoTime::hourminsec();
                $self->{id} ||= nextid();
                
-               my ($name) = ref $self =~ /::(\w+)$/;
+               my ($name) = (ref $self) =~ /::(\w+)$/;
                $self->{'-xml'} = $xs->XMLout($self, RootName =>lc $name, NumericEscape=>1);
        }
        return $self->{'-xml'};