… | |
… | |
135 | and do interprocess message passing we first have to make sure some things |
135 | and do interprocess message passing we first have to make sure some things |
136 | have been set up correctly for our nodes to talk to each other. |
136 | have been set up correctly for our nodes to talk to each other. |
137 | |
137 | |
138 | =head1 System Requirements and System Setup |
138 | =head1 System Requirements and System Setup |
139 | |
139 | |
140 | 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 |
141 | system. |
141 | your system. |
142 | |
142 | |
143 | First we have to setup a I<shared secret>: for two L<AnyEvent::MP> I<nodes> to |
143 | First we have to setup a I<shared secret>: for two L<AnyEvent::MP> |
144 | be able to communicate with each other and authenticate each other it is |
144 | I<nodes> to be able to communicate with each other over the network it is |
145 | necessary to setup the same I<shared secret> for both of them (or use TLS |
145 | necessary to setup the same I<shared secret> for both of them, so they can |
146 | certificates). |
146 | prove their trustworthyness to each other. |
147 | |
147 | |
148 | The easiest way is to set this up is to use the F<aemp> utility: |
148 | The easiest way is to set this up is to use the F<aemp> utility: |
149 | |
149 | |
150 | aemp gensecret |
150 | aemp gensecret |
151 | |
151 | |
152 | This creates a F<$HOME/.perl-anyevent-mp> config file and generates a random |
152 | This creates a F<$HOME/.perl-anyevent-mp> config file and generates a |
153 | shared secret. You can copy this file to any other system and then communicate |
153 | random shared secret. You can copy this file to any other system and |
154 | over the network (via TCP) with it. You can also select your own shared secret |
154 | then communicate over the network (via TCP) with it. You can also select |
155 | (F<aemp setsecret>) and for increased security requirements you can even create |
155 | your own shared secret (F<aemp setsecret>) and for increased security |
156 | a TLS certificate (F<aemp gencert>), causing connections to not just be |
156 | requirements you can even create (or configure) a TLS certificate (F<aemp |
157 | authenticated, but also to be encrypted. |
157 | gencert>), causing connections to not just be securely authenticated, but |
|
|
158 | also to be encrypted and protected against tinkering. |
158 | |
159 | |
159 | Connections will only be successful when the I<nodes> that want to connect to |
160 | Connections will only be successfully established when the I<nodes> |
160 | each other have the same I<shared secret> (or successfully verify the TLS |
161 | that want to connect to each other have the same I<shared secret> (or |
161 | certificate of the other side). |
162 | successfully verify the TLS certificate of the other side, in which case |
|
|
163 | no shared secret is required). |
162 | |
164 | |
163 | B<If something does not work as expected, and for example tcpdump shows |
165 | B<If something does not work as expected, and for example tcpdump shows |
164 | that the connections are closed almost immediately, you should make sure |
166 | that the connections are closed almost immediately, you should make sure |
165 | that F<~/.perl-anyevent-mp> is the same on all hosts/user accounts that |
167 | that F<~/.perl-anyevent-mp> is the same on all hosts/user accounts that |
166 | you try to connect with each other!> |
168 | you try to connect with each other!> |
167 | |
169 | |
168 | Thats all for now, there is more fiddling around with the C<aemp> utility |
170 | Thats is all for now, you will find some more advanced fiddling with the |
169 | later. |
171 | C<aemp> utility later. |
|
|
172 | |
170 | |
173 | |
171 | =head1 Passing Messages Between Processes |
174 | =head1 Passing Messages Between Processes |
172 | |
175 | |
173 | =head2 The Receiver |
176 | =head2 The Receiver |
174 | |
177 | |
175 | Lets split the previous example up into two small programs. First the |
178 | Lets split the previous example up into two programs: one that contains |
176 | receiver application: |
179 | the sender and one for the receiver. First the receiver application, in |
|
|
180 | full: |
177 | |
181 | |
178 | #!/opt/perl/bin/perl |
|
|
179 | use AnyEvent; |
182 | use AnyEvent; |
180 | use AnyEvent::MP; |
183 | use AnyEvent::MP; |
181 | use AnyEvent::MP::Global; |
184 | use AnyEvent::MP::Global; |
182 | |
185 | |
183 | initialise_node "eg_simple_receiver"; |
186 | initialise_node "eg_simple_receiver"; |
… | |
… | |
194 | |
197 | |
195 | AnyEvent->condvar->recv; |
198 | AnyEvent->condvar->recv; |
196 | |
199 | |
197 | =head3 AnyEvent::MP::Global |
200 | =head3 AnyEvent::MP::Global |
198 | |
201 | |
199 | Now, that wasn't too bad, was it? Ok, lets step through the new functions |
202 | Now, that wasn't too bad, was it? Ok, let's step through the new functions |
200 | and modules that have been used. For starters there is now an additional |
203 | and modules that have been used. |
201 | module loaded: L<AnyEvent::MP::Global>. |
|
|
202 | |
204 | |
203 | That module provides us with a I<global registry>, which lets us share data |
205 | For starters, there is now an additional module being |
204 | among all I<nodes> in a network. Why do we need it you might ask? |
206 | used: L<AnyEvent::MP::Global>. This module provides us with a I<global |
|
|
207 | registry>, which lets us register ports in groups that are visible on all |
|
|
208 | I<nodes> in a network. |
205 | |
209 | |
206 | The thing is, that the I<port ids> are just random strings, assigned by |
210 | What is this useful for? Well, the I<port IDs> are random-looking strings, |
207 | L<AnyEvent::MP>. We can't know those I<port ids> in advance, so we don't know |
211 | assigned by L<AnyEvent::MP>. We cannot know those I<port IDs> in advance, |
208 | which I<port id> to send messages to if the message is to be passed between |
212 | so we don't know which I<port ID> to send messages to, especially when the |
209 | I<nodes> (or UNIX processes). To find the right I<port> of another I<node> in |
213 | message is to be passed between different I<nodes> (or UNIX processes). To |
210 | the network we will need to communicate that somehow to the sender. And |
214 | find the right I<port> of another I<node> in the network we will need |
|
|
215 | to communicate this somehow to the sender. And exactly that is what |
211 | exactly that is what L<AnyEvent::MP::Global> provides. |
216 | L<AnyEvent::MP::Global> provides. |
212 | |
217 | |
|
|
218 | Especially in larger, more anonymous networks this is handy: imagine you |
|
|
219 | have a few database backends, a few web frontends and some processing |
|
|
220 | distributed over a number of hosts: all of these would simply register |
|
|
221 | themselves in the appropriate group, and your web frontends can start to |
|
|
222 | find some database backend. |
|
|
223 | |
213 | =head3 initialise_node And The Network |
224 | =head3 C<initialise_node> And The Network |
214 | |
225 | |
215 | Now, lets have a look at the next new thing, the C<initialise_node>: |
226 | Now, lets have a look at the next new thing, the C<initialise_node>: |
216 | |
227 | |
217 | initialise_node "eg_simple_receiver"; |
228 | initialise_node "eg_simple_receiver"; |
218 | |
229 | |