/[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 49 by knops.gerd, Mon Feb 4 22:30:35 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();
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]+)\/(\S+)/i)
138                                  if($formElement=~/^channel=(.+)$/)                  {
139                                  {                          my $subscriberID=$1;
140                                          $channelName=$1;                          $self->{'mode'}=$2;
141                                  }                          my $persist=$self->getConf('Persist');
142                                  elsif($formElement=~/^restartfrom=(\d*)$/)                          
143                                  {                          my $maxTime=$self->getConf('MaxTime');
144                                          $startIndex=$1;                          if($maxTime>0)
145                                          $startIndex='' unless(defined($startIndex));                          {
146                                  }                                  $self->{'ConnectionTimeLimit'}=$self->{'ConnectionStart'}+$maxTime;
147                                  elsif($formElement=~/^backtrack=(\d+)$/)                          }
148                                  {                          
149                                          $backtrack=$1;                          my @channelData=split('/',$3);
150                                          $backtrack=0 unless(defined($backtrack));                          my $channels={};
151                                  }                          my $channelName;
152                                  elsif($formElement=~/^persist=(?i)(yes|true|1|no|false|0)$/)                          my $offset;
153                                  {                          foreach my $chandef (@channelData) {
154                                          $persist=0 if($1=~/(no|false|0)/i);                                  if($chandef=~/^([a-z0-9]+)(.(r|b|h)([0-9]*))?$/i) {
155                                  }                                          $channelName = $1;
156                                  elsif($formElement=~/^id=(.+)$/)                                          $channels->{$channelName}->{'startIndex'} = undef;
157                                  {                                          if ($3) {
158                                          $subscriberID=$1;                                             $offset = $4;
159                                  }                                             if ($3 eq 'r') { $channels->{$channelName}->{'startIndex'} = $offset; }
160                                  elsif($formElement=~/^maxmessages=(\d+)$/i)                                             if ($3 eq 'b') { $channels->{$channelName}->{'startIndex'} = -$offset; }
161                                  {                                             if ($3 eq 'h') { $channels->{$channelName}->{'startIndex'} = 0; }
162                                          $self->{'MaxMessageCount'}=$1;                                          }
163                                  }                                  }
164                                  elsif($formElement=~/^template=(\d+)$/i)                          }
165                                  {                          
166                                          $self->{'HeaderTemplateNumber'}=$1;                          delete($self->{'headerBuffer'});
167                                  }                          
168                                  elsif($formElement=~/^maxtime=(\d+)$/i)                          if($persist)
169                                  {                          {
170                                          my $clientRequest=$1;                                  $self->{'subscriberID'}=$subscriberID;
171                                          my $serverDefault=$::CONF{'MaxTime'};                                  $self->deleteSubscriberWithID($subscriberID);
172                                                                            $PersistentConnections{$subscriberID}=$self;
173                                          if($serverDefault==0 || $serverDefault>$clientRequest)                          }
174                                          {                          
175                                                  $self->{'ConnectionTimeLimit'}=$self->{'ConnectionStart'}+$clientRequest;                          if(scalar(keys %{$channels}))
176                                          }                          {
177                                  }                                  $self->emitOKHeader();
178                          }                                  $self->setChannels($channels,$persist,$self->{'mode'},'');
179                                                                                    $self->close(1) unless($persist);
180                          delete($self->{'headerBuffer'});                                  return;
181                                                    }
182                          if(defined($startIndex) && defined($backtrack))                  }
183                          {                  elsif($self->{'headerBuffer'}=~/GET\s+\/disconnect\/(\S+)/)
184                                  $self->emitHeader("404 Cannot use both 'restartfrom' and 'backtrack'");                  {
185                                  $self->close();                          $self->deleteSubscriberWithID($1);
186                                                            $self->emitOKHeader();
187                                  return;                          $self->close(1);
188                          }                          return;
189                                            }
190                          if(defined($subscriberID) && $persist)                  elsif($self->{'headerBuffer'}=~/GET\s+([^\s\?]+)/)
191                          {                  {
192                                  $self->{'subscriberID'}=$subscriberID;                          Meteor::Document->serveFileToClient($1,$self);
193                                  $self->deleteSubscriberWithID($subscriberID);                          $self->close(1);
194                                  $PersistentConnections{$subscriberID}=$self;                          return;
195                          }                  }
196                                            
197                          if(defined($channelName))                  #
198                          {                  # If we fall through we did not understand the request
199                                  $self->emitOKHeader();                  #
200                                                    $self->emitErrorHeader();
201                                  $startIndex=-$backtrack if(!defined($startIndex) && defined($backtrack));          }
202                                    }
203                                  $self->setChannelName($channelName,$startIndex,$persist);  
204                                    sub setChannels {
205                                  $self->close(1) unless($persist);          my $self=shift;
206                                            my $channels=shift;
207                                  return;          my $persist=shift;
208                          }          my $mode=shift || '';
209                  }          my $userAgent=shift || '';
210                  elsif($self->{'headerBuffer'}=~/GET\s+([^\s\?]+)/)          
211                  {          foreach my $channelName (keys %{$channels})
212                          Meteor::Document->serveFileToClient($1,$self);          {
213                                            my $startIndex=$channels->{$channelName}->{'startIndex'};
214                          $self->close(1);                  
215                                            my $channel=Meteor::Channel->channelWithName($channelName);
216                          return;                  
217                  }                  $self->{'channels'}->{$channelName}=$channel if($persist);
218                                    
219                  #                  $channel->addSubscriber($self,$startIndex,$persist,$mode,$userAgent);
220                  # If we fall through we did not understand the request          }
221                  #  }
222                  $self->emitErrorHeader();  
223          }  sub emitOKHeader {
224  }          my $self=shift;
225            
226  sub setChannelName {          $self->emitHeader('200 OK');
227          my $self=shift;  }
228          my $channelName=shift;  
229          my $startIndex=shift;  sub emitErrorHeader {
230          my $persist=shift;          my $self=shift;
231                    
232          my $channel=Meteor::Channel->channelWithName($channelName);          $self->emitHeader('404 Not Found');
233          $self->{'channel'}=$channel if($persist);          $::Statistics->{'errors_served'}++;
234                    
235          $channel->addSubscriber($self,$startIndex,$persist);          # close up shop here!
236  }          $self->close();
237    }
238  sub emitOKHeader {  
239          my $self=shift;  sub emitHeader {
240                    my $self=shift;
241          $self->emitHeader('200 OK');          my $status=shift;
242  }          
243            my $header=$self->getConf('HeaderTemplate');
244  sub emitErrorHeader {          
245          my $self=shift;          $header=~s/~([^~]*)~/
246                            if(!defined($1) || $1 eq '')
247          $self->emitHeader('404 Not Found');                  {
248                                    '~';
249          # close up shop here!                  }
250          $self->close();                  elsif($1 eq 'server')
251  }                  {
252                            $::PGM;
253  sub emitHeader {                  }
254          my $self=shift;                  elsif($1 eq 'status')
255          my $status=shift;                  {
256                                    $status;
257          my $header=undef;                  }
258          if(exists($self->{'HeaderTemplateNumber'}))                  elsif($1 eq 'servertime')
259          {                  {
260                  my $hn='HeaderTemplate'.$self->{'HeaderTemplateNumber'};                          time;
261                                    }
262                  $header=$::CONF{$hn};                  elsif($1 eq 'channelinfo')
263          }                  {
264          $header=$::CONF{'HeaderTemplate'} unless(defined($header));                          Meteor::Channel->listChannelsUsingTemplate($self->getConf('ChannelInfoTemplate'));
265                            }
266          $header=~s/~([^~]+)~/                  else
267                  if(!defined($1) || $1 eq '')                  {
268                  {                          '';
269                          '~';                  }
270                  }          /gex;
271                  elsif($1 eq 'server')          
272                  {          $self->write($header);
273                          $::PGM;  }
274                  }  
275                  elsif($1 eq 'status')  sub sendMessages {
276                  {          my $self=shift;
277                          $status;          
278                  }          my $numMessages=0;
279                  elsif($1 eq 'servertime')          my $msgTemplate=$self->getConf('Messagetemplate');
280                  {          my $msgData='';
281                          time;          
282                  }          foreach my $message (@_)
283                  else          {
284                  {                  $msgData.=$message->messageWithTemplate($msgTemplate);
285                          '';                  $numMessages++;
286                  }          }
287          /gex;          
288                    return if($numMessages<1);
289          $self->write($header);          
290  }          $self->write($msgData);
291            
292  sub sendMessage {          $::Statistics->{'messages_served'}+=$numMessages;
293          my $self=shift;          
294          my $msg=shift;          my $msgCount=$self->{'MessageCount'};
295                    $msgCount+=$numMessages;
296          $self->write($msg);          $self->{'MessageCount'}=$msgCount;
297                    
298          my $msgCount=++$self->{'MessageCount'};          my $maxMsg=$self->getConf('MaxMessages');
299                    if(defined($maxMsg) && $maxMsg>0 && $msgCount>=$maxMsg)
300          my $maxMsg=$::CONF{'MaxMessages'};          {
301          if(defined($maxMsg) && $maxMsg>0 && $msgCount>=$maxMsg)                  $self->close(1);
302          {          }
303                  $self->close(1);          
304          }          if($self->{'MaxMessageCount'}>0 && $msgCount>=$self->{'MaxMessageCount'})
305                    {
306          if($self->{'MaxMessageCount'}>0 && $msgCount>=$self->{'MaxMessageCount'})                  $self->close(1);
307          {          }
308                  $self->close(1);          
309          }  }
310  }  
311    sub ping {
312  sub close {          my $self=shift;
313          my $self=shift;          my $msg=$self->getConf('PingMessage');
314          my $noShutdownMsg=shift;          
315                    $self->write($msg);
316          $self->{'channel'}->removeSubscriber($self) if($self->{'channel'});  }
317          delete($self->{'channel'});  
318            sub closeChannel {
319          if(exists($self->{'subscriberID'}))          my $self=shift;
320          {          my $channelName=shift;
321                  delete($PersistentConnections{$self->{'subscriberID'}});          
322          }          return unless(exists($self->{'channels'}->{$channelName}));
323                    
324          #          my $channel=$self->{'channels'}->{$channelName};
325          # Send shutdown message unless remote closed or          $channel->removeSubscriber($self,'channelClose');
326          # connection not yet established          
327          #          delete($self->{'channels'}->{$channelName});
328          unless($noShutdownMsg || $self->{'remoteClosed'} || exists($self->{'headerBuffer'}))          
329          {          $self->close(0,'channelsClosed') if(scalar(keys %{$self->{'channels'}})==0);
330                  my $msg=$::CONF{'SubscriberShutdownMsg'};  }
331                  if(defined($msg) && $msg ne '')  
332                  {  sub close {
333                          $self->write($msg);          my $self=shift;
334                  }          my $noShutdownMsg=shift;
335          }          
336                    foreach my $channelName (keys %{$self->{'channels'}})
337          $self->SUPER::close();          {
338  }                  my $channel=$self->{'channels'}->{$channelName};
339                    $channel->removeSubscriber($self,'subscriberClose');
340  sub checkForMaxTime {          }
341          my $self=shift;          delete($self->{'channels'});
342          my $time=shift;          
343                    if(exists($self->{'subscriberID'}))
344          $self->close(1) if(exists($self->{'ConnectionTimeLimit'}) && $self->{'ConnectionTimeLimit'}<$time);          {
345  }                  delete($PersistentConnections{$self->{'subscriberID'}});
346            }
347  1;          
348            #
349            # Send shutdown message unless remote closed or
350            # connection not yet established
351            #
352            unless($noShutdownMsg || $self->{'remoteClosed'} || exists($self->{'headerBuffer'}))
353            {
354                    my $msg=$self->getConf('SubscriberShutdownMsg');
355                    if(defined($msg) && $msg ne '')
356                    {
357                            $self->write($msg);
358                    }
359            }
360            
361            $self->SUPER::close();
362    }
363    
364    sub didClose {
365            
366            $::Statistics->{'current_subscribers'}--;
367    }
368    
369    sub checkForMaxTime {
370            my $self=shift;
371            my $time=shift;
372            
373            $self->close(1) if(exists($self->{'ConnectionTimeLimit'}) && $self->{'ConnectionTimeLimit'}<$time);
374    }
375    
376    sub getConf {
377            my $self=shift;
378            my $key=shift;
379            
380            if(exists($self->{'mode'}) && $self->{'mode'} ne '')
381            {
382                    my $k=$key.'.'.$self->{'mode'};
383                    
384                    return $::CONF{$k} if(exists($::CONF{$k}));
385            }
386            
387            $::CONF{$key};
388    }
389    
390    1;
391  ############################################################################EOF  ############################################################################EOF

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

  ViewVC Help
Powered by ViewVC 1.1.26