/[meteor]/googlecode.com/svn/trunk/Meteor/Subscriber.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /googlecode.com/svn/trunk/Meteor/Subscriber.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 50 by andrew.betts, Wed Feb 27 13:55:35 2008 UTC revision 62 by andrew.betts, Thu Nov 27 00:33:21 2008 UTC
# Line 55  sub newFromServer { Line 55  sub newFromServer {
55          my $self=$class->SUPER::newFromServer(shift);          my $self=$class->SUPER::newFromServer(shift);
56                    
57          $self->{'headerBuffer'}='';          $self->{'headerBuffer'}='';
58          $self->{'MessageCount'}=0;          $self->{'messageCount'}=0;
         $self->{'MaxMessageCount'}=0;  
59                    
         $self->{'ConnectionStart'}=time;  
60          my $maxTime=$::CONF{'MaxTime'};          my $maxTime=$::CONF{'MaxTime'};
61          if($maxTime>0)          if($maxTime>0)
62          {          {
63                  $self->{'ConnectionTimeLimit'}=$self->{'ConnectionStart'}+$maxTime;                  $self->{'connectionTimeLimit'}=$self->{'connectionStart'}+$maxTime;
64          }          }
65                    
66          $::Statistics->{'current_subscribers'}++;          $::Statistics->{'current_subscribers'}++;
# Line 84  sub deleteSubscriberWithID { Line 82  sub deleteSubscriberWithID {
82          }          }
83  }  }
84    
85    sub subscriberExists {
86            my $class=shift;
87            my $id=shift;
88    
89            return exists($PersistentConnections{$id});
90    }
91    
92  sub pingPersistentConnections {  sub pingPersistentConnections {
93          my $class=shift;          my $class=shift;
94                    
# Line 106  sub numSubscribers { Line 111  sub numSubscribers {
111          return scalar(keys %PersistentConnections);          return scalar(keys %PersistentConnections);
112  }  }
113    
114    sub listSubscribers {
115            my $class=shift;
116            my $list='';
117            foreach my $subscriber (keys %PersistentConnections)
118            {
119                    my $sub = $PersistentConnections{$subscriber};
120                    $list .= $subscriber.' '.$sub->{'ip'}.' '.$sub->{'connectionStart'}.' '.$sub->{'connectionTimeLimit'}.' '.($sub->{'connectionTimeLimit'}-time).' '.$sub->{'messageCount'}.' "'.$sub->{'useragent'}."\"$::CRLF";
121            }
122            $list;
123    }
124    
125  ###############################################################################  ###############################################################################
126  # Instance methods  # Instance methods
127  ###############################################################################  ###############################################################################
# Line 113  sub processLine { Line 129  sub processLine {
129          my $self=shift;          my $self=shift;
130          my $line=shift;          my $line=shift;
131                    
132          # Once the header was processed we ignore any input          # Once the header was processed we ignore any input - Meteor does not accept or process request bodies
133          return unless(exists($self->{'headerBuffer'}));          return unless(exists($self->{'headerBuffer'}));
134                    
135          if($line ne '')          if($line ne '')
# Line 134  sub processLine { Line 150  sub processLine {
150                  #                  #
151                  # Find the 'GET' line                  # Find the 'GET' line
152                  #                  #
153                  if($self->{'headerBuffer'}=~/GET\s+$::CONF{'SubscriberDynamicPageAddress'}\/([0-9a-z]+)\/([0-9a-z]+)\/(\S+)/i)                  if($self->{'headerBuffer'}=~/GET\s+$::CONF{'SubscriberDynamicPageAddress'}\/([0-9a-z]+)\/([0-9a-z]+)\/([a-z0-9_\-\%\.\/]+).*?/i)
154                  {                  {
155                          $self->{'subscriberID'}=$1;                          $self->{'subscriberID'}=$1;
156                          $self->{'mode'}=$2;                          $self->{'mode'}=$2;
157                          my $persist=$self->getConf('Persist');                          my $persist=$self->getConf('Persist');
158                          my $maxTime=$self->getConf('MaxTime');                          my $maxTime=$self->getConf('MaxTime');
159                          $self->{'ConnectionTimeLimit'} = ($self->{'ConnectionStart'}+$maxTime) if ($maxTime>0);                          $self->{'connectionTimeLimit'} = ($self->{'connectionStart'}+$maxTime) if ($maxTime>0);
160                                                    
161                          my @channelData=split('/',$3);                          my @channelData=split('/',$3);
162                          my $channels={};                          my $channels={};
163                          my $channelName;                          my $channelName;
164                          my $offset;                          my $offset;
165                          foreach my $chandef (@channelData) {                          foreach my $chandef (@channelData) {
166                                  if($chandef=~/^([a-z0-9]+)(.(r|b|h)([0-9]*))?$/i) {                                  if($chandef=~/^([a-z0-9_\-\%]+)(.(r|b|h)([0-9]*))?$/i) {
167                                          $channelName = $1;                                          $channelName = $1;
168                                          $channels->{$channelName}->{'startIndex'} = undef;                                          $channels->{$channelName}->{'startIndex'} = undef;
169                                          if ($3) {                                          if ($3) {
# Line 158  sub processLine { Line 174  sub processLine {
174                                          }                                          }
175                                  }                                  }
176                          }                          }
177                          my $useragent = ($self->{'headerBuffer'}=~/User-Agent: (.+)/i) ? $1 : "-";                          $self->{'useragent'} = ($self->{'headerBuffer'}=~/User-Agent: (.+)/i) ? $1 : "-";
                           
                         delete($self->{'headerBuffer'});  
178                                                    
179                          if ($persist) {                          if ($persist) {
180                                    # New persistent connection: kill any existing connection with same ID
181                                  $self->deleteSubscriberWithID($self->{'subscriberID'});                                  $self->deleteSubscriberWithID($self->{'subscriberID'});
182                                    # Add new persistent connection to collection
183                                  $PersistentConnections{$self->{'subscriberID'}}=$self;                                  $PersistentConnections{$self->{'subscriberID'}}=$self;
184                            } else {
185                                    $::Pollers->{$self->{'subscriberID'}} = time;
186                          }                          }
187                                                    
188                          if(scalar(keys %{$channels}))                          if(scalar(keys %{$channels})) {
189                          {  
190                                    $self->{'channelinfo'} = '';
191                                    my $citemplate = $self->getConf('ChannelInfoTemplate');
192                                    foreach $channelName (keys %{$channels}) {
193                                            my $channel=Meteor::Channel->channelWithName($channelName);
194                                            $self->{'channels'}->{$channelName}=$channel;
195                                            if (defined($self->{'channels'}->{$channelName}->{'startIndex'}) && $self->{'channels'}->{$channelName}->{'startIndex'} > 0) {
196                                                    $self->{'channelinfo'} .= $channel->descriptionWithTemplate($citemplate);
197                                            }
198                                    }
199                                  $self->emitOKHeader();                                  $self->emitOKHeader();
200                                  $self->setChannels($channels,$persist,$self->{'mode'},$useragent);                                  foreach $channelName (keys %{$channels}) {
201                                            my $startIndex=$channels->{$channelName}->{'startIndex'};
202                                            $self->{'channels'}->{$channelName}->addSubscriber($self,$startIndex,$persist,$self->{'mode'},$self->{'useragent'});
203                                    }
204                                    delete ($self->{'channels'}) unless($persist);
205                                  $self->close(1, 'responseComplete') unless($persist);                                  $self->close(1, 'responseComplete') unless($persist);
206                                    $self->close(1, 'closedOnEvent') unless($self->{'messageCount'} == 0);
207                                    delete($self->{'headerBuffer'});
208                                  return;                                  return;
209                          }                          }
210                  }                  }
# Line 196  sub processLine { Line 229  sub processLine {
229          }          }
230  }  }
231    
 sub setChannels {  
         my $self=shift;  
         my $channels=shift;  
         my $persist=shift;  
         my $mode=shift || '';  
         my $userAgent=shift || '';  
           
         foreach my $channelName (keys %{$channels})  
         {  
                 my $startIndex=$channels->{$channelName}->{'startIndex'};  
                   
                 my $channel=Meteor::Channel->channelWithName($channelName);  
                   
                 $self->{'channels'}->{$channelName}=$channel if($persist);  
                   
                 $channel->addSubscriber($self,$startIndex,$persist,$mode,$userAgent);  
         }  
 }  
   
232  sub emitOKHeader {  sub emitOKHeader {
233          my $self=shift;          my $self=shift;
234                    
# Line 238  sub emitHeader { Line 252  sub emitHeader {
252          my $header=$self->getConf('HeaderTemplate');          my $header=$self->getConf('HeaderTemplate');
253                    
254          $header=~s/~([^~]*)~/          $header=~s/~([^~]*)~/
255                  if(!defined($1) || $1 eq '')                  if(!defined($1) || $1 eq '') {
                 {  
256                          '~';                          '~';
257                  }                  } elsif($1 eq 'server') {
                 elsif($1 eq 'server')  
                 {  
258                          $::PGM;                          $::PGM;
259                  }                  } elsif($1 eq 'status') {
                 elsif($1 eq 'status')  
                 {  
260                          $status;                          $status;
261                  }                  } elsif($1 eq 'servertime') {
                 elsif($1 eq 'servertime')  
                 {  
262                          time;                          time;
263                  }                  } elsif($1 eq 'channelinfo') {
264                  elsif($1 eq 'channelinfo')                          $self->{'channelinfo'};
265                  {                  } else {
                         Meteor::Channel->listChannelsUsingTemplate($self->getConf('ChannelInfoTemplate'));  
                 }  
                 else  
                 {  
266                          '';                          '';
267                  }                  }
268          /gex;          /gex;
# Line 286  sub sendMessages { Line 289  sub sendMessages {
289                    
290          $::Statistics->{'messages_served'}+=$numMessages;          $::Statistics->{'messages_served'}+=$numMessages;
291                    
292          my $msgCount=$self->{'MessageCount'};          my $msgCount=$self->{'messageCount'};
293          $msgCount+=$numMessages;          $msgCount+=$numMessages;
294          $self->{'MessageCount'}=$msgCount;          $self->{'messageCount'}=$msgCount;
           
         my $maxMsg=$self->getConf('MaxMessages');  
         if(defined($maxMsg) && $maxMsg>0 && $msgCount>=$maxMsg)  
         {  
                 $self->close(1, 'maxMessageCountReached');  
         }  
295                    
296          if($self->{'MaxMessageCount'}>0 && $msgCount>=$self->{'MaxMessageCount'})          # If long polling, close connection, as a message has now been sent.
297          {          # Don't close if still processing the header (we may be sending a backlog from multiple channels)
298                  $self->close(1, 'maxMessageCountReached');          if($self->getConf('CloseOnEvent') && !exists($self->{'headerBuffer'})) {
299                    $self->close(1, 'closedOnEvent');
300          }          }
           
301  }  }
302    
303  sub ping {  sub ping {
# Line 354  sub close { Line 351  sub close {
351                          $self->write($msg);                          $self->write($msg);
352                  }                  }
353          }          }
354            
355            my $fmsg=$self->getConf('FooterTemplate');
356            if(defined($fmsg) && $fmsg ne '')
357            {
358                    $self->write($fmsg);
359            }
360    
361          $self->SUPER::close();          $self->SUPER::close();
362  }  }
363    
# Line 367  sub checkForMaxTime { Line 370  sub checkForMaxTime {
370          my $self=shift;          my $self=shift;
371          my $time=shift;          my $time=shift;
372                    
373          $self->close(1,'maxTime') if(exists($self->{'ConnectionTimeLimit'}) && $self->{'ConnectionTimeLimit'}<$time);          $self->close(1,'maxTime') if(exists($self->{'connectionTimeLimit'}) && $self->{'connectionTimeLimit'}<$time);
374  }  }
375    
376  sub getConf {  sub getConf {

Legend:
Removed from v.50  
changed lines
  Added in v.62

  ViewVC Help
Powered by ViewVC 1.1.26