/[cvs]/joko/TestArea/perl/runtime/POE/cluster/simpsons.pl
ViewVC logotype

Contents of /joko/TestArea/perl/runtime/POE/cluster/simpsons.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Sun May 11 21:48:48 2003 UTC (21 years, 4 months ago) by joko
Branch: MAIN
CVS Tags: HEAD
File MIME type: text/plain
initial commit

1 #!/usr/local/bin/perl -w
2 #
3 # This example is really just for fun. It demonstrates how the PoCo::Election::Bully module works.
4 # Although we use two Bully Component instances in this example, in normal situations you would
5 # probably have one bully component per POE instance since the module was designed for distributed
6 # coordination between POE processes. Don't let me stop you from being creative with its use though. ;)
7 #
8 # $Id: simpsons.pl,v 1.0 2002-06-03 15:37:19+02 amo Exp amo $
9 #
10
11 use strict;
12
13 use POE::Component::Election::Bully;
14 use POE;
15
16 # these are the nodes participating in the election process
17 my $node1 = new POE::Component::Election::Bully::Node(['localhost', 5122, 1]);
18 my $node2 = new POE::Component::Election::Bully::Node(['localhost', 5121, 2]);
19
20 my $n = 0;
21 my $passes = 0;
22
23 POE::Session->create
24 ( inline_states =>
25 {
26 _start => \&start_session,
27 start_election1=> \&start_election1,
28 start_election2=> \&start_election2,
29 elected1 => \&election_results1,
30 elected2 => \&election_results2,
31 coup1 => \&coup1,
32 coup2 => \&coup2,
33 do_work1 => \&do_work1,
34 do_work2 => \&do_work2,
35 vacation => \&vacation,
36 back_to_work => \&start_session,
37 }
38 );
39
40 $poe_kernel->run();
41
42 exit;
43
44
45 #--- States
46
47 sub start_session {
48 my ($kernel, $state) = @_[KERNEL, STATE];
49
50 if($state ne 'back_to_work') {
51 print "Nelson walks outside waiting for his first victim.";
52 }
53 else {
54 print "Nelson comes running back outside.";
55 }
56
57 print " (restarts the election)\n";
58
59 $_[KERNEL]->yield('start_election2') unless $state eq 'back_to_work';
60 $_[KERNEL]->delay_add('start_election1', 5);
61 }
62
63 sub start_election1 {
64 POE::Component::Election::Bully->new
65 ( Alias => 'component1',
66 Port => 5121,
67 Priority => 2,
68 ElectionState => 'elected1',
69 CoupState => 'coup1',
70 RestartTimer => 3,
71 NotifyTimer => 3,
72 );
73 $_[KERNEL]->post('component1', 'start_election', $node1);
74 }
75
76 sub start_election2 {
77 POE::Component::Election::Bully->new
78 ( Alias => 'component2',
79 Port => 5122,
80 Priority => 1,
81 ElectionState => 'elected2',
82 CoupState => 'coup2',
83 RestartTimer => 3,
84 NotifyTimer => 3,
85 );
86 $_[KERNEL]->post('component2', 'start_election', $node2);
87 }
88
89 sub election_results1 {
90 print "Nelson runs over and takes Milhouse's milk money from his pocket. (Nelson elected)\n";
91 $_[KERNEL]->delay_add('do_work1', 5);
92 $_[KERNEL]->delay_add('vacation', 9);
93 }
94
95 sub election_results2 {
96 if(++$passes == 1) {
97 print "Milhouse starts to walk past Nelson's house.";
98 }
99 else {
100 print "Milhouse trys to sneak by Nelson's house.";
101 }
102 print " (Milhouse elected)\n";
103 $_[KERNEL]->delay_add('do_work2', 5);
104 }
105
106 sub do_work1 {
107 print "Nelson waits outside for Milhouse to return with more milk money. (Nelson does some work)\n";
108 $_[KERNEL]->delay_add('do_work1', 5);
109 }
110
111 sub do_work2 {
112 if($n == 5) {
113 print "Milhouse decides to go home and stay there forever\n";
114 $_[KERNEL]->delay('do_work2');
115 $_[KERNEL]->post('component2', 'stop_election');
116 return;
117 }
118
119 my @worries = (
120 "Milhouse starts to get really scared.",
121 "Milhouse starts breathing very quickly."
122 );
123 print $worries[int(rand($#worries))];
124 print " (Milhouse does some work)\n";
125 $_[KERNEL]->delay_add('do_work2', 5);
126 }
127
128 sub coup1 {
129 print "Nelson just go bullied! (Nelson was bullied)\n";
130 $_[KERNEL]->delay('do_work1');
131 }
132
133 sub coup2 {
134 print "Milhouse starts crying and runs home to his mother. (Millhouse was bullied)\n";
135 $_[KERNEL]->delay('do_work2');
136 }
137
138 sub vacation {
139 if (++ $n < 5) {
140 print "Nelson decides to go back inside and watch from there. (Nelson pulls out of the election temporarily)\n";
141 $_[KERNEL]->delay('do_work1');
142 $_[KERNEL]->post('component1', 'stop_election');
143 $_[KERNEL]->delay_add('back_to_work', 5);
144 }
145 else {
146 print "Nelson gets bored and goes to school. (Nelson pulls out of the election permanently)\n";
147 $_[KERNEL]->delay('do_work1');
148 $_[KERNEL]->post('component1', 'stop_election');
149 }
150 }
151
152 __END__
153

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed