| 1 |
joko |
1.1 |
## ------------------------------------------------------------------------ |
| 2 |
|
|
## $Id: WebCache.pm,v 1.3 2003/06/25 23:37:04 joko Exp $ |
| 3 |
|
|
## ------------------------------------------------------------------------ |
| 4 |
|
|
## $Log: WebCache.pm,v $ |
| 5 |
|
|
## ------------------------------------------------------------------------ |
| 6 |
|
|
|
| 7 |
|
|
|
| 8 |
|
|
package POE::Component::ServiceRegistrar; |
| 9 |
|
|
|
| 10 |
|
|
use strict; |
| 11 |
|
|
use warnings; |
| 12 |
|
|
|
| 13 |
|
|
use POE qw( Session ); |
| 14 |
|
|
#use Data::Storage::Container; |
| 15 |
|
|
use Data::Dumper; |
| 16 |
|
|
|
| 17 |
|
|
|
| 18 |
|
|
sub new { |
| 19 |
|
|
my $classname = shift; |
| 20 |
|
|
my $self = {}; |
| 21 |
|
|
bless $self, $classname; |
| 22 |
|
|
|
| 23 |
|
|
$self->{config} = shift; |
| 24 |
|
|
|
| 25 |
|
|
POE::Session->create( |
| 26 |
|
|
object_states => [ |
| 27 |
|
|
$self => [qw( _start _stop register_lease renew_lease remote_register )] |
| 28 |
|
|
] |
| 29 |
|
|
); |
| 30 |
|
|
|
| 31 |
|
|
} |
| 32 |
|
|
|
| 33 |
|
|
# This is not a POE method. It's a plain OO one. |
| 34 |
|
|
sub debug { |
| 35 |
|
|
my $self = shift; |
| 36 |
|
|
my $msg = shift; |
| 37 |
|
|
$msg ||= ''; |
| 38 |
|
|
print STDERR __PACKAGE__ . ": " . $msg, "\n"; |
| 39 |
|
|
} |
| 40 |
|
|
|
| 41 |
|
|
|
| 42 |
|
|
sub _start { |
| 43 |
|
|
my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ]; |
| 44 |
|
|
$self->debug("_start"); |
| 45 |
|
|
|
| 46 |
|
|
# V1 - Announce ourselves as global singleton at Kernel side. |
| 47 |
|
|
$kernel->alias_set("ServiceRegistrar"); |
| 48 |
|
|
|
| 49 |
|
|
# V2 - Also announce to the IKC infrastructure component. |
| 50 |
|
|
$kernel->post('IKC', 'publish', 'ServiceRegistrar', [qw( register_lease )]); |
| 51 |
|
|
|
| 52 |
|
|
# tell IKC monitor to start up and intercept registration and renewal requests from client(s) |
| 53 |
|
|
$kernel->post( IKC => 'monitor', '*' => { register => 'remote_register' }); |
| 54 |
|
|
#$kernel->post( IKC => 'monitor', '*' => { renew => 'catch_event' }); |
| 55 |
|
|
|
| 56 |
|
|
}; |
| 57 |
|
|
|
| 58 |
|
|
sub _stop { |
| 59 |
|
|
my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ]; |
| 60 |
|
|
$self->debug("_stop"); |
| 61 |
|
|
|
| 62 |
|
|
# does this work? |
| 63 |
|
|
$kernel->post( IKC => 'retract', 'ServiceRegistrar' => [qw( register_lease )]); |
| 64 |
|
|
}; |
| 65 |
|
|
|
| 66 |
|
|
sub register_lease { |
| 67 |
|
|
my ( $self, $kernel, $heap, $sender ) = @_[ OBJECT, KERNEL, HEAP, SENDER ]; |
| 68 |
|
|
|
| 69 |
|
|
$self->debug("register_lease"); |
| 70 |
|
|
|
| 71 |
|
|
my $payload = $_[ARG0]; |
| 72 |
|
|
print Dumper($payload); |
| 73 |
|
|
#my $result = $heap->{STORAGE}->sendQuery($query); |
| 74 |
|
|
#$kernel->post( $sender => handle_storage_result => $result ); |
| 75 |
|
|
|
| 76 |
|
|
# publish new remote methods on the fly to service following renewal requests from client(s) |
| 77 |
|
|
$kernel->post('IKC', 'publish', 'ServiceRegistrar', [qw( renew_lease )]); |
| 78 |
|
|
|
| 79 |
|
|
# send response back to client session - does it actually work asynchronously? |
| 80 |
|
|
$kernel->post($_[SENDER], 'response', 'REG_OK'); |
| 81 |
|
|
|
| 82 |
|
|
}; |
| 83 |
|
|
|
| 84 |
|
|
sub renew_lease { |
| 85 |
|
|
my ( $self, $kernel, $heap, $sender ) = @_[ OBJECT, KERNEL, HEAP, SENDER ]; |
| 86 |
|
|
|
| 87 |
|
|
my $from = $sender->[0]->{from}->{kernel}; |
| 88 |
|
|
|
| 89 |
|
|
$self->debug("renew_lease: Lease renewal requested from '$from'."); |
| 90 |
|
|
#$self->debug($sender->ID()); |
| 91 |
|
|
#$self->debug($heap->{name}); |
| 92 |
|
|
#print Dumper($sender); |
| 93 |
|
|
#$self->debug($sender->[0]->{from}->{kernel}); |
| 94 |
|
|
|
| 95 |
|
|
# postback value via rsvp-mechanism (see perldoc POE::Component::IKC::Responder) |
| 96 |
|
|
return 'LEASE_OK2'; |
| 97 |
|
|
} |
| 98 |
|
|
|
| 99 |
|
|
|
| 100 |
|
|
sub remote_register { |
| 101 |
|
|
my ( $self, $kernel, $heap, $sender ) = @_[ OBJECT, KERNEL, HEAP, SENDER ]; |
| 102 |
|
|
|
| 103 |
|
|
#$self->debug("catch_event"); |
| 104 |
|
|
|
| 105 |
|
|
my ($name, $real) = @_[ARG1, ARG2]; |
| 106 |
|
|
print "- Remote kernel ", ($real ? '' : "alias "), "$name connected\n"; |
| 107 |
|
|
$heap->{name} = $real if $real; |
| 108 |
|
|
|
| 109 |
|
|
} |
| 110 |
|
|
|
| 111 |
|
|
|
| 112 |
|
|
#------------------------------------------------------------------------------ |
| 113 |
|
|
# This event keeps this POE kernel alive. |
| 114 |
|
|
# (stolen from POE::Component::IKC::Server, but not used 'til now...) |
| 115 |
|
|
sub waste_time |
| 116 |
|
|
{ |
| 117 |
|
|
my($kernel, $heap)=@_[KERNEL, HEAP]; |
| 118 |
|
|
return if $heap->{'is a child'}; |
| 119 |
|
|
|
| 120 |
|
|
unless($heap->{'been told we are parent'}) { |
| 121 |
|
|
warn "$$: Telling everyone we are the parent\n"; |
| 122 |
|
|
$heap->{'been told we are parent'}=1; |
| 123 |
|
|
$kernel->signal($kernel, '__parent'); |
| 124 |
|
|
} |
| 125 |
|
|
if($heap->{'die'}) { |
| 126 |
|
|
DEBUG and warn "$$: Orderly shutdown\n"; |
| 127 |
|
|
} else { |
| 128 |
|
|
$kernel->delay('waste_time', 60); |
| 129 |
|
|
} |
| 130 |
|
|
return; |
| 131 |
|
|
} |
| 132 |
|
|
|
| 133 |
|
|
1; |
| 134 |
|
|
__END__ |