… | |
… | |
84 | |
84 | |
85 | use base "Exporter"; |
85 | use base "Exporter"; |
86 | |
86 | |
87 | our $VERSION = '0.02'; |
87 | our $VERSION = '0.02'; |
88 | our @EXPORT = qw( |
88 | our @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 | |
… | |
… | |
118 | JSON is used, then only strings, numbers and arrays and hashes consisting |
118 | JSON is used, then only strings, numbers and arrays and hashes consisting |
119 | of those are allowed (no objects). When Storable is used, then anything |
119 | of those are allowed (no objects). When Storable is used, then anything |
120 | that Storable can serialise and deserialise is allowed, and for the local |
120 | that Storable can serialise and deserialise is allowed, and for the local |
121 | node, anything can be passed. |
121 | node, anything can be passed. |
122 | |
122 | |
|
|
123 | =item mon $portid, sub { } |
|
|
124 | |
|
|
125 | #TODO monitor the given port |
|
|
126 | |
|
|
127 | =cut |
|
|
128 | |
|
|
129 | sub 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 | |
125 | Create a new local port object. See the next section for allowed methods. |
144 | Create a new local port object. See the next section for allowed methods. |
126 | |
145 | |
127 | =cut |
146 | =cut |
128 | |
147 | |
129 | sub create_port { |
148 | sub 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"; |
… | |
… | |
156 | }; |
175 | }; |
157 | |
176 | |
158 | $self |
177 | $self |
159 | } |
178 | } |
160 | |
179 | |
161 | =item $portid = create_miniport { } |
180 | =item $portid = miniport { my @msg = @_; $finished } |
162 | |
181 | |
163 | Creates a "mini port", that is, a port without much #TODO |
182 | Creates a "mini port", that is, a very lightweight port without any |
|
|
183 | pattern matching behind it, and returns its ID. |
164 | |
184 | |
165 | =cut |
185 | The block will be called for every message received on the port. When the |
|
|
186 | callback returns a true value its job is considered "done" and the port |
|
|
187 | will be destroyed. Otherwise it will stay alive. |
166 | |
188 | |
|
|
189 | The message will be passed as-is, no extra argument (i.e. no port id) will |
|
|
190 | be passed to the callback. |
|
|
191 | |
|
|
192 | If you need the local port id in the callback, this works nicely: |
|
|
193 | |
|
|
194 | my $port; $port = miniport { |
|
|
195 | snd $otherport, reply => $port; |
|
|
196 | }; |
|
|
197 | |
|
|
198 | =cut |
|
|
199 | |
167 | sub create_miniport(&) { |
200 | sub miniport(&) { |
168 | my $cb = shift; |
201 | my $cb = shift; |
169 | my $id = "$AnyEvent::MP::Base::UNIQ." . ++$AnyEvent::MP::Base::ID; |
202 | my $id = "$AnyEvent::MP::Base::UNIQ." . $AnyEvent::MP::Base::ID++; |
170 | |
203 | |
171 | $AnyEvent::MP::Base::PORT{$id} = sub { |
204 | $AnyEvent::MP::Base::PORT{$id} = sub { |
172 | # unshift @_, "$NODE#$id"; |
|
|
173 | &$cb |
205 | &$cb |
174 | and delete $AnyEvent::MP::Base::PORT{$id}; |
206 | and del $id; |
175 | }; |
207 | }; |
176 | |
208 | |
177 | "$NODE#$id" |
209 | "$NODE#$id" |
178 | } |
210 | } |
179 | |
211 | |
… | |
… | |
193 | =cut |
225 | =cut |
194 | |
226 | |
195 | use overload |
227 | use overload |
196 | '""' => sub { $_[0]{id} }, |
228 | '""' => sub { $_[0]{id} }, |
197 | fallback => 1; |
229 | fallback => 1; |
|
|
230 | |
|
|
231 | sub TO_JSON { $_[0]{id} } |
198 | |
232 | |
199 | =item $port->rcv (type => $callback->($port, @msg)) |
233 | =item $port->rcv (type => $callback->($port, @msg)) |
200 | |
234 | |
201 | =item $port->rcv ($smartmatch => $callback->($port, @msg)) |
235 | =item $port->rcv ($smartmatch => $callback->($port, @msg)) |
202 | |
236 | |
… | |
… | |
262 | =cut |
296 | =cut |
263 | |
297 | |
264 | sub destroy { |
298 | sub destroy { |
265 | my ($self) = @_; |
299 | my ($self) = @_; |
266 | |
300 | |
|
|
301 | AnyEvent::MP::Base::del $self->{id}; |
|
|
302 | |
267 | delete $AnyEvent::MP::Base::WKP{ $self->{wkname} }; |
303 | delete $AnyEvent::MP::Base::WKP{ $self->{wkname} }; |
268 | |
304 | |
269 | delete $AnyEvent::MP::Base::PORT{$_} |
305 | delete $AnyEvent::MP::Base::PORT{$_} |
270 | for @{ $self->{names} }; |
306 | for @{ $self->{names} }; |
271 | } |
307 | } |