… | |
… | |
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 _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 | |
… | |
… | |
124 | Create a new local port object. See the next section for allowed methods. |
125 | Create a new local port object. See the next section for allowed methods. |
125 | |
126 | |
126 | =cut |
127 | =cut |
127 | |
128 | |
128 | sub create_port { |
129 | sub create_port { |
129 | my $id = "$AnyEvent::MP::Base::UNIQ." . ++$AnyEvent::MP::Base::ID; |
130 | my $id = "$AnyEvent::MP::Base::UNIQ." . $AnyEvent::MP::Base::ID++; |
130 | |
131 | |
131 | my $self = bless { |
132 | my $self = bless { |
132 | id => "$NODE#$id", |
133 | id => "$NODE#$id", |
133 | names => [$id], |
134 | names => [$id], |
134 | }, "AnyEvent::MP::Port"; |
135 | }, "AnyEvent::MP::Port"; |
… | |
… | |
153 | && undef $_; |
154 | && undef $_; |
154 | } |
155 | } |
155 | }; |
156 | }; |
156 | |
157 | |
157 | $self |
158 | $self |
|
|
159 | } |
|
|
160 | |
|
|
161 | =item $portid = miniport { my @msg = @_; $finished } |
|
|
162 | |
|
|
163 | Creates a "mini port", that is, a very lightweight port without any |
|
|
164 | pattern matching behind it, and returns its ID. |
|
|
165 | |
|
|
166 | The block will be called for every message received on the port. When the |
|
|
167 | callback returns a true value its job is considered "done" and the port |
|
|
168 | will be destroyed. Otherwise it will stay alive. |
|
|
169 | |
|
|
170 | The message will be passed as-is, no extra argument (i.e. no port id) will |
|
|
171 | be passed to the callback. |
|
|
172 | |
|
|
173 | If you need the local port id in the callback, this works nicely: |
|
|
174 | |
|
|
175 | my $port; $port = miniport { |
|
|
176 | snd $otherport, reply => $port; |
|
|
177 | }; |
|
|
178 | |
|
|
179 | =cut |
|
|
180 | |
|
|
181 | sub miniport(&) { |
|
|
182 | my $cb = shift; |
|
|
183 | my $id = "$AnyEvent::MP::Base::UNIQ." . $AnyEvent::MP::Base::ID++; |
|
|
184 | |
|
|
185 | $AnyEvent::MP::Base::PORT{$id} = sub { |
|
|
186 | &$cb |
|
|
187 | and delete $AnyEvent::MP::Base::PORT{$id}; |
|
|
188 | }; |
|
|
189 | |
|
|
190 | "$NODE#$id" |
158 | } |
191 | } |
159 | |
192 | |
160 | package AnyEvent::MP::Port; |
193 | package AnyEvent::MP::Port; |
161 | |
194 | |
162 | =back |
195 | =back |