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
105 eval {$xref = $xs->XMLin($line)};
112 $id = 0 if $id > 999;
120 unless (exists $self->{'-xml'}) {
121 $self->{o} ||= $main::mycall;
122 $self->{t} ||= IsoTime::dayminsec();
123 $self->{id} ||= nextid();
125 my ($name) = (ref $self) =~ /::(\w+)$/;
126 $self->{'-xml'} = $xs->XMLout($self, RootName =>lc $name, NumericEscape=>1);
128 return $self->{'-xml'};
134 my $fromdxchan = shift;
136 my $via = $to || $self->{'-via'} || $self->{to};
139 dbg("XML: no route specified (" . $self->toxml . ")") if isdbg('chanerr');
142 if (ref $fromdxchan && $via && $fromdxchan->call eq $via) {
143 dbg("XML: Trying to route back to source (" . $self->toxml . ")") if isdbg('chanerr');
147 # always send it down the local interface if available
148 my $dxchan = DXChannel::get($via);
150 dbg("route: $via -> $dxchan->{call} direct" ) if isdbg('route');
152 my $cl = Route::get($via);
153 $dxchan = $cl->dxchan if $cl;
154 dbg("route: $via -> $dxchan->{call} using normal route" ) if isdbg('route');
157 # try the backstop method
159 my $rcall = RouteDB::get($via);
161 $dxchan = DXChannel::get($rcall);
162 dbg("route: $via -> $rcall using RouteDB" ) if isdbg('route') && $dxchan;
167 dbg("XML: no route available to $via") if isdbg('chanerr');
171 if ($fromdxchan->call eq $via) {
172 dbg("XML: Trying to route back to source (" . $self->toxml . ")") if isdbg('chanerr');
176 if ($dxchan == $main::me) {
177 dbg("XML: Trying to route to me (" . $self->toxml . ")") if isdbg('chanerr');
181 if ($dxchan->handle_xml) {
182 $dxchan->send($self->toxml);
184 $self->{o} ||= $main::mycall;
185 $self->{id} ||= nextid();
186 $self->{'-timet'} ||= $main::systime;
187 $dxchan->send($self->topcxx);
193 return exists $_[0]->{'-xml'};
198 return exists $_[0]->{'-pcxx'};
203 return exists $_[0]->{'-cmd'};