+ my $t = time;
+ my @dxchan = DXChannel::get_all();
+ my $dxchan;
+
+ foreach $dxchan (@dxchan) {
+ next unless $dxchan->is_node;
+ next unless $dxchan->handle_xml;
+ next if $dxchan == $main::me;
+
+ # send a ping out on this channel
+ if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
+ if ($dxchan->{nopings} <= 0) {
+ $dxchan->disconnect;
+ } else {
+ DXXml::Ping::add($main::me, $dxchan->call);
+ $dxchan->{nopings} -= 1;
+ $dxchan->{lastping} = $t;
+ }
+ }
+ }
+
+
+ # every ten seconds
+ if (!$last10 || $t - $last10 >= 10) {
+ $last10 = $t;
+ }
+
+ # every hour
+ if (!$last_hour || $main::systime - 3600 > $last_hour) {
+ $last_hour = $main::systime;
+ }
+
+}
+
+sub decode_xml
+{
+ my $pkg = shift;
+ my $dxchan = shift;
+ my $line = shift;
+
+ my $xref;
+ eval {$xref = $xs->XMLin($line)};
+ return $xref;
+}
+
+sub nextid
+{
+ my $r = $id++;
+ $id = 0 if $id > 999;
+ return $r;
+}
+
+sub toxml
+{
+ my $self = shift;
+
+ unless (exists $self->{'-xml'}) {
+ $self->{o} ||= $main::mycall;
+ $self->{t} ||= IsoTime::hourminsec();
+ $self->{id} ||= nextid();
+
+ my ($name) = (ref $self) =~ /::(\w+)$/;
+ $self->{'-xml'} = $xs->XMLout($self, RootName =>lc $name, NumericEscape=>1);
+ }
+ return $self->{'-xml'};
+}
+
+sub route
+{
+ my $self = shift;
+ my $fromdxchan = shift;
+ my $to = shift;
+ my $via = $to;
+ my $dxchan;
+
+ if (my $u = $self->{u} && $self->{to} eq $main::mycall) {
+ $via ||= $u if ($dxchan = DXChannel::get($u));
+ }
+ $via ||= $self->{'-via'} || $self->{to};
+
+ unless ($via) {
+ dbg("XML: no route specified (" . dd($self) . ")") if isdbg('chanerr');
+ return;
+ }
+ if (ref $fromdxchan && $via && $fromdxchan->call eq $via) {
+ dbg("XML: Trying to route back to source (" . dd($self) . ")") if isdbg('chanerr');
+ return;
+ }
+
+ # always send it down the local interface if available
+ $dxchan ||= DXChannel::get($via);
+ if ($dxchan) {
+ dbg("route: $via -> $dxchan->{call} direct" ) if isdbg('route');
+ } else {
+ my $cl = Route::get($via);
+ $dxchan = $cl->dxchan if $cl;
+ dbg("route: $via -> $dxchan->{call} using normal route" ) if isdbg('route');
+ }
+
+ # try the backstop method
+ unless ($dxchan) {
+ my $rcall = RouteDB::get($via);
+ if ($rcall) {
+ $dxchan = DXChannel::get($rcall);
+ dbg("route: $via -> $rcall using RouteDB" ) if isdbg('route') && $dxchan;
+ }
+ }
+
+ unless ($dxchan) {
+ dbg("XML: no route available to $via") if isdbg('chanerr');
+ return;
+ }
+
+ if ($fromdxchan->call eq $via) {
+ dbg("XML: Trying to route back to source (" . dd($self) . ")") if isdbg('chanerr');
+ return;
+ }
+
+ if ($dxchan == $main::me) {
+ dbg("XML: Trying to route to me (" . dd($self) . ")") if isdbg('chanerr');
+ return;
+ }
+
+ $self->{o} ||= $main::mycall;
+ $self->{id} ||= nextid();
+ $self->{'-timet'} ||= $main::systime;
+
+ if ($dxchan->handle_xml) {
+ $dxchan->send($self->toxml);
+ } elsif ($dxchan->is_node) {
+ my $ref = $self->topcxx($dxchan);
+ $dxchan->send($ref);
+ } else {
+ my $ref = $self->tocmd($dxchan);
+ $dxchan->send($ref);
+ }
+}
+
+sub has_xml
+{
+ return exists $_[0]->{'-xml'};
+}