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.46 by root, Thu Aug 13 01:46:10 2009 UTC vs.
Revision 1.64 by root, Fri Aug 28 00:58:44 2009 UTC

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
72A port is something you can send messages to (with the C<snd> function). 70A port is something you can send messages to (with the C<snd> function).
73 71
74Some ports allow you to register C<rcv> handlers that can match specific 72Ports allow you to register C<rcv> handlers that can match all or just
75messages. All C<rcv> handlers will receive messages they match, messages 73some messages. Messages send to ports will not be queued, regardless of
76will not be queued. 74anything was listening for them or not.
77 75
78=item port id - C<noderef#portname> 76=item port ID - C<noderef#portname>
79 77
80A port id is normaly the concatenation of a noderef, a hash-mark (C<#>) as 78A port ID is the concatenation of a noderef, a hash-mark (C<#>) as
81separator, and a port name (a printable string of unspecified format). An 79separator, and a port name (a printable string of unspecified format). An
82exception is the the node port, whose ID is identical to its node 80exception is the the node port, whose ID is identical to its node
83reference. 81reference.
84 82
85=item node 83=item node
86 84
87A node is a single process containing at least one port - the node 85A node is a single process containing at least one port - the node port,
88port. You can send messages to node ports to find existing ports or to 86which provides nodes to manage each other remotely, and to create new
89create new ports, among other things. 87ports.
90 88
91Nodes are either private (single-process only), slaves (connected to a 89Nodes are either private (single-process only), slaves (can only talk to
92master node only) or public nodes (connectable from unrelated nodes). 90public nodes, but do not need an open port) or public nodes (connectable
91from 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
96A node reference is a string that either simply identifies the node (for 95A node ID is a string that uniquely identifies the node within a
97private and slave nodes), or contains a recipe on how to reach a given 96network. Depending on the configuration used, node IDs can look like a
98node (for public nodes). 97hostname, a hostname and a port, or a random string. AnyEvent::MP itself
98doesn't interpret node IDs in any way.
99 99
100This recipe is simply a comma-separated list of C<address:port> pairs (for 100=item binds - C<ip:port>
101TCP/IP, other protocols might look different).
102 101
103Node references come in two flavours: resolved (containing only numerical 102Nodes can only talk to each other by creating some kind of connection to
104addresses) or unresolved (where hostnames are used instead of addresses). 103each other. To do this, nodes should listen on one or more local transport
104endpoints - binds. Currently, only standard C<ip:port> specifications can
105be used, which specify TCP ports to listen on.
105 106
106Before using an unresolved node reference in a message you first have to 107=item seeds - C<host:port>
107resolve it. 108
109When a node starts, it knows nothing about the network. To teach the node
110about the network it first has to contact some other node within the
111network. This node is called a seed.
112
113Seeds are transport endpoint(s) of as many nodes as one wants. Those nodes
114are expected to be long-running, and at least one of those should always
115be available. When nodes run out of connections (e.g. due to a network
116error), they try to re-establish connections to some seednodes again to
117join the network.
108 118
109=back 119=back
110 120
111=head1 VARIABLES/FUNCTIONS 121=head1 VARIABLES/FUNCTIONS
112 122
127use base "Exporter"; 137use base "Exporter";
128 138
129our $VERSION = $AnyEvent::MP::Kernel::VERSION; 139our $VERSION = $AnyEvent::MP::Kernel::VERSION;
130 140
131our @EXPORT = qw( 141our @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
138our $SELF; 148our $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
148The C<NODE> function returns, and the C<$NODE> variable contains 158The C<NODE> function returns, and the C<$NODE> variable contains the node
149the noderef of the local node. The value is initialised by a call 159ID of the node running in the current process. This value is initialised by
150to C<become_public> or C<become_slave>, after which all local port 160a call to C<initialise_node>.
151identifiers become invalid.
152 161
153=item $noderef = node_of $port 162=item $nodeid = node_of $port
154 163
155Extracts and returns the noderef from a portid or a noderef. 164Extracts 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
161Before a node can talk to other nodes on the network it has to initialise 168Before a node can talk to other nodes on the network (i.e. enter
162itself - 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
163it should know the noderefs of some other nodes in the network. 170to know is its own name, and optionally it should know the addresses of
171some other nodes in the network to discover other nodes.
164 172
165This function initialises a node - it must be called exactly once (or 173This function initialises a node - it must be called exactly once (or
166never) before calling other AnyEvent::MP functions. 174never) before calling other AnyEvent::MP functions.
167 175
168All arguments are noderefs, which can be either resolved or unresolved. 176The first argument is a profile name. If it is C<undef> or missing, then
177the current nodename will be used instead (i.e. F<uname -n>).
169 178
170There are two types of networked nodes, public nodes and slave nodes: 179The function then looks up the profile in the aemp configuration (see the
180L<aemp> commandline utility).
171 181
172=over 4 182If the profile specifies a node ID, then this will become the node ID of
183this process. If not, then the profile name will be used as node ID. The
184special node ID of C<anon/> will be replaced by a random node ID.
173 185
174=item public nodes 186The next step is to look up the binds in the profile, followed by binding
187aemp protocol listeners on all binds specified (it is possible and valid
188to have no binds, meaning that the node cannot be contacted form the
189outside. This means the node cannot talk to other nodes that also have no
190binds, but it can still talk to all "normal" nodes).
175 191
176For public nodes, C<$noderef> must either be a (possibly unresolved) 192If the profile does not specify a binds list, then the node ID will be
177noderef, in which case it will be resolved, or C<undef> (or missing), in 193treated as if it were of the form C<host:port>, which will be resolved and
178which case the noderef will be guessed. 194used as binds list.
179 195
180Afterwards, the node will bind itself on all endpoints and try to connect 196Lastly, the seeds list from the profile is passed to the
181to all additional C<$seednodes> that are specified. Seednodes are optional 197L<AnyEvent::MP::Global> module, which will then use it to keep
182and can be used to quickly bootstrap the node into an existing network. 198connectivity with at least on of those seed nodes at any point in time.
183 199
184=item slave nodes 200Example: become a distributed node listening on the guessed noderef, or
185 201the one specified via C<aemp> for the current node. This should be the
186When the C<$noderef> is the special string C<slave/>, then the node will 202most common form of invocation for "daemon"-type nodes.
187become a slave node. Slave nodes cannot be contacted from outside and will
188route most of their traffic to the master node that they attach to.
189
190At least one additional noderef is required: The node will try to connect
191to all of them and will become a slave attached to the first node it can
192successfully connect to.
193
194=back
195
196This function will block until all nodes have been resolved and, for slave
197nodes, until it has successfully established a connection to a master
198server.
199
200Example: become a public node listening on the default node.
201 203
202 initialise_node; 204 initialise_node;
203 205
204Example: become a public node, and try to contact some well-known master 206Example: become an anonymous node. This form is often used for commandline
205servers to become part of the network. 207clients.
206 208
207 initialise_node undef, "master1", "master2";
208
209Example: become a public node listening on port C<4041>.
210
211 initialise_node 4041; 209 initialise_node "anon/";
212 210
213Example: become a public node, only visible on localhost port 4044. 211Example: become a distributed node. If there is no profile of the given
212name, or no binds list was specified, resolve C<localhost:4044> and bind
213on the resulting addresses.
214 214
215 initialise_node "locahost:4044"; 215 initialise_node "localhost:4044";
216
217Example: 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
223Takes an unresolved node reference that may contain hostnames and
224abbreviated IDs, resolves all of them and returns a resolved node
225reference.
226
227In addition to C<address:port> pairs allowed in resolved noderefs, the
228following forms are supported:
229
230=over 4
231
232=item the empty string
233
234An empty-string component gets resolved as if the default port (4040) was
235specified.
236
237=item naked port numbers (e.g. C<1234>)
238
239These are resolved by prepending the local nodename and a colon, to be
240further resolved.
241
242=item hostnames (e.g. C<localhost:1234>, C<localhost>)
243
244These are resolved by using AnyEvent::DNS to resolve them, optionally
245looking up SRV records for the C<aemp=4040> port, if no port was
246specified.
247
248=back
249 216
250=item $SELF 217=item $SELF
251 218
252Contains the current port id while executing C<rcv> callbacks or C<psub> 219Contains the current port id while executing C<rcv> callbacks or C<psub>
253blocks. 220blocks.
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
265Send the given message to the given port ID, which can identify either 232Send the given message to the given port ID, which can identify either
266a local or a remote port, and can be either a string or soemthignt hat 233a local or a remote port, and must be a port ID.
267stringifies a sa port ID (such as a port object :).
268 234
269While the message can be about anything, it is highly recommended to use a 235While the message can be about anything, it is highly recommended to use a
270string as first element (a portid, or some word that indicates a request 236string as first element (a port ID, or some word that indicates a request
271type etc.). 237type etc.).
272 238
273The message data effectively becomes read-only after a call to this 239The message data effectively becomes read-only after a call to this
274function: modifying any argument is not allowed and can cause many 240function: modifying any argument is not allowed and can cause many
275problems. 241problems.
280that Storable can serialise and deserialise is allowed, and for the local 246that Storable can serialise and deserialise is allowed, and for the local
281node, anything can be passed. 247node, anything can be passed.
282 248
283=item $local_port = port 249=item $local_port = port
284 250
285Create a new local port object that can be used either as a pattern 251Create a new local port object and returns its port ID. Initially it has
286matching port ("full port") or a single-callback port ("miniport"), 252no callbacks set and will throw an error when it receives messages.
287depending 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
291Creates a "miniport", that is, a very lightweight port without any pattern 256Creates a new local port, and returns its ID. Semantically the same as
292matching behind it, and returns its ID. Semantically the same as creating
293a port and calling C<rcv $port, $callback> on it. 257creating a port and calling C<rcv $port, $callback> on it.
294 258
295The block will be called for every message received on the port. When the 259The block will be called for every message received on the port, with the
296callback returns a true value its job is considered "done" and the port 260global variable C<$SELF> set to the port ID. Runtime errors will cause the
297will be destroyed. Otherwise it will stay alive. 261port 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
299The message will be passed as-is, no extra argument (i.e. no port id) will 264If you want to stop/destroy the port, simply C<kil> it:
300be passed to the callback.
301 265
302If 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
310sub rcv($@); 274sub rcv($@);
275
276sub _kilme {
277 die "received message on port without callback";
278}
311 279
312sub port(;&) { 280sub 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
329Registers the given port (or C<$SELF><<< if missing) under the name
330C<$name>. If the name already exists it is replaced.
331
332A port can only be registered under one well known name.
333
334A port automatically becomes unregistered when it is killed.
335
336=cut
337
338sub 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
346Replaces the callback on the specified miniport (after converting it to 291Replaces the default callback on the specified port. There is no way to
347one if required). 292remove the default callback: use C<sub { }> to disable it, or better
348 293C<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
355Register callbacks to be called on matching messages on the given full
356port (after converting it to one if required) and return the port.
357
358The callback has to return a true value when its work is done, after
359which is will be removed, or a false value in which case it will stay
360registered.
361 294
362The global C<$SELF> (exported by this module) contains C<$port> while 295The global C<$SELF> (exported by this module) contains C<$port> while
363executing the callback. 296executing the callback. Runtime errors during callback execution will
297result in the port being C<kil>ed.
364 298
365Runtime errors during callback execution will result in the port being 299The default callback received all messages not matched by a more specific
366C<kil>ed. 300C<tag> match.
367 301
368If the match is an array reference, then it will be matched against the 302=item rcv $local_port, tag => $callback->(@msg_without_tag), ...
369first elements of the message, otherwise only the first element is being
370matched.
371 303
372Any element in the match that is specified as C<_any_> (a function 304Register (or replace) callbacks to be called on messages starting with the
373exported by this module) matches any single element of the message. 305given tag on the given port (and return the port), or unregister it (when
306C<$callback> is C<$undef> or missing). There can only be one callback
307registered for each tag.
374 308
375While not required, it is highly recommended that the first matching 309The original message will be passed to the callback, after the first
376element is a string identifying the message. The one-string-only match is 310element (the tag) has been removed. The callback will use the same
377also the most efficient match (by far). 311environment as the default callback (see above).
378 312
379Example: create a port and bind receivers on it in one go. 313Example: 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
386Example: create a port, bind receivers and send it in a message elsewhere 320Example: create a port, bind receivers and send it in a message elsewhere
387in one go: 321in 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
329Example: 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
397sub rcv($@) { 340sub 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
522will arrive, or the monitoring action will be invoked after possible 447will arrive, or the monitoring action will be invoked after possible
523message loss has been detected. No messages will be lost "in between" 448message 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
525port). After the monitoring action was invoked, further messages might get 450port). After the monitoring action was invoked, further messages might get
526delivered again. 451delivered again.
452
453Note that monitoring-actions are one-shot: once released, they are removed
454and will not trigger again.
527 455
528In the first form (callback), the callback is simply called with any 456In the first form (callback), the callback is simply called with any
529number of C<@reason> elements (no @reason means that the port was deleted 457number 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
531C<eval> if unsure. 459C<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
707Nodes understand the following messages sent to them. Many of them take 634Either sends the given message, or call the given callback, after the
708arguments called C<@reply>, which will simply be used to compose a reply 635specified number of seconds.
709message - C<$reply[0]> is the port to reply to, C<$reply[1]> the type and
710the remaining arguments are simply the message data.
711 636
712While other messages exist, they are not public and subject to change. 637This 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 641sub after($@) {
642 my ($timeout, @action) = @_;
719 643
720Replies 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]()
724Generic data sink/CPU heat conversion. 648 : snd @action;
725 649 };
726=item relay => $port, @msg 650}
727
728Simply forwards the message to the given port.
729
730=item eval => $string[ @reply]
731
732Evaluates the given string. If C<@reply> is given, then a message of the
733form C<@reply, $@, @evalres> is sent.
734
735Example: crash another node.
736
737 snd $othernode, eval => "exit";
738
739=item time => @reply
740
741Replies the the current node time to C<@reply>.
742
743Example: tell the current node to send the current time to C<$myport> in a
744C<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
771convenience functionality. 674convenience functionality.
772 675
773This means that AEMP requires a less tightly controlled environment at the 676This means that AEMP requires a less tightly controlled environment at the
774cost of longer node references and a slightly higher management overhead. 677cost 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
680uses "local ports are like remote ports".
681
682The failure modes for local ports are quite different (runtime errors
683only) then for remote ports - when a local port dies, you I<know> it dies,
684when a connection to another node dies, you know nothing about the other
685port.
686
687Erlang pretends remote ports are as reliable as local ports, even when
688they are not.
689
690AEMP encourages a "treat remote ports differently" philosophy, with local
691ports being the special case/exception, where transport errors cannot
692occur.
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
778Erlang uses processes that selctively receive messages, and therefore 696Erlang uses processes that selectively receive messages, and therefore
779needs a queue. AEMP is event based, queuing messages would serve no useful 697needs a queue. AEMP is event based, queuing messages would serve no
780purpose. 698useful purpose. For the same reason the pattern-matching abilities of
699AnyEvent::MP are more limited, as there is little need to be able to
700filter 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
786Sending messages in Erlang is synchronous and blocks the process. AEMP 706Sending messages in Erlang is synchronous and blocks the process (and
787sends are immediate, connection establishment is handled in the 707so does not need a queue that can overflow). AEMP sends are immediate,
788background. 708connection 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
792Erlang makes few guarantees on messages delivery - messages can get lost 712Erlang makes few guarantees on messages delivery - messages can get lost
793without any of the processes realising it (i.e. you send messages a, b, 713without any of the processes realising it (i.e. you send messages a, b,
794and c, and the other side only receives messages a and c). 714and c, and the other side only receives messages a and c).
795 715
807eventually be killed - it cannot happen that a node detects a port as dead 727eventually be killed - it cannot happen that a node detects a port as dead
808and then later sends messages to it, finding it is still alive. 728and 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
812In Erlang it is quite possible that a node that restarts reuses a process 732In Erlang it is quite likely that a node that restarts reuses a process ID
813ID known to other nodes for a completely different process, causing 733known to other nodes for a completely different process, causing messages
814messages destined for that process to end up in an unrelated process. 734destined for that process to end up in an unrelated process.
815 735
816AEMP never reuses port IDs, so old messages or old port IDs floating 736AEMP never reuses port IDs, so old messages or old port IDs floating
817around in the network will not be sent to an unrelated port. 737around 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
868the network frequently, the serialising/deserialising would add lots of 788the network frequently, the serialising/deserialising would add lots of
869overhead, as well as having to keep a proxy object. 789overhead, as well as having to keep a proxy object.
870 790
871Strings can easily be printed, easily serialised etc. and need no special 791Strings can easily be printed, easily serialised etc. and need no special
872procedures to be "valid". 792procedures to be "valid".
793
794And a a miniport consists of a single closure stored in a global hash - it
795can'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
876In fact, any AnyEvent::MP node will happily accept Storable as framing 799In fact, any AnyEvent::MP node will happily accept Storable as framing
877format, but currently there is no way to make a node use Storable by 800format, but currently there is no way to make a node use Storable by

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines