6 # Copyright (c) Dirk Koopman, G1TLH
22 use vars qw($VERSION $BRANCH $xs $id $max_old_age $max_future_age);
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
33 # generate a new XML sentence structure
37 my $class = ref $pkg || $pkg;
38 return bless{@_}, $class;
42 # note that this a function not a method
46 return unless $main::do_xml;
48 eval { require XML::Simple };
49 eval { require XML::Parser } unless $@;
51 LogDbg('err', "do_xml was set to 1 and the XML routines failed to load ($@)");
54 $XML::Simple::PREFERRED_PARSER = 'XML::Parser';
56 $DXProt::handle_xml = 1;
57 $xs = new XML::Simple(Cache=>[]);
63 # note that this a function not a method
70 unless ($main::do_xml) {
71 dbg("xml not enabled, IGNORED") if isdbg('chanerr');
75 my ($rootname) = $line =~ '<(\w+) ';
76 my $pkg = "DXXml::" . ucfirst lc "$rootname";
78 unless (defined *{"${pkg}::"} && $pkg->can('handle_input')) {
79 dbg("xml sentence $rootname not recognised, IGNORED") if isdbg('chanerr');
84 unless ($xref = $pkg->decode_xml($dxchan, $line)) {
85 dbg("invalid XML ($@), IGNORED") if isdbg('chanerr');
90 # do some basic checks
92 unless (exists $xref->{o} && is_callsign($o = $xref->{o})) {
93 dbg("Invalid origin, not a callsign") if isdbg('chanerr');
96 unless (exists $xref->{t} && ($t = IsoTime::unixtime($xref->{t}))) {
97 dbg("Invalid, non-existant or zero time") if isdbg('chanerr');
100 unless ($t < $main::systime - $max_old_age || $t > $main::systime + $max_future_age) {
101 dbg("Too old or too far in the future") if isdbg('chanerr');
104 unless (exists $xref->{id} && ($id = $xref->{id}) >= 0 && $id <= 9999) {
105 dbg("Invalid or non-existant id") if isdbg('chanerr');
109 # mark the handle as accepting xml (but only if they
110 # have at least one right)
111 $dxchan->handle_xml(1);
113 # now check that we have not seen this before
114 # this is based on the tuple (o (origin), t (time, normalised to time_t), id)
115 $xref->{'-timet'} = $t;
116 return if DXDupe::check("xml,$o,$t,$id");
118 $xref = bless $xref, $pkg;
119 $xref->{'-xml'} = $line;
120 $xref->handle_input($dxchan);
124 # note that this a function not a method
133 my @dxchan = DXChannel::get_all();
136 foreach $dxchan (@dxchan) {
137 next unless $dxchan->is_node;
138 next unless $dxchan->handle_xml;
139 next if $dxchan == $main::me;
141 # send a ping out on this channel
142 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
143 if ($dxchan->{nopings} <= 0) {
146 DXXml::Ping::add($main::me, $dxchan->call);
147 $dxchan->{nopings} -= 1;
148 $dxchan->{lastping} = $t;
149 $dxchan->{lastping} += $dxchan->{pingint} / 2 unless @{$dxchan->{pingtime}};
156 if (!$last10 || $t - $last10 >= 10) {
161 if (!$last_hour || $main::systime - 3600 > $last_hour) {
162 $last_hour = $main::systime;
174 eval {$xref = $xs->XMLin($line)};
181 $id = 0 if $id > 999;
189 unless (exists $self->{'-xml'}) {
190 $self->{o} ||= $main::mycall;
191 $self->{t} ||= IsoTime::dayminsec();
192 $self->{id} ||= nextid();
194 my ($name) = (ref $self) =~ /::(\w+)$/;
195 $self->{'-xml'} = $xs->XMLout($self, RootName =>lc $name, NumericEscape=>1);
197 return $self->{'-xml'};
203 my $fromdxchan = shift;
205 my $via = $to || $self->{'-via'} || $self->{to};
208 dbg("XML: no route specified (" . $self->toxml . ")") if isdbg('chanerr');
211 if (ref $fromdxchan && $via && $fromdxchan->call eq $via) {
212 dbg("XML: Trying to route back to source (" . $self->toxml . ")") if isdbg('chanerr');
216 # always send it down the local interface if available
217 my $dxchan = DXChannel::get($via);
219 dbg("route: $via -> $dxchan->{call} direct" ) if isdbg('route');
221 my $cl = Route::get($via);
222 $dxchan = $cl->dxchan if $cl;
223 dbg("route: $via -> $dxchan->{call} using normal route" ) if isdbg('route');
226 # try the backstop method
228 my $rcall = RouteDB::get($via);
230 $dxchan = DXChannel::get($rcall);
231 dbg("route: $via -> $rcall using RouteDB" ) if isdbg('route') && $dxchan;
236 dbg("XML: no route available to $via") if isdbg('chanerr');
240 if ($fromdxchan->call eq $via) {
241 dbg("XML: Trying to route back to source (" . $self->toxml . ")") if isdbg('chanerr');
245 if ($dxchan == $main::me) {
246 dbg("XML: Trying to route to me (" . $self->toxml . ")") if isdbg('chanerr');
250 if ($dxchan->handle_xml) {
251 $dxchan->send($self->toxml);
253 $self->{o} ||= $main::mycall;
254 $self->{id} ||= nextid();
255 $self->{'-timet'} ||= $main::systime;
256 $dxchan->send($self->topcxx);
262 return exists $_[0]->{'-xml'};
267 return exists $_[0]->{'-pcxx'};
272 return exists $_[0]->{'-cmd'};