… | |
… | |
6 | module, which allows us to transparently pass messages to our own process |
6 | module, which allows us to transparently pass messages to our own process |
7 | and to other processes on another or the same host. |
7 | and to other processes on another or the same host. |
8 | |
8 | |
9 | What kind of messages? Well, basically a message here means a list of Perl |
9 | What kind of messages? Well, basically a message here means a list of Perl |
10 | strings, numbers, hashes and arrays, anything that can be expressed as a |
10 | strings, numbers, hashes and arrays, anything that can be expressed as a |
11 | L<JSON> text (as JSON is used by default in the protocol). |
11 | L<JSON> text (as JSON is used by default in the protocol). Here is an example: |
12 | |
12 | |
13 | It's custom in L<AnyEvent::MP> to have a string which describes the type of the |
13 | [123, ["a", "b", "c"], { foo => 'bar' }] |
|
|
14 | |
|
|
15 | When using L<AnyEvent::MP> you should usually use a string as first element |
|
|
16 | of a message, which describes the type of the |
14 | message as first element (this is called a I<tag> in L<AnyEvent::MP>), as some |
17 | message. That element is called a I<tag> in L<AnyEvent::MP>, as some |
15 | API functions (C<rcv>) support matching it directly. So supposedly you want to |
18 | API functions (C<rcv>) support matching it directly. So supposedly you want to |
16 | send a ping message with your current time to something, this is how such a |
19 | send a ping message with your current time to something, this is how such a |
17 | message might look like (in Perl syntax): |
20 | message might look like (in Perl syntax): |
18 | |
21 | |
19 | ['ping', 1251381636] |
22 | ['ping', 1251381636] |
20 | |
23 | |
21 | And next you might ask: between which entities are those messages being |
24 | The next interesting question is: Between which entities are those messages |
22 | I<passed>? They are I<passed> between I<ports>. I<ports> are just sources and |
25 | being I<passed>? They are I<passed> between I<ports>. I<ports> are just sources |
23 | destinations for messages. How do these ports relate to things you know? Well, |
26 | and destinations for messages. How do these ports relate to things you know? |
24 | each I<port> belongs to a I<node>, and a I<node> is just the UNIX process that |
27 | Well, each I<port> belongs to a I<node>, and a I<node> is just the UNIX process |
25 | runs your L<AnyEvent::MP> application. |
28 | that runs your L<AnyEvent::MP> application. |
26 | |
29 | |
27 | Each I<node> is distinguished from other I<nodes> running on the same host or |
30 | Each I<node> is distinguished from other I<nodes>, that run on the same host or |
28 | multiple hosts in a network by it's I<node ID>. A I<node ID> can be manually |
31 | multiple hosts in a network, by it's I<node ID>. A I<node ID> can be manually |
29 | assigned or L<AnyEvent::MP> will assign one it self for you. |
32 | assigned or L<AnyEvent::MP> will assign one it self for you. |
30 | |
33 | |
31 | So, you might want to visualize it like this (setup is two nodes (more are of |
34 | Here is a visualisation about how I<nodes>, I<ports> and UNIX processes |
|
|
35 | relate to each other. The setup consists of two nodes (more are of |
32 | course possible): Node C<A> (in UNIX process 7066) with ports C<ABC> and C<DEF> |
36 | course possible): Node C<A> (in UNIX process 7066) with the ports C<ABC> and C<DEF>. |
33 | and C<B> (in UNIX process 8321) with ports C<FOO> and C<BAR>). |
37 | And the node C<B> (in UNIX process 8321) with the ports C<FOO> and C<BAR>. |
34 | |
38 | |
35 | |
39 | |
36 | |- PID: 7066 -| |- PID: 8321 -| |
40 | |- PID: 7066 -| |- PID: 8321 -| |
37 | | | | | |
41 | | | | | |
38 | | Node ID: A | | Node ID: B | |
42 | | Node ID: A | | Node ID: B | |
… | |
… | |
41 | | | X | | |
45 | | | X | | |
42 | | Port DEF =|= <----/ \-----> =|= Port BAR | |
46 | | Port DEF =|= <----/ \-----> =|= Port BAR | |
43 | | | | | |
47 | | | | | |
44 | |-------------| |-------------| |
48 | |-------------| |-------------| |
45 | |
49 | |
46 | The strings for the ports here are just for illustrative purposes. Even if in |
50 | The strings for the I<port ids> here are just for illustrative purposes. Even |
47 | reality I<ports> in L<AnyEvent::MP> are also identified by strings they can't |
51 | if in reality I<ports> in L<AnyEvent::MP> are also identified by strings they |
48 | be choosen manually and are assigned randomly. These I<port ids> should also |
52 | can't be choosen manually and are assigned randomly. These I<port ids> should |
49 | not be used directly for other purposes than referring to an endpoint for |
53 | also not be used directly for other purposes than referring to an endpoint for |
50 | messages. |
54 | messages. |
51 | |
55 | |
52 | The next sections will explain the API of L<AnyEvent::MP>. First the API is |
56 | The next sections will explain the API of L<AnyEvent::MP> by going through a |
53 | laid out by simple examples. Later some more complex idioms are introduced, |
57 | few simple examples. Later some more complex idioms are introduced, which are |
54 | which are maybe useful to solve some real world purposes. |
58 | maybe useful to solve some real world problems. |
55 | |
|
|
56 | # In this tutorial I'll show you how to write a simple chat server based on |
|
|
57 | # L<AnyEvent::MP>. This example is used because it nicely shows how to organise a |
|
|
58 | # simple application, but keep in mind that every node trusts any other, so this |
|
|
59 | # chat cannot be used to implement a real public chat server and client system, |
|
|
60 | # but it can be used to implement a distributed chat server for example. |
|
|
61 | |
59 | |
62 | =head1 Passing Your First Message |
60 | =head1 Passing Your First Message |
63 | |
61 | |
64 | As start lets have a look at the messaging API. The next example is just a |
62 | As start lets have a look at the messaging API. The next example is just a |
65 | demo to show the basic elements of message passing with L<AnyEvent::MP>. |
63 | demo to show the basic elements of message passing with L<AnyEvent::MP>. |