| 1 |
joko |
1.1 |
## ------------------------------------------------------------------------ |
| 2 |
joko |
1.2 |
## $Id: LookupService.pm,v 1.1 2003/06/29 01:35:30 joko Exp $ |
| 3 |
joko |
1.1 |
## ------------------------------------------------------------------------ |
| 4 |
joko |
1.2 |
## $Log: LookupService.pm,v $ |
| 5 |
|
|
## Revision 1.1 2003/06/29 01:35:30 joko |
| 6 |
|
|
## initial commit |
| 7 |
|
|
## |
| 8 |
joko |
1.1 |
## ------------------------------------------------------------------------ |
| 9 |
|
|
|
| 10 |
|
|
|
| 11 |
|
|
package POE::Component::LookupService; |
| 12 |
|
|
|
| 13 |
|
|
use strict; |
| 14 |
|
|
use warnings; |
| 15 |
|
|
|
| 16 |
|
|
use POE qw( Component::RPCXML::Server Component::StorageAgent Component::ServiceRegistrar ); |
| 17 |
|
|
use RPC::XML; |
| 18 |
|
|
use Data::Dumper; |
| 19 |
|
|
|
| 20 |
|
|
|
| 21 |
|
|
sub new { |
| 22 |
|
|
my $classname = shift; |
| 23 |
|
|
my @args = @_; |
| 24 |
|
|
|
| 25 |
|
|
my $self = {}; |
| 26 |
|
|
bless $self, $classname; |
| 27 |
|
|
|
| 28 |
|
|
$self->{options} = { @args }; |
| 29 |
|
|
|
| 30 |
|
|
#my $event_handler = lookupd->new(); |
| 31 |
|
|
POE::Session->create( |
| 32 |
|
|
object_states => [ |
| 33 |
|
|
$self => [qw( _start _stop boot_intercom boot_storage boot_registrar handle_request send_response )] |
| 34 |
|
|
] |
| 35 |
|
|
); |
| 36 |
|
|
|
| 37 |
|
|
} |
| 38 |
|
|
|
| 39 |
|
|
# This is not a POE method. It's a plain OO one. |
| 40 |
|
|
sub debug { |
| 41 |
|
|
my $self = shift; |
| 42 |
|
|
my $msg = shift; |
| 43 |
|
|
$msg ||= ''; |
| 44 |
|
|
print STDERR __PACKAGE__ . ": " . $msg, "\n"; |
| 45 |
|
|
} |
| 46 |
|
|
|
| 47 |
|
|
# Controller's event handlers |
| 48 |
|
|
|
| 49 |
|
|
sub _start { |
| 50 |
|
|
my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ]; |
| 51 |
|
|
$self->debug("_start"); |
| 52 |
|
|
|
| 53 |
|
|
#$kernel->alias_set("controller"); |
| 54 |
|
|
|
| 55 |
|
|
#$kernel->post( controller => 'start_daemon' ); |
| 56 |
|
|
$kernel->yield( 'boot_storage' ); |
| 57 |
|
|
$kernel->yield( 'boot_registrar' ); |
| 58 |
|
|
$kernel->yield( 'boot_intercom' ); |
| 59 |
|
|
#$_[HEAP]->{subsession} = POE::Session->create( inline_states => { _start => sub { print "WORKER!", "\n"; } } ); |
| 60 |
|
|
#$_[HEAP]->{subsession}->yield( |
| 61 |
|
|
|
| 62 |
|
|
}; |
| 63 |
|
|
|
| 64 |
|
|
sub _stop { |
| 65 |
|
|
my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ]; |
| 66 |
|
|
$self->debug("_stop"); |
| 67 |
joko |
1.2 |
$kernel->post( IKC => 'shutdown' ); |
| 68 |
joko |
1.1 |
}; |
| 69 |
|
|
|
| 70 |
|
|
sub boot_intercom { |
| 71 |
|
|
|
| 72 |
|
|
my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ]; |
| 73 |
|
|
$self->debug("boot_intercom"); |
| 74 |
|
|
|
| 75 |
|
|
# Server component - encapsulates some session(s) and/or wheel(s)? |
| 76 |
|
|
|
| 77 |
|
|
# V1 - windy?! |
| 78 |
|
|
# - dies on empty payloads |
| 79 |
|
|
# - requires addressing of "session"-parameter inside url (security flaw?) |
| 80 |
|
|
# + method publishing/rescinding |
| 81 |
|
|
#POE::Component::Server::XMLRPC->new( alias => "xmlrpc", port => 3332 ); |
| 82 |
|
|
|
| 83 |
|
|
# V2 - seems to be times more robust! i.e. offers these features: |
| 84 |
|
|
# + doesn't die on empty xml-payloads in request |
| 85 |
|
|
# + doesn't require session addressing through url |
| 86 |
|
|
# + more flexible event dispatching |
| 87 |
|
|
# - no remote method publishing and/or rescinding |
| 88 |
|
|
# + doesn't even call the dispatching method on empty payloads! ("method-missing", etc.) |
| 89 |
|
|
POE::Component::RPCXML::Server->new( |
| 90 |
|
|
'Alias' => 'rpcxml-server', |
| 91 |
|
|
'Port' => $self->{options}->{daemon}->{port}, |
| 92 |
|
|
'DispatchEvent' => 'handle_request', |
| 93 |
|
|
'Debug' => 0, |
| 94 |
|
|
); |
| 95 |
|
|
|
| 96 |
|
|
# V3 - IKC |
| 97 |
|
|
# Don't do that here! It doesn't work! |
| 98 |
|
|
#use POE::Component::IKC::Server; |
| 99 |
|
|
#create_ikc_server( port => 30, name => 'LookupService' ); |
| 100 |
|
|
|
| 101 |
|
|
}; |
| 102 |
|
|
|
| 103 |
|
|
sub boot_storage { |
| 104 |
|
|
|
| 105 |
|
|
my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ]; |
| 106 |
|
|
$self->debug("boot_storage"); |
| 107 |
|
|
|
| 108 |
|
|
#print Dumper($self->{options}->{storage}); |
| 109 |
|
|
$heap->{db} = POE::Component::StorageAgent->new( $self->{options}->{storage} ); |
| 110 |
|
|
|
| 111 |
|
|
} |
| 112 |
|
|
|
| 113 |
|
|
sub boot_registrar { |
| 114 |
|
|
my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ]; |
| 115 |
|
|
$self->debug("boot_registrar"); |
| 116 |
|
|
$heap->{reg} = POE::Component::ServiceRegistrar->new(); |
| 117 |
|
|
} |
| 118 |
|
|
|
| 119 |
|
|
sub handle_request { |
| 120 |
|
|
my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ]; |
| 121 |
|
|
|
| 122 |
|
|
my $request = $_[ARG0]; |
| 123 |
|
|
my $wheel = $_[ARG1]; |
| 124 |
|
|
|
| 125 |
|
|
#print Dumper($request); |
| 126 |
|
|
$self->debug("handle_request: method='$request->{name}'"); |
| 127 |
|
|
|
| 128 |
|
|
#my $payload = {}; |
| 129 |
|
|
|
| 130 |
|
|
my $method = $request->{name}; |
| 131 |
|
|
if ($method eq 'ServiceRegistrar.register') { |
| 132 |
|
|
#$payload->{msg} = 'Registration running'; |
| 133 |
|
|
my $authenticated = 1; |
| 134 |
|
|
if ($authenticated) { |
| 135 |
|
|
# send response back to client |
| 136 |
|
|
#my $response = RPC::XML::response->new($payload); |
| 137 |
|
|
$kernel->yield( send_response => $wheel => 'ok' ); |
| 138 |
|
|
|
| 139 |
|
|
# handle registration |
| 140 |
|
|
my $query = { |
| 141 |
|
|
'abc' => 'def', |
| 142 |
|
|
}; |
| 143 |
|
|
$kernel->post( storage => 'query' => $query ); |
| 144 |
|
|
} |
| 145 |
|
|
} elsif ($method eq 'ServiceRegistrar.list') { |
| 146 |
|
|
} elsif ($method eq 'Lease.renew') { |
| 147 |
|
|
} elsif ($method eq 'system.shutdown') { |
| 148 |
joko |
1.2 |
#$kernel->yield('_stop'); |
| 149 |
joko |
1.1 |
exit; |
| 150 |
|
|
#$kernel->signal( 'TERM' ); |
| 151 |
|
|
} elsif ($method eq 'storage.deploy') { |
| 152 |
|
|
$kernel->post( storage => 'deploy' ); |
| 153 |
|
|
} |
| 154 |
|
|
|
| 155 |
|
|
#my $response = RPC::XML::response->new('ok'); |
| 156 |
|
|
#POE::Component::RPCXML::Server->send_resp($wheel, $response->as_string); |
| 157 |
|
|
#$kernel->yield( send_response => $response, $wheel ); |
| 158 |
|
|
|
| 159 |
|
|
}; |
| 160 |
|
|
|
| 161 |
|
|
sub send_response { |
| 162 |
|
|
my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ]; |
| 163 |
|
|
$self->debug("send_response"); |
| 164 |
|
|
my $wheel = $_[ARG0]; |
| 165 |
|
|
my $response = $_[ARG1]; |
| 166 |
|
|
if (ref $response ne 'RPC::XML::response') { |
| 167 |
|
|
print "NEW!", "\n"; |
| 168 |
|
|
$response = RPC::XML::response->new( $response ); |
| 169 |
|
|
} |
| 170 |
|
|
POE::Component::RPCXML::Server->send_resp($wheel, $response->as_string); |
| 171 |
|
|
} |
| 172 |
|
|
|
| 173 |
|
|
1; |
| 174 |
|
|
__END__ |