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 |
|
|
49 |
############################################################################### |
############################################################################### |
50 |
# Factory methods |
# Factory methods |
65 |
$self->{'ConnectionTimeLimit'}=$self->{'ConnectionStart'}+$maxTime; |
$self->{'ConnectionTimeLimit'}=$self->{'ConnectionStart'}+$maxTime; |
66 |
} |
} |
67 |
|
|
68 |
|
$::Statistics->{'current_subscribers'}++; |
69 |
|
$::Statistics->{'subscriber_connections_accepted'}++; |
70 |
|
|
71 |
$self; |
$self; |
72 |
} |
} |
73 |
|
|
80 |
|
|
81 |
if(exists($PersistentConnections{$id})) |
if(exists($PersistentConnections{$id})) |
82 |
{ |
{ |
83 |
$PersistentConnections{$id}->close(1); |
$PersistentConnections{$id}->close(); |
84 |
} |
} |
85 |
} |
} |
86 |
|
|
102 |
map { $_->checkForMaxTime($time) } @cons; |
map { $_->checkForMaxTime($time) } @cons; |
103 |
} |
} |
104 |
|
|
105 |
|
sub numSubscribers { |
106 |
|
|
107 |
|
return scalar(keys %PersistentConnections); |
108 |
|
} |
109 |
|
|
110 |
############################################################################### |
############################################################################### |
111 |
# Instance methods |
# Instance methods |
112 |
############################################################################### |
############################################################################### |
131 |
# Analyze header, register with appropiate channel |
# Analyze header, register with appropiate channel |
132 |
# and send pending messages. |
# and send pending messages. |
133 |
# |
# |
134 |
# GET $::CONF{'SubscriberDynamicPageAddress'}?channel=ml123&restartfrom=1 HTTP/1.1 |
# GET $::CONF{'SubscriberDynamicPageAddress'}/hostid/streamtype/channeldefs HTTP/1.1 |
135 |
# |
# |
136 |
# Find the 'GET' line |
# Find the 'GET' line |
137 |
# |
# |
138 |
if($self->{'headerBuffer'}=~/GET\s+$::CONF{'SubscriberDynamicPageAddress'}\?(\S+)/) |
if($self->{'headerBuffer'}=~/GET\s+$::CONF{'SubscriberDynamicPageAddress'}\/([0-9a-z]+)\/([0-9a-z]+)\/(\S+)/i) |
139 |
{ |
{ |
140 |
my @formData=split('&',$1); |
my $subscriberID=$1; |
141 |
my $channelName=undef; |
my $persist=0; |
142 |
my $startIndex=undef; |
$self->{'mode'}=$2; |
143 |
my $backtrack=undef; |
if ($self->{'mode'} eq "xhrinteractive" || $self->{'mode'} eq "iframe" || $self->{'mode'} eq "serversent" || $self->{'mode'} eq "longpoll") { |
144 |
my $persist=1; |
$persist=1; |
145 |
my $subscriberID=undef; |
$self->{'MaxMessageCount'}=1 unless(!($self->{'mode'} eq "longpoll")); |
146 |
foreach my $formElement (@formData) |
} |
147 |
{ |
if ($self->{'mode'} eq "iframe") { |
148 |
if($formElement=~/^channel=(.+)$/) |
$self->{'HeaderTemplateNumber'}=1; |
149 |
{ |
} else { |
150 |
$channelName=$1; |
$self->{'HeaderTemplateNumber'}=2; |
151 |
} |
} |
152 |
elsif($formElement=~/^restartfrom=(\d*)$/) |
my @channelData=split('/',$3); |
153 |
{ |
my $channels={}; |
154 |
$startIndex=$1; |
my $channelName; |
155 |
$startIndex='' unless(defined($startIndex)); |
my $offset; |
156 |
} |
foreach my $chandef (@channelData) { |
157 |
elsif($formElement=~/^backtrack=(\d+)$/) |
if($chandef=~/^([a-z0-9]+)(.(r|b|h)([0-9]*))?$/i) { |
158 |
{ |
$channelName = $1; |
159 |
$backtrack=$1; |
$channels->{$channelName}->{'startIndex'} = undef; |
160 |
$backtrack=0 unless(defined($backtrack)); |
if ($3) { |
161 |
} |
$offset = $4; |
162 |
elsif($formElement=~/^persist=(?i)(yes|true|1|no|false|0)$/) |
if ($3 eq 'r') { $channels->{$channelName}->{'startIndex'} = $offset; } |
163 |
{ |
if ($3 eq 'b') { $channels->{$channelName}->{'startIndex'} = -$offset; } |
164 |
$persist=0 if($1=~/(no|false|0)/i); |
if ($3 eq 'h') { $channels->{$channelName}->{'startIndex'} = 0; } |
|
} |
|
|
elsif($formElement=~/^id=(.+)$/) |
|
|
{ |
|
|
$subscriberID=$1; |
|
|
} |
|
|
elsif($formElement=~/^maxmessages=(\d+)$/i) |
|
|
{ |
|
|
$self->{'MaxMessageCount'}=$1; |
|
|
} |
|
|
elsif($formElement=~/^template=(\d+)$/i) |
|
|
{ |
|
|
$self->{'HeaderTemplateNumber'}=$1; |
|
|
} |
|
|
elsif($formElement=~/^maxtime=(\d+)$/i) |
|
|
{ |
|
|
my $clientRequest=$1; |
|
|
my $serverDefault=$::CONF{'MaxTime'}; |
|
|
|
|
|
if($serverDefault==0 || $serverDefault>$clientRequest) |
|
|
{ |
|
|
$self->{'ConnectionTimeLimit'}=$self->{'ConnectionStart'}+$clientRequest; |
|
165 |
} |
} |
166 |
} |
} |
167 |
} |
} |
|
|
|
|
delete($self->{'headerBuffer'}); |
|
168 |
|
|
169 |
if(defined($startIndex) && defined($backtrack)) |
delete($self->{'headerBuffer'}); |
|
{ |
|
|
$self->emitHeader("404 Cannot use both 'restartfrom' and 'backtrack'"); |
|
|
$self->close(); |
|
|
|
|
|
return; |
|
|
} |
|
170 |
|
|
171 |
if(defined($subscriberID) && $persist) |
if($persist) |
172 |
{ |
{ |
173 |
$self->{'subscriberID'}=$subscriberID; |
$self->{'subscriberID'}=$subscriberID; |
174 |
$self->deleteSubscriberWithID($subscriberID); |
$self->deleteSubscriberWithID($subscriberID); |
175 |
$PersistentConnections{$subscriberID}=$self; |
$PersistentConnections{$subscriberID}=$self; |
176 |
} |
} |
177 |
|
|
178 |
if(defined($channelName)) |
if(scalar(keys %{$channels})) |
179 |
{ |
{ |
180 |
$self->emitOKHeader(); |
$self->emitOKHeader(); |
181 |
|
$self->setChannels($channels,$persist); |
|
$startIndex=-$backtrack if(!defined($startIndex) && defined($backtrack)); |
|
|
|
|
|
$self->setChannelName($channelName,$startIndex,$persist); |
|
|
|
|
182 |
$self->close(1) unless($persist); |
$self->close(1) unless($persist); |
|
|
|
183 |
return; |
return; |
184 |
} |
} |
185 |
} |
} |
186 |
|
elsif($self->{'headerBuffer'}=~/GET\s+\/disconnect\/(\S+)/) |
187 |
|
{ |
188 |
|
$self->deleteSubscriberWithID($1); |
189 |
|
$self->emitOKHeader(); |
190 |
|
$self->close(1); |
191 |
|
return; |
192 |
|
} |
193 |
elsif($self->{'headerBuffer'}=~/GET\s+([^\s\?]+)/) |
elsif($self->{'headerBuffer'}=~/GET\s+([^\s\?]+)/) |
194 |
{ |
{ |
195 |
Meteor::Document->serveFileToClient($1,$self); |
Meteor::Document->serveFileToClient($1,$self); |
|
|
|
196 |
$self->close(1); |
$self->close(1); |
|
|
|
197 |
return; |
return; |
198 |
} |
} |
199 |
|
|
204 |
} |
} |
205 |
} |
} |
206 |
|
|
207 |
sub setChannelName { |
sub setChannels { |
208 |
my $self=shift; |
my $self=shift; |
209 |
my $channelName=shift; |
my $channels=shift; |
|
my $startIndex=shift; |
|
210 |
my $persist=shift; |
my $persist=shift; |
211 |
|
|
212 |
my $channel=Meteor::Channel->channelWithName($channelName); |
foreach my $channelName (keys %{$channels}) |
213 |
$self->{'channel'}=$channel if($persist); |
{ |
214 |
|
my $startIndex=$channels->{$channelName}->{'startIndex'}; |
215 |
$channel->addSubscriber($self,$startIndex,$persist); |
|
216 |
|
my $channel=Meteor::Channel->channelWithName($channelName); |
217 |
|
|
218 |
|
$self->{'channels'}->{$channelName}=$channel if($persist); |
219 |
|
|
220 |
|
$channel->addSubscriber($self,$startIndex,$persist); |
221 |
|
} |
222 |
} |
} |
223 |
|
|
224 |
sub emitOKHeader { |
sub emitOKHeader { |
231 |
my $self=shift; |
my $self=shift; |
232 |
|
|
233 |
$self->emitHeader('404 Not Found'); |
$self->emitHeader('404 Not Found'); |
234 |
|
$::Statistics->{'errors_served'}++; |
235 |
|
|
236 |
# close up shop here! |
# close up shop here! |
237 |
$self->close(); |
$self->close(); |
279 |
sub sendMessage { |
sub sendMessage { |
280 |
my $self=shift; |
my $self=shift; |
281 |
my $msg=shift; |
my $msg=shift; |
282 |
|
my $numMsgInThisBatch=shift; |
283 |
|
|
284 |
|
$numMsgInThisBatch=1 unless(defined($numMsgInThisBatch)); |
285 |
|
|
286 |
$self->write($msg); |
$self->write($msg); |
287 |
|
|
288 |
|
$::Statistics->{'messages_served'}+=$numMsgInThisBatch; |
289 |
|
|
290 |
my $msgCount=++$self->{'MessageCount'}; |
my $msgCount=++$self->{'MessageCount'}; |
291 |
|
|
292 |
my $maxMsg=$::CONF{'MaxMessages'}; |
my $maxMsg=$::CONF{'MaxMessages'}; |
301 |
} |
} |
302 |
} |
} |
303 |
|
|
304 |
|
sub closeChannel { |
305 |
|
my $self=shift; |
306 |
|
my $channelName=shift; |
307 |
|
|
308 |
|
return unless(exists($self->{'channels'}->{$channelName})); |
309 |
|
|
310 |
|
my $channel=$self->{'channels'}->{$channelName}; |
311 |
|
$channel->removeSubscriber($self); |
312 |
|
|
313 |
|
delete($self->{'channels'}->{$channelName}); |
314 |
|
|
315 |
|
$self->close() if(scalar(keys %{$self->{'channels'}})==0); |
316 |
|
} |
317 |
|
|
318 |
sub close { |
sub close { |
319 |
my $self=shift; |
my $self=shift; |
320 |
my $noShutdownMsg=shift; |
my $noShutdownMsg=shift; |
321 |
|
|
322 |
$self->{'channel'}->removeSubscriber($self) if($self->{'channel'}); |
foreach my $channelName (keys %{$self->{'channels'}}) |
323 |
delete($self->{'channel'}); |
{ |
324 |
|
my $channel=$self->{'channels'}->{$channelName}; |
325 |
|
$channel->removeSubscriber($self); |
326 |
|
} |
327 |
|
delete($self->{'channels'}); |
328 |
|
|
329 |
if(exists($self->{'subscriberID'})) |
if(exists($self->{'subscriberID'})) |
330 |
{ |
{ |
347 |
$self->SUPER::close(); |
$self->SUPER::close(); |
348 |
} |
} |
349 |
|
|
350 |
|
sub didClose { |
351 |
|
|
352 |
|
$::Statistics->{'current_subscribers'}--; |
353 |
|
} |
354 |
|
|
355 |
sub checkForMaxTime { |
sub checkForMaxTime { |
356 |
my $self=shift; |
my $self=shift; |
357 |
my $time=shift; |
my $time=shift; |