/[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 11 by knops.gerd, Thu Dec 14 16:29:42 2006 UTC revision 53 by andrew.betts, Wed Feb 27 21:58:56 2008 UTC
# Line 43  package Meteor::Subscriber; Line 43  package Meteor::Subscriber;
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
# Line 63  sub newFromServer { Line 65  sub newFromServer {
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    
# Line 75  sub deleteSubscriberWithID { Line 80  sub deleteSubscriberWithID {
80                    
81          if(exists($PersistentConnections{$id}))          if(exists($PersistentConnections{$id}))
82          {          {
83                  $PersistentConnections{$id}->close(1);                  $PersistentConnections{$id}->close(0,'newSubscriberWithSameID');
84          }          }
85  }  }
86    
87  sub pingPersistentConnections {  sub pingPersistentConnections {
88          my $class=shift;          my $class=shift;
89                    
         my $msg=$::CONF{'PingMessage'};  
90          my @cons=values %PersistentConnections;          my @cons=values %PersistentConnections;
91                    
92          map { $_->write($msg) } @cons;          map { $_->ping() } @cons;
93  }  }
94    
95  sub checkPersistentConnectionsForMaxTime {  sub checkPersistentConnectionsForMaxTime {
# Line 97  sub checkPersistentConnectionsForMaxTime Line 101  sub checkPersistentConnectionsForMaxTime
101          map { $_->checkForMaxTime($time) } @cons;          map { $_->checkForMaxTime($time) } @cons;
102  }  }
103    
104    sub numSubscribers {
105            
106            return scalar(keys %PersistentConnections);
107    }
108    
109  ###############################################################################  ###############################################################################
110  # Instance methods  # Instance methods
111  ###############################################################################  ###############################################################################
# Line 121  sub processLine { Line 130  sub processLine {
130                  # Analyze header, register with appropiate channel                  # Analyze header, register with appropiate channel
131                  # and send pending messages.                  # and send pending messages.
132                  #                  #
133                  # GET $::CONF{'SubscriberDynamicPageAddress'}?channel=ml123&restartfrom=1 HTTP/1.1                  # GET $::CONF{'SubscriberDynamicPageAddress'}/hostid/streamtype/channeldefs HTTP/1.1
134                  #                  #
135                  # Find the 'GET' line                  # Find the 'GET' line
136                  #                  #
137                  if($self->{'headerBuffer'}=~/GET\s+$::CONF{'SubscriberDynamicPageAddress'}\?(\S+)/)                  if($self->{'headerBuffer'}=~/GET\s+$::CONF{'SubscriberDynamicPageAddress'}\/([0-9a-z]+)\/([0-9a-z]+)\/(\S+)/i)
138                  {                  {
139                          my @formData=split('&',$1);                          $self->{'subscriberID'}=$1;
140                          my $channelName=undef;                          $self->{'mode'}=$2;
141                          my $startIndex=undef;                          my $persist=$self->getConf('Persist');
142                          my $backtrack=undef;                          my $maxTime=$self->getConf('MaxTime');
143                          my $persist=1;                          $self->{'ConnectionTimeLimit'} = ($self->{'ConnectionStart'}+$maxTime) if ($maxTime>0);
144                          my $subscriberID=undef;                          
145                          foreach my $formElement (@formData)                          my @channelData=split('/',$3);
146                          {                          my $channels={};
147                                  if($formElement=~/^channel=(.+)$/)                          my $channelName;
148                                  {                          my $offset;
149                                          $channelName=$1;                          foreach my $chandef (@channelData) {
150                                  }                                  if($chandef=~/^([a-z0-9_\-\%]+)(.(r|b|h)([0-9]*))?$/i) {
151                                  elsif($formElement=~/^restartfrom=(\d*)$/)                                          $channelName = $1;
152                                  {                                          $channels->{$channelName}->{'startIndex'} = undef;
153                                          $startIndex=$1;                                          if ($3) {
154                                          $startIndex='' unless(defined($startIndex));                                             $offset = $4;
155                                  }                                             if ($3 eq 'r') { $channels->{$channelName}->{'startIndex'} = $offset; }
156                                  elsif($formElement=~/^backtrack=(\d+)$/)                                             if ($3 eq 'b') { $channels->{$channelName}->{'startIndex'} = -$offset; }
157                                  {                                             if ($3 eq 'h') { $channels->{$channelName}->{'startIndex'} = 0; }
                                         $backtrack=$1;  
                                         $backtrack=0 unless(defined($backtrack));  
                                 }  
                                 elsif($formElement=~/^persist=(?i)(yes|true|1|no|false|0)$/)  
                                 {  
                                         $persist=0 if($1=~/(no|false|0)/i);  
                                 }  
                                 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;  
158                                          }                                          }
159                                  }                                  }
160                          }                          }
161                                                                            my $useragent = ($self->{'headerBuffer'}=~/User-Agent: (.+)/i) ? $1 : "-";
                         delete($self->{'headerBuffer'});  
162                                                    
163                          if(defined($startIndex) && defined($backtrack))                          delete($self->{'headerBuffer'});
                         {  
                                 $self->emitHeader("404 Cannot use both 'restartfrom' and 'backtrack'");  
                                 $self->close();  
                                   
                                 return;  
                         }  
164                                                    
165                          if(defined($subscriberID) && $persist)                          if ($persist) {
166                          {                                  $self->deleteSubscriberWithID($self->{'subscriberID'});
167                                  $self->{'subscriberID'}=$subscriberID;                                  $PersistentConnections{$self->{'subscriberID'}}=$self;
                                 $self->deleteSubscriberWithID($subscriberID);  
                                 $PersistentConnections{$subscriberID}=$self;  
168                          }                          }
169                                                    
170                          if(defined($channelName))                          if(scalar(keys %{$channels})) {
171                          {  
172                                    $self->{'channelinfo'} = '';
173                                    my $citemplate = $self->getConf('ChannelInfoTemplate');
174                                    foreach $channelName (keys %{$channels}) {
175                                            my $channel=Meteor::Channel->channelWithName($channelName);
176                                            $self->{'channels'}->{$channelName}=$channel;
177                                            $self->{'channelinfo'} .= $channel->descriptionWithTemplate($citemplate);
178                                            
179                                    }
180                                  $self->emitOKHeader();                                  $self->emitOKHeader();
181                                                                    foreach $channelName (keys %{$channels}) {
182                                  $startIndex=-$backtrack if(!defined($startIndex) && defined($backtrack));                                          my $startIndex=$channels->{$channelName}->{'startIndex'};
183                                                                            $self->{'channels'}->{$channelName}->addSubscriber($self,$startIndex,$persist,$self->{'mode'},$useragent);
184                                  $self->setChannelName($channelName,$startIndex,$persist);                                  }
185                                                                    delete ($self->{'channels'}) unless($persist);
186                                  $self->close(1) unless($persist);                                  $self->close(1, 'responseComplete') unless($persist);
                                   
187                                  return;                                  return;
188                          }                          }
189                  }                  }
190                    elsif($self->{'headerBuffer'}=~/GET\s+\/disconnect\/(\S+)/)
191                    {
192                            $self->deleteSubscriberWithID($1);
193                            $self->emitOKHeader();
194                            $self->close(1, 'disconnectRequested');
195                            return;
196                    }
197                  elsif($self->{'headerBuffer'}=~/GET\s+([^\s\?]+)/)                  elsif($self->{'headerBuffer'}=~/GET\s+([^\s\?]+)/)
198                  {                  {
199                          Meteor::Document->serveFileToClient($1,$self);                          Meteor::Document->serveFileToClient($1,$self);
200                                                    $self->close(1, 'responseComplete');
                         $self->close(1);  
                           
201                          return;                          return;
202                  }                  }
203                                    
# Line 223  sub processLine { Line 208  sub processLine {
208          }          }
209  }  }
210    
 sub setChannelName {  
         my $self=shift;  
         my $channelName=shift;  
         my $startIndex=shift;  
         my $persist=shift;  
           
         my $channel=Meteor::Channel->channelWithName($channelName);  
         $self->{'channel'}=$channel if($persist);  
           
         $channel->addSubscriber($self,$startIndex,$persist);  
 }  
   
