--- googlecode.com/svn/trunk/Meteor/Subscriber.pm 2008/02/04 19:02:49 45 +++ googlecode.com/svn/trunk/Meteor/Subscriber.pm 2008/02/27 13:55:35 50 @@ -80,7 +80,7 @@ if(exists($PersistentConnections{$id})) { - $PersistentConnections{$id}->close(); + $PersistentConnections{$id}->close(0,'newSubscriberWithSameID'); } } @@ -136,25 +136,11 @@ # if($self->{'headerBuffer'}=~/GET\s+$::CONF{'SubscriberDynamicPageAddress'}\/([0-9a-z]+)\/([0-9a-z]+)\/(\S+)/i) { - my $subscriberID=$1; + $self->{'subscriberID'}=$1; $self->{'mode'}=$2; my $persist=$self->getConf('Persist'); - - if ($self->{'mode'} eq "xhrinteractive" || $self->{'mode'} eq "iframe" || $self->{'mode'} eq "serversent" || $self->{'mode'} eq "longpoll") { - $persist=1; - $self->{'MaxMessageCount'}=1 unless(!($self->{'mode'} eq "longpoll")); - } - if ($self->{'mode'} eq "iframe") { - $self->{'HeaderTemplateNumber'}=1; - } else { - $self->{'HeaderTemplateNumber'}=2; - } - my $maxTime=$self->getConf('MaxTime'); - if($maxTime>0) - { - $self->{'ConnectionTimeLimit'}=$self->{'ConnectionStart'}+$maxTime; - } + $self->{'ConnectionTimeLimit'} = ($self->{'ConnectionStart'}+$maxTime) if ($maxTime>0); my @channelData=split('/',$3); my $channels={}; @@ -172,21 +158,20 @@ } } } + my $useragent = ($self->{'headerBuffer'}=~/User-Agent: (.+)/i) ? $1 : "-"; delete($self->{'headerBuffer'}); - if($persist) - { - $self->{'subscriberID'}=$subscriberID; - $self->deleteSubscriberWithID($subscriberID); - $PersistentConnections{$subscriberID}=$self; + if ($persist) { + $self->deleteSubscriberWithID($self->{'subscriberID'}); + $PersistentConnections{$self->{'subscriberID'}}=$self; } if(scalar(keys %{$channels})) { $self->emitOKHeader(); - $self->setChannels($channels,$persist); - $self->close(1) unless($persist); + $self->setChannels($channels,$persist,$self->{'mode'},$useragent); + $self->close(1, 'responseComplete') unless($persist); return; } } @@ -194,13 +179,13 @@ { $self->deleteSubscriberWithID($1); $self->emitOKHeader(); - $self->close(1); + $self->close(1, 'disconnectRequested'); return; } elsif($self->{'headerBuffer'}=~/GET\s+([^\s\?]+)/) { Meteor::Document->serveFileToClient($1,$self); - $self->close(1); + $self->close(1, 'responseComplete'); return; } @@ -215,6 +200,8 @@ my $self=shift; my $channels=shift; my $persist=shift; + my $mode=shift || ''; + my $userAgent=shift || ''; foreach my $channelName (keys %{$channels}) { @@ -224,7 +211,7 @@ $self->{'channels'}->{$channelName}=$channel if($persist); - $channel->addSubscriber($self,$startIndex,$persist); + $channel->addSubscriber($self,$startIndex,$persist,$mode,$userAgent); } } @@ -241,21 +228,14 @@ $::Statistics->{'errors_served'}++; # close up shop here! - $self->close(); + $self->close(0, 'error'); } sub emitHeader { my $self=shift; my $status=shift; - my $header=undef; - if(exists($self->{'HeaderTemplateNumber'})) - { - my $hn='HeaderTemplate'.$self->{'HeaderTemplateNumber'}; - - $header=$self->getConf($hn); - } - $header=$self->getConf('HeaderTemplate') unless(defined($header)); + my $header=$self->getConf('HeaderTemplate'); $header=~s/~([^~]*)~/ if(!defined($1) || $1 eq '') @@ -291,7 +271,7 @@ my $self=shift; my $numMessages=0; - my $msgTemplate=$self->getConf('Messagetemplate'); + my $msgTemplate=$self->getConf('MessageTemplate'); my $msgData=''; foreach my $message (@_) @@ -313,12 +293,12 @@ my $maxMsg=$self->getConf('MaxMessages'); if(defined($maxMsg) && $maxMsg>0 && $msgCount>=$maxMsg) { - $self->close(1); + $self->close(1, 'maxMessageCountReached'); } if($self->{'MaxMessageCount'}>0 && $msgCount>=$self->{'MaxMessageCount'}) { - $self->close(1); + $self->close(1, 'maxMessageCountReached'); } } @@ -337,33 +317,35 @@ return unless(exists($self->{'channels'}->{$channelName})); my $channel=$self->{'channels'}->{$channelName}; - $channel->removeSubscriber($self); + $channel->removeSubscriber($self,'channelClose'); delete($self->{'channels'}->{$channelName}); - $self->close() if(scalar(keys %{$self->{'channels'}})==0); + $self->close(0,'channelClose') if(scalar(keys %{$self->{'channels'}})==0); } sub close { my $self=shift; my $noShutdownMsg=shift; + my $reason=shift; foreach my $channelName (keys %{$self->{'channels'}}) { my $channel=$self->{'channels'}->{$channelName}; - $channel->removeSubscriber($self); + $channel->removeSubscriber($self,$reason); } delete($self->{'channels'}); - if(exists($self->{'subscriberID'})) - { + # If this connection is in the PersistentConnections array, delete it, then anonymise + # it so that if we have to wait for the write buffer to empty before close, it's only + # removed once. + if(exists($self->{'subscriberID'})) { delete($PersistentConnections{$self->{'subscriberID'}}); + delete($self->{'subscriberID'}); } - # # Send shutdown message unless remote closed or # connection not yet established - # unless($noShutdownMsg || $self->{'remoteClosed'} || exists($self->{'headerBuffer'})) { my $msg=$self->getConf('SubscriberShutdownMsg'); @@ -385,7 +367,7 @@ my $self=shift; my $time=shift; - $self->close(1) if(exists($self->{'ConnectionTimeLimit'}) && $self->{'ConnectionTimeLimit'}<$time); + $self->close(1,'maxTime') if(exists($self->{'ConnectionTimeLimit'}) && $self->{'ConnectionTimeLimit'}<$time); } sub getConf { @@ -394,9 +376,11 @@ if(exists($self->{'mode'}) && $self->{'mode'} ne '') { - my $k=$key.'.'.$self->{'mode'}; + my $k=$key.$self->{'mode'}; - return $::CONF{$k} if(exists($::CONF{$k})); + if(exists($::CONF{$k})) { + return $::CONF{$k}; + } } $::CONF{$key};