ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/MP/Kernel.pm
(Generate patch)

Comparing AnyEvent-MP/MP/Kernel.pm (file contents):
Revision 1.2 by root, Wed Aug 12 22:03:15 2009 UTC vs.
Revision 1.3 by root, Thu Aug 13 00:23:36 2009 UTC

1=head1 NAME 1=head1 NAME
2 2
3AnyEvent::MP::Kernel - basis for AnyEvent::MP and Coro::MP 3AnyEvent::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
11This module provides most of the basic functionality of AnyEvent::MP, 11This module provides most of the basic functionality of AnyEvent::MP,
12exposed through higher level interfaces such as L<AnyEvent::MP> and 12exposed through higher level interfaces such as L<AnyEvent::MP> and
13L<Coro::MP>. 13L<Coro::MP>.
14 14
15=head1 GLOBALS 15This module is mainly of interest when knowledge about connectivity,
16connected 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
99sub gen_uniq { 102sub 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
108A boolean indicating whether this is a full/public node, which can create
109and accept direct connections form othe rnodes.
110
111=item $AnyEvent::MP::Kernel::SLAVE
112
113A boolean indicating whether this node is a slave node, i.e. does most of it's
114message sending/receiving through some master node.
115
116=item $AnyEvent::MP::Kernel::MASTER
117
118Defined only in slave mode, in which cas eit contains the noderef of the
119master node.
120
121=cut
102 122
103our $PUBLIC = 0; 123our $PUBLIC = 0;
104our $SLAVE = 0; 124our $SLAVE = 0;
105our $MASTER; # master noderef when $SLAVE 125our $MASTER; # master noderef when $SLAVE
106 126
266} 286}
267 287
268sub initialise_node(@) { 288sub 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
352sub _uniq_nodes {
353 my %node;
354
355 @node{values %NODE} = values %NODE;
356
357 values %node;
358}
359
360=item known_nodes
361
362Returns the noderefs of all nodes connected to this node.
363
364=cut
365
366sub known_nodes {
367 map $_->{noderef}, _uniq_nodes
368}
369
370=item up_nodes
371
372Return the noderefs of all nodes that are currently connected (excluding
373the node itself).
374
375=cut
376
377sub up_nodes {
378 map $_->{noderef}, grep $_->{transport}, _uniq_nodes
379}
380
381=item $guard = mon_nodes $callback->($noderef, $is_up, @reason)
382
383Registers a callback that is called each time a node goes up (connection
384is established) or down (connection is lost).
385
386Node up messages can only be followed by node down messages for the same
387node, and vice versa.
388
389The fucntino returns an optional guard which can be used to de-register
390the monitoring callback again.
391
392=cut
393
394our %MON_NODES;
395
396sub 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
404sub _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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines