use SOAP::Lite +trace =>
'all';
use SOAP::Transport::HTTP;
use Node;
# don't want to die on 'Broken pipe' or Ctrl-C
$SIG{PIPE} = $SIG{INT} = 'IGNORE';
$daemon = SOAP::Transport::HTTP::Daemon
-> new (LocalPort => 8080)
-> objects_by_reference('Node')
-> dispatch_to('Node');
print "Contact to SOAP server at ", $daemon->url, "\n";
$daemon->handle;
####
use SOAP::Lite +autodispatch =>
uri => 'urn:',
proxy => 'http://localhost:8080/'
;
my $root = new Node(12);
print $root->toString(), "\n";
for (my $i = 0; $i < 3; $i++) {
my $child = new Node($i+1);
print $child->toString(), "\n";
$root->addChild($child);
print $root->toString(), "\n";
}
my $aChild = $root->child(1);
my $aParent = $aChild->parent();
print $aParent->toString(), "\n";
####
package Node;
use strict;
sub new {
my ($pkg, $value) = @_;
my $node = bless {
value => $value,
parent => undef,
nrChildren => 0,
children => []
}, $pkg;
return $node;
}
sub addChild {
my ($node, $child) = @_;
$node->{children}->[$node->{nrChildren}++] = $child;
$child->{parent} = $node;
}
sub numberOfChildren {
my ($node) = @_;
return scalar(@{$node->{children}});
}
sub child {
my ($node, $index) = @_;
return $node->{children}->[$index];
}
sub parent {
my ($node) = @_;
return $node->{parent};
}
sub toString {
my ($node) = @_;
return "node[$node->{value}]: " . scalar(@{$node->{children}}) . " children";
}
1;