211  sub emitOKHeader {  sub emitOKHeader {
212          my $self=shift;          my $self=shift;
213                    
# Line 245  sub emitErrorHeader { Line 218  sub emitErrorHeader {
218          my $self=shift;          my $self=shift;
219                    
220          $self->emitHeader('404 Not Found');          $self->emitHeader('404 Not Found');
221            $::Statistics->{'errors_served'}++;
222                    
223          # close up shop here!          # close up shop here!
224          $self->close();          $self->close(0, 'error');
225  }  }
226    
227  sub emitHeader {  sub emitHeader {
228          my $self=shift;          my $self=shift;
229          my $status=shift;          my $status=shift;
230                    
231          my $header=undef;          my $header=$self->getConf('HeaderTemplate');
         if(exists($self->{'HeaderTemplateNumber'}))  
         {  
                 my $hn='HeaderTemplate'.$self->{'HeaderTemplateNumber'};  
                   
                 $header=$::CONF{$hn};  
         }  
         $header=$::CONF{'HeaderTemplate'} unless(defined($header));  
232                    
233          $header=~s/~([^~]+)~/          $header=~s/~([^~]*)~/
234                  if(!defined($1) || $1 eq '')                  if(!defined($1) || $1 eq '') {
                 {  
235                          '~';                          '~';
236                  }                  } elsif($1 eq 'server') {
                 elsif($1 eq 'server')  
                 {  
237                          $::PGM;                          $::PGM;
238                  }                  } elsif($1 eq 'status') {
                 elsif($1 eq 'status')  
                 {  
239                          $status;                          $status;
240                  }                  } elsif($1 eq 'servertime') {
                 elsif($1 eq 'servertime')  
                 {  
241                          time;                          time;
242                  }                  } elsif($1 eq 'channelinfo') {
243                  else                          $self->{'channelinfo'};
244                  {                  } else {
245                          '';                          '';
246                  }                  }
247          /gex;          /gex;
# Line 289  sub emitHeader { Line 249  sub emitHeader {
249          $self->write($header);          $self->write($header);
250  }  }
251    
252  sub sendMessage {  sub sendMessages {
253          my $self=shift;          my $self=shift;
         my $msg=shift;  
254                    
255          $self->write($msg);          my $numMessages=0;
256            my $msgTemplate=$self->getConf('MessageTemplate');
257            my $msgData='';
258            
259            foreach my $message (@_)
260            {
261                    $msgData.=$message->messageWithTemplate($msgTemplate);
262                    $numMessages++;
263            }
264            
265            return if($numMessages<1);
266            
267            $self->write($msgData);
268                    
269          my $msgCount=++$self->{'MessageCount'};          $::Statistics->{'messages_served'}+=$numMessages;
270                    
271          my $maxMsg=$::CONF{'MaxMessages'};          my $msgCount=$self->{'MessageCount'};
272            $msgCount+=$numMessages;
273            $self->{'MessageCount'}=$msgCount;
274            
275            my $maxMsg=$self->getConf('MaxMessages');
276          if(defined($maxMsg) && $maxMsg>0 && $msgCount>=$maxMsg)          if(defined($maxMsg) && $maxMsg>0 && $msgCount>=$maxMsg)
277          {          {
278                  $self->close(1);                  $self->close(1, 'maxMessageCountReached');
279          }          }
280                    
281          if($self->{'MaxMessageCount'}>0 && $msgCount>=$self->{'MaxMessageCount'})          if($self->{'MaxMessageCount'}>0 && $msgCount>=$self->{'MaxMessageCount'})
282          {          {
283                  $self->close(1);                  $self->close(1, 'maxMessageCountReached');
284          }          }
285            
286    }
287    
288    sub ping {
289            my $self=shift;
290            my $msg=$self->getConf('PingMessage');
291            
292            $self->write($msg);
293    }
294    
295    sub closeChannel {
296            my $self=shift;
297            my $channelName=shift;
298            
299            return unless(exists($self->{'channels'}->{$channelName}));
300            
301            my $channel=$self->{'channels'}->{$channelName};
302            $channel->removeSubscriber($self,'channelClose');
303            
304            delete($self->{'channels'}->{$channelName});
305            
306            $self->close(0,'channelClose') if(scalar(keys %{$self->{'channels'}})==0);
307  }  }
308    
309  sub close {  sub close {
310          my $self=shift;          my $self=shift;
311          my $noShutdownMsg=shift;          my $noShutdownMsg=shift;
312            my $reason=shift;
313                    
314          $self->{'channel'}->removeSubscriber($self) if($self->{'channel'});          foreach my $channelName (keys %{$self->{'channels'}})
         delete($self->{'channel'});  
           
         if(exists($self->{'subscriberID'}))  
315          {          {
316                    my $channel=$self->{'channels'}->{$channelName};
317                    $channel->removeSubscriber($self,$reason);
318            }
319            delete($self->{'channels'});
320            
321            # If this connection is in the PersistentConnections array, delete it, then anonymise
322            # it so that if we have to wait for the write buffer to empty before close, it's only
323            # removed once.
324            if(exists($self->{'subscriberID'})) {
325                  delete($PersistentConnections{$self->{'subscriberID'}});                  delete($PersistentConnections{$self->{'subscriberID'}});
326                    delete($self->{'subscriberID'});
327          }          }
328                    
         #  
329          # Send shutdown message unless remote closed or          # Send shutdown message unless remote closed or
330          # connection not yet established          # connection not yet established
         #  
331          unless($noShutdownMsg || $self->{'remoteClosed'} || exists($self->{'headerBuffer'}))          unless($noShutdownMsg || $self->{'remoteClosed'} || exists($self->{'headerBuffer'}))
332          {          {
333                  my $msg=$::CONF{'SubscriberShutdownMsg'};                  my $msg=$self->getConf('SubscriberShutdownMsg');
334                  if(defined($msg) && $msg ne '')                  if(defined($msg) && $msg ne '')
335                  {                  {
336                          $self->write($msg);                          $self->write($msg);
# Line 337  sub close { Line 340  sub close {
340          $self->SUPER::close();          $self->SUPER::close();
341  }  }
342    
343    sub didClose {
344            
345            $::Statistics->{'current_subscribers'}--;
346    }
347    
348  sub checkForMaxTime {  sub checkForMaxTime {
349          my $self=shift;          my $self=shift;
350          my $time=shift;          my $time=shift;
351                    
352          $self->close(1) if(exists($self->{'ConnectionTimeLimit'}) && $self->{'ConnectionTimeLimit'}<$time);          $self->close(1,'maxTime') if(exists($self->{'ConnectionTimeLimit'}) && $self->{'ConnectionTimeLimit'}<$time);
353    }
354    
355    sub getConf {
356            my $self=shift;
357            my $key=shift;
358            
359            if(exists($self->{'mode'}) && $self->{'mode'} ne '')
360            {
361                    my $k=$key.$self->{'mode'};
362                    
363                    if(exists($::CONF{$k})) {
364                            return $::CONF{$k};
365                    }
366            }
367            
368            $::CONF{$key};
369  }  }
370    
371  1;  1;

Legend:
Removed from v.11  
changed lines
  Added in v.53

  ViewVC Help
Powered by ViewVC 1.1.26