| 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::LookupClient; |
| 9 |
|
|
|
| 10 |
|
|
use strict; |
| 11 |
|
|
use warnings; |
| 12 |
|
|
|
| 13 |
|
|
use POE qw( Session Component::IKC::Client ); |
| 14 |
|
|
use Data::Dumper; |
| 15 |
|
|
|
| 16 |
|
|
|
| 17 |
|
|
sub new { |
| 18 |
|
|
my $classname = shift; |
| 19 |
|
|
my @args = @_; |
| 20 |
|
|
|
| 21 |
|
|
my $self = {}; |
| 22 |
|
|
bless $self, $classname; |
| 23 |
|
|
|
| 24 |
|
|
$self->{options} = { @args }; |
| 25 |
|
|
|
| 26 |
|
|
#my $event_handler = lookupd->new(); |
| 27 |
|
|
POE::Session->create( |
| 28 |
|
|
object_states => [ |
| 29 |
|
|
$self => [qw( _start _stop boot_intercom )] |
| 30 |
|
|
] |
| 31 |
|
|
); |
| 32 |
|
|
|
| 33 |
|
|
} |
| 34 |
|
|
|
| 35 |
|
|
# This is not a POE method. It's a plain OO one. |
| 36 |
|
|
sub debug { |
| 37 |
|
|
my $self = shift; |
| 38 |
|
|
my $msg = shift; |
| 39 |
|
|
$msg ||= ''; |
| 40 |
|
|
print STDERR __PACKAGE__ . ": " . $msg, "\n"; |
| 41 |
|
|
} |
| 42 |
|
|
|
| 43 |
|
|
# Controller's event handlers |
| 44 |
|
|
|
| 45 |
|
|
sub _start { |
| 46 |
|
|
my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ]; |
| 47 |
|
|
$self->debug("_start"); |
| 48 |
|
|
|
| 49 |
|
|
#$kernel->alias_set("controller"); |
| 50 |
|
|
|
| 51 |
|
|
#$kernel->post( controller => 'start_daemon' ); |
| 52 |
|
|
#$kernel->yield( 'boot_storage' ); |
| 53 |
|
|
$kernel->yield( 'boot_intercom' ); |
| 54 |
|
|
#$_[HEAP]->{subsession} = POE::Session->create( inline_states => { _start => sub { print "WORKER!", "\n"; } } ); |
| 55 |
|
|
#$_[HEAP]->{subsession}->yield( |
| 56 |
|
|
|
| 57 |
|
|
}; |
| 58 |
|
|
|
| 59 |
|
|
sub _stop { |
| 60 |
|
|
my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ]; |
| 61 |
|
|
$self->debug("_stop"); |
| 62 |
|
|
}; |
| 63 |
|
|
|
| 64 |
|
|
sub boot_intercom { |
| 65 |
|
|
|
| 66 |
|
|
my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ]; |
| 67 |
|
|
$self->debug("boot_intercom"); |
| 68 |
|
|
|
| 69 |
|
|
# Client component - encapsulates some session(s) and/or wheel(s)? |
| 70 |
|
|
|
| 71 |
|
|
my $host = "localhost"; |
| 72 |
|
|
#create_ikc_client( host => $host, port => 30, name => 'Client', on_connect => $self->{options}->{on_connect} ); |
| 73 |
|
|
create_ikc_client( |
| 74 |
|
|
host => $host, |
| 75 |
|
|
port => 30, |
| 76 |
|
|
name => 'Client', |
| 77 |
|
|
on_connect => sub { $self->build(); }, |
| 78 |
|
|
subscribe => [qw( poe://LookupService/ServiceRegistrar/ )], |
| 79 |
|
|
); |
| 80 |
|
|
|
| 81 |
|
|
}; |
| 82 |
|
|
|
| 83 |
|
|
sub build { |
| 84 |
|
|
my $self = shift; |
| 85 |
|
|
#print "BUILD", "\n"; |
| 86 |
|
|
# create sessions that depend on the foreign kernel. |
| 87 |
|
|
POE::Component::LookupClient::Session->new(); |
| 88 |
|
|
} |
| 89 |
|
|
|
| 90 |
|
|
|
| 91 |
|
|
|
| 92 |
|
|
package POE::Component::LookupClient::Session; |
| 93 |
|
|
|
| 94 |
|
|
use strict; |
| 95 |
|
|
use warnings; |
| 96 |
|
|
|
| 97 |
|
|
use POE qw( Session ); |
| 98 |
|
|
use Data::Dumper; |
| 99 |
|
|
|
| 100 |
|
|
|
| 101 |
|
|
sub new { |
| 102 |
|
|
my $classname = shift; |
| 103 |
|
|
my @args = @_; |
| 104 |
|
|
|
| 105 |
|
|
my $self = {}; |
| 106 |
|
|
bless $self, $classname; |
| 107 |
|
|
|
| 108 |
|
|
$self->{options} = { @args }; |
| 109 |
|
|
|
| 110 |
|
|
POE::Session->create( |
| 111 |
|
|
object_states => [ |
| 112 |
|
|
$self => [qw( _start _stop response register_lease renew_lease )] |
| 113 |
|
|
] |
| 114 |
|
|
); |
| 115 |
|
|
|
| 116 |
|
|
} |
| 117 |
|
|
|
| 118 |
|
|
|
| 119 |
|
|
# This is not a POE method. It's a plain OO one. |
| 120 |
|
|
sub debug { |
| 121 |
|
|
my $self = shift; |
| 122 |
|
|
my $msg = shift; |
| 123 |
|
|
$msg ||= ''; |
| 124 |
|
|
print STDERR __PACKAGE__ . ": " . $msg, "\n"; |
| 125 |
|
|
} |
| 126 |
|
|
|
| 127 |
|
|
# Controller's event handlers |
| 128 |
|
|
|
| 129 |
|
|
sub _start { |
| 130 |
|
|
my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ]; |
| 131 |
|
|
$self->debug("_start"); |
| 132 |
|
|
|
| 133 |
|
|
#$kernel->alias_set(''); |
| 134 |
|
|
#$kernel->post(); |
| 135 |
|
|
|
| 136 |
|
|
# set up communication channel for asynchronous responses |
| 137 |
|
|
$kernel->alias_set('DeviceClient'); |
| 138 |
|
|
$kernel->post('IKC', 'publish', 'DeviceClient', [qw( response )]); |
| 139 |
|
|
|
| 140 |
|
|
# try to register on startup |
| 141 |
|
|
$kernel->yield('register_lease'); |
| 142 |
|
|
|
| 143 |
|
|
}; |
| 144 |
|
|
|
| 145 |
|
|
sub _stop { |
| 146 |
|
|
my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ]; |
| 147 |
|
|
$self->debug("_stop"); |
| 148 |
|
|
}; |
| 149 |
|
|
|
| 150 |
|
|
# Main response dispatcher, this should dispatch to local states programmatically. |
| 151 |
|
|
sub response { |
| 152 |
|
|
my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ]; |
| 153 |
|
|
#$self->debug("response"); |
| 154 |
|
|
|
| 155 |
|
|
my $payload = $_[ARG0]; |
| 156 |
|
|
$payload ||= ''; |
| 157 |
|
|
|
| 158 |
|
|
# If registration succeeded, start the renewal cycle. |
| 159 |
|
|
if ($payload eq 'REG_OK') { |
| 160 |
|
|
$self->debug("Starting lease loop."); |
| 161 |
|
|
$kernel->yield( 'renew_lease' ); |
| 162 |
|
|
|
| 163 |
|
|
} elsif ($payload eq 'LEASE_OK') { |
| 164 |
|
|
$self->debug("Received 'LEASE_OK'."); |
| 165 |
|
|
#$kernel->yield( 'renew_lease' ); |
| 166 |
|
|
|
| 167 |
|
|
# 1. otherwise dump anything sent to us for debugging purposes |
| 168 |
|
|
# 2. destroy lease in any case on unknown payload |
| 169 |
|
|
} else { |
| 170 |
|
|
#print Dumper($payload); |
| 171 |
|
|
$heap->{'destroy lease'} = 1; |
| 172 |
|
|
|
| 173 |
|
|
} |
| 174 |
|
|
|
| 175 |
|
|
}; |
| 176 |
|
|
|
| 177 |
|
|
sub register_lease { |
| 178 |
|
|
my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ]; |
| 179 |
|
|
$self->debug("register_lease"); |
| 180 |
|
|
my $ONE_arg = "Hello World!"; |
| 181 |
|
|
|
| 182 |
|
|
# V1 - without subscription |
| 183 |
|
|
#$kernel->post('IKC', 'post', "poe://LookupService/ServiceRegistrar/register_lease", $ONE_arg); |
| 184 |
|
|
# V2 - with subscription |
| 185 |
|
|
$kernel->post( "poe://LookupService/ServiceRegistrar", "register_lease", $ONE_arg); |
| 186 |
|
|
} |
| 187 |
|
|
|
| 188 |
|
|
sub renew_lease { |
| 189 |
|
|
my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ]; |
| 190 |
|
|
|
| 191 |
|
|
if ($heap->{'destroy lease'}) { |
| 192 |
|
|
$heap->{'destroy lease'} = 0; |
| 193 |
|
|
$self->debug("destroyed lease"); |
| 194 |
|
|
return; |
| 195 |
|
|
} |
| 196 |
|
|
|
| 197 |
|
|
$self->debug("renew_lease"); |
| 198 |
|
|
|
| 199 |
|
|
my $ONE_arg = ''; |
| 200 |
|
|
#$kernel->post('IKC', 'post', "poe://LookupService/ServiceRegistrar/renew_lease", $ONE_arg); |
| 201 |
|
|
# V1 - without subscription |
| 202 |
|
|
$kernel->post('IKC', 'call', "poe://LookupService/ServiceRegistrar/renew_lease", $ONE_arg, 'poe:response'); |
| 203 |
|
|
# V2 - with subscription |
| 204 |
|
|
#my $resp = $kernel->call( "poe://LookupService/ServiceRegistrar", "renew_lease", $ONE_arg); |
| 205 |
|
|
#print $resp, "\n"; |
| 206 |
|
|
|
| 207 |
|
|
# and again... |
| 208 |
|
|
$kernel->delay('renew_lease', 15); |
| 209 |
|
|
} |
| 210 |
|
|
|
| 211 |
|
|
1; |
| 212 |
|
|
__END__ |