… | |
… | |
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 |
… | |
… | |
163 | it should know the noderefs of some other nodes in the network. |
168 | it should know the noderefs of some other nodes in the network. |
164 | |
169 | |
165 | This function initialises a node - it must be called exactly once (or |
170 | This function initialises a node - it must be called exactly once (or |
166 | never) before calling other AnyEvent::MP functions. |
171 | never) before calling other AnyEvent::MP functions. |
167 | |
172 | |
168 | All arguments are noderefs, which can be either resolved or unresolved. |
173 | All arguments (optionally except for the first) are noderefs, which can be |
|
|
174 | either resolved or unresolved. |
|
|
175 | |
|
|
176 | The 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 |
|
|
178 | the relevant configuration profile (see L<aemp>). If none is found then |
|
|
179 | the default configuration is used. The configuration supplies additional |
|
|
180 | seed/master nodes and can override the actual noderef. |
169 | |
181 | |
170 | There are two types of networked nodes, public nodes and slave nodes: |
182 | There are two types of networked nodes, public nodes and slave nodes: |
171 | |
183 | |
172 | =over 4 |
184 | =over 4 |
173 | |
185 | |
174 | =item public nodes |
186 | =item public nodes |
175 | |
187 | |
176 | For public nodes, C<$noderef> must either be a (possibly unresolved) |
188 | For public nodes, C<$noderef> (supplied either directly to |
177 | noderef, in which case it will be resolved, or C<undef> (or missing), in |
189 | C<initialise_node> or indirectly via a profile or the nodename) must be a |
178 | which case the noderef will be guessed. |
190 | noderef (possibly unresolved, in which case it will be resolved). |
179 | |
191 | |
180 | Afterwards, the node will bind itself on all endpoints and try to connect |
192 | After resolving, the node will bind itself on all endpoints and try to |
181 | to all additional C<$seednodes> that are specified. Seednodes are optional |
193 | connect to all additional C<$seednodes> that are specified. Seednodes are |
182 | and can be used to quickly bootstrap the node into an existing network. |
194 | optional and can be used to quickly bootstrap the node into an existing |
|
|
195 | network. |
183 | |
196 | |
184 | =item slave nodes |
197 | =item slave nodes |
185 | |
198 | |
186 | When the C<$noderef> is the special string C<slave/>, then the node will |
199 | When the C<$noderef> (either as given or overriden by the config file) |
|
|
200 | is the special string C<slave/>, then the node will become a slave |
187 | become a slave node. Slave nodes cannot be contacted from outside and will |
201 | node. Slave nodes cannot be contacted from outside and will route most of |
188 | route most of their traffic to the master node that they attach to. |
202 | their traffic to the master node that they attach to. |
189 | |
203 | |
190 | At least one additional noderef is required: The node will try to connect |
204 | At least one additional noderef is required (either by specifying it |
191 | to all of them and will become a slave attached to the first node it can |
205 | directly or because it is part of the configuration profile): The node |
192 | successfully connect to. |
206 | will try to connect to all of them and will become a slave attached to the |
|
|
207 | first node it can successfully connect to. |
193 | |
208 | |
194 | =back |
209 | =back |
195 | |
210 | |
196 | This function will block until all nodes have been resolved and, for slave |
211 | 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 |
212 | nodes, until it has successfully established a connection to a master |
198 | server. |
213 | server. |
199 | |
214 | |
200 | Example: become a public node listening on the default node. |
215 | Example: become a public node listening on the guessed noderef, or the one |
|
|
216 | specified via C<aemp> for the current node. This should be the most common |
|
|
217 | form of invocation for "daemon"-type nodes. |
201 | |
218 | |
202 | initialise_node; |
219 | initialise_node; |
|
|
220 | |
|
|
221 | Example: become a slave node to any of the the seednodes specified via |
|
|
222 | C<aemp>. This form is often used for commandline clients. |
|
|
223 | |
|
|
224 | initialise_node "slave/"; |
|
|
225 | |
|
|
226 | Example: become a slave node to any of the specified master servers. This |
|
|
227 | form is also often used for commandline clients. |
|
|
228 | |
|
|
229 | initialise_node "slave/", "master1", "192.168.13.17", "mp.example.net"; |
203 | |
230 | |
204 | Example: become a public node, and try to contact some well-known master |
231 | Example: become a public node, and try to contact some well-known master |
205 | servers to become part of the network. |
232 | servers to become part of the network. |
206 | |
233 | |
207 | initialise_node undef, "master1", "master2"; |
234 | initialise_node undef, "master1", "master2"; |
… | |
… | |
210 | |
237 | |
211 | initialise_node 4041; |
238 | initialise_node 4041; |
212 | |
239 | |
213 | Example: become a public node, only visible on localhost port 4044. |
240 | Example: become a public node, only visible on localhost port 4044. |
214 | |
241 | |
215 | initialise_node "locahost:4044"; |
242 | 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 | |
243 | |
221 | =item $cv = resolve_node $noderef |
244 | =item $cv = resolve_node $noderef |
222 | |
245 | |
223 | Takes an unresolved node reference that may contain hostnames and |
246 | Takes an unresolved node reference that may contain hostnames and |
224 | abbreviated IDs, resolves all of them and returns a resolved node |
247 | abbreviated IDs, resolves all of them and returns a resolved node |