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.43 by root, Sun Aug 9 16:08:16 2009 UTC vs.
Revision 1.49 by root, Thu Aug 13 15:29:58 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; # -OR-
17 initialise_node "localhost:4040"; # -OR-
18 initialise_node "slave/", "localhost:4040"
14 19
15 # ports are message endpoints 20 # ports are message endpoints
16 21
17 # sending messages 22 # sending messages
18 snd $port, type => data...; 23 snd $port, type => data...;
19 snd $port, @msg; 24 snd $port, @msg;
20 snd @msg_with_first_element_being_a_port; 25 snd @msg_with_first_element_being_a_port;
21 26
22 # miniports 27 # creating/using miniports
23 my $miniport = port { my @msg = @_; 0 }; 28 my $miniport = port { my @msg = @_; 0 };
24 29
25 # full ports 30 # creating/using full ports
26 my $port = port; 31 my $port = port;
27 rcv $port, smartmatch => $cb->(@msg); 32 rcv $port, smartmatch => $cb->(@msg);
28 rcv $port, ping => sub { snd $_[0], "pong"; 0 }; 33 rcv $port, ping => sub { snd $_[0], "pong"; 0 };
29 rcv $port, pong => sub { warn "pong received\n"; 0 }; 34 rcv $port, pong => sub { warn "pong received\n"; 0 };
30 35
31 # remote ports
32 my $port = spawn $node, $initfunc, @initdata;
33
34 # more, smarter, matches (_any_ is exported by this module) 36 # more, smarter, matches (_any_ is exported by this module)
35 rcv $port, [child_died => $pid] => sub { ... 37 rcv $port, [child_died => $pid] => sub { ...
36 rcv $port, [_any_, _any_, 3] => sub { .. $_[2] is 3 38 rcv $port, [_any_, _any_, 3] => sub { .. $_[2] is 3
39
40 # create a port on another node
41 my $port = spawn $node, $initfunc, @initdata;
37 42
38 # monitoring 43 # monitoring
39 mon $port, $cb->(@msg) # callback is invoked on death 44 mon $port, $cb->(@msg) # callback is invoked on death
40 mon $port, $otherport # kill otherport on abnormal death 45 mon $port, $otherport # kill otherport on abnormal death
41 mon $port, $otherport, @msg # send message on death 46 mon $port, $otherport, @msg # send message on death
42 47
48=head1 CURRENT STATUS
49
50 AnyEvent::MP - stable API, should work
51 AnyEvent::MP::Intro - outdated
52 AnyEvent::MP::Kernel - WIP
53 AnyEvent::MP::Transport - mostly stable
54
55 stay tuned.
56
43=head1 DESCRIPTION 57=head1 DESCRIPTION
44 58
45This module (-family) implements a simple message passing framework. 59This module (-family) implements a simple message passing framework.
46 60
47Despite its simplicity, you can securely message other processes running 61Despite its simplicity, you can securely message other processes running
50For an introduction to this module family, see the L<AnyEvent::MP::Intro> 64For an introduction to this module family, see the L<AnyEvent::MP::Intro>
51manual page. 65manual page.
52 66
53At the moment, this module family is severly broken and underdocumented, 67At the moment, this module family is severly broken and underdocumented,
54so do not use. This was uploaded mainly to reserve the CPAN namespace - 68so do not use. This was uploaded mainly to reserve the CPAN namespace -
55stay tuned! The basic API should be finished, however. 69stay tuned!
56 70
57=head1 CONCEPTS 71=head1 CONCEPTS
58 72
59=over 4 73=over 4
60 74
105 119
106=cut 120=cut
107 121
108package AnyEvent::MP; 122package AnyEvent::MP;
109 123
110use AnyEvent::MP::Base; 124use AnyEvent::MP::Kernel;
111 125
112use common::sense; 126use common::sense;
113 127
114use Carp (); 128use Carp ();
115 129
116use AE (); 130use AE ();
117 131
118use base "Exporter"; 132use base "Exporter";
119 133
120our $VERSION = $AnyEvent::MP::Base::VERSION; 134our $VERSION = $AnyEvent::MP::Kernel::VERSION;
121 135
122our @EXPORT = qw( 136our @EXPORT = qw(
123 NODE $NODE *SELF node_of _any_ 137 NODE $NODE *SELF node_of _any_
124 resolve_node initialise_node 138 resolve_node initialise_node
125 snd rcv mon kil reg psub spawn 139 snd rcv mon kil reg psub spawn
154it should know the noderefs of some other nodes in the network. 168it should know the noderefs of some other nodes in the network.
155 169
156This function initialises a node - it must be called exactly once (or 170This function initialises a node - it must be called exactly once (or
157never) before calling other AnyEvent::MP functions. 171never) before calling other AnyEvent::MP functions.
158 172
159All arguments are noderefs, which can be either resolved or unresolved. 173All arguments (optionally except for the first) are noderefs, which can be
174either resolved or unresolved.
175
176The first argument will be looked up in the configuration database first
177(if it is C<undef> then the current nodename will be used instead) to find
178the relevant configuration profile (see L<aemp>). If none is found then
179the default configuration is used. The configuration supplies additional
180seed/master nodes and can override the actual noderef.
160 181
161There are two types of networked nodes, public nodes and slave nodes: 182There are two types of networked nodes, public nodes and slave nodes:
162 183
163=over 4 184=over 4
164 185
165=item public nodes 186=item public nodes
166 187
167For public nodes, C<$noderef> must either be a (possibly unresolved) 188For public nodes, C<$noderef> (supplied either directly to
168noderef, in which case it will be resolved, or C<undef> (or missing), in 189C<initialise_node> or indirectly via a profile or the nodename) must be a
169which case the noderef will be guessed. 190noderef (possibly unresolved, in which case it will be resolved).
170 191
171Afterwards, the node will bind itself on all endpoints and try to connect 192After resolving, the node will bind itself on all endpoints and try to
172to all additional C<$seednodes> that are specified. Seednodes are optional 193connect to all additional C<$seednodes> that are specified. Seednodes are
173and can be used to quickly bootstrap the node into an existing network. 194optional and can be used to quickly bootstrap the node into an existing
195network.
174 196
175=item slave nodes 197=item slave nodes
176 198
177When the C<$noderef> is the special string C<slave/>, then the node will 199When the C<$noderef> (either as given or overriden by the config file)
200is the special string C<slave/>, then the node will become a slave
178become a slave node. Slave nodes cannot be contacted from outside and will 201node. Slave nodes cannot be contacted from outside and will route most of
179route most of their traffic to the master node that they attach to. 202their traffic to the master node that they attach to.
180 203
181At least one additional noderef is required: The node will try to connect 204At least one additional noderef is required (either by specifying it
182to all of them and will become a slave attached to the first node it can 205directly or because it is part of the configuration profile): The node
183successfully connect to. 206will try to connect to all of them and will become a slave attached to the
207first node it can successfully connect to.
184 208
185=back 209=back
186 210
187This function will block until all nodes have been resolved and, for slave 211This function will block until all nodes have been resolved and, for slave
188nodes, until it has successfully established a connection to a master 212nodes, until it has successfully established a connection to a master
189server. 213server.
190 214
191Example: become a public node listening on the default node. 215Example: become a public node listening on the guessed noderef, or the one
216specified via C<aemp> for the current node. This should be the most common
217form of invocation for "daemon"-type nodes.
192 218
193 initialise_node; 219 initialise_node;
220
221Example: become a slave node to any of the the seednodes specified via
222C<aemp>. This form is often used for commandline clients.
223
224 initialise_node "slave/";
225
226Example: become a slave node to any of the specified master servers. This
227form is also often used for commandline clients.
228
229 initialise_node "slave/", "master1", "192.168.13.17", "mp.example.net";
194 230
195Example: become a public node, and try to contact some well-known master 231Example: become a public node, and try to contact some well-known master
196servers to become part of the network. 232servers to become part of the network.
197 233
198 initialise_node undef, "master1", "master2"; 234 initialise_node undef, "master1", "master2";
201 237
202 initialise_node 4041; 238 initialise_node 4041;
203 239
204Example: become a public node, only visible on localhost port 4044. 240Example: become a public node, only visible on localhost port 4044.
205 241
206 initialise_node "locahost:4044"; 242 initialise_node "localhost:4044";
207
208Example: become a slave node to any of the specified master servers.
209
210 initialise_node "slave/", "master1", "192.168.13.17", "mp.example.net";
211 243
212=item $cv = resolve_node $noderef 244=item $cv = resolve_node $noderef
213 245
214Takes an unresolved node reference that may contain hostnames and 246Takes an unresolved node reference that may contain hostnames and
215abbreviated IDs, resolves all of them and returns a resolved node 247abbreviated IDs, resolves all of them and returns a resolved node
846This also saves round-trips and avoids sending messages to the wrong port 878This also saves round-trips and avoids sending messages to the wrong port
847(hard to do in Erlang). 879(hard to do in Erlang).
848 880
849=back 881=back
850 882
883=head1 RATIONALE
884
885=over 4
886
887=item Why strings for ports and noderefs, why not objects?
888
889We considered "objects", but found that the actual number of methods
890thatc an be called are very low. Since port IDs and noderefs travel over
891the network frequently, the serialising/deserialising would add lots of
892overhead, as well as having to keep a proxy object.
893
894Strings can easily be printed, easily serialised etc. and need no special
895procedures to be "valid".
896
897And a a miniport consists of a single closure stored in a global hash - it
898can't become much cheaper.
899
900=item Why favour JSON, why not real serialising format such as Storable?
901
902In fact, any AnyEvent::MP node will happily accept Storable as framing
903format, but currently there is no way to make a node use Storable by
904default.
905
906The default framing protocol is JSON because a) JSON::XS is many times
907faster for small messages and b) most importantly, after years of
908experience we found that object serialisation is causing more problems
909than it gains: Just like function calls, objects simply do not travel
910easily over the network, mostly because they will always be a copy, so you
911always have to re-think your design.
912
913Keeping your messages simple, concentrating on data structures rather than
914objects, will keep your messages clean, tidy and efficient.
915
916=back
917
851=head1 SEE ALSO 918=head1 SEE ALSO
852 919
853L<AnyEvent>. 920L<AnyEvent>.
854 921
855=head1 AUTHOR 922=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines