1 |
#!/usr/local/bin/perl -w |
2 |
# |
3 |
# This script is an example of a primary/backup architecture. It is meant to be ran with the example named "one.pl". |
4 |
# I like to run one.pl in one window and then two.pl in another and watch them battle it out. Normally one.pl, which |
5 |
# is the bully in this case because of the higher priority number, would always be elected and two.pl would never do |
6 |
# anything but send election requests and get denied. To make things a little more interesting I made one.pl take |
7 |
# a short vacation so that two.pl could get a chance to do something. Enjoy! |
8 |
# |
9 |
# $Id: two.pl,v 1.0 2002-06-03 15:37:19+02 amo Exp amo $ |
10 |
# |
11 |
|
12 |
|
13 |
use strict; |
14 |
|
15 |
|
16 |
use POE::Component::Election::Bully; |
17 |
use POE; |
18 |
|
19 |
# these are the nodes participating in the election process |
20 |
my $node2 = new POE::Component::Election::Bully::Node(['127.0.0.1', 5121, 2]); |
21 |
|
22 |
|
23 |
POE::Session->create |
24 |
( inline_states => |
25 |
{ |
26 |
_start => \&start_session, |
27 |
_stop => sub { print "Session finished\n" }, |
28 |
elected2 => \&election_results2, |
29 |
coup2 => sub { |
30 |
print "Session 2 has been overthrown!\n"; |
31 |
$_[KERNEL]->delay('do_work2'); |
32 |
}, |
33 |
do_work2 => \&do_work2, |
34 |
vacation => sub { |
35 |
print "It's time for a vacation!\n"; |
36 |
$_[KERNEL]->delay('do_work2'); |
37 |
$_[KERNEL]->post('component2', 'stop_election'); |
38 |
$_[KERNEL]->delay_add('start_work', $_[ARG0]); |
39 |
}, |
40 |
start_work => sub { |
41 |
$_[KERNEL]->post('component2', 'start_election', $node2); |
42 |
}, |
43 |
} |
44 |
); |
45 |
|
46 |
$poe_kernel->run(); |
47 |
|
48 |
exit; |
49 |
|
50 |
|
51 |
#--- States ---# |
52 |
|
53 |
sub start_session { |
54 |
print "election starting\n"; |
55 |
|
56 |
POE::Component::Election::Bully->new |
57 |
( Alias => 'component2', |
58 |
Port => 5122, |
59 |
Priority => 1, |
60 |
ElectionState => 'elected2', |
61 |
CoupState => 'coup2', |
62 |
RestartTimer => 3, |
63 |
NotifyTimer => 3, |
64 |
Debugging => 0, |
65 |
); |
66 |
|
67 |
$_[KERNEL]->post('component2', 'start_election', $node2); |
68 |
} |
69 |
|
70 |
sub election_results2 { |
71 |
print "Session 2 has been elected!\n"; |
72 |
$_[KERNEL]->delay_add('do_work2', 5); |
73 |
} |
74 |
|
75 |
sub do_work2 { |
76 |
print "Session 2 doing some work while elected\n"; |
77 |
$_[KERNEL]->delay_add('do_work2', 5); |
78 |
} |
79 |
|
80 |
|
81 |
__END__ |
82 |
|