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.9 by root, Sun Aug 2 15:47:04 2009 UTC vs.
Revision 1.20 by root, Mon Aug 3 22:05:55 2009 UTC

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 _any_ 89 NODE $NODE $PORT snd rcv mon del _any_
90 create_port create_port_on 90 create_port create_port_on
91 create_miniport
91 become_slave become_public 92 become_slave become_public
92); 93);
93 94
94=item NODE / $NODE 95=item NODE / $NODE
95 96
117JSON is used, then only strings, numbers and arrays and hashes consisting 118JSON is used, then only strings, numbers and arrays and hashes consisting
118of those are allowed (no objects). When Storable is used, then anything 119of those are allowed (no objects). When Storable is used, then anything
119that Storable can serialise and deserialise is allowed, and for the local 120that Storable can serialise and deserialise is allowed, and for the local
120node, anything can be passed. 121node, anything can be passed.
121 122
123=item $guard = mon $portid, $cb->()
124
125Monitor the given port and call the given callback when the port is
126destroyed or connection to it's node is lost.
127
128#TODO
129
130=cut
131
132sub mon {
133 my ($noderef, $port) = split /#/, shift, 2;
134
135 my $node = AnyEvent::MP::Base::add_node $noderef;
136
137 my $cb = shift;
138
139 $node->monitor ($port, $cb);
140
141 defined wantarray
142 and AnyEvent::Util::guard { $node->unmonitor ($port, $cb) }
143}
144
122=item $local_port = create_port 145=item $local_port = create_port
123 146
124Create a new local port object. See the next section for allowed methods. 147Create a new local port object. See the next section for allowed methods.
125 148
126=cut 149=cut
127 150
128sub create_port { 151sub create_port {
129 my $id = "$AnyEvent::MP::Base::UNIQ." . ++$AnyEvent::MP::Base::ID; 152 my $id = "$AnyEvent::MP::Base::UNIQ." . $AnyEvent::MP::Base::ID++;
130 153
131 my $self = bless { 154 my $self = bless {
132 id => "$NODE#$id", 155 id => "$NODE#$id",
133 names => [$id], 156 names => [$id],
134 }, "AnyEvent::MP::Port"; 157 }, "AnyEvent::MP::Port";
155 }; 178 };
156 179
157 $self 180 $self
158} 181}
159 182
183=item $portid = miniport { my @msg = @_; $finished }
184
185Creates a "mini port", that is, a very lightweight port without any
186pattern matching behind it, and returns its ID.
187
188The block will be called for every message received on the port. When the
189callback returns a true value its job is considered "done" and the port
190will be destroyed. Otherwise it will stay alive.
191
192The message will be passed as-is, no extra argument (i.e. no port id) will
193be passed to the callback.
194
195If you need the local port id in the callback, this works nicely:
196
197 my $port; $port = miniport {
198 snd $otherport, reply => $port;
199 };
200
201=cut
202
203sub miniport(&) {
204 my $cb = shift;
205 my $id = "$AnyEvent::MP::Base::UNIQ." . $AnyEvent::MP::Base::ID++;
206
207 $AnyEvent::MP::Base::PORT{$id} = sub {
208 &$cb
209 and del $id;
210 };
211
212 "$NODE#$id"
213}
214
160package AnyEvent::MP::Port; 215package AnyEvent::MP::Port;
161 216
162=back 217=back
163 218
164=head1 METHODS FOR PORT OBJECTS 219=head1 METHODS FOR PORT OBJECTS
173=cut 228=cut
174 229
175use overload 230use overload
176 '""' => sub { $_[0]{id} }, 231 '""' => sub { $_[0]{id} },
177 fallback => 1; 232 fallback => 1;
233
234sub TO_JSON { $_[0]{id} }
178 235
179=item $port->rcv (type => $callback->($port, @msg)) 236=item $port->rcv (type => $callback->($port, @msg))
180 237
181=item $port->rcv ($smartmatch => $callback->($port, @msg)) 238=item $port->rcv ($smartmatch => $callback->($port, @msg))
182 239
242=cut 299=cut
243 300
244sub destroy { 301sub destroy {
245 my ($self) = @_; 302 my ($self) = @_;
246 303
304 AnyEvent::MP::Base::del $self->{id};
305
247 delete $AnyEvent::MP::Base::WKP{ $self->{wkname} }; 306 delete $AnyEvent::MP::Base::WKP{ $self->{wkname} };
248 307
249 delete $AnyEvent::MP::Base::PORT{$_} 308 delete $AnyEvent::MP::Base::PORT{$_}
250 for @{ $self->{names} }; 309 for @{ $self->{names} };
251} 310}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines