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::SAX } unless $@;
46 eval { require XML::SAX::Expat } unless $@;
48 LogDbg('err', "do_xml was set to 1 and the XML routines failed to load ($@)");
51 $XML::Simple::PREFERRED_PARSER = 'XML::SAX::Expat';
53 $DXProt::handle_xml = 1;
54 $xs = new XML::Simple(Cache=>[]);
60 # note that this a function not a method
67 unless ($main::do_xml) {
68 dbg("xml not enabled, IGNORED") if isdbg('chanerr');
72 my ($rootname) = $line =~ '<(\w+) ';
73 my $pkg = "DXXml::" . ucfirst lc "$rootname";
75 unless (defined *{"${pkg}::"} && $pkg->can('handle_input')) {
76 dbg("xml sentence $rootname not recognised, IGNORED") if isdbg('chanerr');
81 unless ($xref = $pkg->decode_xml($dxchan, $line)) {
82 dbg("invalid XML ($@), IGNORED") if isdbg('chanerr');
87 # mark the handle as accepting xml (but only if they
88 # have at least one right)
89 $dxchan->handle_xml(1);
91 $xref = bless $xref, $pkg;
92 $xref->{'-xml'} = $line;
93 $xref->handle_input($dxchan);
97 # note that this a function not a method
106 my @dxchan = DXChannel::get_all();
109 foreach $dxchan (@dxchan) {
110 next unless $dxchan->is_node;
111 next unless $dxchan->handle_xml;
112 next if $dxchan == $main::me;
114 # send a ping out on this channel
115 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
116 if ($dxchan->{nopings} <= 0) {
119 DXXml::Ping::add($main::me, $dxchan->call);
120 $dxchan->{nopings} -= 1;
121 $dxchan->{lastping} = $t;
122 $dxchan->{lastping} += $dxchan->{pingint} / 2 unless @{$dxchan->{pingtime}};
129 if (!$last10 || $t - $last10 >= 10) {
134 if (!$last_hour || $main::systime - 3600 > $last_hour) {
135 $last_hour = $main::systime;
147 eval {$xref = $xs->XMLin($line)};
154 $id = 0 if $id > 999;
162 unless (exists $self->{'-xml'}) {
163 $self->{o} ||= $main::mycall;
164 $self->{t} ||= IsoTime::dayminsec();
165 $self->{id} ||= nextid();
167 my ($name) = (ref $self) =~ /::(\w+)$/;
168 $self->{'-xml'} = $xs->XMLout($self, RootName =>lc $name, NumericEscape=>1);
170 return $self->{'-xml'};
176 my $fromdxchan = shift;
178 my $via = $to || $self->{'-via'} || $self->{to};
181 dbg("XML: no route specified (" . $self->toxml . ")") if isdbg('chanerr');
184 if (ref $fromdxchan && $via && $fromdxchan->call eq $via) {
185 dbg("XML: Trying to route back to source (" . $self->toxml . ")") if isdbg('chanerr');
189 # always send it down the local interface if available
190 my $dxchan = DXChannel::get($via);
192 dbg("route: $via -> $dxchan->{call} direct" ) if isdbg('route');
194 my $cl = Route::get($via);
195 $dxchan = $cl->dxchan if $cl;
196 dbg("route: $via -> $dxchan->{call} using normal route" ) if isdbg('route');
199 # try the backstop method
201 my $rcall = RouteDB::get($via);
203 $dxchan = DXChannel::get($rcall);
204 dbg("route: $via -> $rcall using RouteDB" ) if isdbg('route') && $dxchan;
209 dbg("XML: no route available to $via") if isdbg('chanerr');
213 if ($fromdxchan->call eq $via) {
214 dbg("XML: Trying to route back to source (" . $self->toxml . ")") if isdbg('chanerr');
218 if ($dxchan == $main::me) {
219 dbg("XML: Trying to route to me (" . $self->toxml . ")") if isdbg('chanerr');
223 if ($dxchan->handle_xml) {
224 $dxchan->send($self->toxml);
226 $self->{o} ||= $main::mycall;
227 $self->{id} ||= nextid();
228 $self->{'-timet'} ||= $main::systime;
229 $dxchan->send($self->topcxx);
235 return exists $_[0]->{'-xml'};
240 return exists $_[0]->{'-pcxx'};
245 return exists $_[0]->{'-cmd'};