/[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 9 by andrew.betts, Fri Dec 8 16:52:58 2006 UTC revision 56 by andrew.betts, Thu Feb 28 13:33:06 2008 UTC
# Line 1  Line 1 
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  ###############################################################################  ###############################################################################
50  sub newFromServer {  # Factory methods
51          my $class=shift;  ###############################################################################
52            sub newFromServer {
53          my $self=$class->SUPER::newFromServer(shift);          my $class=shift;
54                    
55          $self->{'headerBuffer'}='';          my $self=$class->SUPER::newFromServer(shift);
56          $self->{'MessageCount'}=0;          
57          $self->{'MaxMessageCount'}=0;          $self->{'headerBuffer'}='';
58                    $self->{'MessageCount'}=0;
59          $self->{'ConnectionStart'}=time;          $self->{'MaxMessageCount'}=0;
60          my $maxTime=$::CONF{'MaxTime'};          
61          if($maxTime>0)          $self->{'ConnectionStart'}=time;
62          {          my $maxTime=$::CONF{'MaxTime'};
63                  $self->{'ConnectionTimeLimit'}=$self->{'ConnectionStart'}+$maxTime;          if($maxTime>0)
64          }          {
65                            $self->{'ConnectionTimeLimit'}=$self->{'ConnectionStart'}+$maxTime;
66          $self;          }
67  }          
68            $::Statistics->{'current_subscribers'}++;
69  ###############################################################################          $::Statistics->{'subscriber_connections_accepted'}++;
70  # Class methods          
71  ###############################################################################          $self;
72  sub deleteSubscriberWithID {  }
73          my $class=shift;  
74          my $id=shift;  ###############################################################################
75            # Class methods
76          if(exists($PersistentConnections{$id}))  ###############################################################################
77          {  sub deleteSubscriberWithID {
78                  $PersistentConnections{$id}->close(1);          my $class=shift;
79          }          my $id=shift;
80  }          
81            if(exists($PersistentConnections{$id}))
82  sub pingPersistentConnections {          {
83          my $class=shift;                  $PersistentConnections{$id}->close(0,'newSubscriberWithSameID');
84                    }
85          my $msg=$::CONF{'PingMessage'};  }
86          my @cons=values %PersistentConnections;  
87            sub pingPersistentConnections {
88          map { $_->write($msg) } @cons;          my $class=shift;
89  }          
90            my @cons=values %PersistentConnections;
91  sub checkPersistentConnectionsForMaxTime {          
92          my $class=shift;          map { $_->ping() } @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]+)\/([a-z0-9_\-\%\.\/]+).*?/i)
138                                  if($formElement=~/^channel=(.+)$/)                  {
139                                  {                          $self->{'subscriberID'}=$1;
140                                          $channelName=$1;                          $self->{'mode'}=$2;
141                                  }                          my $persist=$self->getConf('Persist');
142                                  elsif($formElement=~/^restartfrom=(\d*)$/)                          my $maxTime=$self->getConf('MaxTime');
143                                  {                          $self->{'ConnectionTimeLimit'} = ($self->{'ConnectionStart'}+$maxTime) if ($maxTime>0);
144                                          $startIndex=$1;                          
145                                          $startIndex='' unless(defined($startIndex));                          my @channelData=split('/',$3);
146                                  }                          my $channels={};
147                                  elsif($formElement=~/^backtrack=(\d+)$/)                          my $channelName;
148                                  {                          my $offset;
149                                          $backtrack=$1;                          foreach my $chandef (@channelData) {
150                                          $backtrack=0 unless(defined($backtrack));                                  if($chandef=~/^([a-z0-9_\-\%]+)(.(r|b|h)([0-9]*))?$/i) {
151                                  }                                          $channelName = $1;
152                                  elsif($formElement=~/^persist=(?i)(yes|true|1|no|false|0)$/)                                          $channels->{$channelName}->{'startIndex'} = undef;
153                                  {                                          if ($3) {
154                                          $persist=0 if($1=~/(no|false|0)/i);                                             $offset = $4;
155                                  }                                             if ($3 eq 'r') { $channels->{$channelName}->{'startIndex'} = $offset; }
156                                  elsif($formElement=~/^id=(.+)$/)                                             if ($3 eq 'b') { $channels->{$channelName}->{'startIndex'} = -$offset; }
157                                  {                                             if ($3 eq 'h') { $channels->{$channelName}->{'startIndex'} = 0; }
158                                          $subscriberID=$1;                                          }
159                                  }                                  }
160                                  elsif($formElement=~/^maxmessages=(\d+)$/i)                          }
161                                  {                          my $useragent = ($self->{'headerBuffer'}=~/User-Agent: (.+)/i) ? $1 : "-";
162                                          $self->{'MaxMessageCount'}=$1;                          
163                                  }                          delete($self->{'headerBuffer'});
164                                  elsif($formElement=~/^template=(\d+)$/i)                          
165                                  {                          if ($persist) {
166                                          $self->{'HeaderTemplateNumber'}=$1;                                  $self->deleteSubscriberWithID($self->{'subscriberID'});
167                                  }                                  $PersistentConnections{$self->{'subscriberID'}}=$self;
168                                  elsif($formElement=~/^maxtime=(\d+)$/i)                          }
169                                  {                          
170                                          my $clientRequest=$1;                          if(scalar(keys %{$channels})) {
171                                          my $serverDefault=$::CONF{'MaxTime'};  
172                                                                            $self->{'channelinfo'} = '';
173                                          if($serverDefault==0 || $serverDefault>$clientRequest)                                  my $citemplate = $self->getConf('ChannelInfoTemplate');
174                                          {                                  foreach $channelName (keys %{$channels}) {
175                                                  $self->{'ConnectionTimeLimit'}=$self->{'ConnectionStart'}+$clientRequest;                                          my $channel=Meteor::Channel->channelWithName($channelName);
176                                          }                                          $self->{'channels'}->{$channelName}=$channel;
177                                  }                                          $self->{'channelinfo'} .= $channel->descriptionWithTemplate($citemplate);
178                          }                                          
179                                                                                    }
180                          delete($self->{'headerBuffer'});                                  $self->emitOKHeader();
181                                                            foreach $channelName (keys %{$channels}) {
182                          if(defined($startIndex) && defined($backtrack))                                          my $startIndex=$channels->{$channelName}->{'startIndex'};
183                          {                                          $self->{'channels'}->{$channelName}->addSubscriber($self,$startIndex,$persist,$self->{'mode'},$useragent);
184                                  $self->emitHeader("404 Cannot use both 'restartfrom' and 'backtrack'");                                  }
185                                  $self->close();                                  delete ($self->{'channels'}) unless($persist);
186                                                                    $self->close(1, 'responseComplete') unless($persist);
187                                  return;                                  return;
188                          }                          }
189                                            }
190                          if(defined($subscriberID) && $persist)                  elsif($self->{'headerBuffer'}=~/GET\s+\/disconnect\/(\S+)/)
191                          {                  {
192                                  $self->{'subscriberID'}=$subscriberID;                          $self->deleteSubscriberWithID($1);
193                                  $self->deleteSubscriberWithID($subscriberID);                          $self->emitOKHeader();
194                                  $PersistentConnections{$subscriberID}=$self;                          $self->close(1, 'disconnectRequested');
195                          }                          return;
196                                            }
197                          if(defined($channelName))                  elsif($self->{'headerBuffer'}=~/GET\s+([^\s\?]+)/)
198                          {                  {
199                                  $self->emitOKHeader();                          Meteor::Document->serveFileToClient($1,$self);
200                                                            $self->close(1, 'responseComplete');
201                                  $startIndex=-$backtrack if(!defined($startIndex) && defined($backtrack));                          return;
202                                                    }
203                                  $self->setChannelName($channelName,$startIndex,$persist);                  
204                                                    #
205                                  $self->close(1) unless($persist);                  # If we fall through we did not understand the request
206                                                    #
207                                  return;                  $self->emitErrorHeader();
208                          }          }
209                  }  }
210                  elsif($self->{'headerBuffer'}=~/GET\s+([^\s\?]+)/)  
211                  {  sub emitOKHeader {
212                          Meteor::Document->serveFileToClient($1,$self);          my $self=shift;
213                                    
214                          $self->close(1);          $self->emitHeader('200 OK');
215                            }
216                          return;  
217                  }  sub emitErrorHeader {
218                            my $self=shift;
219                  #          
220                  # If we fall through we did not understand the request          $self->emitHeader('404 Not Found');
221                  #          $::Statistics->{'errors_served'}++;
222                  $self->emitErrorHeader();          
223          }          # close up shop here!
224  }          $self->close(0, 'error');
225    }
226  sub setChannelName {  
227          my $self=shift;  sub emitHeader {
228          my $channelName=shift;          my $self=shift;
229          my $startIndex=shift;          my $status=shift;
230          my $persist=shift;          
231                    my $header=$self->getConf('HeaderTemplate');
232          my $channel=Meteor::Channel->channelWithName($channelName);          
233          $self->{'channel'}=$channel if($persist);          $header=~s/~([^~]*)~/
234                            if(!defined($1) || $1 eq '') {
235          $channel->addSubscriber($self,$startIndex,$persist);                          '~';
236  }                  } elsif($1 eq 'server') {
237                            $::PGM;
238  sub emitOKHeader {                  } elsif($1 eq 'status') {
239          my $self=shift;                          $status;
240                            } elsif($1 eq 'servertime') {
241          $self->emitHeader('200 OK');                          time;
242  }                  } elsif($1 eq 'channelinfo') {
243                            $self->{'channelinfo'};
244  sub emitErrorHeader {                  } else {
245          my $self=shift;                          '';
246                            }
247          $self->emitHeader('404 Not Found');          /gex;
248                    
249          # close up shop here!          $self->write($header);
250          $self->close();  }
251  }  
252    sub sendMessages {
253  sub emitHeader {          my $self=shift;
254          my $self=shift;          
255          my $status=shift;          my $numMessages=0;
256                    my $msgTemplate=$self->getConf('MessageTemplate');
257          my $header=undef;          my $msgData='';
258          if(exists($self->{'HeaderTemplateNumber'}))          
259          {          foreach my $message (@_)
260                  my $hn='HeaderTemplate'.$self->{'HeaderTemplateNumber'};          {
261                                    $msgData.=$message->messageWithTemplate($msgTemplate);
262                  $header=$::CONF{$hn};                  $numMessages++;
263          }          }
264          $header=$::CONF{'HeaderTemplate'} unless(defined($header));          
265                    return if($numMessages<1);
266          $header=~s/~([^~]+)~/          
267                  if(!defined($1) || $1 eq '')          $self->write($msgData);
268                  {          
269                          '~';          $::Statistics->{'messages_served'}+=$numMessages;
270                  }          
271                  elsif($1 eq 'server')          my $msgCount=$self->{'MessageCount'};
272                  {          $msgCount+=$numMessages;
273                          $::PGM;          $self->{'MessageCount'}=$msgCount;
274                  }          
275                  elsif($1 eq 'status')          my $maxMsg=$self->getConf('MaxMessages');
276                  {          if(defined($maxMsg) && $maxMsg>0 && $msgCount>=$maxMsg)
277                          $status;          {
278                  }                  $self->close(1, 'maxMessageCountReached');
279                  elsif($1 eq 'servertime')          }
280                  {          
281                          time;          if($self->{'MaxMessageCount'}>0 && $msgCount>=$self->{'MaxMessageCount'})
282                  }          {
283                  else                  $self->close(1, 'maxMessageCountReached');
284                  {          }
285                          '';          
286                  }  }
287          /gex;  
288            sub ping {
289          $self->write($header);          my $self=shift;
290  }          my $msg=$self->getConf('PingMessage');
291            
292  sub sendMessage {          $self->write($msg);
293          my $self=shift;  }
294          my $msg=shift;  
295            sub closeChannel {
296          $self->write($msg);          my $self=shift;
297                    my $channelName=shift;
298          my $msgCount=++$self->{'MessageCount'};          
299                    return unless(exists($self->{'channels'}->{$channelName}));
300          my $maxMsg=$::CONF{'MaxMessages'};          
301          if(defined($maxMsg) && $maxMsg>0 && $msgCount>=$maxMsg)          my $channel=$self->{'channels'}->{$channelName};
302          {          $channel->removeSubscriber($self,'channelClose');
303                  $self->close(1);          
304          }          delete($self->{'channels'}->{$channelName});
305                    
306          if($self->{'MaxMessageCount'}>0 && $msgCount>=$self->{'MaxMessageCount'})          $self->close(0,'channelClose') if(scalar(keys %{$self->{'channels'}})==0);
307          {  }
308                  $self->close(1);  
309          }  sub close {
310  }          my $self=shift;
311            my $noShutdownMsg=shift;
312  sub close {          my $reason=shift;
313          my $self=shift;          
314          my $noShutdownMsg=shift;          foreach my $channelName (keys %{$self->{'channels'}})
315                    {
316          $self->{'channel'}->removeSubscriber($self) if($self->{'channel'});                  my $channel=$self->{'channels'}->{$channelName};
317          delete($self->{'channel'});                  $channel->removeSubscriber($self,$reason);
318                    }
319          if(exists($self->{'subscriberID'}))          delete($self->{'channels'});
320          {          
321                  delete($PersistentConnections{$self->{'subscriberID'}});          # 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          # Send shutdown message unless remote closed or                  delete($PersistentConnections{$self->{'subscriberID'}});
326          # connection not yet established                  delete($self->{'subscriberID'});
327          #          }
328          unless($noShutdownMsg || $self->{'remoteClosed'} || exists($self->{'headerBuffer'}))          
329          {          # Send shutdown message unless remote closed or
330                  my $msg=$::CONF{'SubscriberShutdownMsg'};          # connection not yet established
331                  if(defined($msg) && $msg ne '')          unless($noShutdownMsg || $self->{'remoteClosed'} || exists($self->{'headerBuffer'}))
332                  {          {
333                          $self->write($msg);                  my $msg=$self->getConf('SubscriberShutdownMsg');
334                  }                  if(defined($msg) && $msg ne '')
335          }                  {
336                                    $self->write($msg);
337          $self->SUPER::close();                  }
338  }          }
339            
340  sub checkForMaxTime {          $self->SUPER::close();
341          my $self=shift;  }
342          my $time=shift;  
343            sub didClose {
344          $self->close(1) if(exists($self->{'ConnectionTimeLimit'}) && $self->{'ConnectionTimeLimit'}<$time);          
345  }          $::Statistics->{'current_subscribers'}--;
346    }
347  1;  
348    sub checkForMaxTime {
349            my $self=shift;
350            my $time=shift;
351            
352            $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;
372  ############################################################################EOF  ############################################################################EOF

Legend:
Removed from v.9  
changed lines
  Added in v.56

  ViewVC Help
Powered by ViewVC 1.1.26