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

Comparing cvsroot/AnyEvent-MP/MP.pm (file contents):
Revision 1.10 by root, Sun Aug 2 18:05:43 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
160=item $portid = create_miniport { } 183=item $portid = miniport { my @msg = @_; $finished }
161 184
162Creates a "mini port", that is, a port without much #TODO 185Creates a "mini port", that is, a very lightweight port without any
186pattern matching behind it, and returns its ID.
163 187
164=cut 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.
165 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
166sub create_miniport(&) { 203sub miniport(&) {
167 my $cb = shift; 204 my $cb = shift;
168 my $id = "$AnyEvent::MP::Base::UNIQ." . ++$AnyEvent::MP::Base::ID; 205 my $id = "$AnyEvent::MP::Base::UNIQ." . $AnyEvent::MP::Base::ID++;
169 206
170 $AnyEvent::MP::Base::PORT{$id} = sub { 207 $AnyEvent::MP::Base::PORT{$id} = sub {
171 &$cb 208 &$cb
172 and delete $AnyEvent::MP::Base::PORT{$id}; 209 and del $id;
173 }; 210 };
174 211
175 "$NODE#$id" 212 "$NODE#$id"
176} 213}
177 214
191=cut 228=cut
192 229
193use overload 230use overload
194 '""' => sub { $_[0]{id} }, 231 '""' => sub { $_[0]{id} },
195 fallback => 1; 232 fallback => 1;
233
234sub TO_JSON { $_[0]{id} }
196 235
197=item $port->rcv (type => $callback->($port, @msg)) 236=item $port->rcv (type => $callback->($port, @msg))
198 237
199=item $port->rcv ($smartmatch => $callback->($port, @msg)) 238=item $port->rcv ($smartmatch => $callback->($port, @msg))
200 239
260=cut 299=cut
261 300
262sub destroy { 301sub destroy {
263 my ($self) = @_; 302 my ($self) = @_;
264 303
304 AnyEvent::MP::Base::del $self->{id};
305
265 delete $AnyEvent::MP::Base::WKP{ $self->{wkname} }; 306 delete $AnyEvent::MP::Base::WKP{ $self->{wkname} };
266 307
267 delete $AnyEvent::MP::Base::PORT{$_} 308 delete $AnyEvent::MP::Base::PORT{$_}
268 for @{ $self->{names} }; 309 for @{ $self->{names} };
269} 310}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines