… | |
… | |
46 | |
46 | |
47 | # execute callbacks in $SELF port context |
47 | # execute callbacks in $SELF port context |
48 | my $timer = AE::timer 1, 0, psub { |
48 | my $timer = AE::timer 1, 0, psub { |
49 | die "kill the port, delayed"; |
49 | die "kill the port, delayed"; |
50 | }; |
50 | }; |
|
|
51 | |
|
|
52 | # distributed database - modification |
|
|
53 | db_set $family => $subkey [=> $value] # add a subkey |
|
|
54 | db_del $family => $subkey... # delete one or more subkeys |
|
|
55 | db_reg $family => $port [=> $value] # register a port |
|
|
56 | |
|
|
57 | # distributed database - queries |
|
|
58 | db_family $family => $cb->(\%familyhash) |
|
|
59 | db_keys $family => $cb->(\@keys) |
|
|
60 | db_values $family => $cb->(\@values) |
|
|
61 | |
|
|
62 | # distributed database - monitoring a family |
|
|
63 | db_mon $family => $cb->(\%familyhash, \@added, \@changed, \@deleted) |
51 | |
64 | |
52 | =head1 DESCRIPTION |
65 | =head1 DESCRIPTION |
53 | |
66 | |
54 | This module (-family) implements a simple message passing framework. |
67 | This module (-family) implements a simple message passing framework. |
55 | |
68 | |
… | |
… | |
170 | |
183 | |
171 | package AnyEvent::MP; |
184 | package AnyEvent::MP; |
172 | |
185 | |
173 | use AnyEvent::MP::Config (); |
186 | use AnyEvent::MP::Config (); |
174 | use AnyEvent::MP::Kernel; |
187 | use AnyEvent::MP::Kernel; |
175 | use AnyEvent::MP::Kernel qw(%NODE %PORT %PORT_DATA $UNIQ $RUNIQ $ID); |
188 | use AnyEvent::MP::Kernel qw( |
|
|
189 | %NODE %PORT %PORT_DATA $UNIQ $RUNIQ $ID |
|
|
190 | add_node load_func |
|
|
191 | |
|
|
192 | NODE $NODE |
|
|
193 | configure |
|
|
194 | node_of port_is_local |
|
|
195 | snd kil |
|
|
196 | db_set db_del |
|
|
197 | db_mon db_family db_keys db_values |
|
|
198 | ); |
176 | |
199 | |
177 | use common::sense; |
200 | use common::sense; |
178 | |
201 | |
179 | use Carp (); |
202 | use Carp (); |
180 | |
203 | |
… | |
… | |
184 | use base "Exporter"; |
207 | use base "Exporter"; |
185 | |
208 | |
186 | our $VERSION = $AnyEvent::MP::Config::VERSION; |
209 | our $VERSION = $AnyEvent::MP::Config::VERSION; |
187 | |
210 | |
188 | our @EXPORT = qw( |
211 | our @EXPORT = qw( |
189 | NODE $NODE *SELF node_of after |
212 | NODE $NODE |
190 | configure |
213 | configure |
|
|
214 | node_of port_is_local |
|
|
215 | snd kil |
|
|
216 | db_set db_del |
|
|
217 | db_mon db_family db_keys db_values |
|
|
218 | |
|
|
219 | *SELF |
|
|
220 | |
191 | snd rcv mon mon_guard kil psub peval spawn cal |
221 | port rcv mon mon_guard psub peval spawn cal |
192 | port |
|
|
193 | db_set db_del db_reg |
222 | db_set db_del db_reg |
194 | db_mon db_family db_keys db_values |
223 | db_mon db_family db_keys db_values |
|
|
224 | |
|
|
225 | after |
195 | ); |
226 | ); |
196 | |
227 | |
197 | our $SELF; |
228 | our $SELF; |
198 | |
229 | |
199 | sub _self_die() { |
230 | sub _self_die() { |
… | |
… | |
210 | |
241 | |
211 | =item $nodeid = node_of $port |
242 | =item $nodeid = node_of $port |
212 | |
243 | |
213 | Extracts and returns the node ID from a port ID or a node ID. |
244 | Extracts and returns the node ID from a port ID or a node ID. |
214 | |
245 | |
|
|
246 | =item $is_local = port_is_local $port |
|
|
247 | |
|
|
248 | Returns true iff the port is a local port. |
|
|
249 | |
215 | =item configure $profile, key => value... |
250 | =item configure $profile, key => value... |
216 | |
251 | |
217 | =item configure key => value... |
252 | =item configure key => value... |
218 | |
253 | |
219 | Before a node can talk to other nodes on the network (i.e. enter |
254 | Before a node can talk to other nodes on the network (i.e. enter |
… | |
… | |
238 | =item force => $boolean (default false) |
273 | =item force => $boolean (default false) |
239 | |
274 | |
240 | IF true, then the values specified in the C<configure> will take |
275 | IF true, then the values specified in the C<configure> will take |
241 | precedence over any values configured via the rc file. The default is for |
276 | precedence over any values configured via the rc file. The default is for |
242 | the rc file to override any options specified in the program. |
277 | the rc file to override any options specified in the program. |
243 | |
|
|
244 | =item secure => $pass->(@msg) |
|
|
245 | |
|
|
246 | In addition to specifying a boolean, you can specify a code reference that |
|
|
247 | is called for every code execution attempt - the execution request is |
|
|
248 | granted iff the callback returns a true value. |
|
|
249 | |
|
|
250 | Most of the time the callback should look only at |
|
|
251 | C<$AnyEvent::MP::Kernel::SRCNODE> to make a decision, and not at the |
|
|
252 | actual message (which can be about anything, and is mostly provided for |
|
|
253 | diagnostic purposes). |
|
|
254 | |
|
|
255 | See F<semp setsecure> for more info. |
|
|
256 | |
278 | |
257 | =back |
279 | =back |
258 | |
280 | |
259 | =over 4 |
281 | =over 4 |
260 | |
282 | |
… | |
… | |
464 | |
486 | |
465 | sub rcv($@) { |
487 | sub rcv($@) { |
466 | my $port = shift; |
488 | my $port = shift; |
467 | my ($nodeid, $portid) = split /#/, $port, 2; |
489 | my ($nodeid, $portid) = split /#/, $port, 2; |
468 | |
490 | |
469 | $NODE{$nodeid} == $NODE{""} |
491 | $nodeid eq $NODE |
470 | or Carp::croak "$port: rcv can only be called on local ports, caught"; |
492 | or Carp::croak "$port: rcv can only be called on local ports, caught"; |
471 | |
493 | |
472 | while (@_) { |
494 | while (@_) { |
473 | if (ref $_[0]) { |
495 | if (ref $_[0]) { |
474 | if (my $self = $PORT_DATA{$portid}) { |
496 | if (my $self = $PORT_DATA{$portid}) { |
… | |
… | |
1033 | =item db_values $family => $cb->(\@values) |
1055 | =item db_values $family => $cb->(\@values) |
1034 | |
1056 | |
1035 | Same as C<db_family>, except it only queries the family I<values> and passes them |
1057 | Same as C<db_family>, except it only queries the family I<values> and passes them |
1036 | as array reference to the callback. |
1058 | as array reference to the callback. |
1037 | |
1059 | |
1038 | =item $guard = db_mon $family => $cb->($familyhash, \@added, \@changed, \@deleted) |
1060 | =item $guard = db_mon $family => $cb->(\%familyhash, \@added, \@changed, \@deleted) |
1039 | |
1061 | |
1040 | Creates a monitor on the given database family. Each time a key is set |
1062 | Creates a monitor on the given database family. Each time a key is set |
1041 | or or is deleted the callback is called with a hash containing the |
1063 | or or is deleted the callback is called with a hash containing the |
1042 | database family and three lists of added, changed and deleted subkeys, |
1064 | database family and three lists of added, changed and deleted subkeys, |
1043 | respectively. If no keys have changed then the array reference might be |
1065 | respectively. If no keys have changed then the array reference might be |