6 # Copyright (c) Dirk Koopman, G1TLH
24 use vars qw($VERSION $BRANCH $xs $id $max_old_age $max_future_age $dupeage);
25 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
26 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
27 $main::build += $VERSION;
28 $main::branch += $BRANCH;
30 $xs = undef; # the XML::Simple parser instance
31 $id = 0; # the next ID to be used
32 $max_old_age = 3600; # how old a sentence we will accept
33 $max_future_age = 900; # how far into the future we will accept
34 $dupeage = 12*60*60; # duplicates stored half a day
37 # generate a new XML sentence structure
41 my $class = ref $pkg || $pkg;
42 my $self = bless{}, $class;
46 $self->{$key} = $val if defined $val;
52 # note that this a function not a method
56 return unless $main::do_xml;
58 eval { require XML::Simple };
59 eval { require XML::Parser } unless $@;
61 LogDbg('err', "do_xml was set to 1 and the XML routines failed to load ($@)");
64 $XML::Simple::PREFERRED_PARSER = 'XML::Parser';
66 $DXProt::handle_xml = 1;
67 $xs = new XML::Simple(ContentKey=>'content', ForceArray=>1);
73 # note that this a function not a method
80 unless ($main::do_xml) {
81 dbg("xml not enabled, IGNORED") if isdbg('chanerr');
85 my ($rootname) = $line =~ '<(\w+) ';
86 my $pkg = "DXXml::" . ucfirst lc "$rootname";
88 unless (defined *{"${pkg}::"} && $pkg->can('handle_input')) {
89 dbg("xml sentence $rootname not recognised, IGNORED") if isdbg('chanerr');
94 unless ($xref = $pkg->decode_xml($dxchan, $line)) {
95 dbg("invalid XML ($@), IGNORED") if isdbg('chanerr');
100 # do some basic checks
102 unless (exists $xref->{o} && is_callsign($o = $xref->{o})) {
103 dbg("Invalid origin, not a callsign") if isdbg('chanerr');
106 unless (exists $xref->{t} && ($t = IsoTime::unixtime($xref->{t}))) {
107 dbg("Invalid, non-existant or zero time") if isdbg('chanerr');
110 unless ($t > $main::systime - $max_old_age && $t < $main::systime + $max_future_age) {
111 dbg("Too old or too far in the future") if isdbg('chanerr');
114 unless (exists $xref->{id} && ($id = $xref->{id}) >= 0 && $id <= 999) {
115 dbg("Invalid or non-existant id") if isdbg('chanerr');
119 # mark the handle as accepting xml (but only if they
120 # have at least one right)
121 $dxchan->handle_xml(1);
123 # now check that we have not seen this before
124 # this is based on the tuple (o (origin), t (time, normalised to time_t), id)
125 $xref->{'-timet'} = $t;
126 return if DXDupe::check("xml,$o,$t,$id", $dupeage);
128 my $r = bless $xref, $pkg;
129 $r->{'-xml'} = $line;
130 $r->handle_input($dxchan);
134 # note that this a function not a method
143 my @dxchan = DXChannel::get_all();
146 foreach $dxchan (@dxchan) {
147 next unless $dxchan->is_node;
148 next unless $dxchan->handle_xml;
149 next if $dxchan == $main::me;
151 # send a ping out on this channel
152 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
153 if ($dxchan->{nopings} <= 0) {
156 DXXml::Ping::add($main::me, $dxchan->call);
157 $dxchan->{nopings} -= 1;
158 $dxchan->{lastping} = $t;
165 if (!$last10 || $t - $last10 >= 10) {
170 if (!$last_hour || $main::systime - 3600 > $last_hour) {
171 $last_hour = $main::systime;
183 eval {$xref = $xs->XMLin($line)};
190 $id = 0 if $id > 999;
198 unless (exists $self->{'-xml'}) {
199 $self->{o} ||= $main::mycall;
200 $self->{t} ||= IsoTime::hourminsec();
201 $self->{id} ||= nextid();
203 my ($name) = (ref $self) =~ /::(\w+)$/;
204 $self->{'-xml'} = $xs->XMLout($self, RootName =>lc $name, NumericEscape=>1);
206 return $self->{'-xml'};
212 my $fromdxchan = shift;
217 if (my $u = $self->{u}) {
218 $via ||= $u if ($dxchan = DXChannel::get($u));
220 $via ||= $self->{'-via'} || $self->{to};
223 dbg("XML: no route specified (" . $self->toxml . ")") if isdbg('chanerr');
226 if (ref $fromdxchan && $via && $fromdxchan->call eq $via) {
227 dbg("XML: Trying to route back to source (" . $self->toxml . ")") if isdbg('chanerr');
231 # always send it down the local interface if available
232 $dxchan ||= DXChannel::get($via);
234 dbg("route: $via -> $dxchan->{call} direct" ) if isdbg('route');
236 my $cl = Route::get($via);
237 $dxchan = $cl->dxchan if $cl;
238 dbg("route: $via -> $dxchan->{call} using normal route" ) if isdbg('route');
241 # try the backstop method
243 my $rcall = RouteDB::get($via);
245 $dxchan = DXChannel::get($rcall);
246 dbg("route: $via -> $rcall using RouteDB" ) if isdbg('route') && $dxchan;
251 dbg("XML: no route available to $via") if isdbg('chanerr');
255 if ($fromdxchan->call eq $via) {
256 dbg("XML: Trying to route back to source (" . $self->toxml . ")") if isdbg('chanerr');
260 if ($dxchan == $main::me) {
261 dbg("XML: Trying to route to me (" . $self->toxml . ")") if isdbg('chanerr');
265 $self->{o} ||= $main::mycall;
266 $self->{id} ||= nextid();
267 $self->{'-timet'} ||= $main::systime;
269 if ($dxchan->handle_xml) {
270 $dxchan->send($self->toxml);
271 } elsif ($dxchan->is_node) {
272 my $ref = $self->topcxx($dxchan);
275 my $ref = $self->tocmd($dxchan);
282 return exists $_[0]->{'-xml'};
287 return exists $_[0]->{'-pcxx'};
292 return exists $_[0]->{'-cmd'};