1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | AnyEvent::MP::Kernel - basis for AnyEvent::MP and Coro::MP |
3 | AnyEvent::MP::Kernel - the actual message passing kernel |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | # use AnyEvent::MP or Coro::MP instead |
7 | use AnyEvent::MP::Kernel; |
8 | |
8 | |
9 | =head1 DESCRIPTION |
9 | =head1 DESCRIPTION |
10 | |
10 | |
11 | This module provides most of the basic functionality of AnyEvent::MP, |
11 | This module provides most of the basic functionality of AnyEvent::MP, |
12 | exposed through higher level interfaces such as L<AnyEvent::MP> and |
12 | exposed through higher level interfaces such as L<AnyEvent::MP> and |
13 | L<Coro::MP>. |
13 | L<Coro::MP>. |
14 | |
14 | |
15 | =head1 GLOBALS |
15 | This module is mainly of interest when knowledge about connectivity, |
|
|
16 | connected nodes etc. are needed. |
|
|
17 | |
|
|
18 | =head1 GLOBALS AND FUNCTIONS |
16 | |
19 | |
17 | =over 4 |
20 | =over 4 |
18 | |
21 | |
19 | =cut |
22 | =cut |
20 | |
23 | |
… | |
… | |
97 | } |
100 | } |
98 | |
101 | |
99 | sub gen_uniq { |
102 | sub gen_uniq { |
100 | asciibits pack "wNa*", $$, time, nonce 2 |
103 | asciibits pack "wNa*", $$, time, nonce 2 |
101 | } |
104 | } |
|
|
105 | |
|
|
106 | =item $AnyEvent::MP::Kernel::PUBLIC |
|
|
107 | |
|
|
108 | A boolean indicating whether this is a full/public node, which can create |
|
|
109 | and accept direct connections form othe rnodes. |
|
|
110 | |
|
|
111 | =item $AnyEvent::MP::Kernel::SLAVE |
|
|
112 | |
|
|
113 | A boolean indicating whether this node is a slave node, i.e. does most of it's |
|
|
114 | message sending/receiving through some master node. |
|
|
115 | |
|
|
116 | =item $AnyEvent::MP::Kernel::MASTER |
|
|
117 | |
|
|
118 | Defined only in slave mode, in which cas eit contains the noderef of the |
|
|
119 | master node. |
|
|
120 | |
|
|
121 | =cut |
102 | |
122 | |
103 | our $PUBLIC = 0; |
123 | our $PUBLIC = 0; |
104 | our $SLAVE = 0; |
124 | our $SLAVE = 0; |
105 | our $MASTER; # master noderef when $SLAVE |
125 | our $MASTER; # master noderef when $SLAVE |
106 | |
126 | |
… | |
… | |
266 | } |
286 | } |
267 | |
287 | |
268 | sub initialise_node(@) { |
288 | sub initialise_node(@) { |
269 | my ($noderef, @others) = @_; |
289 | my ($noderef, @others) = @_; |
270 | |
290 | |
|
|
291 | @others = @{ $AnyEvent::MP::Config::CFG{seeds} } |
|
|
292 | unless @others; |
|
|
293 | |
271 | if ($noderef =~ /^slave\/(.*)$/) { |
294 | if ($noderef =~ /^slave\/(.*)$/) { |
272 | $SLAVE = AE::cv; |
295 | $SLAVE = AE::cv; |
273 | my $name = $1; |
296 | my $name = $1; |
274 | $name = $NODE unless length $name; |
297 | $name = $NODE unless length $name; |
275 | $noderef = AE::cv; |
298 | $noderef = AE::cv; |
… | |
… | |
318 | |
341 | |
319 | $_->send (["", iam => $NODE]) |
342 | $_->send (["", iam => $NODE]) |
320 | for values %NODE; |
343 | for values %NODE; |
321 | |
344 | |
322 | $SLAVE = 1; |
345 | $SLAVE = 1; |
|
|
346 | } |
|
|
347 | } |
|
|
348 | |
|
|
349 | ############################################################################# |
|
|
350 | # node moniotirng and info |
|
|
351 | |
|
|
352 | sub _uniq_nodes { |
|
|
353 | my %node; |
|
|
354 | |
|
|
355 | @node{values %NODE} = values %NODE; |
|
|
356 | |
|
|
357 | values %node; |
|
|
358 | } |
|
|
359 | |
|
|
360 | =item known_nodes |
|
|
361 | |
|
|
362 | Returns the noderefs of all nodes connected to this node. |
|
|
363 | |
|
|
364 | =cut |
|
|
365 | |
|
|
366 | sub known_nodes { |
|
|
367 | map $_->{noderef}, _uniq_nodes |
|
|
368 | } |
|
|
369 | |
|
|
370 | =item up_nodes |
|
|
371 | |
|
|
372 | Return the noderefs of all nodes that are currently connected (excluding |
|
|
373 | the node itself). |
|
|
374 | |
|
|
375 | =cut |
|
|
376 | |
|
|
377 | sub up_nodes { |
|
|
378 | map $_->{noderef}, grep $_->{transport}, _uniq_nodes |
|
|
379 | } |
|
|
380 | |
|
|
381 | =item $guard = mon_nodes $callback->($noderef, $is_up, @reason) |
|
|
382 | |
|
|
383 | Registers a callback that is called each time a node goes up (connection |
|
|
384 | is established) or down (connection is lost). |
|
|
385 | |
|
|
386 | Node up messages can only be followed by node down messages for the same |
|
|
387 | node, and vice versa. |
|
|
388 | |
|
|
389 | The fucntino returns an optional guard which can be used to de-register |
|
|
390 | the monitoring callback again. |
|
|
391 | |
|
|
392 | =cut |
|
|
393 | |
|
|
394 | our %MON_NODES; |
|
|
395 | |
|
|
396 | sub mon_nodes($) { |
|
|
397 | my ($cb) = @_; |
|
|
398 | |
|
|
399 | $MON_NODES{$cb+0} = $cb; |
|
|
400 | |
|
|
401 | wantarray && AnyEvent::Util::guard { delete $MON_NODES{$cb+0} } |
|
|
402 | } |
|
|
403 | |
|
|
404 | sub _inject_nodeevent($$;@) { |
|
|
405 | my ($node, @args) = @_; |
|
|
406 | |
|
|
407 | unshift @args, $node->{noderef}; |
|
|
408 | |
|
|
409 | for my $cb (values %MON_NODES) { |
|
|
410 | eval { $cb->(@args); 1 } |
|
|
411 | or $WARN->($@); |
323 | } |
412 | } |
324 | } |
413 | } |
325 | |
414 | |
326 | ############################################################################# |
415 | ############################################################################# |
327 | # self node code |
416 | # self node code |