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.15 by root, Fri Aug 14 23:19:20 2009 UTC vs.
Revision 1.16 by elmex, Wed Aug 26 14:02:11 2009 UTC

13And next you might ask: between which entities are those messages being 13And next you might ask: between which entities are those messages being
14"passed"? Physically within or between I<nodes>: a nodes is basically a 14"passed"? Physically within or between I<nodes>: a nodes is basically a
15process/program that use L<AnyEvent::MP> and can run either on the same or 15process/program that use L<AnyEvent::MP> and can run either on the same or
16different hosts. 16different hosts.
17 17
18To make this more managable, every node can contain any number of 18To make this more manageable, every node can contain any number of
19I<ports>: Ports are ultimately the receivers of your messages. 19I<ports>: Ports are ultimately the receivers of your messages.
20 20
21In this tutorial I'll show you how to write a simple chat server based 21In this tutorial I'll show you how to write a simple chat server based on
22on L<AnyEvent::MP>. This example is used because it nicely shows how to 22L<AnyEvent::MP>. This example is used because it nicely shows how to organise a
23organise a simple application, but keep in mind that every node trusts any 23simple application, but keep in mind that every node trusts any other, so this
24other, so this chat cannot be used to implement a real chat server and 24chat cannot be used to implement a real public chat server and client system,
25client system, but it can be used to implement a distributed chat server 25but it can be used to implement a distributed chat server for example.
26for example.
27 26
28=head1 System Requirements and System Setup 27=head1 System Requirements and System Setup
29 28
30Before we can start we have to make sure some things work on your 29Before we can start we have to make sure some things work on your
31system. 30system.
33You should of course first make sure that L<AnyEvent> and L<AnyEvent::MP> 32You should of course first make sure that L<AnyEvent> and L<AnyEvent::MP>
34are installed. But how to do that is out of scope of this tutorial. 33are installed. But how to do that is out of scope of this tutorial.
35 34
36Then we have to setup a I<shared secret>: for two L<AnyEvent::MP> nodes to 35Then we have to setup a I<shared secret>: for two L<AnyEvent::MP> nodes to
37be able to communicate with each other and authenticate each other it is 36be able to communicate with each other and authenticate each other it is
38necessary to setup the same I<shared secret> for both of them (use use TLS 37necessary to setup the same I<shared secret> for both of them (or use TLS
39certificates). 38certificates).
40 39
41The easiest way is to set this up is to use the F<aemp> utility: 40The easiest way is to set this up is to use the F<aemp> utility:
42 41
43 aemp gensecret 42 aemp gensecret
52Connections will only be successful when the nodes that want to connect to 51Connections will only be successful when the nodes that want to connect to
53each other have the same I<shared secret> (or successfully verify the TLS 52each other have the same I<shared secret> (or successfully verify the TLS
54certificate of the other side). 53certificate of the other side).
55 54
56B<If something does not work as expected, and for example tcpdump shows 55B<If something does not work as expected, and for example tcpdump shows
57that the connections are closed almost immediatly, you should make sure 56that the connections are closed almost immediately, you should make sure
58that F<~/.perl-anyevent-mp> is the same on all hosts/user accounts that 57that F<~/.perl-anyevent-mp> is the same on all hosts/user accounts that
59you try to connect with each other!> 58you try to connect with each other!>
59
60=head1 Passing Your First Message
61
62As start lets have a look at the messaging API. The next example is just a
63demo to show the basic elements of message passing with L<AnyEvent::MP>.
64It shout just print: "Ending with: 123". So here the code:
65
66 use AnyEvent;
67 use AnyEvent::MP;
68
69 initialise_node;
70
71 my $end_cv = AnyEvent->condvar;
72
73 my $port = port;
74
75 rcv $port, test => sub {
76 my ($data) = @_;
77 $end_cv->send ($data);
78 };
79
80 snd $port, test => 123;
81
82 print "Ending with: " . $end_cv->recv . "\n";
83
84It already contains lots of the API that we are going to use. First there
85is C<initialise_node>, which will initialise the L<AnyEvent::MP> node for that
86process.
87
88Next there is the C<port> function which will create a I<port id> for us, where
89we can wait for messages and send messages to. The port id is a simple string,
90which acts as identifier for a port, with the form C<noderef#portname>. The
91I<noderef> is basically a string that refers to the node and also contains
92enough information to contact the node from the outside. The I<portname> is
93usually just a random string.
94
95Next the call to C<rcv> sets up a receiver callback. The first element in
96every message is usually denoting it's I<type> or I<tag>. Which should be a
97simple string, the second argument to C<rcv> lets us match the tag of a
98message. If it matches, the callback will be called, with the remaining
99elements of the message as arguments.
100
101C<snd> sends a message. The message consists of two elements: The string
102C<'test'> and the number C<123>.
103
104The message arrives in the callback we setup with C<rcv> and will trigger the
105condition variable C<$end_cv> to deliver the result value and end the program.
60 106
61=head1 The Chat Client 107=head1 The Chat Client
62 108
63OK, lets start by implementing the "frontend" of the client. We will 109OK, lets start by implementing the "frontend" of the client. We will
64develop the client first and postpone the server for later, as the most 110develop the client first and postpone the server for later, as the most

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines