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.47 by root, Sun Mar 4 19:45:03 2012 UTC vs.
Revision 1.48 by root, Tue Mar 6 13:33:53 2012 UTC

323new: The first argument is the name of a I<database family> and the second 323new: The first argument is the name of a I<database family> and the second
324argument is the name of a I<subkey> within that family. The third argument 324argument is the name of a I<subkey> within that family. The third argument
325would be the I<value> to be associated with the family and subkey, but, 325would be the I<value> to be associated with the family and subkey, but,
326since it is missing, it will simply be C<undef>. 326since it is missing, it will simply be C<undef>.
327 327
328Ok, what's this weird tlak about families you wonder - AnyEvent::MP comes 328OK, what's this weird talk about families you wonder - AnyEvent::MP comes
329with a distributed database. This database runs on so-called "global" 329with a distributed database. This database runs on so-called "global"
330nodes, which usually are the seed nodes of your network. The database 330nodes, which usually are the seed nodes of your network. The database
331structure is "simply" a hash of hashes of values. 331structure is "simply" a hash of hashes of values.
332 332
333In other words, if the database were stored in C<%DB>, then the C<db_set> 333In other words, if the database were stored in C<%DB>, then the C<db_set>
339is simply the key in this hash. And C<db_set> very much works like an 339is simply the key in this hash. And C<db_set> very much works like an
340assignment. 340assignment.
341 341
342The family namespace is shared by all nodes in a network, so the names 342The family namespace is shared by all nodes in a network, so the names
343should be reasonably unique, for example, they could start with the name 343should be reasonably unique, for example, they could start with the name
344of your module, or the name of the program. 344of your module, or the name of the program, using your port name or node
345name as subkey.
345 346
346The purpose behind adding this key to the database is that the sender can 347The purpose behind adding this key to the database is that the sender can
347look it up and find our port. We will shortly see how. 348look it up and find our port. We will shortly see how.
348 349
349The last step in the example is to set up a receiver callback for those 350The last step in the example is to set up a receiver callback for those
352application after receiving the first message. Instead we continue to wait 353application after receiving the first message. Instead we continue to wait
353for new messages indefinitely. 354for new messages indefinitely.
354 355
355=head2 The Sender 356=head2 The Sender
356 357
357Ok, now let's take a look at the sender code: 358OK, now let's take a look at the sender code:
358 359
359 use AnyEvent; 360 use AnyEvent;
360 use AnyEvent::MP; 361 use AnyEvent::MP;
361 362
362 configure nodeid => "eg_sender/%u", seeds => ["*:4040"]; 363 configure nodeid => "eg_sender/%u", seeds => ["*:4040"];
371 }; 372 };
372 373
373 AnyEvent->condvar->recv; 374 AnyEvent->condvar->recv;
374 375
375It's even less code. The C<configure> serves the same purpose as in the 376It's even less code. The C<configure> serves the same purpose as in the
376receiver, but instead of specifying binds we specify a list of seeds - 377receiver, but instead of specifying binds we specify a list of seeds - the
377the seed happens to be the same as the bind used by the receiver, which 378only seed happens to be the same as the bind used by the receiver, which
378therefore becomes our seed node. 379therefore becomes our seed node.
379 380
380Remember the part about having to wait till things become available? After 381Remember the part about having to wait till things become available? Well,
381configure returns, nothing has been done yet - the node is not connected 382after configure returns, nothing has been done yet - the node is not
382to the network, knows nothing about the database contents, and it can take 383connected to the network, knows nothing about the database contents, and
383ages (for a computer :) for this situation to change. 384it can take ages (for a computer :) for this situation to change.
384 385
385Therefore, the sender waits, in this case by using the C<db_mon> 386Therefore, the sender waits, in this case by using the C<db_mon>
386function. This function registers an interest in a specific database 387function. This function registers an interest in a specific database
387family (C<eg_receivers>). Each time something inside the family changes 388family (in this case C<eg_receivers>). Each time something inside the
388(a key is added, changed or deleted), it will call our callback with the 389family changes (a key is added, changed or deleted), it will call our
389family hash as first argument, and the list of keys as second argument. 390callback with the family hash as first argument, and the list of keys as
391second argument.
390 392
391The callback only checks whether the C<%$family> has is empty - in this 393The callback only checks whether the C<%$family> has is empty - if it is,
392case it does nothing. 394then it doesn't do anything. But eventually the family will contain the
393 395port subkey we set in the sender. Then it will send a message to it (and
394Eventually the family will, however, contain the port we set in the 396any other receiver in the same family). Likewise, should the receiver go
395sender. Then it will send a message to it and any other receiver in the 397away and come back, or should another receiver come up, it will again send
396group. 398a message to all of them.
397 399
398You can experiment by having multiple receivers - you have to change the 400You can experiment by having multiple receivers - you have to change the
399"binds" parameter in the receiver to the seeds used in the sender to start 401"binds" parameter in the receiver to the seeds used in the sender to start
400up additional receivers, but then you can start as many as you like. If 402up additional receivers, but then you can start as many as you like. If
401you specify proper IP addresses for the seeds, you can even run them on 403you specify proper IP addresses for the seeds, you can even run them on
402different computers. 404different computers.
403 405
404Each time you start the sender, it will send a message to all receivers it 406Each time you start the sender, it will send a message to all receivers it
405finds (you have to interrupt it manualy afterwards). 407finds (you have to interrupt it manually afterwards).
406 408
407Things you could try include using C<PERL_ANYEVENT_MP_TRACE=1> to see 409Additional things you could try include using C<PERL_ANYEVENT_MP_TRACE=1>
408which messages are exchanged, or starting the sender first and see how 410to see which messages are exchanged, or starting the sender first and see
409long it takes it to find the receiver. 411how long it takes it to find the receiver.
410 412
411=head3 Splitting Network Configuration and Application Code 413=head3 Splitting Network Configuration and Application Code
412 414
413#TODO# 415#TODO#
414OK, so far, this works. In the real world, however, the person configuring 416OK, so far, this works. In the real world, however, the person configuring

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines