1 |
joko |
1.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 "two.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: one.pl,v 1.0 2002-06-03 15:37:19+02 amo Exp amo $ |
10 |
|
|
# |
11 |
|
|
|
12 |
|
|
use strict; |
13 |
|
|
|
14 |
|
|
use POE::Component::Election::Bully; |
15 |
|
|
use POE; |
16 |
|
|
|
17 |
|
|
# these are the nodes participating in the election process |
18 |
|
|
my $node1 = new POE::Component::Election::Bully::Node(['127.0.0.1', 5122, 1]); |
19 |
|
|
|
20 |
|
|
my $n = 0; |
21 |
|
|
|
22 |
|
|
POE::Session->create |
23 |
|
|
( inline_states => |
24 |
|
|
{ |
25 |
|
|
_start => \&start_session, |
26 |
|
|
_stop => sub { print "Session finished\n" }, |
27 |
|
|
elected1 => \&election_results, |
28 |
|
|
coup1 => sub { |
29 |
|
|
print "Session 1 has been overthrown!\n"; |
30 |
|
|
$_[KERNEL]->delay('do_work1'); |
31 |
|
|
}, |
32 |
|
|
do_work1 => \&do_work1, |
33 |
|
|
vacation => sub { |
34 |
|
|
# make sure that we retire before we work too much |
35 |
|
|
if (++ $n < 5) { |
36 |
|
|
print "It's time for a vacation!\n"; |
37 |
|
|
$_[KERNEL]->delay('do_work1'); |
38 |
|
|
$_[KERNEL]->post('component1', 'stop_election'); |
39 |
|
|
$_[KERNEL]->delay_add('_start', 15); |
40 |
|
|
} |
41 |
|
|
else { |
42 |
|
|
print "It's time to retire.....later.\n"; |
43 |
|
|
$_[KERNEL]->delay('do_work1'); |
44 |
|
|
$_[KERNEL]->post('component1', 'stop_election'); |
45 |
|
|
} |
46 |
|
|
}, |
47 |
|
|
} |
48 |
|
|
); |
49 |
|
|
|
50 |
|
|
$poe_kernel->run(); |
51 |
|
|
|
52 |
|
|
exit; |
53 |
|
|
|
54 |
|
|
|
55 |
|
|
#--- States |
56 |
|
|
|
57 |
|
|
sub start_session { |
58 |
|
|
print "election starting\n"; |
59 |
|
|
|
60 |
|
|
POE::Component::Election::Bully->new |
61 |
|
|
( Alias => 'component1', |
62 |
|
|
Port => 5121, |
63 |
|
|
Priority => 2, |
64 |
|
|
ElectionState => 'elected1', |
65 |
|
|
CoupState => 'coup1', |
66 |
|
|
RestartTimer => 3, |
67 |
|
|
NotifyTimer => 3, |
68 |
|
|
); |
69 |
|
|
|
70 |
|
|
$_[KERNEL]->post('component1', 'start_election', $node1); |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
sub election_results { |
74 |
|
|
print "Session 1 has been elected!\n"; |
75 |
|
|
$_[KERNEL]->delay_add('do_work1', 5); |
76 |
|
|
$_[KERNEL]->delay_add('vacation', 12, 5); |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
sub do_work1 { |
80 |
|
|
print "Session 1 doing some work while elected\n"; |
81 |
|
|
$_[KERNEL]->delay_add('do_work1', 5); |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
|
85 |
|
|
__END__ |
86 |
|
|
|