… | |
… | |
68 | a few simple examples. Later some more complex idioms are introduced, |
68 | a few simple examples. Later some more complex idioms are introduced, |
69 | which are hopefully useful to solve some real world problems. |
69 | which are hopefully useful to solve some real world problems. |
70 | |
70 | |
71 | =head1 Passing Your First Message |
71 | =head1 Passing Your First Message |
72 | |
72 | |
73 | As start lets have a look at the messaging API. The next example is just a |
73 | As a start lets have a look at the messaging API. The following example |
74 | demo to show the basic elements of message passing with L<AnyEvent::MP>. |
74 | is just a demo to show the basic elements of message passing with |
75 | It shout just print: "Ending with: 123". So here the code: |
75 | L<AnyEvent::MP>. |
|
|
76 | |
|
|
77 | The example should print: C<Ending with: 123>, in a rather complicated |
|
|
78 | way, by passing some message to a port. |
76 | |
79 | |
77 | use AnyEvent; |
80 | use AnyEvent; |
78 | use AnyEvent::MP; |
81 | use AnyEvent::MP; |
79 | |
82 | |
80 | my $end_cv = AnyEvent->condvar; |
83 | my $end_cv = AnyEvent->condvar; |
… | |
… | |
88 | |
91 | |
89 | snd $port, test => 123; |
92 | snd $port, test => 123; |
90 | |
93 | |
91 | print "Ending with: " . $end_cv->recv . "\n"; |
94 | print "Ending with: " . $end_cv->recv . "\n"; |
92 | |
95 | |
93 | It already contains most functions of the essential L<AnyEvent::MP> API. |
96 | It already uses most of the essential functions inside |
|
|
97 | L<AnyEvent::MP>: First there is the C<port> function which will create a |
|
|
98 | I<port> and will return it's I<port ID>, a simple string. |
94 | |
99 | |
95 | First there is the C<port> function which will create a I<port> and will return |
100 | This I<port ID> can be used to send messages to the port and install |
96 | it's I<port id>. |
101 | handlers to receive messages on the port. Since it is a simple string |
97 | |
|
|
98 | That I<port id> can be used to send and receive messages. That I<port id> is a |
|
|
99 | simple string and can be safely passed to other I<nodes> in the network to |
102 | it can be safely passed to other I<nodes> in the network when you want |
100 | refer to that specific port (usually used for RPC, where you need to |
103 | to refer to that specific port (usually used for RPC, where you need |
101 | tell the other end which I<port> to send the reply to). |
104 | to tell the other end which I<port> to send the reply to - messages in |
|
|
105 | L<AnyEvent::MP> have a destination, but no source). |
102 | |
106 | |
103 | Next function is C<rcv>: |
107 | The next function is C<rcv>: |
104 | |
108 | |
105 | rcv $port, test => sub { ... }; |
109 | rcv $port, test => sub { ... }; |
106 | |
110 | |
107 | It sets up a receiver callback on a specific I<port> which needs to be |
111 | It installs a receiver callback on the I<port> that specified as the first |
108 | specified as the first argument. The next argument, in this example C<test>, is |
112 | argument (it only works for "local" ports, i.e. ports created on the same |
|
|
113 | node). The next argument, in this example C<test>, specifies a I<tag> to |
109 | a I<tag> match. This means that whenever a message, with the first element |
114 | match. This means that whenever a message with the first element being |
110 | being the string C<tag>, is received the callback is called with the remaining |
115 | the string C<test> is received, the callback is called with the remaining |
111 | parts of that message. |
116 | parts of that message. |
112 | |
117 | |
113 | Messages can be send with the C<snd> function, which looks like this in the |
118 | Messages can be sent with the C<snd> function, which is used like this in |
114 | example above: |
119 | the example above: |
115 | |
120 | |
116 | snd $port, test => 123; |
121 | snd $port, test => 123; |
117 | |
122 | |
118 | This will send the message C<['test', 123]> to the I<port> with the I<port id> |
123 | This will send the message C<'test', 123> to the I<port> with the I<port |
119 | in C<$port>. The receiver got a I<tag> match on C<test> and will call the |
124 | ID> stored in C<$port>. Since in this case the receiver has a I<tag> match |
120 | callback with the first argument being the number C<123>. |
125 | on C<test> it will call the callback with the first argument being the |
|
|
126 | number C<123>. |
121 | |
127 | |
122 | That callback then just passes that number on to the I<condition variable> |
128 | The callback is a typicall AnyEvent idiom: the callback just passes |
123 | C<$end_cv> which will then pass the value to the print. But I<condition |
129 | that number on to the I<condition variable> C<$end_cv> which will then |
124 | variables> are out of the scope of this tutorial. So please consult the |
130 | pass the value to the print. Condition variables are out of the scope |
|
|
131 | of this tutorial and not often used with ports, so please consult the |
125 | L<AnyEvent::Intro> about them. |
132 | L<AnyEvent::Intro> about them. |
126 | |
133 | |
127 | But passing messages inside one process is boring, but before we can continue |
134 | Passing messages inside just one process is boring. Before we can move on |
128 | and take the next step to interprocess message passing we first have to make |
135 | and do interprocess message passing we first have to make sure some things |
129 | sure some things have been setup. |
136 | have been set up correctly for our nodes to talk to each other. |
130 | |
137 | |
131 | =head1 System Requirements and System Setup |
138 | =head1 System Requirements and System Setup |
132 | |
139 | |
133 | Before we can start with real IPC we have to make sure some things work on your |
140 | Before we can start with real IPC we have to make sure some things work on your |
134 | system. |
141 | system. |