1 |
=head1 NAME |
2 |
|
3 |
AnyEvent::MP::Global - some network-global services |
4 |
|
5 |
=head1 SYNOPSIS |
6 |
|
7 |
use AnyEvent::MP::Global; |
8 |
|
9 |
=head1 DESCRIPTION |
10 |
|
11 |
This module maintains a fully-meshed network between global nodes and |
12 |
tries to have connections with all nodes in the network. |
13 |
|
14 |
It also manages named port groups - ports can register themselves in any |
15 |
number of groups that will be available network-wide, which is great for |
16 |
discovering services. |
17 |
|
18 |
Running it on one node will automatically run it on all nodes, although, |
19 |
at the moment, the global service is started by default anyways. |
20 |
|
21 |
=head1 GLOBALS AND FUNCTIONS |
22 |
|
23 |
=over 4 |
24 |
|
25 |
=cut |
26 |
|
27 |
package AnyEvent::MP::Global; |
28 |
|
29 |
use common::sense; |
30 |
use Carp (); |
31 |
|
32 |
use AnyEvent (); |
33 |
use AnyEvent::Util (); |
34 |
|
35 |
use AnyEvent::MP; |
36 |
use AnyEvent::MP::Kernel; |
37 |
use AnyEvent::MP::Transport (); |
38 |
|
39 |
use base "Exporter"; |
40 |
|
41 |
our @EXPORT = qw( |
42 |
grp_reg |
43 |
grp_get |
44 |
grp_mon |
45 |
); |
46 |
|
47 |
$AnyEvent::MP::Kernel::WARN->(7, "starting global service."); |
48 |
|
49 |
############################################################################# |
50 |
# node protocol parts for global nodes |
51 |
|
52 |
{ |
53 |
package AnyEvent::MP::Kernel; |
54 |
|
55 |
# TODO: this is ugly, maybe this should go into MP::Kernel or a separate module #d# |
56 |
|
57 |
our $GLOBAL; |
58 |
our $MASTER; |
59 |
our $NODE_ADDR; |
60 |
our $GLOBAL_ADDR; |
61 |
our $NODE; |
62 |
our %GLOBAL_SLAVE; |
63 |
our $GLOBAL_MON; |
64 |
our $LISTENER; |
65 |
|
66 |
# switch to global mode |
67 |
$GLOBAL = 1; |
68 |
$MASTER = $NODE; |
69 |
undef $GLOBAL_MON; |
70 |
|
71 |
$GLOBAL_ADDR->{$NODE} = $LISTENER; |
72 |
|
73 |
$GLOBAL_MON = mon_nodes sub { |
74 |
return if $_[1]; |
75 |
|
76 |
delete $NODE_ADDR->{$_[0]}; |
77 |
|
78 |
if (delete $GLOBAL_ADDR->{$_[0]}) { |
79 |
# if the node is global, tell our slaves |
80 |
|
81 |
our %GLOBAL_SLAVE; # ugh, will be in AnyEvent::MP::Global |
82 |
snd $_, g_del => $_[0] |
83 |
for keys %GLOBAL_SLAVE; |
84 |
} |
85 |
}; |
86 |
|
87 |
# tell everybody who connects that we are a global node |
88 |
push @AnyEvent::MP::Transport::HOOK_GREET, sub { |
89 |
$_[0]{local_greeting}{global} = 1; |
90 |
}; |
91 |
|
92 |
# tell every global node that connects that we are global too |
93 |
push @AnyEvent::MP::Transport::HOOK_CONNECT, sub { |
94 |
snd $_[0], g_add => $NODE, $LISTENER |
95 |
if $_[0]{remote_greeting}{global}; |
96 |
}; |
97 |
|
98 |
# tell everybody else that we are global now |
99 |
snd $_ => g_add => $NODE, $LISTENER |
100 |
for up_nodes; |
101 |
} |
102 |
|
103 |
=item $guard = grp_reg $group, $port |
104 |
|
105 |
Register the given (local!) port in the named global group C<$group>. |
106 |
|
107 |
The port will be unregistered automatically when the port is destroyed. |
108 |
|
109 |
When not called in void context, then a guard object will be returned that |
110 |
will also cause the name to be unregistered when destroyed. |
111 |
|
112 |
=cut |
113 |
|
114 |
# register local port |
115 |
sub grp_reg($$) { |
116 |
my ($group, $port) = @_; |
117 |
|
118 |
port_is_local $port |
119 |
or Carp::croak "AnyEvent::MP::Global::grp_reg can only be called for local ports, caught"; |
120 |
|
121 |
defined wantarray && AnyEvent::Util::guard { unregister ($port, $group) } |
122 |
} |
123 |
|
124 |
=item $ports = grp_get $group |
125 |
|
126 |
Returns all the ports currently registered to the given group (as |
127 |
read-only(!) array reference). When the group has no registered members, |
128 |
return C<undef>. |
129 |
|
130 |
=cut |
131 |
|
132 |
sub grp_get($) { |
133 |
} |
134 |
|
135 |
=item $guard = grp_mon $group, $callback->($ports, $add, $del) |
136 |
|
137 |
Installs a monitor on the given group. Each time there is a change it |
138 |
will be called with the current group members as an arrayref as the |
139 |
first argument. The second argument only contains ports added, the third |
140 |
argument only ports removed. |
141 |
|
142 |
Unlike C<grp_get>, all three arguments will always be array-refs, even if |
143 |
the array is empty. None of the arrays must be modified in any way. |
144 |
|
145 |
The first invocation will be with the first two arguments set to the |
146 |
current members, as if all of them were just added, but only when the |
147 |
group is actually non-empty. |
148 |
|
149 |
Optionally returns a guard object that uninstalls the watcher when it is |
150 |
destroyed. |
151 |
|
152 |
=cut |
153 |
|
154 |
sub grp_mon($$) { |
155 |
my ($grp, $cb) = @_; |
156 |
} |
157 |
|
158 |
=back |
159 |
|
160 |
=head1 SEE ALSO |
161 |
|
162 |
L<AnyEvent::MP>. |
163 |
|
164 |
=head1 AUTHOR |
165 |
|
166 |
Marc Lehmann <schmorp@schmorp.de> |
167 |
http://home.schmorp.de/ |
168 |
|
169 |
=cut |
170 |
|
171 |
1 |
172 |
|