6 # Copyright (c) Dirk Koopman, G1TLH
20 use vars qw($VERSION $BRANCH $xs $id);
21 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
22 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
23 $main::build += $VERSION;
24 $main::branch += $BRANCH;
26 $xs = undef; # the XML::Simple parser instance
27 $id = 0; # the next ID to be used
29 # generate a new XML sentence structure
33 my $class = ref $pkg || $pkg;
34 return bless{@_}, $class;
38 # note that this a function not a method
42 return unless $main::do_xml;
44 eval { require XML::Simple };
45 eval { require XML::Parser } unless $@;
47 LogDbg('err', "do_xml was set to 1 and the XML routines failed to load ($@)");
50 $XML::Simple::PREFERRED_PARSER = 'XML::Parser';
52 $DXProt::handle_xml = 1;
53 $xs = new XML::Simple(Cache=>[]);
59 # note that this a function not a method
66 unless ($main::do_xml) {
67 dbg("xml not enabled, IGNORED") if isdbg('chanerr');
71 my ($rootname) = $line =~ '<(\w+) ';
72 my $pkg = "DXXml::" . ucfirst lc "$rootname";
74 unless (defined *{"${pkg}::"} && $pkg->can('handle_input')) {
75 dbg("xml sentence $rootname not recognised, IGNORED") if isdbg('chanerr');
80 unless ($xref = $pkg->decode_xml($dxchan, $line)) {
81 dbg("invalid XML ($@), IGNORED") if isdbg('chanerr');
86 # mark the handle as accepting xml (but only if they
87 # have at least one right)
88 $dxchan->handle_xml(1);
90 $xref = bless $xref, $pkg;
91 $xref->{'-xml'} = $line;
92 $xref->handle_input($dxchan);
96 # note that this a function not a method
105 my @dxchan = DXChannel::get_all();
108 foreach $dxchan (@dxchan) {
109 next unless $dxchan->is_node;
110 next unless $dxchan->handle_xml;
111 next if $dxchan == $main::me;
113 # send a ping out on this channel
114 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
115 if ($dxchan->{nopings} <= 0) {
118 DXXml::Ping::add($main::me, $dxchan->call);
119 $dxchan->{nopings} -= 1;
120 $dxchan->{lastping} = $t;
121 $dxchan->{lastping} += $dxchan->{pingint} / 2 unless @{$dxchan->{pingtime}};
128 if (!$last10 || $t - $last10 >= 10) {
133 if (!$last_hour || $main::systime - 3600 > $last_hour) {
134 $last_hour = $main::systime;
146 eval {$xref = $xs->XMLin($line)};
153 $id = 0 if $id > 999;
161 unless (exists $self->{'-xml'}) {
162 $self->{o} ||= $main::mycall;
163 $self->{t} ||= IsoTime::dayminsec();
164 $self->{id} ||= nextid();
166 my ($name) = (ref $self) =~ /::(\w+)$/;
167 $self->{'-xml'} = $xs->XMLout($self, RootName =>lc $name, NumericEscape=>1);
169 return $self->{'-xml'};
175 my $fromdxchan = shift;
177 my $via = $to || $self->{'-via'} || $self->{to};
180 dbg("XML: no route specified (" . $self->toxml . ")") if isdbg('chanerr');
183 if (ref $fromdxchan && $via && $fromdxchan->call eq $via) {
184 dbg("XML: Trying to route back to source (" . $self->toxml . ")") if isdbg('chanerr');
188 # always send it down the local interface if available
189 my $dxchan = DXChannel::get($via);
191 dbg("route: $via -> $dxchan->{call} direct" ) if isdbg('route');
193 my $cl = Route::get($via);
194 $dxchan = $cl->dxchan if $cl;
195 dbg("route: $via -> $dxchan->{call} using normal route" ) if isdbg('route');
198 # try the backstop method
200 my $rcall = RouteDB::get($via);
202 $dxchan = DXChannel::get($rcall);
203 dbg("route: $via -> $rcall using RouteDB" ) if isdbg('route') && $dxchan;
208 dbg("XML: no route available to $via") if isdbg('chanerr');
212 if ($fromdxchan->call eq $via) {
213 dbg("XML: Trying to route back to source (" . $self->toxml . ")") if isdbg('chanerr');
217 if ($dxchan == $main::me) {
218 dbg("XML: Trying to route to me (" . $self->toxml . ")") if isdbg('chanerr');
222 if ($dxchan->handle_xml) {
223 $dxchan->send($self->toxml);
225 $self->{o} ||= $main::mycall;
226 $self->{id} ||= nextid();
227 $self->{'-timet'} ||= $main::systime;
228 $dxchan->send($self->topcxx);
234 return exists $_[0]->{'-xml'};
239 return exists $_[0]->{'-pcxx'};
244 return exists $_[0]->{'-cmd'};