… | |
… | |
222 | print "Received data: " . $data . "\n"; |
222 | print "Received data: " . $data . "\n"; |
223 | }; |
223 | }; |
224 | |
224 | |
225 | AnyEvent->condvar->recv; |
225 | AnyEvent->condvar->recv; |
226 | |
226 | |
227 | =head3 AnyEvent::MP::Global |
|
|
228 | |
|
|
229 | Now, that wasn't too bad, was it? OK, let's step through the new functions |
227 | Now, that wasn't too bad, was it? OK, let's go through the new functions |
230 | that have been used. |
228 | that have been used. |
231 | |
229 | |
232 | =head3 C<configure> and Joining and Maintaining the Network |
230 | =head3 C<configure> and Joining and Maintaining the Network |
233 | |
231 | |
234 | First let's have a look at C<configure>: |
232 | First let's have a look at C<configure>: |
… | |
… | |
317 | |
315 | |
318 | my $port = port; |
316 | my $port = port; |
319 | db_set eg_receivers => $port; |
317 | db_set eg_receivers => $port; |
320 | |
318 | |
321 | The C<port> function has already been discussed. It simply creates a new |
319 | The C<port> function has already been discussed. It simply creates a new |
322 | I<port> and returns the I<port ID>. The C<db_reg> function, however, is |
320 | I<port> and returns the I<port ID>. The C<db_set> function, however, is |
323 | new: The first argument is the name of a I<database family> and the second |
321 | new: The first argument is the name of a I<database family> and the second |
324 | argument is the name of a I<subkey> within that family. The third argument |
322 | argument is the name of a I<subkey> within that family. The third argument |
325 | would be the I<value> to be associated with the family and subkey, but, |
323 | would be the I<value> to be associated with the family and subkey, but, |
326 | since it is missing, it will simply be C<undef>. |
324 | since it is missing, it will simply be C<undef>. |
327 | |
325 | |
328 | OK, what's this weird talk about families you wonder - AnyEvent::MP comes |
326 | What is a "family" you wonder? Well, AnyEvent::MP comes with a distributed |
329 | with a distributed database. This database runs on so-called "global" |
327 | database. This database runs on so-called "global" nodes, which usually |
330 | nodes, which usually are the seed nodes of your network. The database |
328 | are the seed nodes of your network. The database structure is "simply" a |
331 | structure is "simply" a hash of hashes of values. |
329 | hash of hashes of values. |
332 | |
330 | |
333 | In other words, if the database were stored in C<%DB>, then the C<db_set> |
331 | To illustrate this with Perl syntax, assume the database was stored in |
334 | function more or less would do this: |
332 | C<%DB>, then the C<db_set> function more or less would do this: |
335 | |
333 | |
336 | $DB{eg_receivers}{$port} = undef; |
334 | $DB{eg_receivers}{$port} = undef; |
337 | |
335 | |
338 | So the ominous "family" selects a hash in the database, and the "subkey" |
336 | So the ominous "family" selects a hash in the database, and the "subkey" |
339 | is simply the key in this hash. And C<db_set> very much works like an |
337 | is simply the key in this hash - C<db_set> very much works like this |
340 | assignment. |
338 | assignment. |
341 | |
339 | |
342 | The family namespace is shared by all nodes in a network, so the names |
340 | The family namespace is shared by all nodes in a network, so the names |
343 | should be reasonably unique, for example, they could start with the name |
341 | should be reasonably unique, for example, they could start with the name |
344 | of your module, or the name of the program, using your port name or node |
342 | of your module, or the name of the program, using your port name or node |
… | |
… | |
404 | different computers. |
402 | different computers. |
405 | |
403 | |
406 | Each time you start the sender, it will send a message to all receivers it |
404 | Each time you start the sender, it will send a message to all receivers it |
407 | finds (you have to interrupt it manually afterwards). |
405 | finds (you have to interrupt it manually afterwards). |
408 | |
406 | |
409 | Additional things you could try include using C<PERL_ANYEVENT_MP_TRACE=1> |
407 | Additional experiments you could try include using |
410 | to see which messages are exchanged, or starting the sender first and see |
408 | C<PERL_ANYEVENT_MP_TRACE=1> to see which messages are exchanged, or |
411 | how long it takes it to find the receiver. |
409 | starting the sender before the receiver and see how long it then takes to |
|
|
410 | find the receiver. |
412 | |
411 | |
413 | =head3 Splitting Network Configuration and Application Code |
412 | =head3 Splitting Network Configuration and Application Code |
414 | |
413 | |
415 | OK, so far, this works reasonably. In the real world, however, the person |
414 | OK, so far, this works reasonably. In the real world, however, the person |
416 | configuring your application to run on a specific network (the end user |
415 | configuring your application to run on a specific network (the end user |
… | |
… | |
442 | We bind the seed node to port 4040 on all interfaces: |
441 | We bind the seed node to port 4040 on all interfaces: |
443 | |
442 | |
444 | aemp profile seed binds "*:4040" |
443 | aemp profile seed binds "*:4040" |
445 | |
444 | |
446 | And we configure all nodes to use this as seed node (this only works when |
445 | And we configure all nodes to use this as seed node (this only works when |
447 | running on the same host, for multiple machines you would provide the IP |
446 | running on the same host, for multiple machines you would replace the C<*> |
448 | address or hostname of the node running the seed), by changing the global |
447 | by the IP address or hostname of the node running the seed), by changing |
449 | settings shared between all profiles: |
448 | the global settings shared between all profiles: |
450 | |
449 | |
451 | aemp seeds "*:4040" |
450 | aemp seeds "*:4040" |
452 | |
451 | |
453 | Then we run the seed node: |
452 | Then we run the seed node: |
454 | |
453 | |
… | |
… | |
533 | warn "$port was killed (with reason @_)"; |
532 | warn "$port was killed (with reason @_)"; |
534 | }; |
533 | }; |
535 | |
534 | |
536 | AnyEvent->condvar->recv; |
535 | AnyEvent->condvar->recv; |
537 | |
536 | |
538 | This time we will get something like: |
537 | This time we will get something else: |
539 | |
538 | |
|
|
539 | 2012-03-21 00:50:36 <2> unmonitored local port fADb died with reason: die oops at - line 3. |
540 | anon/zpX.a was killed (with reason no_such_port cannot monitor nonexistent port) |
540 | anon/fADb was killed (with reason no_such_port cannot monitor nonexistent port) |
541 | |
541 | |
542 | Since the port was already gone, the kill reason is now C<no_such_port> |
542 | The first line is a warning that is printed when a port dies that isn't |
543 | with some descriptive (we hope) error message. |
543 | being monitored, because that is normally a bug. When later a C<mon> is |
|
|
544 | attempted, it is immediately killed, because the port is already gone. The |
|
|
545 | kill reason is now C<no_such_port> with some descriptive (we hope) error |
|
|
546 | message. |
544 | |
547 | |
545 | In fact, the kill reason is usually some identifier as first argument |
548 | As you probably suspect from these examples, the kill reason is usually |
546 | and a human-readable error message as second argument, but can be about |
549 | some identifier as first argument and a human-readable error message as |
547 | anything (it is simply a list of values you cna choose yourself) or even |
550 | second argument - all kill reasons by AnyEvent::MP itself follow this |
|
|
551 | pattern. But the kill reason can be anything: it is simply a list of |
|
|
552 | values you can choose yourself. It can even be nothing (an empty list) - |
548 | nothing - which is called a "normal" kill. |
553 | this is called a "normal" kill. |
549 | |
554 | |
550 | You can kill ports manually using the C<kil> function, which will be |
555 | Apart from die'ing, you can kill ports manually using the C<kil> |
551 | treated like an error when any reason is specified: |
556 | function. Using the C<kil> function will be treated like an error when a |
|
|
557 | non-empty reason is specified: |
552 | |
558 | |
553 | kil $port, custom_error => "don't like your steenking face"; |
559 | kil $port, custom_error => "don't like your steenking face"; |
554 | |
560 | |
555 | And a clean kill without any reason arguments: |
561 | And a I<normal> kill without any reason arguments: |
556 | |
562 | |
557 | kil $port; |
563 | kil $port; |
558 | |
564 | |
559 | By now you probably wonder what this "normal" kill business is: A common |
565 | By now you probably wonder what this "normal" kill business is: A common |
560 | idiom is to not specify a callback to C<mon>, but another port, such as |
566 | idiom is to not specify a callback to C<mon>, but another port, such as |
561 | C<$SELF>: |
567 | C<$SELF>: |
562 | |
568 | |
563 | mon $port, $SELF; |
569 | mon $port, $SELF; |
564 | |
570 | |
565 | This basically means "monitor $port and kill me when it crashes". And a |
571 | This basically means "monitor $port and kill me when it crashes" - and |
566 | "normal" kill does not count as a crash. This way you can easily link |
572 | the thing is, a "normal" kill does not count as a crash. This way you can |
567 | ports together and make them crash together on errors, while allowing you |
573 | easily link ports together and make them crash together on errors, while |
568 | to remove a port silently. |
574 | allowing you to remove a port silently when it has done it's job properly. |
569 | |
575 | |
570 | =head3 Port Context |
576 | =head3 Port Context |
571 | |
577 | |
572 | When code runs in a port context, that means C<$SELF> contains its own |
578 | Code runs in the so-called "port context". That means C<$SELF> contains |
573 | port ID and exceptions that the code throws will be caught. |
579 | its own port ID and exceptions that the code throws will be caught. |
574 | |
580 | |
575 | Since AnyEvent::MP is event-based, it is not uncommon to register |
581 | Since AnyEvent::MP is event-based, it is not uncommon to register |
576 | callbacks from C<rcv> handlers. As example, assume that the port receive |
582 | callbacks from within C<rcv> handlers. As example, assume that the |
577 | handler wants to C<die> a second later, using C<after>: |
583 | following port receive handler wants to C<die> a second later, using |
|
|
584 | C<after>: |
578 | |
585 | |
579 | my $port = port { |
586 | my $port = port { |
580 | after 1, sub { die "oops" }; |
587 | after 1, sub { die "oops" }; |
581 | }; |
588 | }; |
582 | |
589 | |
583 | Then you will find it does not work - when the after callback is executed, |
590 | If you try this out, you would find it does not work - when the C<after> |
584 | it does not run in port context anymore, so exceptions will not be caught. |
591 | callback is executed, it does not run in the port context anymore, so |
|
|
592 | exceptions will not be caught. |
585 | |
593 | |
586 | For these cases, AnyEvent::MP exports a special "closure constructor" |
594 | For these cases, AnyEvent::MP exports a special "closure constructor" |
587 | called C<psub>, which works just like perls built-in C<sub>: |
595 | called C<psub>, which works mostly like perl's built-in C<sub>: |
588 | |
596 | |
589 | my $port = port { |
597 | my $port = port { |
590 | after 1, psub { die "oops" }; |
598 | after 1, psub { die "oops" }; |
591 | }; |
599 | }; |
592 | |
600 | |
593 | C<psub> stores C<$SELF> and returns a code reference. When the code |
601 | C<psub> remembers the port context and returns a code reference. When the |
594 | reference is invoked, it will run the code block within the context of |
602 | code reference is invoked, it will run the code block within the context |
595 | that port, so exception handling once more works as expected. |
603 | that it was created in, so exception handling once more works as expected. |
596 | |
604 | |
597 | There is even a way to temporarily execute code in the context of some |
605 | There is even a way to temporarily execute code in the context of some |
598 | port, namely C<peval>: |
606 | port, namely C<peval>: |
599 | |
607 | |
600 | peval $port, sub { |
608 | peval $port, sub { |