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.15 by root, Sun Aug 2 19:25:27 2009 UTC vs.
Revision 1.18 by root, Mon Aug 3 21:35:03 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 create_miniport
92 become_slave become_public 92 become_slave become_public
93); 93);
94 94
118JSON is used, then only strings, numbers and arrays and hashes consisting 118JSON is used, then only strings, numbers and arrays and hashes consisting
119of those are allowed (no objects). When Storable is used, then anything 119of those are allowed (no objects). When Storable is used, then anything
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 mon $portid, sub { }
124
125#TODO monitor the given port
126
127=cut
128
129sub mon {
130 my ($noderef, $port) = split /#/, shift, 2;
131
132 my $node = AnyEvent::MP::Base::add_node $noderef;
133
134 my $cb = shift;
135
136 $node->monitor ($port, $cb);
137
138 defined wantarray
139 and AnyEvent::Util::guard { $node->unmonitor ($port, $cb) }
140}
141
123=item $local_port = create_port 142=item $local_port = create_port
124 143
125Create a new local port object. See the next section for allowed methods. 144Create a new local port object. See the next section for allowed methods.
126 145
127=cut 146=cut
128 147
129sub create_port { 148sub create_port {
130 my $id = "$AnyEvent::MP::Base::UNIQ." . ++$AnyEvent::MP::Base::ID; 149 my $id = "$AnyEvent::MP::Base::UNIQ." . $AnyEvent::MP::Base::ID++;
131 150
132 my $self = bless { 151 my $self = bless {
133 id => "$NODE#$id", 152 id => "$NODE#$id",
134 names => [$id], 153 names => [$id],
135 }, "AnyEvent::MP::Port"; 154 }, "AnyEvent::MP::Port";
165 184
166The block will be called for every message received on the port. When the 185The block will be called for every message received on the port. When the
167callback returns a true value its job is considered "done" and the port 186callback returns a true value its job is considered "done" and the port
168will be destroyed. Otherwise it will stay alive. 187will be destroyed. Otherwise it will stay alive.
169 188
170The message will be passed as-is, no extra argument (ie.. no port id) will 189The message will be passed as-is, no extra argument (i.e. no port id) will
171be passed to the callback. 190be passed to the callback.
172 191
173If you need the local port id in the callback, this works nicely: 192If you need the local port id in the callback, this works nicely:
174 193
175 my $port; $port = miniport { 194 my $port; $port = miniport {
178 197
179=cut 198=cut
180 199
181sub miniport(&) { 200sub miniport(&) {
182 my $cb = shift; 201 my $cb = shift;
183 my $id = "$AnyEvent::MP::Base::UNIQ." . ++$AnyEvent::MP::Base::ID; 202 my $id = "$AnyEvent::MP::Base::UNIQ." . $AnyEvent::MP::Base::ID++;
184 203
185 $AnyEvent::MP::Base::PORT{$id} = sub { 204 $AnyEvent::MP::Base::PORT{$id} = sub {
186 &$cb 205 &$cb
187 and delete $AnyEvent::MP::Base::PORT{$id}; 206 and delete $AnyEvent::MP::Base::PORT{$id};
188 }; 207 };
206=cut 225=cut
207 226
208use overload 227use overload
209 '""' => sub { $_[0]{id} }, 228 '""' => sub { $_[0]{id} },
210 fallback => 1; 229 fallback => 1;
230
231sub TO_JSON { $_[0]{id} }
211 232
212=item $port->rcv (type => $callback->($port, @msg)) 233=item $port->rcv (type => $callback->($port, @msg))
213 234
214=item $port->rcv ($smartmatch => $callback->($port, @msg)) 235=item $port->rcv ($smartmatch => $callback->($port, @msg))
215 236
275=cut 296=cut
276 297
277sub destroy { 298sub destroy {
278 my ($self) = @_; 299 my ($self) = @_;
279 300
301 AnyEvent::MP::Base::del $self->{id};
302
280 delete $AnyEvent::MP::Base::WKP{ $self->{wkname} }; 303 delete $AnyEvent::MP::Base::WKP{ $self->{wkname} };
281 304
282 delete $AnyEvent::MP::Base::PORT{$_} 305 delete $AnyEvent::MP::Base::PORT{$_}
283 for @{ $self->{names} }; 306 for @{ $self->{names} };
284} 307}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines