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

Comparing AnyEvent-MP/MP/Global.pm (file contents):
Revision 1.14 by root, Fri Aug 28 20:57:42 2009 UTC vs.
Revision 1.15 by root, Fri Aug 28 23:06:33 2009 UTC

6 6
7 use AnyEvent::MP::Global; 7 use AnyEvent::MP::Global;
8 8
9=head1 DESCRIPTION 9=head1 DESCRIPTION
10 10
11This module provides an assortment of network-global functions: group name 11This module maintains a fully-meshed network, if possible, and tries to
12registration and non-local locks. 12ensure that we are connected to at least one other node.
13 13
14It will also try to build and maintain a full mesh of all network nodes. 14It also manages named port groups - ports can register themselves in any
15number of groups that will be available network-wide, which is great for
16discovering services.
15 17
16While it isn't mandatory to run the global services, running it on one 18Running it on one node will automatically run it on all nodes, although,
17node will automatically run it on all nodes. 19at the moment, the global service is started by default anyways.
18 20
19=head1 GLOBALS AND FUNCTIONS 21=head1 GLOBALS AND FUNCTIONS
20 22
21=over 4 23=over 4
22 24
110 while (my ($k, $v) = each %$lreg) { 112 while (my ($k, $v) = each %$lreg) {
111 push @{ $greg{$k} }, @$v; 113 push @{ $greg{$k} }, @$v;
112 } 114 }
113} 115}
114 116
117=item $guard = register $port, $group
118
119Register the given (local!) port in the named global group C<$group>.
120
121The port will be unregistered automatically when the port is destroyed.
122
123When not called in void context, then a guard object will be returned that
124will also cause the name to be unregistered when destroyed.
125
126=cut
127
128# register port from any node
129sub _register {
130 my ($port, $group) = @_;
131
132 push @{ $greg{$group} }, $port;
133}
134
135# unregister port from any node
136sub _unregister {
137 my ($port, $group) = @_;
138
139 @{ $greg{$group} } = grep $_ ne $port, @{ $greg{$group} };
140}
141
142# unregister local port
143sub unregister {
144 my ($port, $group) = @_;
145
146 delete $lmon{"$group\x00$port"};
147 @{ $lreg{$group} } = grep $_ ne $port, @{ $lreg{$group} };
148
149 _unregister $port, $group;
150
151 snd $_, reg0 => $port, $group
152 for values %port;
153}
154
155# register local port
156sub register($$) {
157 my ($port, $group) = @_;
158
159 port_is_local $port
160 or Carp::croak "AnyEvent::MP::Global::register can only be called for local ports, caught";
161
162 $lmon{"$group\x00$port"} = mon $port, sub { unregister $port, $group };
163 push @{ $lreg{$group} }, $port;
164
165 snd $_, reg1 => $port, $group
166 for values %port;
167
168 _register $port, $group;
169
170 wantarray && AnyEvent::Util::guard { unregister $port, $group }
171}
172
115=item $ports = find $group 173=item $ports = find $group
116 174
117Returns all the ports currently registered to the given group (as 175Returns all the ports currently registered to the given group (as
118read-only array reference). When the group has no registered members, 176read-only array reference). When the group has no registered members,
119return C<undef>. 177return C<undef>.
122 180
123sub find($) { 181sub find($) {
124 @{ $greg{$_[0]} } 182 @{ $greg{$_[0]} }
125 ? $greg{$_[0]} 183 ? $greg{$_[0]}
126 : undef 184 : undef
127}
128
129=item $guard = register $port, $group
130
131Register the given (local!) port in the named global group C<$group>.
132
133The port will be unregistered automatically when the port is destroyed.
134
135When not called in void context, then a guard object will be returned that
136will also cause the name to be unregistered when destroyed.
137
138=cut
139
140# register port from any node
141sub _register {
142 my ($port, $group) = @_;
143
144 push @{ $greg{$group} }, $port;
145}
146
147# unregister port from any node
148sub _unregister {
149 my ($port, $group) = @_;
150
151 @{ $greg{$group} } = grep $_ ne $port, @{ $greg{$group} };
152}
153
154# unregister local port
155sub unregister {
156 my ($port, $group) = @_;
157
158 delete $lmon{"$group\x00$port"};
159 @{ $lreg{$group} } = grep $_ ne $port, @{ $lreg{$group} };
160
161 _unregister $port, $group;
162
163 snd $_, reg0 => $port, $group
164 for values %port;
165}
166
167# register local port
168sub register($$) {
169 my ($port, $group) = @_;
170
171 port_is_local $port
172 or Carp::croak "AnyEvent::MP::Global::register can only be called for local ports, caught";
173
174 $lmon{"$group\x00$port"} = mon $port, sub { unregister $port, $group };
175 push @{ $lreg{$group} }, $port;
176
177 snd $_, reg1 => $port, $group
178 for values %port;
179
180 _register $port, $group;
181
182 wantarray && AnyEvent::Util::guard { unregister $port, $group }
183} 185}
184 186
185sub start_node { 187sub start_node {
186 my ($node) = @_; 188 my ($node) = @_;
187 189

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines