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.38 by root, Mon Aug 31 18:45:05 2009 UTC vs.
Revision 1.39 by root, Mon Aug 31 22:14:43 2009 UTC

66 66
67The next sections will explain the API of L<AnyEvent::MP> by going through 67The next sections will explain the API of L<AnyEvent::MP> by going through
68a few simple examples. Later some more complex idioms are introduced, 68a few simple examples. Later some more complex idioms are introduced,
69which are hopefully useful to solve some real world problems. 69which are hopefully useful to solve some real world problems.
70 70
71=head1 Passing Your First Message 71=head2 Passing Your First Message
72 72
73As a start lets have a look at the messaging API. The following example 73As a start lets have a look at the messaging API. The following example
74is just a demo to show the basic elements of message passing with 74is just a demo to show the basic elements of message passing with
75L<AnyEvent::MP>. 75L<AnyEvent::MP>.
76 76
133 133
134Passing messages inside just one process is boring. Before we can move on 134Passing messages inside just one process is boring. Before we can move on
135and do interprocess message passing we first have to make sure some things 135and do interprocess message passing we first have to make sure some things
136have been set up correctly for our nodes to talk to each other. 136have been set up correctly for our nodes to talk to each other.
137 137
138=head1 System Requirements and System Setup 138=head2 System Requirements and System Setup
139 139
140Before we can start with real IPC we have to make sure some things work on 140Before we can start with real IPC we have to make sure some things work on
141your system. 141your system.
142 142
143First we have to setup a I<shared secret>: for two L<AnyEvent::MP> 143First we have to setup a I<shared secret>: for two L<AnyEvent::MP>
899 899
900 warn "server ready.\n"; 900 warn "server ready.\n";
901 901
902 AnyEvent->condvar->recv; 902 AnyEvent->condvar->recv;
903 903
904It starts not much different, except that this time, we register the node 904It starts out not much different then the previous example, except that
905port and not any special port - the clients only want to know which node 905this time, we register the node port in the global group and not any port
906the server should be running, and in fact, they could also sue some kind 906we created - the clients only want to know which node the server should be
907of election mechanism or similar. 907running on. In fact, they could also use some kind of election mechanism,
908to find the node with lowest load or something like that.
908 909
909The interesting change is that no port is created - the server is all 910The more interesting change is that indeed no server port is created -
910code, and does nothing. All it does is define a function C<client_connect> 911the server consists only of code, and "does" nothing by itself. All it
911that expects a client port and a nick as arguments. It then monitors the 912does is define a function C<client_connect>, which expects a client port
912client port and binds a receive callback on C<$SELF> that expects messages 913and a nick name as arguments. It then monitors the client port and binds
914a receive callback on C<$SELF>, which expects messages that in turn are
913to broadcast to all clients. 915broadcast to all clients.
914 916
915The two C<mon> calls are a bit tricky - the first C<mon> is a shorthand 917The two C<mon> calls are a bit tricky - the first C<mon> is a shorthand
916for C<mon $client, $SELF>. The second does the normal "client has gone 918for C<mon $client, $SELF>. The second does the normal "client has gone
917away" clean-up action. Both could actually be rolled into one C<mon> 919away" clean-up action. Both could actually be rolled into one C<mon>
918action. 920action.
919 921
920C<$SELF> is a good hint that something interetsing is going on. And 922C<$SELF> is a good hint that something interesting is going on. And
921indeed, when looking at the client, there is a new function, C<spawn>: 923indeed, when looking at the client code, there is a new function,
924C<spawn>:
922 925
923 use common::sense; 926 use common::sense;
924 use AnyEvent::MP; 927 use AnyEvent::MP;
925 use AnyEvent::MP::Global; 928 use AnyEvent::MP::Global;
926 929
961 964
962 print "> "; 965 print "> ";
963 AnyEvent->condvar->recv; 966 AnyEvent->condvar->recv;
964 967
965The client is quite similar to the previous one, but instead of contacting 968The client is quite similar to the previous one, but instead of contacting
966the server port (which no longer exists), it C<spawn>s a new port on the 969the server I<port> (which no longer exists), it C<spawn>s (creates) a new
967server I<node>: 970the server I<port on node>:
968 971
969 $server = spawn $servernodes->[0], "::client_connect", $client, $nick; 972 $server = spawn $servernodes->[0], "::client_connect", $client, $nick;
970 mon $server, $client; 973 mon $server, $client;
971 974
972And of course immediately monitors it. The C<spawn> function creates a new 975And of course the first thing after creating it is monitoring it.
973port on a remote node and returns its port ID. After creating the port it
974calls a function on the remote node, passing any remaining arguments to
975it, and - most importantly - within the context of the new port. The init
976function can reside in a module (actually it normally I<should> reside
977in a module) - AnyEvent::MP will automatically load the module if the
978function isn't defined.
979 976
977The C<spawn> function creates a new port on a remote node and returns
978its port ID. After creating the port it calls a function on the remote
979node, passing any remaining arguments to it, and - most importantly -
980executes the function within the context of the new port, so it can be
981manipulated by refering to C<$SELF>. The init function can reside in a
982module (actually it normally I<should> reside in a module) - AnyEvent::MP
983will automatically load the module if the function isn't defined.
984
980The C<spawn> function returns immediately, which means you can immediately 985The C<spawn> function returns immediately, which means you can instantly
981send messages to the port, long before the remote node has even heard 986send messages to the port, long before the remote node has even heard
982of our request to create a port on it. In fact, the remote node might 987of our request to create a port on it. In fact, the remote node might
983not even be running. Despite these troubling facts, everything should 988not even be running. Despite these troubling facts, everything should
984work just fine: if the node isn't running (or the init function throws an 989work just fine: if the node isn't running (or the init function throws an
985exception), then the monitor will trigger because the port doesn't exist. 990exception), then the monitor will trigger because the port doesn't exist.
986 991
987If the spawn message gets delivered, but the monitoring message is not 992If the spawn message gets delivered, but the monitoring message is not
988because of network problems (monitoring, after all, is implemented by 993because of network problems (extremely unlikely, but monitoring, after
989passing a message, and messages can get lost), then this connection loss 994all, is implemented by passing a message, and messages can get lost), then
990will eventually trigger the monitoring action. On the remote node (which 995this connection loss will eventually trigger the monitoring action. On the
991reciprocally monitors the client) the port will also be cleaned up on 996remote node (which in return monitors the client) the port will also be
992connection loss. When the node comes up and our monitoring message can be 997cleaned up on connection loss. When the remote node comes up again and our
993delivered it will instantly fail because the port has been cleaned up in 998monitoring message can be delivered, it will instantly fail because the
994the meantime. 999port has been cleaned up in the meantime.
995 1000
996If your head is spinning by now, that's fine - just keep in mind, after 1001If your head is spinning by now, that's fine - just keep in mind, after
997creating a port, monitor "the other side" from it, and all will be cleaned 1002creating a port, monitor it on the local node, and monitor "the other
998up just fine. 1003side" from the remote node, and all will be cleaned up just fine.
999 1004
1000=head2 Services 1005=head2 Services
1001 1006
1002Above it was mentioned that C<spawn> automatically loads modules, and this 1007Above it was mentioned that C<spawn> automatically loads modules, and this
1003can be exploited in various ways. 1008can be exploited in various ways.
1015AnyEvent::MP::Global), then you can configure this service statically: 1020AnyEvent::MP::Global), then you can configure this service statically:
1016 1021
1017 aemp profile mysrvnode services mymod::service:: 1022 aemp profile mysrvnode services mymod::service::
1018 aemp run profile mysrvnode 1023 aemp run profile mysrvnode
1019 1024
1020And the module will automatically be started in the node, as specifying a 1025And the module will automatically be loaded in the node, as specifying a
1021module name (with C<::>-suffix) will simply load the module, which is then 1026module name (with C<::>-suffix) will simply load the module, which is then
1022free to do whatever it wants. 1027free to do whatever it wants.
1023 1028
1024Of course, you can also do it in the much more standard way by writing 1029Of course, you can also do it in the much more standard way by writing
1025a module (e.g. C<BK::Backend::IRC>), installing it as part of a module 1030a module (e.g. C<BK::Backend::IRC>), installing it as part of a module
1026distribution and then configure nodes, for example, if I want to run the 1031distribution and then configure nodes, for example, if I want to run the
1027Bummskraut IRC backend on a machine named "ruth", I could do this: 1032Bummskraut IRC backend on a machine named "ruth", I could do this:
1028 1033
1029 aemp profile ruth addservice BK::Backend::IRC:: 1034 aemp profile ruth addservice BK::Backend::IRC::
1030 1035
1031And any F<aemp run> on that host will automaticlaly have the bummskraut 1036And any F<aemp run> on that host will automaticllay have the bummskraut
1032irc backend running. 1037irc backend running.
1033 1038
1034That's plenty of possibilities you can use - it's all up to you how you 1039That's plenty of possibilities you can use - it's all up to you how you
1035structure your application. 1040structure your application.
1036 1041

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines