6 # Copyright (c) Dirk Koopman, G1TLH
24 use vars qw($xs $id $max_old_age $max_future_age $dupeage);
25 $xs = undef; # the XML::Simple parser instance
26 $id = 0; # the next ID to be used
27 $max_old_age = 3600; # how old a sentence we will accept
28 $max_future_age = 900; # how far into the future we will accept
29 $dupeage = 12*60*60; # duplicates stored half a day
32 # generate a new XML sentence structure
36 my $class = ref $pkg || $pkg;
37 my $self = bless{}, $class;
41 $self->{$key} = $val if defined $val;
47 # note that this a function not a method
51 return unless $main::do_xml;
53 eval { require XML::Simple };
54 eval { require XML::Parser } unless $@;
56 LogDbg('err', "do_xml was set to 1 and the XML routines failed to load ($@)");
57 $main::do_xml = $xs = 0;
59 $XML::Simple::PREFERRED_PARSER = 'XML::Parser';
61 $xs = new XML::Simple(ContentKey=>'content', ForceArray=>1);
66 # is XML avaiable and active
69 return $main::do_xml && $xs;
73 # note that this a function not a method
80 unless (available()) {
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} && $self->{to} eq $main::mycall) {
218 $via ||= $u if ($dxchan = DXChannel::get($u));
220 $via ||= $self->{'-via'} || $self->{to};
223 dbg("XML: no route specified (" . dd($self) . ")") if isdbg('chanerr');
226 if (ref $fromdxchan && $via && $fromdxchan->call eq $via) {
227 dbg("XML: Trying to route back to source (" . dd($self) . ")") 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');
242 dbg("XML: no route available to $via") if isdbg('chanerr');
246 if ($fromdxchan->call eq $via) {
247 dbg("XML: Trying to route back to source (" . dd($self) . ")") if isdbg('chanerr');
251 if ($dxchan == $main::me) {
252 dbg("XML: Trying to route to me (" . dd($self) . ")") if isdbg('chanerr');
256 $self->{o} ||= $main::mycall;
257 $self->{id} ||= nextid();
258 $self->{'-timet'} ||= $main::systime;
260 if ($dxchan->handle_xml) {
261 $dxchan->send($self->toxml);
262 } elsif ($dxchan->is_node) {
263 my $ref = $self->topcxx($dxchan);
266 my $ref = $self->tocmd($dxchan);
273 return exists $_[0]->{'-xml'};
278 return exists $_[0]->{'-pcxx'};
283 return exists $_[0]->{'-cmd'};