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.65 by root, Fri Aug 28 01:00:34 2009 UTC vs.
Revision 1.73 by root, Mon Aug 31 11:08:25 2009 UTC

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 14
15 # initialise the node so it can send/receive messages 15 # initialise the node so it can send/receive messages
16 initialise_node; 16 configure;
17 17
18 # ports are message endpoints 18 # ports are message endpoints
19 19
20 # sending messages 20 # sending messages
21 snd $port, type => data...; 21 snd $port, type => data...;
22 snd $port, @msg; 22 snd $port, @msg;
23 snd @msg_with_first_element_being_a_port; 23 snd @msg_with_first_element_being_a_port;
24 24
25 # creating/using ports, the simple way 25 # creating/using ports, the simple way
26 my $simple_port = port { my @msg = @_; 0 }; 26 my $simple_port = port { my @msg = @_ };
27 27
28 # creating/using ports, tagged message matching 28 # creating/using ports, tagged message matching
29 my $port = port; 29 my $port = port;
30 rcv $port, ping => sub { snd $_[0], "pong"; 0 }; 30 rcv $port, ping => sub { snd $_[0], "pong" };
31 rcv $port, pong => sub { warn "pong received\n"; 0 }; 31 rcv $port, pong => sub { warn "pong received\n" };
32 32
33 # create a port on another node 33 # create a port on another node
34 my $port = spawn $node, $initfunc, @initdata; 34 my $port = spawn $node, $initfunc, @initdata;
35 35
36 # monitoring 36 # monitoring
38 mon $port, $otherport # kill otherport on abnormal death 38 mon $port, $otherport # kill otherport on abnormal death
39 mon $port, $otherport, @msg # send message on death 39 mon $port, $otherport, @msg # send message on death
40 40
41=head1 CURRENT STATUS 41=head1 CURRENT STATUS
42 42
43 bin/aemp - stable.
43 AnyEvent::MP - stable API, should work 44 AnyEvent::MP - stable API, should work.
44 AnyEvent::MP::Intro - outdated 45 AnyEvent::MP::Intro - uptodate, but incomplete.
45 AnyEvent::MP::Kernel - WIP
46 AnyEvent::MP::Transport - mostly stable 46 AnyEvent::MP::Kernel - mostly stable.
47 AnyEvent::MP::Global - stable API, protocol not yet final.
47 48
48 stay tuned. 49 stay tuned.
49 50
50=head1 DESCRIPTION 51=head1 DESCRIPTION
51 52
52This module (-family) implements a simple message passing framework. 53This module (-family) implements a simple message passing framework.
53 54
54Despite its simplicity, you can securely message other processes running 55Despite its simplicity, you can securely message other processes running
55on the same or other hosts. 56on the same or other hosts, and you can supervise entities remotely.
56 57
57For an introduction to this module family, see the L<AnyEvent::MP::Intro> 58For an introduction to this module family, see the L<AnyEvent::MP::Intro>
58manual page. 59manual page and the examples under F<eg/>.
59 60
60At the moment, this module family is severly broken and underdocumented, 61At the moment, this module family is a bit underdocumented.
61so do not use. This was uploaded mainly to reserve the CPAN namespace -
62stay tuned!
63 62
64=head1 CONCEPTS 63=head1 CONCEPTS
65 64
66=over 4 65=over 4
67 66
71 70
72Ports allow you to register C<rcv> handlers that can match all or just 71Ports allow you to register C<rcv> handlers that can match all or just
73some messages. Messages send to ports will not be queued, regardless of 72some messages. Messages send to ports will not be queued, regardless of
74anything was listening for them or not. 73anything was listening for them or not.
75 74
76=item port ID - C<noderef#portname> 75=item port ID - C<nodeid#portname>
77 76
78A port ID is the concatenation of a noderef, a hash-mark (C<#>) as 77A port ID is the concatenation of a node ID, a hash-mark (C<#>) as
79separator, and a port name (a printable string of unspecified format). An 78separator, and a port name (a printable string of unspecified format).
80exception is the the node port, whose ID is identical to its node
81reference.
82 79
83=item node 80=item node
84 81
85A node is a single process containing at least one port - the node port, 82A node is a single process containing at least one port - the node port,
86which provides nodes to manage each other remotely, and to create new 83which enables nodes to manage each other remotely, and to create new
87ports. 84ports.
88 85
89Nodes are either private (single-process only), slaves (can only talk to 86Nodes are either public (have one or more listening ports) or private
90public nodes, but do not need an open port) or public nodes (connectable 87(no listening ports). Private nodes cannot talk to other private nodes
91from any other node). 88currently.
92 89
93=item node ID - C<[a-za-Z0-9_\-.:]+> 90=item node ID - C<[a-za-Z0-9_\-.:]+>
94 91
95A node ID is a string that uniquely identifies the node within a 92A node ID is a string that uniquely identifies the node within a
96network. Depending on the configuration used, node IDs can look like a 93network. Depending on the configuration used, node IDs can look like a
114are expected to be long-running, and at least one of those should always 111are 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 112be 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 113error), they try to re-establish connections to some seednodes again to
117join the network. 114join the network.
118 115
116Apart from being sued for seeding, seednodes are not special in any way -
117every public node can be a seednode.
118
119=back 119=back
120 120
121=head1 VARIABLES/FUNCTIONS 121=head1 VARIABLES/FUNCTIONS
122 122
123=over 4 123=over 4
138 138
139our $VERSION = $AnyEvent::MP::Kernel::VERSION; 139our $VERSION = $AnyEvent::MP::Kernel::VERSION;
140 140
141our @EXPORT = qw( 141our @EXPORT = qw(
142 NODE $NODE *SELF node_of after 142 NODE $NODE *SELF node_of after
143 resolve_node initialise_node 143 configure
144 snd rcv mon mon_guard kil reg psub spawn 144 snd rcv mon mon_guard kil reg psub spawn
145 port 145 port
146); 146);
147 147
148our $SELF; 148our $SELF;
153 kil $SELF, die => $msg; 153 kil $SELF, die => $msg;
154} 154}
155 155
156=item $thisnode = NODE / $NODE 156=item $thisnode = NODE / $NODE
157 157
158The C<NODE> function returns, and the C<$NODE> variable contains the node 158The C<NODE> function returns, and the C<$NODE> variable contains, the node
159ID of the node running in the current process. This value is initialised by 159ID of the node running in the current process. This value is initialised by
160a call to C<initialise_node>. 160a call to C<configure>.
161 161
162=item $nodeid = node_of $port 162=item $nodeid = node_of $port
163 163
164Extracts and returns the node ID part from a port ID or a node ID. 164Extracts and returns the node ID from a port ID or a node ID.
165 165
166=item initialise_node $profile_name 166=item configure key => value...
167 167
168Before a node can talk to other nodes on the network (i.e. enter 168Before a node can talk to other nodes on the network (i.e. enter
169"distributed mode") it has to initialise itself - the minimum a node needs 169"distributed mode") it has to configure itself - the minimum a node needs
170to know is its own name, and optionally it should know the addresses of 170to know is its own name, and optionally it should know the addresses of
171some other nodes in the network to discover other nodes. 171some other nodes in the network to discover other nodes.
172 172
173This function initialises a node - it must be called exactly once (or 173This function configures a node - it must be called exactly once (or
174never) before calling other AnyEvent::MP functions. 174never) before calling other AnyEvent::MP functions.
175 175
176The first argument is a profile name. If it is C<undef> or missing, then 176=over 4
177the current nodename will be used instead (i.e. F<uname -n>).
178 177
178=item step 1, gathering configuration from profiles
179
179The function then looks up the profile in the aemp configuration (see the 180The function first looks up a profile in the aemp configuration (see the
180L<aemp> commandline utility). 181L<aemp> commandline utility). The profile name can be specified via the
182named C<profile> parameter. If it is missing, then the nodename (F<uname
183-n>) will be used as profile name.
184
185The profile data is then gathered as follows:
186
187First, all remaining key => value pairs (all of which are conviniently
188undocumented at the moment) will be interpreted as configuration
189data. Then they will be overwritten by any values specified in the global
190default configuration (see the F<aemp> utility), then the chain of
191profiles chosen by the profile name (and any C<parent> attributes).
192
193That means that the values specified in the profile have highest priority
194and the values specified directly via C<configure> have lowest priority,
195and can only be used to specify defaults.
181 196
182If the profile specifies a node ID, then this will become the node ID of 197If 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 198this 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. 199special node ID of C<anon/> will be replaced by a random node ID.
200
201=item step 2, bind listener sockets
185 202
186The next step is to look up the binds in the profile, followed by binding 203The 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 204aemp protocol listeners on all binds specified (it is possible and valid
188to have no binds, meaning that the node cannot be contacted form the 205to 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 206outside. This means the node cannot talk to other nodes that also have no
190binds, but it can still talk to all "normal" nodes). 207binds, but it can still talk to all "normal" nodes).
191 208
192If the profile does not specify a binds list, then the node ID will be 209If the profile does not specify a binds list, then a default of C<*> is
193treated as if it were of the form C<host:port>, which will be resolved and 210used, meaning the node will bind on a dynamically-assigned port on every
194used as binds list. 211local IP address it finds.
195 212
213=item step 3, connect to seed nodes
214
196Lastly, the seeds list from the profile is passed to the 215As the last step, the seeds list from the profile is passed to the
197L<AnyEvent::MP::Global> module, which will then use it to keep 216L<AnyEvent::MP::Global> module, which will then use it to keep
198connectivity with at least on of those seed nodes at any point in time. 217connectivity with at least one node at any point in time.
199 218
200Example: become a distributed node listening on the guessed noderef, or 219=back
201the one specified via C<aemp> for the current node. This should be the 220
221Example: become a distributed node using the locla node name as profile.
202most common form of invocation for "daemon"-type nodes. 222This should be the most common form of invocation for "daemon"-type nodes.
203 223
204 initialise_node; 224 configure
205 225
206Example: become an anonymous node. This form is often used for commandline 226Example: become an anonymous node. This form is often used for commandline
207clients. 227clients.
208 228
209 initialise_node "anon/"; 229 configure nodeid => "anon/";
210 230
211Example: become a distributed node. If there is no profile of the given 231Example: configure a node using a profile called seed, which si suitable
212name, or no binds list was specified, resolve C<localhost:4044> and bind 232for a seed node as it binds on all local addresses on a fixed port (4040,
213on the resulting addresses. 233customary for aemp).
214 234
215 initialise_node "localhost:4044"; 235 # use the aemp commandline utility
236 # aemp profile seed setnodeid anon/ setbinds '*:4040'
237
238 # then use it
239 configure profile => "seed";
240
241 # or simply use aemp from the shell again:
242 # aemp run profile seed
243
244 # or provide a nicer-to-remember nodeid
245 # aemp run profile seed nodeid "$(hostname)"
216 246
217=item $SELF 247=item $SELF
218 248
219Contains the current port id while executing C<rcv> callbacks or C<psub> 249Contains the current port id while executing C<rcv> callbacks or C<psub>
220blocks. 250blocks.
221 251
222=item SELF, %SELF, @SELF... 252=item *SELF, SELF, %SELF, @SELF...
223 253
224Due to some quirks in how perl exports variables, it is impossible to 254Due to some quirks in how perl exports variables, it is impossible to
225just export C<$SELF>, all the symbols called C<SELF> are exported by this 255just export C<$SELF>, all the symbols named C<SELF> are exported by this
226module, but only C<$SELF> is currently used. 256module, but only C<$SELF> is currently used.
227 257
228=item snd $port, type => @data 258=item snd $port, type => @data
229 259
230=item snd $port, @msg 260=item snd $port, @msg
231 261
232Send the given message to the given port ID, which can identify either 262Send the given message to the given port, which can identify either a
233a local or a remote port, and must be a port ID. 263local or a remote port, and must be a port ID.
234 264
235While the message can be about anything, it is highly recommended to use a 265While the message can be almost anything, it is highly recommended to
236string as first element (a port ID, or some word that indicates a request 266use a string as first element (a port ID, or some word that indicates a
237type etc.). 267request type etc.) and to consist if only simple perl values (scalars,
268arrays, hashes) - if you think you need to pass an object, think again.
238 269
239The message data effectively becomes read-only after a call to this 270The message data logically becomes read-only after a call to this
240function: modifying any argument is not allowed and can cause many 271function: modifying any argument (or values referenced by them) is
241problems. 272forbidden, as there can be considerable time between the call to C<snd>
273and the time the message is actually being serialised - in fact, it might
274never be copied as within the same process it is simply handed to the
275receiving port.
242 276
243The type of data you can transfer depends on the transport protocol: when 277The type of data you can transfer depends on the transport protocol: when
244JSON is used, then only strings, numbers and arrays and hashes consisting 278JSON is used, then only strings, numbers and arrays and hashes consisting
245of those are allowed (no objects). When Storable is used, then anything 279of those are allowed (no objects). When Storable is used, then anything
246that Storable can serialise and deserialise is allowed, and for the local 280that Storable can serialise and deserialise is allowed, and for the local
247node, anything can be passed. 281node, anything can be passed. Best rely only on the common denominator of
282these.
248 283
249=item $local_port = port 284=item $local_port = port
250 285
251Create a new local port object and returns its port ID. Initially it has 286Create a new local port object and returns its port ID. Initially it has
252no callbacks set and will throw an error when it receives messages. 287no callbacks set and will throw an error when it receives messages.
428 $res 463 $res
429 } 464 }
430 } 465 }
431} 466}
432 467
433=item $guard = mon $port, $cb->(@reason) 468=item $guard = mon $port, $cb->(@reason) # call $cb when $port dies
434 469
435=item $guard = mon $port, $rcvport 470=item $guard = mon $port, $rcvport # kill $rcvport when $port dies
436 471
437=item $guard = mon $port 472=item $guard = mon $port # kill $SELF when $port dies
438 473
439=item $guard = mon $port, $rcvport, @msg 474=item $guard = mon $port, $rcvport, @msg # send a message when $port dies
440 475
441Monitor the given port and do something when the port is killed or 476Monitor the given port and do something when the port is killed or
442messages to it were lost, and optionally return a guard that can be used 477messages to it were lost, and optionally return a guard that can be used
443to stop monitoring again. 478to stop monitoring again.
444 479
445C<mon> effectively guarantees that, in the absence of hardware failures, 480C<mon> effectively guarantees that, in the absence of hardware failures,
446that after starting the monitor, either all messages sent to the port 481after starting the monitor, either all messages sent to the port will
447will arrive, or the monitoring action will be invoked after possible 482arrive, or the monitoring action will be invoked after possible message
448message loss has been detected. No messages will be lost "in between" 483loss has been detected. No messages will be lost "in between" (after
449(after the first lost message no further messages will be received by the 484the first lost message no further messages will be received by the
450port). After the monitoring action was invoked, further messages might get 485port). After the monitoring action was invoked, further messages might get
451delivered again. 486delivered again.
452 487
453Note that monitoring-actions are one-shot: once released, they are removed 488Note that monitoring-actions are one-shot: once messages are lost (and a
454and will not trigger again. 489monitoring alert was raised), they are removed and will not trigger again.
455 490
456In the first form (callback), the callback is simply called with any 491In the first form (callback), the callback is simply called with any
457number of C<@reason> elements (no @reason means that the port was deleted 492number of C<@reason> elements (no @reason means that the port was deleted
458"normally"). Note also that I<< the callback B<must> never die >>, so use 493"normally"). Note also that I<< the callback B<must> never die >>, so use
459C<eval> if unsure. 494C<eval> if unsure.
521is killed, the references will be freed. 556is killed, the references will be freed.
522 557
523Optionally returns a guard that will stop the monitoring. 558Optionally returns a guard that will stop the monitoring.
524 559
525This function is useful when you create e.g. timers or other watchers and 560This function is useful when you create e.g. timers or other watchers and
526want to free them when the port gets killed: 561want to free them when the port gets killed (note the use of C<psub>):
527 562
528 $port->rcv (start => sub { 563 $port->rcv (start => sub {
529 my $timer; $timer = mon_guard $port, AE::timer 1, 1, sub { 564 my $timer; $timer = mon_guard $port, AE::timer 1, 1, psub {
530 undef $timer if 0.9 < rand; 565 undef $timer if 0.9 < rand;
531 }); 566 });
532 }); 567 });
533 568
534=cut 569=cut
543 578
544=item kil $port[, @reason] 579=item kil $port[, @reason]
545 580
546Kill the specified port with the given C<@reason>. 581Kill the specified port with the given C<@reason>.
547 582
548If no C<@reason> is specified, then the port is killed "normally" (linked 583If no C<@reason> is specified, then the port is killed "normally" (ports
549ports will not be kileld, or even notified). 584monitoring other ports will not necessarily die because a port dies
585"normally").
550 586
551Otherwise, linked ports get killed with the same reason (second form of 587Otherwise, linked ports get killed with the same reason (second form of
552C<mon>, see below). 588C<mon>, see above).
553 589
554Runtime errors while evaluating C<rcv> callbacks or inside C<psub> blocks 590Runtime errors while evaluating C<rcv> callbacks or inside C<psub> blocks
555will be reported as reason C<< die => $@ >>. 591will be reported as reason C<< die => $@ >>.
556 592
557Transport/communication errors are reported as C<< transport_error => 593Transport/communication errors are reported as C<< transport_error =>
562=item $port = spawn $node, $initfunc[, @initdata] 598=item $port = spawn $node, $initfunc[, @initdata]
563 599
564Creates a port on the node C<$node> (which can also be a port ID, in which 600Creates a port on the node C<$node> (which can also be a port ID, in which
565case it's the node where that port resides). 601case it's the node where that port resides).
566 602
567The port ID of the newly created port is return immediately, and it is 603The port ID of the newly created port is returned immediately, and it is
568permissible to immediately start sending messages or monitor the port. 604possible to immediately start sending messages or to monitor the port.
569 605
570After the port has been created, the init function is 606After the port has been created, the init function is called on the remote
571called. This function must be a fully-qualified function name 607node, in the same context as a C<rcv> callback. This function must be a
572(e.g. C<MyApp::Chat::Server::init>). To specify a function in the main 608fully-qualified function name (e.g. C<MyApp::Chat::Server::init>). To
573program, use C<::name>. 609specify a function in the main program, use C<::name>.
574 610
575If the function doesn't exist, then the node tries to C<require> 611If the function doesn't exist, then the node tries to C<require>
576the package, then the package above the package and so on (e.g. 612the package, then the package above the package and so on (e.g.
577C<MyApp::Chat::Server>, C<MyApp::Chat>, C<MyApp>) until the function 613C<MyApp::Chat::Server>, C<MyApp::Chat>, C<MyApp>) until the function
578exists or it runs out of package names. 614exists or it runs out of package names.
579 615
580The init function is then called with the newly-created port as context 616The init function is then called with the newly-created port as context
581object (C<$SELF>) and the C<@initdata> values as arguments. 617object (C<$SELF>) and the C<@initdata> values as arguments.
582 618
583A common idiom is to pass your own port, monitor the spawned port, and 619A common idiom is to pass a local port, immediately monitor the spawned
584in the init function, monitor the original port. This two-way monitoring 620port, and in the remote init function, immediately monitor the passed
585ensures that both ports get cleaned up when there is a problem. 621local port. This two-way monitoring ensures that both ports get cleaned up
622when there is a problem.
586 623
587Example: spawn a chat server port on C<$othernode>. 624Example: spawn a chat server port on C<$othernode>.
588 625
589 # this node, executed from within a port context: 626 # this node, executed from within a port context:
590 my $server = spawn $othernode, "MyApp::Chat::Server::connect", $SELF; 627 my $server = spawn $othernode, "MyApp::Chat::Server::connect", $SELF;
632=item after $timeout, $callback 669=item after $timeout, $callback
633 670
634Either sends the given message, or call the given callback, after the 671Either sends the given message, or call the given callback, after the
635specified number of seconds. 672specified number of seconds.
636 673
637This is simply a utility function that come sin handy at times. 674This is simply a utility function that comes in handy at times - the
675AnyEvent::MP author is not convinced of the wisdom of having it, though,
676so it may go away in the future.
638 677
639=cut 678=cut
640 679
641sub after($@) { 680sub after($@) {
642 my ($timeout, @action) = @_; 681 my ($timeout, @action) = @_;
708 747
709Erlang makes few guarantees on messages delivery - messages can get lost 748Erlang makes few guarantees on messages delivery - messages can get lost
710without any of the processes realising it (i.e. you send messages a, b, 749without any of the processes realising it (i.e. you send messages a, b,
711and c, and the other side only receives messages a and c). 750and c, and the other side only receives messages a and c).
712 751
713AEMP guarantees correct ordering, and the guarantee that there are no 752AEMP guarantees correct ordering, and the guarantee that after one message
714holes in the message sequence. 753is lost, all following ones sent to the same port are lost as well, until
715 754monitoring raises an error, so there are no silent "holes" in the message
716=item * In Erlang, processes can be declared dead and later be found to be 755sequence.
717alive.
718
719In Erlang it can happen that a monitored process is declared dead and
720linked processes get killed, but later it turns out that the process is
721still alive - and can receive messages.
722
723In AEMP, when port monitoring detects a port as dead, then that port will
724eventually be killed - it cannot happen that a node detects a port as dead
725and then later sends messages to it, finding it is still alive.
726 756
727=item * Erlang can send messages to the wrong port, AEMP does not. 757=item * Erlang can send messages to the wrong port, AEMP does not.
728 758
729In Erlang it is quite likely that a node that restarts reuses a process ID 759In Erlang it is quite likely that a node that restarts reuses a process ID
730known to other nodes for a completely different process, causing messages 760known to other nodes for a completely different process, causing messages
734around in the network will not be sent to an unrelated port. 764around in the network will not be sent to an unrelated port.
735 765
736=item * Erlang uses unprotected connections, AEMP uses secure 766=item * Erlang uses unprotected connections, AEMP uses secure
737authentication and can use TLS. 767authentication and can use TLS.
738 768
739AEMP can use a proven protocol - SSL/TLS - to protect connections and 769AEMP can use a proven protocol - TLS - to protect connections and
740securely authenticate nodes. 770securely authenticate nodes.
741 771
742=item * The AEMP protocol is optimised for both text-based and binary 772=item * The AEMP protocol is optimised for both text-based and binary
743communications. 773communications.
744 774
745The AEMP protocol, unlike the Erlang protocol, supports both 775The AEMP protocol, unlike the Erlang protocol, supports both programming
746language-independent text-only protocols (good for debugging) and binary, 776language independent text-only protocols (good for debugging) and binary,
747language-specific serialisers (e.g. Storable). 777language-specific serialisers (e.g. Storable). By default, unless TLS is
778used, the protocol is actually completely text-based.
748 779
749It has also been carefully designed to be implementable in other languages 780It has also been carefully designed to be implementable in other languages
750with a minimum of work while gracefully degrading fucntionality to make the 781with a minimum of work while gracefully degrading functionality to make the
751protocol simple. 782protocol simple.
752 783
753=item * AEMP has more flexible monitoring options than Erlang. 784=item * AEMP has more flexible monitoring options than Erlang.
754 785
755In Erlang, you can chose to receive I<all> exit signals as messages 786In Erlang, you can chose to receive I<all> exit signals as messages
758Erlang, as one can choose between automatic kill, exit message or callback 789Erlang, as one can choose between automatic kill, exit message or callback
759on a per-process basis. 790on a per-process basis.
760 791
761=item * Erlang tries to hide remote/local connections, AEMP does not. 792=item * Erlang tries to hide remote/local connections, AEMP does not.
762 793
763Monitoring in Erlang is not an indicator of process death/crashes, 794Monitoring in Erlang is not an indicator of process death/crashes, in the
764as linking is (except linking is unreliable in Erlang). 795same way as linking is (except linking is unreliable in Erlang).
765 796
766In AEMP, you don't "look up" registered port names or send to named ports 797In AEMP, you don't "look up" registered port names or send to named ports
767that might or might not be persistent. Instead, you normally spawn a port 798that might or might not be persistent. Instead, you normally spawn a port
768on the remote node. The init function monitors the you, and you monitor 799on the remote node. The init function monitors you, and you monitor the
769the remote port. Since both monitors are local to the node, they are much 800remote port. Since both monitors are local to the node, they are much more
770more reliable. 801reliable (no need for C<spawn_link>).
771 802
772This also saves round-trips and avoids sending messages to the wrong port 803This also saves round-trips and avoids sending messages to the wrong port
773(hard to do in Erlang). 804(hard to do in Erlang).
774 805
775=back 806=back
776 807
777=head1 RATIONALE 808=head1 RATIONALE
778 809
779=over 4 810=over 4
780 811
781=item Why strings for ports and noderefs, why not objects? 812=item Why strings for port and node IDs, why not objects?
782 813
783We considered "objects", but found that the actual number of methods 814We considered "objects", but found that the actual number of methods
784thatc an be called are very low. Since port IDs and noderefs travel over 815that can be called are quite low. Since port and node IDs travel over
785the network frequently, the serialising/deserialising would add lots of 816the network frequently, the serialising/deserialising would add lots of
786overhead, as well as having to keep a proxy object. 817overhead, as well as having to keep a proxy object everywhere.
787 818
788Strings can easily be printed, easily serialised etc. and need no special 819Strings can easily be printed, easily serialised etc. and need no special
789procedures to be "valid". 820procedures to be "valid".
790 821
791And a a miniport consists of a single closure stored in a global hash - it 822And as a result, a miniport consists of a single closure stored in a
792can't become much cheaper. 823global hash - it can't become much cheaper.
793 824
794=item Why favour JSON, why not real serialising format such as Storable? 825=item Why favour JSON, why not a real serialising format such as Storable?
795 826
796In fact, any AnyEvent::MP node will happily accept Storable as framing 827In fact, any AnyEvent::MP node will happily accept Storable as framing
797format, but currently there is no way to make a node use Storable by 828format, but currently there is no way to make a node use Storable by
798default. 829default (although all nodes will accept it).
799 830
800The default framing protocol is JSON because a) JSON::XS is many times 831The default framing protocol is JSON because a) JSON::XS is many times
801faster for small messages and b) most importantly, after years of 832faster for small messages and b) most importantly, after years of
802experience we found that object serialisation is causing more problems 833experience we found that object serialisation is causing more problems
803than it gains: Just like function calls, objects simply do not travel 834than it solves: Just like function calls, objects simply do not travel
804easily over the network, mostly because they will always be a copy, so you 835easily over the network, mostly because they will always be a copy, so you
805always have to re-think your design. 836always have to re-think your design.
806 837
807Keeping your messages simple, concentrating on data structures rather than 838Keeping your messages simple, concentrating on data structures rather than
808objects, will keep your messages clean, tidy and efficient. 839objects, will keep your messages clean, tidy and efficient.
809 840
810=back 841=back
811 842
812=head1 SEE ALSO 843=head1 SEE ALSO
813 844
845L<AnyEvent::MP::Intro> - a gentle introduction.
846
847L<AnyEvent::MP::Kernel> - more, lower-level, stuff.
848
849L<AnyEvent::MP::Global> - network maintainance and port groups, to find
850your applications.
851
814L<AnyEvent>. 852L<AnyEvent>.
815 853
816=head1 AUTHOR 854=head1 AUTHOR
817 855
818 Marc Lehmann <schmorp@schmorp.de> 856 Marc Lehmann <schmorp@schmorp.de>

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines