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 ($@)");
62 $main::do_xml = $xs = 0;
64 $XML::Simple::PREFERRED_PARSER = 'XML::Parser';
66 $xs = new XML::Simple(ContentKey=>'content', ForceArray=>1);
71 # is XML avaiable and active
74 return $main::do_xml && $xs;
78 # note that this a function not a method
85 unless (available()) {
86 dbg("xml not enabled, IGNORED") if isdbg('chanerr');
90 my ($rootname) = $line =~ '<(\w+) ';
91 my $pkg = "DXXml::" . ucfirst lc "$rootname";
93 unless (defined *{"${pkg}::"} && $pkg->can('handle_input')) {
94 dbg("xml sentence $rootname not recognised, IGNORED") if isdbg('chanerr');
99 unless ($xref = $pkg->decode_xml($dxchan, $line)) {
100 dbg("invalid XML ($@), IGNORED") if isdbg('chanerr');
105 # do some basic checks
107 unless (exists $xref->{o} && is_callsign($o = $xref->{o})) {
108 dbg("Invalid origin, not a callsign") if isdbg('chanerr');
111 unless (exists $xref->{t} && ($t = IsoTime::unixtime($xref->{t}))) {
112 dbg("Invalid, non-existant or zero time") if isdbg('chanerr');
115 unless ($t > $main::systime - $max_old_age && $t < $main::systime + $max_future_age) {
116 dbg("Too old or too far in the future") if isdbg('chanerr');
119 unless (exists $xref->{id} && ($id = $xref->{id}) >= 0 && $id <= 999) {
120 dbg("Invalid or non-existant id") if isdbg('chanerr');
124 # mark the handle as accepting xml (but only if they
125 # have at least one right)
126 $dxchan->handle_xml(1);
128 # now check that we have not seen this before
129 # this is based on the tuple (o (origin), t (time, normalised to time_t), id)
130 $xref->{'-timet'} = $t;
131 return if DXDupe::check("xml,$o,$t,$id", $dupeage);
133 my $r = bless $xref, $pkg;
134 $r->{'-xml'} = $line;
135 $r->handle_input($dxchan);
139 # note that this a function not a method
148 my @dxchan = DXChannel::get_all();
151 foreach $dxchan (@dxchan) {
152 next unless $dxchan->is_node;
153 next unless $dxchan->handle_xml;
154 next if $dxchan == $main::me;
156 # send a ping out on this channel
157 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
158 if ($dxchan->{nopings} <= 0) {
161 DXXml::Ping::add($main::me, $dxchan->call);
162 $dxchan->{nopings} -= 1;
163 $dxchan->{lastping} = $t;
170 if (!$last10 || $t - $last10 >= 10) {
175 if (!$last_hour || $main::systime - 3600 > $last_hour) {
176 $last_hour = $main::systime;
188 eval {$xref = $xs->XMLin($line)};
195 $id = 0 if $id > 999;
203 unless (exists $self->{'-xml'}) {
204 $self->{o} ||= $main::mycall;
205 $self->{t} ||= IsoTime::hourminsec();
206 $self->{id} ||= nextid();
208 my ($name) = (ref $self) =~ /::(\w+)$/;
209 $self->{'-xml'} = $xs->XMLout($self, RootName =>lc $name, NumericEscape=>1);
211 return $self->{'-xml'};
217 my $fromdxchan = shift;
222 if (my $u = $self->{u} && $self->{to} eq $main::mycall) {
223 $via ||= $u if ($dxchan = DXChannel::get($u));
225 $via ||= $self->{'-via'} || $self->{to};
228 dbg("XML: no route specified (" . dd($self) . ")") if isdbg('chanerr');
231 if (ref $fromdxchan && $via && $fromdxchan->call eq $via) {
232 dbg("XML: Trying to route back to source (" . dd($self) . ")") if isdbg('chanerr');
236 # always send it down the local interface if available
237 $dxchan ||= DXChannel::get($via);
239 dbg("route: $via -> $dxchan->{call} direct" ) if isdbg('route');
241 my $cl = Route::get($via);
242 $dxchan = $cl->dxchan if $cl;
243 dbg("route: $via -> $dxchan->{call} using normal route" ) if isdbg('route');
246 # try the backstop method
248 my $rcall = RouteDB::get($via);
250 $dxchan = DXChannel::get($rcall);
251 dbg("route: $via -> $rcall using RouteDB" ) if isdbg('route') && $dxchan;
256 dbg("XML: no route available to $via") if isdbg('chanerr');
260 if ($fromdxchan->call eq $via) {
261 dbg("XML: Trying to route back to source (" . dd($self) . ")") if isdbg('chanerr');
265 if ($dxchan == $main::me) {
266 dbg("XML: Trying to route to me (" . dd($self) . ")") if isdbg('chanerr');
270 $self->{o} ||= $main::mycall;
271 $self->{id} ||= nextid();
272 $self->{'-timet'} ||= $main::systime;
274 if ($dxchan->handle_xml) {
275 $dxchan->send($self->toxml);
276 } elsif ($dxchan->is_node) {
277 my $ref = $self->topcxx($dxchan);
280 my $ref = $self->tocmd($dxchan);
287 return exists $_[0]->{'-xml'};
292 return exists $_[0]->{'-pcxx'};
297 return exists $_[0]->{'-cmd'};