ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/MP/Intro.pod
(Generate patch)

Comparing AnyEvent-MP/MP/Intro.pod (file contents):
Revision 1.6 by root, Mon Aug 3 14:47:25 2009 UTC vs.
Revision 1.7 by elmex, Tue Aug 4 07:58:53 2009 UTC

15processes) that use L<AnyEvent::MP> that run either on the same or different 15processes) that use L<AnyEvent::MP> that run either on the same or different
16hosts. 16hosts.
17 17
18In this tutorial I'll show you how to write a simple chat server based on 18In this tutorial I'll show you how to write a simple chat server based on
19L<AnyEvent::MP>. 19L<AnyEvent::MP>.
20
21=head2 System Requirements
22
23Before we can start we have to make sure some things work on your
24system. First of all the host C<localhost> should resolve to a local
25IP address. Next you should be able to do TCP over that address.
26
27You should of course also make sure that L<AnyEvent> and L<AnyEvent::MP>
28are installed. But how to do that is out of scope of this tutorial.
29
30Then we have to setup a I<shared secret>. For two L<AnyEvent::MP> nodes
31to be able to communicate with each other and authenticate each other
32it is necessary to setup a I<shared secret>. For testing you can write a
33random string followed by a newline into the file C<.aemp-secret> in your
34home directory:
35
36 echo "secret123#4blabla_please_pick_your_own" > ~/.aemp-secret
37
38Only if the nodes that want to connect to each other have the same I<shared
39secret> connections will be successful.
40
41B<If something does not work as expected, and for example tcpdump shows
42that the connections are broken up early, you should make sure that ~/.aemp-secret
43is the same on both hosts/user accounts you are connecting!>
20 44
21=head2 The Chat Client 45=head2 The Chat Client
22 46
23OK, lets start by implementing the "frontend" of the client. We will delay the 47OK, lets start by implementing the "frontend" of the client. We will delay the
24explanation and the code of the server until we finished the client, as the 48explanation and the code of the server until we finished the client, as the
369 393
370This will tell our I<node> to become a I<public> node, which means that it can 394This will tell our I<node> to become a I<public> node, which means that it can
371be contacted via TCP. The first argument should be the I<noderef> the server 395be contacted via TCP. The first argument should be the I<noderef> the server
372wants to be reachable at. In this case it's the TCP port 1299 on localhost. 396wants to be reachable at. In this case it's the TCP port 1299 on localhost.
373 397
374Next we bascially setup two receivers, one for the C<join> messages and 398Next we basically setup two receivers, one for the C<join> messages and
375another one for the actual messages of type C<messsage>. 399another one for the actual messages of type C<messsage>.
376 400
377In the C<join> message we get the client's port, which we just remember in the 401In the C<join> message we get the client's port, which we just remember in the
378C<%client_ports> hash. In the receiver for the message type C<message> we will 402C<%client_ports> hash. In the receiver for the message type C<message> we will
379just iterate through all known C<%client_ports> and relay the message to them. 403just iterate through all known C<%client_ports> and relay the message to them.
389for human-to-human interaction: to know who the other one is :). 413for human-to-human interaction: to know who the other one is :).
390 414
391But aside from these issues I hope this tutorial got you the swing of 415But aside from these issues I hope this tutorial got you the swing of
392L<AnyEvent::MP> and explained some common idioms. 416L<AnyEvent::MP> and explained some common idioms.
393 417
418How to solve the reliability and C<%client_ports> cleanup problem will
419be explained later in this tutorial (TODO).
420
421=head2 Inside The Protocol
422
423Now, for the interested parties, let me explain some details about the protocol
424that L<AnyEvent::MP> nodes use to communicate to each other. If you are not
425interested you can skip this section.
426
427Usually TCP is used for communication. Each I<node>, if configured to be a
428I<public> node with the C<become_public> function will listen on the configured
429TCP port (default is usually 4040).
430
431If now one I<node> wants to send a message to another I<node> it will connect
432to the host and port given in the I<port id>.
433
434Then some handshaking occurs to check whether both I<nodes> have the same
435I<shared secret> configured. Optionally even TLS can be enabled (about how to
436do this exactly please consult the L<AnyEvent::MP> man pages, just a hint: It
437should be enough to put the private key and (self signed) certificate in the
438C<~/.aemp-secret> file of all nodes).
439
440Now the serialized (usually L<JSON> is used for this, but it is also possible
441to use other serialization formats, like L<Storable>) messages are sent over
442the wire using a simple framing protocol.
443
394=head1 SEE ALSO 444=head1 SEE ALSO
395 445
396L<AnyEvent> 446L<AnyEvent>
397 447
398L<AnyEvent::Handle> 448L<AnyEvent::Handle>

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines