6 # Copyright (c) Dirk Koopman, G1TLH
22 use vars qw($VERSION $BRANCH $xs $id $max_old_age $max_future_age $dupeage);
23 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
24 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
25 $main::build += $VERSION;
26 $main::branch += $BRANCH;
28 $xs = undef; # the XML::Simple parser instance
29 $id = 0; # the next ID to be used
30 $max_old_age = 3600; # how old a sentence we will accept
31 $max_future_age = 900; # how far into the future we will accept
32 $dupeage = 12*60*60; # duplicates stored half a day
35 # generate a new XML sentence structure
39 my $class = ref $pkg || $pkg;
40 return bless{@_}, $class;
44 # note that this a function not a method
48 return unless $main::do_xml;
50 eval { require XML::Simple };
51 eval { require XML::Parser } unless $@;
53 LogDbg('err', "do_xml was set to 1 and the XML routines failed to load ($@)");
56 $XML::Simple::PREFERRED_PARSER = 'XML::Parser';
58 $DXProt::handle_xml = 1;
59 $xs = new XML::Simple(Cache=>[]);
65 # note that this a function not a method
72 unless ($main::do_xml) {
73 dbg("xml not enabled, IGNORED") if isdbg('chanerr');
77 my ($rootname) = $line =~ '<(\w+) ';
78 my $pkg = "DXXml::" . ucfirst lc "$rootname";
80 unless (defined *{"${pkg}::"} && $pkg->can('handle_input')) {
81 dbg("xml sentence $rootname not recognised, IGNORED") if isdbg('chanerr');
86 unless ($xref = $pkg->decode_xml($dxchan, $line)) {
87 dbg("invalid XML ($@), IGNORED") if isdbg('chanerr');
92 # do some basic checks
94 unless (exists $xref->{o} && is_callsign($o = $xref->{o})) {
95 dbg("Invalid origin, not a callsign") if isdbg('chanerr');
98 unless (exists $xref->{t} && ($t = IsoTime::unixtime($xref->{t}))) {
99 dbg("Invalid, non-existant or zero time") if isdbg('chanerr');
102 unless ($t > $main::systime - $max_old_age && $t < $main::systime + $max_future_age) {
103 dbg("Too old or too far in the future") if isdbg('chanerr');
106 unless (exists $xref->{id} && ($id = $xref->{id}) >= 0 && $id <= 999) {
107 dbg("Invalid or non-existant id") if isdbg('chanerr');
111 # mark the handle as accepting xml (but only if they
112 # have at least one right)
113 $dxchan->handle_xml(1);
115 # now check that we have not seen this before
116 # this is based on the tuple (o (origin), t (time, normalised to time_t), id)
117 $xref->{'-timet'} = $t;
118 return if DXDupe::check("xml,$o,$t,$id", $dupeage);
120 $xref = bless $xref, $pkg;
121 $xref->{'-xml'} = $line;
122 $xref->handle_input($dxchan);
126 # note that this a function not a method
135 my @dxchan = DXChannel::get_all();
138 foreach $dxchan (@dxchan) {
139 next unless $dxchan->is_node;
140 next unless $dxchan->handle_xml;
141 next if $dxchan == $main::me;
143 # send a ping out on this channel
144 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
145 if ($dxchan->{nopings} <= 0) {
148 DXXml::Ping::add($main::me, $dxchan->call);
149 $dxchan->{nopings} -= 1;
150 $dxchan->{lastping} = $t;
151 $dxchan->{lastping} += $dxchan->{pingint} / 2 unless @{$dxchan->{pingtime}};
158 if (!$last10 || $t - $last10 >= 10) {
163 if (!$last_hour || $main::systime - 3600 > $last_hour) {
164 $last_hour = $main::systime;
176 eval {$xref = $xs->XMLin($line)};
183 $id = 0 if $id > 999;
191 unless (exists $self->{'-xml'}) {
192 $self->{o} ||= $main::mycall;
193 $self->{t} ||= IsoTime::dayminsec();
194 $self->{id} ||= nextid();
196 my ($name) = (ref $self) =~ /::(\w+)$/;
197 $self->{'-xml'} = $xs->XMLout($self, RootName =>lc $name, NumericEscape=>1);
199 return $self->{'-xml'};
205 my $fromdxchan = shift;
207 my $via = $to || $self->{'-via'} || $self->{to};
210 dbg("XML: no route specified (" . $self->toxml . ")") if isdbg('chanerr');
213 if (ref $fromdxchan && $via && $fromdxchan->call eq $via) {
214 dbg("XML: Trying to route back to source (" . $self->toxml . ")") if isdbg('chanerr');
218 # always send it down the local interface if available
219 my $dxchan = DXChannel::get($via);
221 dbg("route: $via -> $dxchan->{call} direct" ) if isdbg('route');
223 my $cl = Route::get($via);
224 $dxchan = $cl->dxchan if $cl;
225 dbg("route: $via -> $dxchan->{call} using normal route" ) if isdbg('route');
228 # try the backstop method
230 my $rcall = RouteDB::get($via);
232 $dxchan = DXChannel::get($rcall);
233 dbg("route: $via -> $rcall using RouteDB" ) if isdbg('route') && $dxchan;
238 dbg("XML: no route available to $via") if isdbg('chanerr');
242 if ($fromdxchan->call eq $via) {
243 dbg("XML: Trying to route back to source (" . $self->toxml . ")") if isdbg('chanerr');
247 if ($dxchan == $main::me) {
248 dbg("XML: Trying to route to me (" . $self->toxml . ")") if isdbg('chanerr');
252 if ($dxchan->handle_xml) {
253 $dxchan->send($self->toxml);
255 $self->{o} ||= $main::mycall;
256 $self->{id} ||= nextid();
257 $self->{'-timet'} ||= $main::systime;
258 $dxchan->send($self->topcxx);
264 return exists $_[0]->{'-xml'};
269 return exists $_[0]->{'-pcxx'};
274 return exists $_[0]->{'-cmd'};