GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: A Camping-inspired Web Microframework for Perl
Homepage: http://groups.google.com/group/squatting-framework
Clone URL: git://github.com/beppu/squatting.git
Click here to lend your support to: squatting and make a donation at www.pledgie.com !
updated comet event controller example
beppu (author)
Mon Sep 29 00:42:20 -0700 2008
commit  2470c63221a9e2e131ddc1c3a82dc98616eaec66
tree    75e21d6708508247ac47404a8ceac95513d6fde0
parent  e14bfa1f7d641cfbf0923bedf07d7f6aa29fd7df
...
98
99
100
101
 
102
103
104
...
111
112
113
114
 
 
 
115
116
117
118
119
120
 
121
 
 
122
123
 
124
 
 
 
125
126
127
 
128
129
130
131
132
133
134
135
136
137
138
139
140
141
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
143
 
 
 
 
 
 
 
 
 
144
145
146
 
 
 
 
 
147
148
149
150
151
 
 
 
 
152
153
154
...
98
99
100
 
101
102
103
104
...
111
112
113
 
114
115
116
117
118
119
120
121
122
123
124
125
126
127
 
128
129
130
131
132
133
134
 
135
136
137
138
139
140
141
 
 
 
 
 
 
 
 
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
 
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
0
@@ -98,7 +98,7 @@ If your app is called C<App>, then:
0
 
0
 =head3 Event Architecture
0
 
0
-TODO - explain using IRC as a metaphor
0
+TODO - explain, possibly using IRC as a metaphor
0
 
0
 Events (and my current preference for ambient event generation)
0
 
0
@@ -111,44 +111,79 @@ Subscribers
0
 
0
 =head3 RESTless Controllers
0
 
0
-I'm figuring this out as I go along...
0
+The following is the C<Event> controller from the Bavl project. It is included
0
+here to give you something to ponder while I think about how to explain this
0
+better. (I'm figuring this out as I go along.)
0
 
0
   C(
0
     Event => [ '/@event' ],
0
     get => sub {
0
       warn "coro [$Coro::current]";
0
       my ($self) = shift;
0
+ my $input = $self->input;
0
       my $cr = $self->cr;
0
+ my @ch = channels($input->{channels});
0
+ my $last = time;
0
       while (1) {
0
- # OUTPUT
0
+ # Output
0
         warn "top of loop";
0
+ my @events =
0
+ grep { defined }
0
+ map { my $ch = $bavl->channels->{$_}; $ch->read } @ch;
0
         my $x = async {
0
           warn "printing...";
0
- $cr->print(encode_json([{ type => 'Chat', message => 'Hi' }]));
0
+ $cr->print(encode_json(\@events));
0
         };
0
         $x->join;
0
 
0
         # Hold for a brief moment until the next long poll request comes in.
0
         warn "waiting for next request";
0
         $cr->next;
0
-
0
- # TODO Try starting up 1 coroutine per channel.
0
- # TODO Each coroutine will have the same Coro::Signal object.
0
- # TODO The first one who sends a signal wins.
0
- # TODO The losers will have to be terminated somehow.
0
-
0
- warn "waiting for signal";
0
- $signal->timed_wait(20);
0
+ $last = time;
0
+ my $channels = [ $cr->param('channels') ];
0
+ @ch = channels($channels);
0
+
0
+ # Try starting up 1 coroutine per channel.
0
+ # Each coroutine will have the same Coro::Signal object => $activity.
0
+ my $activity = Coro::Signal->new;
0
+ my @coros = map {
0
+ my $ch = $bavl->channels->{$_};
0
+ async { $ch->signal->wait; $activity->broadcast };
0
+ } @ch;
0
+
0
+ # The first one who sends a signal to $activity wins.
0
+ warn "waiting for activity on any of (@ch)";
0
+ $activity->timed_wait(20);
0
+
0
+ # Cancel the remaining coros.
0
+ for (@coros) { $_->cancel }
0
       }
0
     },
0
+
0
+ # The current POST action exists for debugging purposes, only.
0
+ # In practice, channel updates will happen ambiently
0
+ # when model data changes.
0
+ # Hooks will be put into place to facilitate this.
0
+ #
0
+ # In the future, the POST action may be used as a notification
0
+ # to the server side that $.ev.stop() happened
0
+ # on the client side.
0
     post => sub {
0
       my ($self) = shift;
0
- $signal->broadcast;
0
+ my $input = $self->input;
0
+ my $ch = $bavl->channels->{ $input->{channels} };
0
+ if ($ch) {
0
+ $ch->write({ type => 'time', value => scalar(localtime) });
0
+ }
0
       1;
0
     },
0
     queue => { get => 'event' },
0
   ),
0
 
0
+This might look scary, but if we're lucky, we'll be able to turn this into
0
+a reusable component.
0
+
0
+
0
 =head3 Long Polling with jQuery on the Client Side
0
 
0
 TODO

Comments

    No one has commented yet.