… | |
… | |
9 | $NODE # contains this node's noderef |
9 | $NODE # contains this node's noderef |
10 | NODE # returns this node's noderef |
10 | NODE # returns this node's noderef |
11 | NODE $port # returns the noderef of the port |
11 | NODE $port # returns the noderef of the port |
12 | |
12 | |
13 | $SELF # receiving/own port id in rcv callbacks |
13 | $SELF # receiving/own port id in rcv callbacks |
|
|
14 | |
|
|
15 | # initialise the node so it can send/receive messages |
|
|
16 | initialise_node; |
14 | |
17 | |
15 | # ports are message endpoints |
18 | # ports are message endpoints |
16 | |
19 | |
17 | # sending messages |
20 | # sending messages |
18 | snd $port, type => data...; |
21 | snd $port, type => data...; |
19 | snd $port, @msg; |
22 | snd $port, @msg; |
20 | snd @msg_with_first_element_being_a_port; |
23 | snd @msg_with_first_element_being_a_port; |
21 | |
24 | |
22 | # miniports |
25 | # creating/using ports, the simple way |
23 | my $miniport = port { my @msg = @_; 0 }; |
26 | my $simple_port = port { my @msg = @_; 0 }; |
24 | |
27 | |
25 | # full ports |
28 | # creating/using ports, tagged message matching |
26 | my $port = port; |
29 | my $port = port; |
27 | rcv $port, smartmatch => $cb->(@msg); |
|
|
28 | rcv $port, ping => sub { snd $_[0], "pong"; 0 }; |
30 | rcv $port, ping => sub { snd $_[0], "pong"; 0 }; |
29 | rcv $port, pong => sub { warn "pong received\n"; 0 }; |
31 | rcv $port, pong => sub { warn "pong received\n"; 0 }; |
30 | |
32 | |
31 | # remote ports |
33 | # create a port on another node |
32 | my $port = spawn $node, $initfunc, @initdata; |
34 | my $port = spawn $node, $initfunc, @initdata; |
33 | |
|
|
34 | # more, smarter, matches (_any_ is exported by this module) |
|
|
35 | rcv $port, [child_died => $pid] => sub { ... |
|
|
36 | rcv $port, [_any_, _any_, 3] => sub { .. $_[2] is 3 |
|
|
37 | |
35 | |
38 | # monitoring |
36 | # monitoring |
39 | mon $port, $cb->(@msg) # callback is invoked on death |
37 | mon $port, $cb->(@msg) # callback is invoked on death |
40 | mon $port, $otherport # kill otherport on abnormal death |
38 | mon $port, $otherport # kill otherport on abnormal death |
41 | mon $port, $otherport, @msg # send message on death |
39 | mon $port, $otherport, @msg # send message on death |
… | |
… | |
69 | |
67 | |
70 | =item port |
68 | =item port |
71 | |
69 | |
72 | A port is something you can send messages to (with the C<snd> function). |
70 | A port is something you can send messages to (with the C<snd> function). |
73 | |
71 | |
74 | Some ports allow you to register C<rcv> handlers that can match specific |
72 | Ports allow you to register C<rcv> handlers that can match all or just |
75 | messages. All C<rcv> handlers will receive messages they match, messages |
73 | some messages. Messages send to ports will not be queued, regardless of |
76 | will not be queued. |
74 | anything was listening for them or not. |
77 | |
75 | |
78 | =item port id - C<noderef#portname> |
76 | =item port ID - C<noderef#portname> |
79 | |
77 | |
80 | A port id is normaly the concatenation of a noderef, a hash-mark (C<#>) as |
78 | A port ID is the concatenation of a noderef, a hash-mark (C<#>) as |
81 | separator, and a port name (a printable string of unspecified format). An |
79 | separator, and a port name (a printable string of unspecified format). An |
82 | exception is the the node port, whose ID is identical to its node |
80 | exception is the the node port, whose ID is identical to its node |
83 | reference. |
81 | reference. |
84 | |
82 | |
85 | =item node |
83 | =item node |
86 | |
84 | |
87 | A node is a single process containing at least one port - the node |
85 | A node is a single process containing at least one port - the node port, |
88 | port. You can send messages to node ports to find existing ports or to |
86 | which provides nodes to manage each other remotely, and to create new |
89 | create new ports, among other things. |
87 | ports. |
90 | |
88 | |
91 | Nodes are either private (single-process only), slaves (connected to a |
89 | Nodes are either private (single-process only), slaves (can only talk to |
92 | master node only) or public nodes (connectable from unrelated nodes). |
90 | public nodes, but do not need an open port) or public nodes (connectable |
|
|
91 | from any other node). |
93 | |
92 | |
94 | =item noderef - C<host:port,host:port...>, C<id@noderef>, C<id> |
93 | =item node ID - C<[a-za-Z0-9_\-.:]+> |
95 | |
94 | |
96 | A node reference is a string that either simply identifies the node (for |
95 | A node ID is a string that uniquely identifies the node within a |
97 | private and slave nodes), or contains a recipe on how to reach a given |
96 | network. Depending on the configuration used, node IDs can look like a |
98 | node (for public nodes). |
97 | hostname, a hostname and a port, or a random string. AnyEvent::MP itself |
|
|
98 | doesn't interpret node IDs in any way. |
99 | |
99 | |
100 | This recipe is simply a comma-separated list of C<address:port> pairs (for |
100 | =item binds - C<ip:port> |
101 | TCP/IP, other protocols might look different). |
|
|
102 | |
101 | |
103 | Node references come in two flavours: resolved (containing only numerical |
102 | Nodes can only talk to each other by creating some kind of connection to |
104 | addresses) or unresolved (where hostnames are used instead of addresses). |
103 | each other. To do this, nodes should listen on one or more local transport |
|
|
104 | endpoints - binds. Currently, only standard C<ip:port> specifications can |
|
|
105 | be used, which specify TCP ports to listen on. |
105 | |
106 | |
106 | Before using an unresolved node reference in a message you first have to |
107 | =item seeds - C<host:port> |
107 | resolve it. |
108 | |
|
|
109 | When a node starts, it knows nothing about the network. To teach the node |
|
|
110 | about the network it first has to contact some other node within the |
|
|
111 | network. This node is called a seed. |
|
|
112 | |
|
|
113 | Seeds are transport endpoint(s) of as many nodes as one wants. Those nodes |
|
|
114 | are expected to be long-running, and at least one of those should always |
|
|
115 | be available. When nodes run out of connections (e.g. due to a network |
|
|
116 | error), they try to re-establish connections to some seednodes again to |
|
|
117 | join the network. |
108 | |
118 | |
109 | =back |
119 | =back |
110 | |
120 | |
111 | =head1 VARIABLES/FUNCTIONS |
121 | =head1 VARIABLES/FUNCTIONS |
112 | |
122 | |
… | |
… | |
127 | use base "Exporter"; |
137 | use base "Exporter"; |
128 | |
138 | |
129 | our $VERSION = $AnyEvent::MP::Kernel::VERSION; |
139 | our $VERSION = $AnyEvent::MP::Kernel::VERSION; |
130 | |
140 | |
131 | our @EXPORT = qw( |
141 | our @EXPORT = qw( |
132 | NODE $NODE *SELF node_of _any_ |
142 | NODE $NODE *SELF node_of after |
133 | resolve_node initialise_node |
143 | resolve_node initialise_node |
134 | snd rcv mon kil reg psub spawn |
144 | snd rcv mon mon_guard kil reg psub spawn |
135 | port |
145 | port |
136 | ); |
146 | ); |
137 | |
147 | |
138 | our $SELF; |
148 | our $SELF; |
139 | |
149 | |
… | |
… | |
143 | kil $SELF, die => $msg; |
153 | kil $SELF, die => $msg; |
144 | } |
154 | } |
145 | |
155 | |
146 | =item $thisnode = NODE / $NODE |
156 | =item $thisnode = NODE / $NODE |
147 | |
157 | |
148 | The C<NODE> function returns, and the C<$NODE> variable contains |
158 | The C<NODE> function returns, and the C<$NODE> variable contains the node |
149 | the noderef of the local node. The value is initialised by a call |
159 | ID of the node running in the current process. This value is initialised by |
150 | to C<become_public> or C<become_slave>, after which all local port |
160 | a call to C<initialise_node>. |
151 | identifiers become invalid. |
|
|
152 | |
161 | |
153 | =item $noderef = node_of $port |
162 | =item $nodeid = node_of $port |
154 | |
163 | |
155 | Extracts and returns the noderef from a portid or a noderef. |
164 | Extracts and returns the node ID part from a port ID or a node ID. |
156 | |
165 | |
157 | =item initialise_node $noderef, $seednode, $seednode... |
166 | =item initialise_node $profile_name |
158 | |
167 | |
159 | =item initialise_node "slave/", $master, $master... |
|
|
160 | |
|
|
161 | Before a node can talk to other nodes on the network it has to initialise |
168 | Before a node can talk to other nodes on the network (i.e. enter |
162 | itself - the minimum a node needs to know is it's own name, and optionally |
169 | "distributed mode") it has to initialise itself - the minimum a node needs |
163 | it should know the noderefs of some other nodes in the network. |
170 | to know is its own name, and optionally it should know the addresses of |
|
|
171 | some other nodes in the network to discover other nodes. |
164 | |
172 | |
165 | This function initialises a node - it must be called exactly once (or |
173 | This function initialises a node - it must be called exactly once (or |
166 | never) before calling other AnyEvent::MP functions. |
174 | never) before calling other AnyEvent::MP functions. |
167 | |
175 | |
168 | All arguments are noderefs, which can be either resolved or unresolved. |
176 | The first argument is a profile name. If it is C<undef> or missing, then |
|
|
177 | the current nodename will be used instead (i.e. F<uname -n>). |
169 | |
178 | |
170 | There are two types of networked nodes, public nodes and slave nodes: |
179 | The function then looks up the profile in the aemp configuration (see the |
|
|
180 | L<aemp> commandline utility). |
171 | |
181 | |
172 | =over 4 |
182 | If the profile specifies a node ID, then this will become the node ID of |
|
|
183 | this process. If not, then the profile name will be used as node ID. The |
|
|
184 | special node ID of C<anon/> will be replaced by a random node ID. |
173 | |
185 | |
174 | =item public nodes |
186 | The next step is to look up the binds in the profile, followed by binding |
|
|
187 | aemp protocol listeners on all binds specified (it is possible and valid |
|
|
188 | to have no binds, meaning that the node cannot be contacted form the |
|
|
189 | outside. This means the node cannot talk to other nodes that also have no |
|
|
190 | binds, but it can still talk to all "normal" nodes). |
175 | |
191 | |
176 | For public nodes, C<$noderef> must either be a (possibly unresolved) |
192 | If the profile does not specify a binds list, then the node ID will be |
177 | noderef, in which case it will be resolved, or C<undef> (or missing), in |
193 | treated as if it were of the form C<host:port>, which will be resolved and |
178 | which case the noderef will be guessed. |
194 | used as binds list. |
179 | |
195 | |
180 | Afterwards, the node will bind itself on all endpoints and try to connect |
196 | Lastly, the seeds list from the profile is passed to the |
181 | to all additional C<$seednodes> that are specified. Seednodes are optional |
197 | L<AnyEvent::MP::Global> module, which will then use it to keep |
182 | and can be used to quickly bootstrap the node into an existing network. |
198 | connectivity with at least on of those seed nodes at any point in time. |
183 | |
199 | |
184 | =item slave nodes |
200 | Example: become a distributed node listening on the guessed noderef, or |
185 | |
201 | the one specified via C<aemp> for the current node. This should be the |
186 | When the C<$noderef> is the special string C<slave/>, then the node will |
202 | most common form of invocation for "daemon"-type nodes. |
187 | become a slave node. Slave nodes cannot be contacted from outside and will |
|
|
188 | route most of their traffic to the master node that they attach to. |
|
|
189 | |
|
|
190 | At least one additional noderef is required: The node will try to connect |
|
|
191 | to all of them and will become a slave attached to the first node it can |
|
|
192 | successfully connect to. |
|
|
193 | |
|
|
194 | =back |
|
|
195 | |
|
|
196 | This function will block until all nodes have been resolved and, for slave |
|
|
197 | nodes, until it has successfully established a connection to a master |
|
|
198 | server. |
|
|
199 | |
|
|
200 | Example: become a public node listening on the default node. |
|
|
201 | |
203 | |
202 | initialise_node; |
204 | initialise_node; |
203 | |
205 | |
204 | Example: become a public node, and try to contact some well-known master |
206 | Example: become an anonymous node. This form is often used for commandline |
205 | servers to become part of the network. |
207 | clients. |
206 | |
208 | |
207 | initialise_node undef, "master1", "master2"; |
|
|
208 | |
|
|
209 | Example: become a public node listening on port C<4041>. |
|
|
210 | |
|
|
211 | initialise_node 4041; |
209 | initialise_node "anon/"; |
212 | |
210 | |
213 | Example: become a public node, only visible on localhost port 4044. |
211 | Example: become a distributed node. If there is no profile of the given |
|
|
212 | name, or no binds list was specified, resolve C<localhost:4044> and bind |
|
|
213 | on the resulting addresses. |
214 | |
214 | |
215 | initialise_node "locahost:4044"; |
215 | initialise_node "localhost:4044"; |
216 | |
|
|
217 | Example: become a slave node to any of the specified master servers. |
|
|
218 | |
|
|
219 | initialise_node "slave/", "master1", "192.168.13.17", "mp.example.net"; |
|
|
220 | |
|
|
221 | =item $cv = resolve_node $noderef |
|
|
222 | |
|
|
223 | Takes an unresolved node reference that may contain hostnames and |
|
|
224 | abbreviated IDs, resolves all of them and returns a resolved node |
|
|
225 | reference. |
|
|
226 | |
|
|
227 | In addition to C<address:port> pairs allowed in resolved noderefs, the |
|
|
228 | following forms are supported: |
|
|
229 | |
|
|
230 | =over 4 |
|
|
231 | |
|
|
232 | =item the empty string |
|
|
233 | |
|
|
234 | An empty-string component gets resolved as if the default port (4040) was |
|
|
235 | specified. |
|
|
236 | |
|
|
237 | =item naked port numbers (e.g. C<1234>) |
|
|
238 | |
|
|
239 | These are resolved by prepending the local nodename and a colon, to be |
|
|
240 | further resolved. |
|
|
241 | |
|
|
242 | =item hostnames (e.g. C<localhost:1234>, C<localhost>) |
|
|
243 | |
|
|
244 | These are resolved by using AnyEvent::DNS to resolve them, optionally |
|
|
245 | looking up SRV records for the C<aemp=4040> port, if no port was |
|
|
246 | specified. |
|
|
247 | |
|
|
248 | =back |
|
|
249 | |
216 | |
250 | =item $SELF |
217 | =item $SELF |
251 | |
218 | |
252 | Contains the current port id while executing C<rcv> callbacks or C<psub> |
219 | Contains the current port id while executing C<rcv> callbacks or C<psub> |
253 | blocks. |
220 | blocks. |
… | |
… | |
261 | =item snd $port, type => @data |
228 | =item snd $port, type => @data |
262 | |
229 | |
263 | =item snd $port, @msg |
230 | =item snd $port, @msg |
264 | |
231 | |
265 | Send the given message to the given port ID, which can identify either |
232 | Send the given message to the given port ID, which can identify either |
266 | a local or a remote port, and can be either a string or soemthignt hat |
233 | a local or a remote port, and must be a port ID. |
267 | stringifies a sa port ID (such as a port object :). |
|
|
268 | |
234 | |
269 | While the message can be about anything, it is highly recommended to use a |
235 | While the message can be about anything, it is highly recommended to use a |
270 | string as first element (a portid, or some word that indicates a request |
236 | string as first element (a port ID, or some word that indicates a request |
271 | type etc.). |
237 | type etc.). |
272 | |
238 | |
273 | The message data effectively becomes read-only after a call to this |
239 | The message data effectively becomes read-only after a call to this |
274 | function: modifying any argument is not allowed and can cause many |
240 | function: modifying any argument is not allowed and can cause many |
275 | problems. |
241 | problems. |
… | |
… | |
280 | that Storable can serialise and deserialise is allowed, and for the local |
246 | that Storable can serialise and deserialise is allowed, and for the local |
281 | node, anything can be passed. |
247 | node, anything can be passed. |
282 | |
248 | |
283 | =item $local_port = port |
249 | =item $local_port = port |
284 | |
250 | |
285 | Create a new local port object that can be used either as a pattern |
251 | Create a new local port object and returns its port ID. Initially it has |
286 | matching port ("full port") or a single-callback port ("miniport"), |
252 | no callbacks set and will throw an error when it receives messages. |
287 | depending on how C<rcv> callbacks are bound to the object. |
|
|
288 | |
253 | |
289 | =item $port = port { my @msg = @_; $finished } |
254 | =item $local_port = port { my @msg = @_ } |
290 | |
255 | |
291 | Creates a "miniport", that is, a very lightweight port without any pattern |
256 | Creates a new local port, and returns its ID. Semantically the same as |
292 | matching behind it, and returns its ID. Semantically the same as creating |
|
|
293 | a port and calling C<rcv $port, $callback> on it. |
257 | creating a port and calling C<rcv $port, $callback> on it. |
294 | |
258 | |
295 | The block will be called for every message received on the port. When the |
259 | The block will be called for every message received on the port, with the |
296 | callback returns a true value its job is considered "done" and the port |
260 | global variable C<$SELF> set to the port ID. Runtime errors will cause the |
297 | will be destroyed. Otherwise it will stay alive. |
261 | port to be C<kil>ed. The message will be passed as-is, no extra argument |
|
|
262 | (i.e. no port ID) will be passed to the callback. |
298 | |
263 | |
299 | The message will be passed as-is, no extra argument (i.e. no port id) will |
264 | If you want to stop/destroy the port, simply C<kil> it: |
300 | be passed to the callback. |
|
|
301 | |
265 | |
302 | If you need the local port id in the callback, this works nicely: |
266 | my $port = port { |
303 | |
267 | my @msg = @_; |
304 | my $port; $port = port { |
268 | ... |
305 | snd $otherport, reply => $port; |
269 | kil $SELF; |
306 | }; |
270 | }; |
307 | |
271 | |
308 | =cut |
272 | =cut |
309 | |
273 | |
310 | sub rcv($@); |
274 | sub rcv($@); |
|
|
275 | |
|
|
276 | sub _kilme { |
|
|
277 | die "received message on port without callback"; |
|
|
278 | } |
311 | |
279 | |
312 | sub port(;&) { |
280 | sub port(;&) { |
313 | my $id = "$UNIQ." . $ID++; |
281 | my $id = "$UNIQ." . $ID++; |
314 | my $port = "$NODE#$id"; |
282 | my $port = "$NODE#$id"; |
315 | |
283 | |
316 | if (@_) { |
284 | rcv $port, shift || \&_kilme; |
317 | rcv $port, shift; |
|
|
318 | } else { |
|
|
319 | $PORT{$id} = sub { }; # nop |
|
|
320 | } |
|
|
321 | |
285 | |
322 | $port |
286 | $port |
323 | } |
287 | } |
324 | |
288 | |
325 | =item reg $port, $name |
|
|
326 | |
|
|
327 | =item reg $name |
|
|
328 | |
|
|
329 | Registers the given port (or C<$SELF><<< if missing) under the name |
|
|
330 | C<$name>. If the name already exists it is replaced. |
|
|
331 | |
|
|
332 | A port can only be registered under one well known name. |
|
|
333 | |
|
|
334 | A port automatically becomes unregistered when it is killed. |
|
|
335 | |
|
|
336 | =cut |
|
|
337 | |
|
|
338 | sub reg(@) { |
|
|
339 | my $port = @_ > 1 ? shift : $SELF || Carp::croak 'reg: called with one argument only, but $SELF not set,'; |
|
|
340 | |
|
|
341 | $REG{$_[0]} = $port; |
|
|
342 | } |
|
|
343 | |
|
|
344 | =item rcv $port, $callback->(@msg) |
289 | =item rcv $local_port, $callback->(@msg) |
345 | |
290 | |
346 | Replaces the callback on the specified miniport (after converting it to |
291 | Replaces the default callback on the specified port. There is no way to |
347 | one if required). |
292 | remove the default callback: use C<sub { }> to disable it, or better |
348 | |
293 | C<kil> the port when it is no longer needed. |
349 | =item rcv $port, tagstring => $callback->(@msg), ... |
|
|
350 | |
|
|
351 | =item rcv $port, $smartmatch => $callback->(@msg), ... |
|
|
352 | |
|
|
353 | =item rcv $port, [$smartmatch...] => $callback->(@msg), ... |
|
|
354 | |
|
|
355 | Register callbacks to be called on matching messages on the given full |
|
|
356 | port (after converting it to one if required) and return the port. |
|
|
357 | |
|
|
358 | The callback has to return a true value when its work is done, after |
|
|
359 | which is will be removed, or a false value in which case it will stay |
|
|
360 | registered. |
|
|
361 | |
294 | |
362 | The global C<$SELF> (exported by this module) contains C<$port> while |
295 | The global C<$SELF> (exported by this module) contains C<$port> while |
363 | executing the callback. |
296 | executing the callback. Runtime errors during callback execution will |
|
|
297 | result in the port being C<kil>ed. |
364 | |
298 | |
365 | Runtime errors during callback execution will result in the port being |
299 | The default callback received all messages not matched by a more specific |
366 | C<kil>ed. |
300 | C<tag> match. |
367 | |
301 | |
368 | If the match is an array reference, then it will be matched against the |
302 | =item rcv $local_port, tag => $callback->(@msg_without_tag), ... |
369 | first elements of the message, otherwise only the first element is being |
|
|
370 | matched. |
|
|
371 | |
303 | |
372 | Any element in the match that is specified as C<_any_> (a function |
304 | Register (or replace) callbacks to be called on messages starting with the |
373 | exported by this module) matches any single element of the message. |
305 | given tag on the given port (and return the port), or unregister it (when |
|
|
306 | C<$callback> is C<$undef> or missing). There can only be one callback |
|
|
307 | registered for each tag. |
374 | |
308 | |
375 | While not required, it is highly recommended that the first matching |
309 | The original message will be passed to the callback, after the first |
376 | element is a string identifying the message. The one-string-only match is |
310 | element (the tag) has been removed. The callback will use the same |
377 | also the most efficient match (by far). |
311 | environment as the default callback (see above). |
378 | |
312 | |
379 | Example: create a port and bind receivers on it in one go. |
313 | Example: create a port and bind receivers on it in one go. |
380 | |
314 | |
381 | my $port = rcv port, |
315 | my $port = rcv port, |
382 | msg1 => sub { ...; 0 }, |
316 | msg1 => sub { ... }, |
383 | msg2 => sub { ...; 0 }, |
317 | msg2 => sub { ... }, |
384 | ; |
318 | ; |
385 | |
319 | |
386 | Example: create a port, bind receivers and send it in a message elsewhere |
320 | Example: create a port, bind receivers and send it in a message elsewhere |
387 | in one go: |
321 | in one go: |
388 | |
322 | |
389 | snd $otherport, reply => |
323 | snd $otherport, reply => |
390 | rcv port, |
324 | rcv port, |
391 | msg1 => sub { ...; 0 }, |
325 | msg1 => sub { ... }, |
392 | ... |
326 | ... |
393 | ; |
327 | ; |
|
|
328 | |
|
|
329 | Example: temporarily register a rcv callback for a tag matching some port |
|
|
330 | (e.g. for a rpc reply) and unregister it after a message was received. |
|
|
331 | |
|
|
332 | rcv $port, $otherport => sub { |
|
|
333 | my @reply = @_; |
|
|
334 | |
|
|
335 | rcv $SELF, $otherport; |
|
|
336 | }; |
394 | |
337 | |
395 | =cut |
338 | =cut |
396 | |
339 | |
397 | sub rcv($@) { |
340 | sub rcv($@) { |
398 | my $port = shift; |
341 | my $port = shift; |
399 | my ($noderef, $portid) = split /#/, $port, 2; |
342 | my ($noderef, $portid) = split /#/, $port, 2; |
400 | |
343 | |
401 | ($NODE{$noderef} || add_node $noderef) == $NODE{""} |
344 | $NODE{$noderef} == $NODE{""} |
402 | or Carp::croak "$port: rcv can only be called on local ports, caught"; |
345 | or Carp::croak "$port: rcv can only be called on local ports, caught"; |
403 | |
346 | |
404 | if (@_ == 1) { |
347 | while (@_) { |
|
|
348 | if (ref $_[0]) { |
|
|
349 | if (my $self = $PORT_DATA{$portid}) { |
|
|
350 | "AnyEvent::MP::Port" eq ref $self |
|
|
351 | or Carp::croak "$port: rcv can only be called on message matching ports, caught"; |
|
|
352 | |
|
|
353 | $self->[2] = shift; |
|
|
354 | } else { |
405 | my $cb = shift; |
355 | my $cb = shift; |
406 | delete $PORT_DATA{$portid}; |
|
|
407 | $PORT{$portid} = sub { |
356 | $PORT{$portid} = sub { |
408 | local $SELF = $port; |
357 | local $SELF = $port; |
409 | eval { |
358 | eval { &$cb }; _self_die if $@; |
410 | &$cb |
359 | }; |
411 | and kil $port; |
|
|
412 | }; |
360 | } |
413 | _self_die if $@; |
361 | } elsif (defined $_[0]) { |
414 | }; |
|
|
415 | } else { |
|
|
416 | my $self = $PORT_DATA{$portid} ||= do { |
362 | my $self = $PORT_DATA{$portid} ||= do { |
417 | my $self = bless { |
363 | my $self = bless [$PORT{$port} || sub { }, { }, $port], "AnyEvent::MP::Port"; |
418 | id => $port, |
|
|
419 | }, "AnyEvent::MP::Port"; |
|
|
420 | |
364 | |
421 | $PORT{$portid} = sub { |
365 | $PORT{$portid} = sub { |
422 | local $SELF = $port; |
366 | local $SELF = $port; |
423 | |
367 | |
424 | eval { |
|
|
425 | for (@{ $self->{rc0}{$_[0]} }) { |
368 | if (my $cb = $self->[1]{$_[0]}) { |
426 | $_ && &{$_->[0]} |
369 | shift; |
427 | && undef $_; |
370 | eval { &$cb }; _self_die if $@; |
428 | } |
371 | } else { |
429 | |
|
|
430 | for (@{ $self->{rcv}{$_[0]} }) { |
|
|
431 | $_ && [@_[1 .. @{$_->[1]}]] ~~ $_->[1] |
|
|
432 | && &{$_->[0]} |
372 | &{ $self->[0] }; |
433 | && undef $_; |
|
|
434 | } |
|
|
435 | |
|
|
436 | for (@{ $self->{any} }) { |
|
|
437 | $_ && [@_[0 .. $#{$_->[1]}]] ~~ $_->[1] |
|
|
438 | && &{$_->[0]} |
|
|
439 | && undef $_; |
|
|
440 | } |
373 | } |
441 | }; |
374 | }; |
442 | _self_die if $@; |
375 | |
|
|
376 | $self |
443 | }; |
377 | }; |
444 | |
378 | |
445 | $self |
|
|
446 | }; |
|
|
447 | |
|
|
448 | "AnyEvent::MP::Port" eq ref $self |
379 | "AnyEvent::MP::Port" eq ref $self |
449 | or Carp::croak "$port: rcv can only be called on message matching ports, caught"; |
380 | or Carp::croak "$port: rcv can only be called on message matching ports, caught"; |
450 | |
381 | |
451 | while (@_) { |
|
|
452 | my ($match, $cb) = splice @_, 0, 2; |
382 | my ($tag, $cb) = splice @_, 0, 2; |
453 | |
383 | |
454 | if (!ref $match) { |
384 | if (defined $cb) { |
455 | push @{ $self->{rc0}{$match} }, [$cb]; |
385 | $self->[1]{$tag} = $cb; |
456 | } elsif (("ARRAY" eq ref $match && !ref $match->[0])) { |
|
|
457 | my ($type, @match) = @$match; |
|
|
458 | @match |
|
|
459 | ? push @{ $self->{rcv}{$match->[0]} }, [$cb, \@match] |
|
|
460 | : push @{ $self->{rc0}{$match->[0]} }, [$cb]; |
|
|
461 | } else { |
386 | } else { |
462 | push @{ $self->{any} }, [$cb, $match]; |
387 | delete $self->[1]{$tag}; |
463 | } |
388 | } |
464 | } |
389 | } |
465 | } |
390 | } |
466 | |
391 | |
467 | $port |
392 | $port |
… | |
… | |
522 | will arrive, or the monitoring action will be invoked after possible |
447 | will arrive, or the monitoring action will be invoked after possible |
523 | message loss has been detected. No messages will be lost "in between" |
448 | message loss has been detected. No messages will be lost "in between" |
524 | (after the first lost message no further messages will be received by the |
449 | (after the first lost message no further messages will be received by the |
525 | port). After the monitoring action was invoked, further messages might get |
450 | port). After the monitoring action was invoked, further messages might get |
526 | delivered again. |
451 | delivered again. |
|
|
452 | |
|
|
453 | Note that monitoring-actions are one-shot: once released, they are removed |
|
|
454 | and will not trigger again. |
527 | |
455 | |
528 | In the first form (callback), the callback is simply called with any |
456 | In the first form (callback), the callback is simply called with any |
529 | number of C<@reason> elements (no @reason means that the port was deleted |
457 | number of C<@reason> elements (no @reason means that the port was deleted |
530 | "normally"). Note also that I<< the callback B<must> never die >>, so use |
458 | "normally"). Note also that I<< the callback B<must> never die >>, so use |
531 | C<eval> if unsure. |
459 | C<eval> if unsure. |
… | |
… | |
692 | my $id = "$RUNIQ." . $ID++; |
620 | my $id = "$RUNIQ." . $ID++; |
693 | |
621 | |
694 | $_[0] =~ /::/ |
622 | $_[0] =~ /::/ |
695 | or Carp::croak "spawn init function must be a fully-qualified name, caught"; |
623 | or Carp::croak "spawn init function must be a fully-qualified name, caught"; |
696 | |
624 | |
697 | ($NODE{$noderef} || add_node $noderef) |
625 | snd_to_func $noderef, "AnyEvent::MP::_spawn" => $id, @_; |
698 | ->send (["", "AnyEvent::MP::_spawn" => $id, @_]); |
|
|
699 | |
626 | |
700 | "$noderef#$id" |
627 | "$noderef#$id" |
701 | } |
628 | } |
702 | |
629 | |
703 | =back |
630 | =item after $timeout, @msg |
704 | |
631 | |
705 | =head1 NODE MESSAGES |
632 | =item after $timeout, $callback |
706 | |
633 | |
707 | Nodes understand the following messages sent to them. Many of them take |
634 | Either sends the given message, or call the given callback, after the |
708 | arguments called C<@reply>, which will simply be used to compose a reply |
635 | specified number of seconds. |
709 | message - C<$reply[0]> is the port to reply to, C<$reply[1]> the type and |
|
|
710 | the remaining arguments are simply the message data. |
|
|
711 | |
636 | |
712 | While other messages exist, they are not public and subject to change. |
637 | This is simply a utility function that come sin handy at times. |
713 | |
638 | |
714 | =over 4 |
|
|
715 | |
|
|
716 | =cut |
639 | =cut |
717 | |
640 | |
718 | =item lookup => $name, @reply |
641 | sub after($@) { |
|
|
642 | my ($timeout, @action) = @_; |
719 | |
643 | |
720 | Replies with the port ID of the specified well-known port, or C<undef>. |
644 | my $t; $t = AE::timer $timeout, 0, sub { |
721 | |
645 | undef $t; |
722 | =item devnull => ... |
646 | ref $action[0] |
723 | |
647 | ? $action[0]() |
724 | Generic data sink/CPU heat conversion. |
648 | : snd @action; |
725 | |
649 | }; |
726 | =item relay => $port, @msg |
650 | } |
727 | |
|
|
728 | Simply forwards the message to the given port. |
|
|
729 | |
|
|
730 | =item eval => $string[ @reply] |
|
|
731 | |
|
|
732 | Evaluates the given string. If C<@reply> is given, then a message of the |
|
|
733 | form C<@reply, $@, @evalres> is sent. |
|
|
734 | |
|
|
735 | Example: crash another node. |
|
|
736 | |
|
|
737 | snd $othernode, eval => "exit"; |
|
|
738 | |
|
|
739 | =item time => @reply |
|
|
740 | |
|
|
741 | Replies the the current node time to C<@reply>. |
|
|
742 | |
|
|
743 | Example: tell the current node to send the current time to C<$myport> in a |
|
|
744 | C<timereply> message. |
|
|
745 | |
|
|
746 | snd $NODE, time => $myport, timereply => 1, 2; |
|
|
747 | # => snd $myport, timereply => 1, 2, <time> |
|
|
748 | |
651 | |
749 | =back |
652 | =back |
750 | |
653 | |
751 | =head1 AnyEvent::MP vs. Distributed Erlang |
654 | =head1 AnyEvent::MP vs. Distributed Erlang |
752 | |
655 | |
… | |
… | |
771 | convenience functionality. |
674 | convenience functionality. |
772 | |
675 | |
773 | This means that AEMP requires a less tightly controlled environment at the |
676 | This means that AEMP requires a less tightly controlled environment at the |
774 | cost of longer node references and a slightly higher management overhead. |
677 | cost of longer node references and a slightly higher management overhead. |
775 | |
678 | |
|
|
679 | =item * Erlang has a "remote ports are like local ports" philosophy, AEMP |
|
|
680 | uses "local ports are like remote ports". |
|
|
681 | |
|
|
682 | The failure modes for local ports are quite different (runtime errors |
|
|
683 | only) then for remote ports - when a local port dies, you I<know> it dies, |
|
|
684 | when a connection to another node dies, you know nothing about the other |
|
|
685 | port. |
|
|
686 | |
|
|
687 | Erlang pretends remote ports are as reliable as local ports, even when |
|
|
688 | they are not. |
|
|
689 | |
|
|
690 | AEMP encourages a "treat remote ports differently" philosophy, with local |
|
|
691 | ports being the special case/exception, where transport errors cannot |
|
|
692 | occur. |
|
|
693 | |
776 | =item * Erlang uses processes and a mailbox, AEMP does not queue. |
694 | =item * Erlang uses processes and a mailbox, AEMP does not queue. |
777 | |
695 | |
778 | Erlang uses processes that selctively receive messages, and therefore |
696 | Erlang uses processes that selectively receive messages, and therefore |
779 | needs a queue. AEMP is event based, queuing messages would serve no useful |
697 | needs a queue. AEMP is event based, queuing messages would serve no |
780 | purpose. |
698 | useful purpose. For the same reason the pattern-matching abilities of |
|
|
699 | AnyEvent::MP are more limited, as there is little need to be able to |
|
|
700 | filter messages without dequeing them. |
781 | |
701 | |
782 | (But see L<Coro::MP> for a more Erlang-like process model on top of AEMP). |
702 | (But see L<Coro::MP> for a more Erlang-like process model on top of AEMP). |
783 | |
703 | |
784 | =item * Erlang sends are synchronous, AEMP sends are asynchronous. |
704 | =item * Erlang sends are synchronous, AEMP sends are asynchronous. |
785 | |
705 | |
786 | Sending messages in Erlang is synchronous and blocks the process. AEMP |
706 | Sending messages in Erlang is synchronous and blocks the process (and |
787 | sends are immediate, connection establishment is handled in the |
707 | so does not need a queue that can overflow). AEMP sends are immediate, |
788 | background. |
708 | connection establishment is handled in the background. |
789 | |
709 | |
790 | =item * Erlang can silently lose messages, AEMP cannot. |
710 | =item * Erlang suffers from silent message loss, AEMP does not. |
791 | |
711 | |
792 | Erlang makes few guarantees on messages delivery - messages can get lost |
712 | Erlang makes few guarantees on messages delivery - messages can get lost |
793 | without any of the processes realising it (i.e. you send messages a, b, |
713 | without any of the processes realising it (i.e. you send messages a, b, |
794 | and c, and the other side only receives messages a and c). |
714 | and c, and the other side only receives messages a and c). |
795 | |
715 | |
… | |
… | |
807 | eventually be killed - it cannot happen that a node detects a port as dead |
727 | eventually be killed - it cannot happen that a node detects a port as dead |
808 | and then later sends messages to it, finding it is still alive. |
728 | and then later sends messages to it, finding it is still alive. |
809 | |
729 | |
810 | =item * Erlang can send messages to the wrong port, AEMP does not. |
730 | =item * Erlang can send messages to the wrong port, AEMP does not. |
811 | |
731 | |
812 | In Erlang it is quite possible that a node that restarts reuses a process |
732 | In Erlang it is quite likely that a node that restarts reuses a process ID |
813 | ID known to other nodes for a completely different process, causing |
733 | known to other nodes for a completely different process, causing messages |
814 | messages destined for that process to end up in an unrelated process. |
734 | destined for that process to end up in an unrelated process. |
815 | |
735 | |
816 | AEMP never reuses port IDs, so old messages or old port IDs floating |
736 | AEMP never reuses port IDs, so old messages or old port IDs floating |
817 | around in the network will not be sent to an unrelated port. |
737 | around in the network will not be sent to an unrelated port. |
818 | |
738 | |
819 | =item * Erlang uses unprotected connections, AEMP uses secure |
739 | =item * Erlang uses unprotected connections, AEMP uses secure |
… | |
… | |
868 | the network frequently, the serialising/deserialising would add lots of |
788 | the network frequently, the serialising/deserialising would add lots of |
869 | overhead, as well as having to keep a proxy object. |
789 | overhead, as well as having to keep a proxy object. |
870 | |
790 | |
871 | Strings can easily be printed, easily serialised etc. and need no special |
791 | Strings can easily be printed, easily serialised etc. and need no special |
872 | procedures to be "valid". |
792 | procedures to be "valid". |
|
|
793 | |
|
|
794 | And a a miniport consists of a single closure stored in a global hash - it |
|
|
795 | can't become much cheaper. |
873 | |
796 | |
874 | =item Why favour JSON, why not real serialising format such as Storable? |
797 | =item Why favour JSON, why not real serialising format such as Storable? |
875 | |
798 | |
876 | In fact, any AnyEvent::MP node will happily accept Storable as framing |
799 | In fact, any AnyEvent::MP node will happily accept Storable as framing |
877 | format, but currently there is no way to make a node use Storable by |
800 | format, but currently there is no way to make a node use Storable by |