1 |
#!/usr/bin/perl -w |
#!/usr/bin/perl -w |
2 |
############################################################################### |
############################################################################### |
3 |
# Meteor |
# Meteor |
4 |
# An HTTP server for the 2.0 web |
# An HTTP server for the 2.0 web |
5 |
# Copyright (c) 2006 contributing authors |
# Copyright (c) 2006 contributing authors |
6 |
# |
# |
7 |
# Subscriber.pm |
# Subscriber.pm |
8 |
# |
# |
9 |
# Description: |
# Description: |
10 |
# A Meteor Subscriber |
# A Meteor Subscriber |
11 |
# |
# |
12 |
############################################################################### |
############################################################################### |
13 |
# |
# |
14 |
# This program is free software; you can redistribute it and/or modify it |
# This program is free software; you can redistribute it and/or modify it |
15 |
# under the terms of the GNU General Public License as published by the Free |
# under the terms of the GNU General Public License as published by the Free |
16 |
# Software Foundation; either version 2 of the License, or (at your option) |
# Software Foundation; either version 2 of the License, or (at your option) |
17 |
# any later version. |
# any later version. |
18 |
# |
# |
19 |
# This program is distributed in the hope that it will be useful, but WITHOUT |
# This program is distributed in the hope that it will be useful, but WITHOUT |
20 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
21 |
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
22 |
# more details. |
# more details. |
23 |
# |
# |
24 |
# You should have received a copy of the GNU General Public License along |
# You should have received a copy of the GNU General Public License along |
25 |
# with this program; if not, write to the Free Software Foundation, Inc., |
# with this program; if not, write to the Free Software Foundation, Inc., |
26 |
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
27 |
# |
# |
28 |
# For more information visit www.meteorserver.org |
# For more information visit www.meteorserver.org |
29 |
# |
# |
30 |
############################################################################### |
############################################################################### |
31 |
|
|
32 |
package Meteor::Subscriber; |
package Meteor::Subscriber; |
33 |
############################################################################### |
############################################################################### |
34 |
# Configuration |
# Configuration |
35 |
############################################################################### |
############################################################################### |
36 |
|
|
37 |
use strict; |
use strict; |
38 |
|
|
39 |
use Meteor::Connection; |
use Meteor::Connection; |
40 |
use Meteor::Channel; |
use Meteor::Channel; |
41 |
use Meteor::Document; |
use Meteor::Document; |
42 |
|
|
43 |
@Meteor::Subscriber::ISA=qw(Meteor::Connection); |
@Meteor::Subscriber::ISA=qw(Meteor::Connection); |
44 |
|
|
45 |
our %PersistentConnections=(); |
our %PersistentConnections=(); |
46 |
|
our $NumAcceptedConnections=0; |
47 |
############################################################################### |
|
48 |
# Factory methods |
############################################################################### |
49 |
############################################################################### |
# Factory methods |
50 |
sub newFromServer { |
############################################################################### |
51 |
my $class=shift; |
sub newFromServer { |
52 |
|
my $class=shift; |
53 |
my $self=$class->SUPER::newFromServer(shift); |
|
54 |
|
my $self=$class->SUPER::newFromServer(shift); |
55 |
$self->{'headerBuffer'}=''; |
|
56 |
$self->{'MessageCount'}=0; |
$self->{'headerBuffer'}=''; |
57 |
$self->{'MaxMessageCount'}=0; |
$self->{'MessageCount'}=0; |
58 |
|
$self->{'MaxMessageCount'}=0; |
59 |
$self->{'ConnectionStart'}=time; |
|
60 |
my $maxTime=$::CONF{'MaxTime'}; |
$self->{'ConnectionStart'}=time; |
61 |
if($maxTime>0) |
my $maxTime=$::CONF{'MaxTime'}; |
62 |
{ |
if($maxTime>0) |
63 |
$self->{'ConnectionTimeLimit'}=$self->{'ConnectionStart'}+$maxTime; |
{ |
64 |
} |
$self->{'ConnectionTimeLimit'}=$self->{'ConnectionStart'}+$maxTime; |
65 |
|
} |
66 |
$self; |
|
67 |
} |
$::Statistics->{'current_subscribers'}++; |
68 |
|
$::Statistics->{'subscriber_connections_accepted'}++; |
69 |
############################################################################### |
|
70 |
# Class methods |
$self; |
71 |
############################################################################### |
} |
72 |
sub deleteSubscriberWithID { |
|
73 |
my $class=shift; |
############################################################################### |
74 |
my $id=shift; |
# Class methods |
75 |
|
############################################################################### |
76 |
if(exists($PersistentConnections{$id})) |
sub deleteSubscriberWithID { |
77 |
{ |
my $class=shift; |
78 |
$PersistentConnections{$id}->close(1); |
my $id=shift; |
79 |
} |
|
80 |
} |
if(exists($PersistentConnections{$id})) |
81 |
|
{ |
82 |
sub pingPersistentConnections { |
$PersistentConnections{$id}->close(1); |
83 |
my $class=shift; |
} |
84 |
|
} |
85 |
my $msg=$::CONF{'PingMessage'}; |
|
86 |
my @cons=values %PersistentConnections; |
sub pingPersistentConnections { |
87 |
|
my $class=shift; |
88 |
map { $_->write($msg) } @cons; |
|
89 |
} |
my $msg=$::CONF{'PingMessage'}; |
90 |
|
my @cons=values %PersistentConnections; |
91 |
sub checkPersistentConnectionsForMaxTime { |
|
92 |
my $class=shift; |
map { $_->write($msg.chr(0)) } @cons; |
93 |
|
} |
94 |
my $time=time; |
|
95 |
my @cons=values %PersistentConnections; |
sub checkPersistentConnectionsForMaxTime { |
96 |
|
my $class=shift; |
97 |
map { $_->checkForMaxTime($time) } @cons; |
|
98 |
} |
my $time=time; |
99 |
|
my @cons=values %PersistentConnections; |
100 |
############################################################################### |
|
101 |
# Instance methods |
map { $_->checkForMaxTime($time) } @cons; |
102 |
############################################################################### |
} |
103 |
sub processLine { |
|
104 |
my $self=shift; |
sub numSubscribers { |
105 |
my $line=shift; |
|
106 |
|
return scalar(keys %PersistentConnections); |
107 |
# Once the header was processed we ignore any input |
} |
108 |
return unless(exists($self->{'headerBuffer'})); |
|
109 |
|
############################################################################### |
110 |
if($line ne '') |
# Instance methods |
111 |
{ |
############################################################################### |
112 |
# |
sub processLine { |
113 |
# Accumulate header |
my $self=shift; |
114 |
# |
my $line=shift; |
115 |
$self->{'headerBuffer'}.="$line\n"; |
|
116 |
} |
# Once the header was processed we ignore any input |
117 |
else |
return unless(exists($self->{'headerBuffer'})); |
118 |
{ |
|
119 |
# |
if($line ne '') |
120 |
# Empty line signals end of header. |
{ |
121 |
# Analyze header, register with appropiate channel |
# |
122 |
# and send pending messages. |
# Accumulate header |
123 |
# |
# |
124 |
# GET $::CONF{'SubscriberDynamicPageAddress'}?channel=ml123&restartfrom=1 HTTP/1.1 |
$self->{'headerBuffer'}.="$line\n"; |
125 |
# |
} |
126 |
# Find the 'GET' line |
else |
127 |
# |
{ |
128 |
if($self->{'headerBuffer'}=~/GET\s+$::CONF{'SubscriberDynamicPageAddress'}\?(\S+)/) |
# |
129 |
{ |
# Empty line signals end of header. |
130 |
my @formData=split('&',$1); |
# Analyze header, register with appropiate channel |
131 |
my $channelName=undef; |
# and send pending messages. |
132 |
my $startIndex=undef; |
# |
133 |
my $backtrack=undef; |
# GET $::CONF{'SubscriberDynamicPageAddress'}/hostid/streamtype/channeldefs HTTP/1.1 |
134 |
my $persist=1; |
# |
135 |
my $subscriberID=undef; |
# Find the 'GET' line |
136 |
foreach my $formElement (@formData) |
# |
137 |
{ |
if($self->{'headerBuffer'}=~/GET\s+$::CONF{'SubscriberDynamicPageAddress'}\/([0-9a-z]+)\/([0-9a-z]+)\/(\S+)/i) |
138 |
if($formElement=~/^channel=(.+)$/) |
{ |
139 |
{ |
my $subscriberID=$1; |
140 |
$channelName=$1; |
my $persist=0; |
141 |
} |
$self->{'mode'}=$2; |
142 |
elsif($formElement=~/^restartfrom=(\d*)$/) |
if ($self->{'mode'} eq "xhrinteractive" || $self->{'mode'} eq "iframe" || $self->{'mode'} eq "serversent" || $self->{'mode'} eq "longpoll") { |
143 |
{ |
$persist=1; |
144 |
$startIndex=$1; |
$self->{'MaxMessageCount'}=1 unless(!($self->{'mode'} eq "longpoll")); |
145 |
$startIndex='' unless(defined($startIndex)); |
} |
146 |
} |
if ($self->{'mode'} eq "iframe") { |
147 |
elsif($formElement=~/^backtrack=(\d+)$/) |
$self->{'HeaderTemplateNumber'}=1; |
148 |
{ |
} else { |
149 |
$backtrack=$1; |
$self->{'HeaderTemplateNumber'}=2; |
150 |
$backtrack=0 unless(defined($backtrack)); |
} |
151 |
} |
my @channelData=split('/',$3); |
152 |
elsif($formElement=~/^persist=(?i)(yes|true|1|no|false|0)$/) |
my $channels={}; |
153 |
{ |
my $channelName; |
154 |
$persist=0 if($1=~/(no|false|0)/i); |
my $offset; |
155 |
} |
foreach my $chandef (@channelData) { |
156 |
elsif($formElement=~/^id=(.+)$/) |
if($chandef=~/^([a-z0-9]+)(.(r|b|h)([0-9]*))?$/i) { |
157 |
{ |
$channelName = $1; |
158 |
$subscriberID=$1; |
$channels->{$channelName}->{'startIndex'} = undef; |
159 |
} |
if ($3) { |
160 |
elsif($formElement=~/^maxmessages=(\d+)$/i) |
$offset = $4; |
161 |
{ |
if ($3 eq 'r') { $channels->{$channelName}->{'startIndex'} = $offset; } |
162 |
$self->{'MaxMessageCount'}=$1; |
if ($3 eq 'b') { $channels->{$channelName}->{'startIndex'} = -$offset; } |
163 |
} |
if ($3 eq 'h') { $channels->{$channelName}->{'startIndex'} = 0; } |
164 |
elsif($formElement=~/^template=(\d+)$/i) |
} |
165 |
{ |
} |
166 |
$self->{'HeaderTemplateNumber'}=$1; |
} |
167 |
} |
|
168 |
elsif($formElement=~/^maxtime=(\d+)$/i) |
delete($self->{'headerBuffer'}); |
169 |
{ |
|
170 |
my $clientRequest=$1; |
if($persist) |
171 |
my $serverDefault=$::CONF{'MaxTime'}; |
{ |
172 |
|
$self->{'subscriberID'}=$subscriberID; |
173 |
if($serverDefault==0 || $serverDefault>$clientRequest) |
$self->deleteSubscriberWithID($subscriberID); |
174 |
{ |
$PersistentConnections{$subscriberID}=$self; |
175 |
$self->{'ConnectionTimeLimit'}=$self->{'ConnectionStart'}+$clientRequest; |
} |
176 |
} |
|
177 |
} |
if(scalar(keys %{$channels})) |
178 |
} |
{ |
179 |
|
$self->emitOKHeader(); |
180 |
delete($self->{'headerBuffer'}); |
$self->setChannels($channels,$persist); |
181 |
|
$self->close(1) unless($persist); |
182 |
if(defined($startIndex) && defined($backtrack)) |
return; |
183 |
{ |
} |
184 |
$self->emitHeader("404 Cannot use both 'restartfrom' and 'backtrack'"); |
} |
185 |
$self->close(); |
elsif($self->{'headerBuffer'}=~/GET\s+\/disconnect\/(\S+)/) |
186 |
|
{ |
187 |
return; |
$self->deleteSubscriberWithID($1); |
188 |
} |
$self->emitOKHeader(); |
189 |
|
$self->close(1); |
190 |
if(defined($subscriberID) && $persist) |
return; |
191 |
{ |
} |
192 |
$self->{'subscriberID'}=$subscriberID; |
elsif($self->{'headerBuffer'}=~/GET\s+([^\s\?]+)/) |
193 |
$self->deleteSubscriberWithID($subscriberID); |
{ |
194 |
$PersistentConnections{$subscriberID}=$self; |
Meteor::Document->serveFileToClient($1,$self); |
195 |
} |
$self->close(1); |
196 |
|
return; |
197 |
if(defined($channelName)) |
} |
198 |
{ |
|
199 |
$self->emitOKHeader(); |
# |
200 |
|
# If we fall through we did not understand the request |
201 |
$startIndex=-$backtrack if(!defined($startIndex) && defined($backtrack)); |
# |
202 |
|
$self->emitErrorHeader(); |
203 |
$self->setChannelName($channelName,$startIndex,$persist); |
} |
204 |
|
} |
205 |
$self->close(1) unless($persist); |
|
206 |
|
sub setChannels { |
207 |
return; |
my $self=shift; |
208 |
} |
my $channels=shift; |
209 |
} |
my $persist=shift; |
210 |
elsif($self->{'headerBuffer'}=~/GET\s+([^\s\?]+)/) |
|
211 |
{ |
foreach my $channelName (keys %{$channels}) |
212 |
Meteor::Document->serveFileToClient($1,$self); |
{ |
213 |
|
my $startIndex=$channels->{$channelName}->{'startIndex'}; |
214 |
$self->close(1); |
|
215 |
|
my $channel=Meteor::Channel->channelWithName($channelName); |
216 |
return; |
|
217 |
} |
$self->{'channels'}->{$channelName}=$channel if($persist); |
218 |
|
|
219 |
# |
$channel->addSubscriber($self,$startIndex,$persist); |
220 |
# If we fall through we did not understand the request |
} |
221 |
# |
} |
222 |
$self->emitErrorHeader(); |
|
223 |
} |
sub emitOKHeader { |
224 |
} |
my $self=shift; |
225 |
|
|
226 |
sub setChannelName { |
$self->emitHeader('200 OK'); |
227 |
my $self=shift; |
} |
228 |
my $channelName=shift; |
|
229 |
my $startIndex=shift; |
sub emitErrorHeader { |
230 |
my $persist=shift; |
my $self=shift; |
231 |
|
|
232 |
my $channel=Meteor::Channel->channelWithName($channelName); |
$self->emitHeader('404 Not Found'); |
233 |
$self->{'channel'}=$channel if($persist); |
$::Statistics->{'errors_served'}++; |
234 |
|
|
235 |
$channel->addSubscriber($self,$startIndex,$persist); |
# close up shop here! |
236 |
} |
$self->close(); |
237 |
|
} |
238 |
sub emitOKHeader { |
|
239 |
my $self=shift; |
sub emitHeader { |
240 |
|
my $self=shift; |
241 |
$self->emitHeader('200 OK'); |
my $status=shift; |
242 |
} |
|
243 |
|
my $header=undef; |
244 |
sub emitErrorHeader { |
if(exists($self->{'HeaderTemplateNumber'})) |
245 |
my $self=shift; |
{ |
246 |
|
my $hn='HeaderTemplate'.$self->{'HeaderTemplateNumber'}; |
247 |
$self->emitHeader('404 Not Found'); |
|
248 |
|
$header=$::CONF{$hn}; |
249 |
# close up shop here! |
} |
250 |
$self->close(); |
$header=$::CONF{'HeaderTemplate'} unless(defined($header)); |
251 |
} |
|
252 |
|
$header=~s/~([^~]+)~/ |
253 |
sub emitHeader { |
if(!defined($1) || $1 eq '') |
254 |
my $self=shift; |
{ |
255 |
my $status=shift; |
'~'; |
256 |
|
} |
257 |
my $header=undef; |
elsif($1 eq 'server') |
258 |
if(exists($self->{'HeaderTemplateNumber'})) |
{ |
259 |
{ |
$::PGM; |
260 |
my $hn='HeaderTemplate'.$self->{'HeaderTemplateNumber'}; |
} |
261 |
|
elsif($1 eq 'status') |
262 |
$header=$::CONF{$hn}; |
{ |
263 |
} |
$status; |
264 |
$header=$::CONF{'HeaderTemplate'} unless(defined($header)); |
} |
265 |
|
elsif($1 eq 'servertime') |
266 |
$header=~s/~([^~]+)~/ |
{ |
267 |
if(!defined($1) || $1 eq '') |
time; |
268 |
{ |
} |
269 |
'~'; |
else |
270 |
} |
{ |
271 |
elsif($1 eq 'server') |
''; |
272 |
{ |
} |
273 |
$::PGM; |
/gex; |
274 |
} |
|
275 |
elsif($1 eq 'status') |
$self->write($header.chr(0)); |
276 |
{ |
} |
277 |
$status; |
|
278 |
} |
sub sendMessage { |
279 |
elsif($1 eq 'servertime') |
my $self=shift; |
280 |
{ |
my $msg=shift; |
281 |
time; |
my $numMsgInThisBatch=shift; |
282 |
} |
|
283 |
else |
$numMsgInThisBatch=1 unless(defined($numMsgInThisBatch)); |
284 |
{ |
|
285 |
''; |
$self->write($msg.chr(0)); |
286 |
} |
|
287 |
/gex; |
$::Statistics->{'messages_served'}+=$numMsgInThisBatch; |
288 |
|
|
289 |
$self->write($header); |
my $msgCount=++$self->{'MessageCount'}; |
290 |
} |
|
291 |
|
my $maxMsg=$::CONF{'MaxMessages'}; |
292 |
sub sendMessage { |
if(defined($maxMsg) && $maxMsg>0 && $msgCount>=$maxMsg) |
293 |
my $self=shift; |
{ |
294 |
my $msg=shift; |
$self->close(1); |
295 |
|
} |
296 |
$self->write($msg); |
|
297 |
|
if($self->{'MaxMessageCount'}>0 && $msgCount>=$self->{'MaxMessageCount'}) |
298 |
my $msgCount=++$self->{'MessageCount'}; |
{ |
299 |
|
$self->close(1); |
300 |
my $maxMsg=$::CONF{'MaxMessages'}; |
} |
301 |
if(defined($maxMsg) && $maxMsg>0 && $msgCount>=$maxMsg) |
} |
302 |
{ |
|
303 |
$self->close(1); |
sub closeChannel { |
304 |
} |
my $self=shift; |
305 |
|
my $channelName=shift; |
306 |
if($self->{'MaxMessageCount'}>0 && $msgCount>=$self->{'MaxMessageCount'}) |
|
307 |
{ |
return unless(exists($self->{'channels'}->{$channelName})); |
308 |
$self->close(1); |
|
309 |
} |
my $channel=$self->{'channels'}->{$channelName}; |
310 |
} |
$channel->removeSubscriber($self); |
311 |
|
|
312 |
sub close { |
delete($self->{'channels'}->{$channelName}); |
313 |
my $self=shift; |
|
314 |
my $noShutdownMsg=shift; |
$self->close() if(scalar(keys %{$self->{'channels'}})==0); |
315 |
|
} |
316 |
$self->{'channel'}->removeSubscriber($self) if($self->{'channel'}); |
|
317 |
delete($self->{'channel'}); |
sub close { |
318 |
|
my $self=shift; |
319 |
if(exists($self->{'subscriberID'})) |
my $noShutdownMsg=shift; |
320 |
{ |
|
321 |
delete($PersistentConnections{$self->{'subscriberID'}}); |
foreach my $channelName (keys %{$self->{'channels'}}) |
322 |
} |
{ |
323 |
|
my $channel=$self->{'channels'}->{$channelName}; |
324 |
# |
$channel->removeSubscriber($self); |
325 |
# Send shutdown message unless remote closed or |
} |
326 |
# connection not yet established |
delete($self->{'channels'}); |
327 |
# |
|
328 |
unless($noShutdownMsg || $self->{'remoteClosed'} || exists($self->{'headerBuffer'})) |
if(exists($self->{'subscriberID'})) |
329 |
{ |
{ |
330 |
my $msg=$::CONF{'SubscriberShutdownMsg'}; |
delete($PersistentConnections{$self->{'subscriberID'}}); |
331 |
if(defined($msg) && $msg ne '') |
} |
332 |
{ |
|
333 |
$self->write($msg); |
# |
334 |
} |
# Send shutdown message unless remote closed or |
335 |
} |
# connection not yet established |
336 |
|
# |
337 |
$self->SUPER::close(); |
unless($noShutdownMsg || $self->{'remoteClosed'} || exists($self->{'headerBuffer'})) |
338 |
} |
{ |
339 |
|
my $msg=$::CONF{'SubscriberShutdownMsg'}; |
340 |
sub checkForMaxTime { |
if(defined($msg) && $msg ne '') |
341 |
my $self=shift; |
{ |
342 |
my $time=shift; |
$self->write($msg); |
343 |
|
} |
344 |
$self->close(1) if(exists($self->{'ConnectionTimeLimit'}) && $self->{'ConnectionTimeLimit'}<$time); |
} |
345 |
} |
|
346 |
|
$::Statistics->{'current_subscribers'}--; |
347 |
1; |
|
348 |
|
$self->SUPER::close(); |
349 |
|
} |
350 |
|
|
351 |
|
sub checkForMaxTime { |
352 |
|
my $self=shift; |
353 |
|
my $time=shift; |
354 |
|
|
355 |
|
$self->close(1) if(exists($self->{'ConnectionTimeLimit'}) && $self->{'ConnectionTimeLimit'}<$time); |
356 |
|
} |
357 |
|
|
358 |
|
1; |
359 |
############################################################################EOF |
############################################################################EOF |