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

Comparing AnyEvent-MP/MP.pm (file contents):
Revision 1.20 by root, Mon Aug 3 22:05:55 2009 UTC vs.
Revision 1.21 by root, Tue Aug 4 14:10:51 2009 UTC

28 28
29Despite its simplicity, you can securely message other processes running 29Despite its simplicity, you can securely message other processes running
30on the same or other hosts. 30on the same or other hosts.
31 31
32At the moment, this module family is severly brokena nd underdocumented, 32At the moment, this module family is severly brokena nd underdocumented,
33so do not use. This was uploaded mainly to resreve the CPAN namespace - 33so do not use. This was uploaded mainly to reserve the CPAN namespace -
34stay tuned! 34stay tuned!
35 35
36=head1 CONCEPTS 36=head1 CONCEPTS
37 37
38=over 4 38=over 4
84 84
85use base "Exporter"; 85use base "Exporter";
86 86
87our $VERSION = '0.02'; 87our $VERSION = '0.02';
88our @EXPORT = qw( 88our @EXPORT = qw(
89 NODE $NODE $PORT snd rcv mon del _any_ 89 NODE $NODE $PORT snd rcv mon kil _any_
90 create_port create_port_on 90 create_port create_port_on
91 create_miniport 91 miniport
92 become_slave become_public 92 become_slave become_public
93); 93);
94 94
95=item NODE / $NODE 95=item NODE / $NODE
96 96
120that Storable can serialise and deserialise is allowed, and for the local 120that Storable can serialise and deserialise is allowed, and for the local
121node, anything can be passed. 121node, anything can be passed.
122 122
123=item $guard = mon $portid, $cb->() 123=item $guard = mon $portid, $cb->()
124 124
125=item $guard = mon $portid, $otherport
126
127=item $guard = mon $portid, $otherport, @msg
128
125Monitor the given port and call the given callback when the port is 129Monitor the given port and call the given callback when the port is
126destroyed or connection to it's node is lost. 130destroyed or connection to it's node is lost.
127 131
128#TODO 132#TODO
129 133
130=cut 134=cut
131 135
132sub mon { 136sub mon {
133 my ($noderef, $port) = split /#/, shift, 2; 137 my ($noderef, $port, $cb) = ((split /#/, shift, 2), shift);
134 138
135 my $node = AnyEvent::MP::Base::add_node $noderef; 139 my $node = AnyEvent::MP::Base::add_node $noderef;
136 140
137 my $cb = shift; 141 #TODO: ports must not be references
142 if (!ref $cb or "AnyEvent::MP::Port" eq ref $cb) {
143 if (@_) {
144 # send a kill info message
145 my (@msg) = ($cb, @_);
146 $cb = sub { snd @msg, @_ };
147 } else {
148 # simply kill other port
149 my $port = $cb;
150 $cb = sub { kil $port, @_ };
151 }
152 }
138 153
139 $node->monitor ($port, $cb); 154 $node->monitor ($port, $cb);
140 155
141 defined wantarray 156 defined wantarray
142 and AnyEvent::Util::guard { $node->unmonitor ($port, $cb) } 157 and AnyEvent::Util::guard { $node->unmonitor ($port, $cb) }
143} 158}
144 159
160=item $guard = mon_guard $port, $ref, $ref...
161
162Monitors the given C<$port> and keeps the passed references. When the port
163is killed, the references will be freed.
164
165Optionally returns a guard that will stop the monitoring.
166
167This function is useful when you create e.g. timers or other watchers and
168want to free them when the port gets killed:
169
170 $port->rcv (start => sub {
171 my $timer; $timer = mon_guard $port, AE::timer 1, 1, sub {
172 undef $timer if 0.9 < rand;
173 });
174 });
175
176=cut
177
178sub mon_guard {
179 my ($port, @refs) = @_;
180
181 mon $port, sub { 0 && @refs }
182}
183
145=item $local_port = create_port 184=item $local_port = create_port
146 185
147Create a new local port object. See the next section for allowed methods. 186Create a new local port object. See the next section for allowed methods.
148 187
149=cut 188=cut
151sub create_port { 190sub create_port {
152 my $id = "$AnyEvent::MP::Base::UNIQ." . $AnyEvent::MP::Base::ID++; 191 my $id = "$AnyEvent::MP::Base::UNIQ." . $AnyEvent::MP::Base::ID++;
153 192
154 my $self = bless { 193 my $self = bless {
155 id => "$NODE#$id", 194 id => "$NODE#$id",
156 names => [$id],
157 }, "AnyEvent::MP::Port"; 195 }, "AnyEvent::MP::Port";
158 196
159 $AnyEvent::MP::Base::PORT{$id} = sub { 197 $AnyEvent::MP::Base::PORT{$id} = sub {
160 unshift @_, $self; 198 unshift @_, $self;
161 199
204 my $cb = shift; 242 my $cb = shift;
205 my $id = "$AnyEvent::MP::Base::UNIQ." . $AnyEvent::MP::Base::ID++; 243 my $id = "$AnyEvent::MP::Base::UNIQ." . $AnyEvent::MP::Base::ID++;
206 244
207 $AnyEvent::MP::Base::PORT{$id} = sub { 245 $AnyEvent::MP::Base::PORT{$id} = sub {
208 &$cb 246 &$cb
209 and del $id; 247 and kil $id;
210 }; 248 };
211 249
212 "$NODE#$id" 250 "$NODE#$id"
213} 251}
214 252
291 329
292=item $port->destroy 330=item $port->destroy
293 331
294Explicitly destroy/remove/nuke/vaporise the port. 332Explicitly destroy/remove/nuke/vaporise the port.
295 333
296Ports are normally kept alive by there mere existance alone, and need to 334Ports are normally kept alive by their mere existance alone, and need to
297be destroyed explicitly. 335be destroyed explicitly.
298 336
299=cut 337=cut
300 338
301sub destroy { 339sub destroy {
302 my ($self) = @_; 340 my ($self) = @_;
303 341
304 AnyEvent::MP::Base::del $self->{id};
305
306 delete $AnyEvent::MP::Base::WKP{ $self->{wkname} }; 342 delete $AnyEvent::MP::Base::WKP{ $self->{wkname} };
307 343
308 delete $AnyEvent::MP::Base::PORT{$_} 344 AnyEvent::MP::Base::kil $self->{id};
309 for @{ $self->{names} };
310} 345}
311 346
312=back 347=back
313 348
314=head1 FUNCTIONS FOR NODES 349=head1 FUNCTIONS FOR NODES

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines