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; };
47 $DXProt::handle_xml = 1;
48 $xs = new XML::Simple();
54 # note that this a function not a method
61 unless ($main::do_xml) {
62 dbg("xml not enabled, IGNORED") if isdbg('chanerr');
66 my ($rootname) = $line =~ '<(\w+) ';
67 my $pkg = "DXXml::" . ucfirst lc "$rootname";
69 unless (defined *{"${pkg}::"} && $pkg->can('handle_input')) {
70 dbg("xml sentence $rootname not recognised, IGNORED") if isdbg('chanerr');
75 unless ($xref = $pkg->decode_xml($dxchan, $line)) {
76 dbg("invalid XML ($@), IGNORED") if isdbg('chanerr');
81 # mark the handle as accepting xml (but only if they
82 # have at least one right)
83 $dxchan->handle_xml(1);
85 $xref = bless $xref, $pkg;
86 $xref->{'-xml'} = $line;
87 $xref->handle_input($dxchan);
91 # note that this a function not a method
100 my @dxchan = DXChannel::get_all();
103 foreach $dxchan (@dxchan) {
104 next unless $dxchan->is_node;
105 next unless $dxchan->handle_xml;
106 next if $dxchan == $main::me;
108 # send a ping out on this channel
109 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
110 if ($dxchan->{nopings} <= 0) {
113 DXXml::Ping::add($main::me, $dxchan->call);
114 $dxchan->{nopings} -= 1;
115 $dxchan->{lastping} = $t;
116 $dxchan->{lastping} += $dxchan->{pingint} / 2 unless @{$dxchan->{pingtime}};
123 if (!$last10 || $t - $last10 >= 10) {
128 if (!$last_hour || $main::systime - 3600 > $last_hour) {
129 $last_hour = $main::systime;
141 eval {$xref = $xs->XMLin($line)};
148 $id = 0 if $id > 999;
156 unless (exists $self->{'-xml'}) {
157 $self->{o} ||= $main::mycall;
158 $self->{t} ||= IsoTime::dayminsec();
159 $self->{id} ||= nextid();
161 my ($name) = (ref $self) =~ /::(\w+)$/;
162 $self->{'-xml'} = $xs->XMLout($self, RootName =>lc $name, NumericEscape=>1);
164 return $self->{'-xml'};
170 my $fromdxchan = shift;
172 my $via = $to || $self->{'-via'} || $self->{to};
175 dbg("XML: no route specified (" . $self->toxml . ")") if isdbg('chanerr');
178 if (ref $fromdxchan && $via && $fromdxchan->call eq $via) {
179 dbg("XML: Trying to route back to source (" . $self->toxml . ")") if isdbg('chanerr');
183 # always send it down the local interface if available
184 my $dxchan = DXChannel::get($via);
186 dbg("route: $via -> $dxchan->{call} direct" ) if isdbg('route');
188 my $cl = Route::get($via);
189 $dxchan = $cl->dxchan if $cl;
190 dbg("route: $via -> $dxchan->{call} using normal route" ) if isdbg('route');
193 # try the backstop method
195 my $rcall = RouteDB::get($via);
197 $dxchan = DXChannel::get($rcall);
198 dbg("route: $via -> $rcall using RouteDB" ) if isdbg('route') && $dxchan;
203 dbg("XML: no route available to $via") if isdbg('chanerr');
207 if ($fromdxchan->call eq $via) {
208 dbg("XML: Trying to route back to source (" . $self->toxml . ")") if isdbg('chanerr');
212 if ($dxchan == $main::me) {
213 dbg("XML: Trying to route to me (" . $self->toxml . ")") if isdbg('chanerr');
217 if ($dxchan->handle_xml) {
218 $dxchan->send($self->toxml);
220 $self->{o} ||= $main::mycall;
221 $self->{id} ||= nextid();
222 $self->{'-timet'} ||= $main::systime;
223 $dxchan->send($self->topcxx);
229 return exists $_[0]->{'-xml'};
234 return exists $_[0]->{'-pcxx'};
239 return exists $_[0]->{'-cmd'};