/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 32 - (hide annotations)
Thu Dec 20 21:24:24 2007 UTC (12 years, 5 months ago) by andrew.betts
File size: 8465 byte(s)
Complete rewrite of the JS web client
Opera and Safari compatibility
Saner request format

1 knops.gerd 11 #!/usr/bin/perl -w
2     ###############################################################################
3     # Meteor
4     # An HTTP server for the 2.0 web
5     # Copyright (c) 2006 contributing authors
6     #
7     # Subscriber.pm
8     #
9     # Description:
10     # A Meteor Subscriber
11     #
12     ###############################################################################
13     #
14     # 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
16     # Software Foundation; either version 2 of the License, or (at your option)
17     # any later version.
18     #
19     # 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
21     # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
22     # more details.
23     #
24     # 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.,
26     # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27     #
28     # For more information visit www.meteorserver.org
29     #
30     ###############################################################################
31    
32     package Meteor::Subscriber;
33     ###############################################################################
34     # Configuration
35     ###############################################################################
36    
37     use strict;
38    
39     use Meteor::Connection;
40     use Meteor::Channel;
41     use Meteor::Document;
42    
43     @Meteor::Subscriber::ISA=qw(Meteor::Connection);
44    
45     our %PersistentConnections=();
46 knops.gerd 25 our $NumAcceptedConnections=0;
47 knops.gerd 11
48     ###############################################################################
49     # Factory methods
50     ###############################################################################
51     sub newFromServer {
52     my $class=shift;
53    
54     my $self=$class->SUPER::newFromServer(shift);
55    
56     $self->{'headerBuffer'}='';
57     $self->{'MessageCount'}=0;
58     $self->{'MaxMessageCount'}=0;
59    
60     $self->{'ConnectionStart'}=time;
61     my $maxTime=$::CONF{'MaxTime'};
62     if($maxTime>0)
63     {
64     $self->{'ConnectionTimeLimit'}=$self->{'ConnectionStart'}+$maxTime;
65     }
66    
67 knops.gerd 25 $::Statistics->{'current_subscribers'}++;
68     $::Statistics->{'subscriber_connections_accepted'}++;
69    
70 knops.gerd 11 $self;
71     }
72    
73     ###############################################################################
74     # Class methods
75     ###############################################################################
76     sub deleteSubscriberWithID {
77     my $class=shift;
78     my $id=shift;
79    
80     if(exists($PersistentConnections{$id}))
81     {
82     $PersistentConnections{$id}->close(1);
83     }
84     }
85    
86     sub pingPersistentConnections {
87     my $class=shift;
88    
89     my $msg=$::CONF{'PingMessage'};
90     my @cons=values %PersistentConnections;
91    
92     map { $_->write($msg) } @cons;
93     }
94    
95     sub checkPersistentConnectionsForMaxTime {
96     my $class=shift;
97    
98     my $time=time;
99     my @cons=values %PersistentConnections;
100    
101     map { $_->checkForMaxTime($time) } @cons;
102     }
103    
104 knops.gerd 25 sub numSubscribers {
105    
106     return scalar(keys %PersistentConnections);
107     }
108    
109 knops.gerd 11 ###############################################################################
110     # Instance methods
111     ###############################################################################
112     sub processLine {
113     my $self=shift;
114     my $line=shift;
115    
116     # Once the header was processed we ignore any input
117     return unless(exists($self->{'headerBuffer'}));
118    
119     if($line ne '')
120     {
121     #
122     # Accumulate header
123     #
124     $self->{'headerBuffer'}.="$line\n";
125     }
126     else
127     {
128     #
129     # Empty line signals end of header.
130     # Analyze header, register with appropiate channel
131     # and send pending messages.
132     #
133 andrew.betts 32 # GET $::CONF{'SubscriberDynamicPageAddress'}/hostid/streamtype/channeldefs HTTP/1.1
134 knops.gerd 11 #
135     # Find the 'GET' line
136     #
137 andrew.betts 32 if($self->{'headerBuffer'}=~/GET\s+$::CONF{'SubscriberDynamicPageAddress'}\/([0-9a-z]+)\/([0-9a-z]+)\/(\S+)/i)
138 knops.gerd 11 {
139 andrew.betts 32 my $subscriberID=$1;
140     my $persist=0;
141     $self->{'mode'}=$2;
142     if ($self->{'mode'} eq "xhrinteractive" || $self->{'mode'} eq "iframe" || $self->{'mode'} eq "serversent" || $self->{'mode'} eq "longpoll") {
143     $persist=1;
144     $self->{'MaxMessageCount'}=1 unless(!($self->{'mode'} eq "longpoll"));
145     }
146     if ($self->{'mode'} eq "iframe") {
147     $self->{'HeaderTemplateNumber'}=1;
148     } else {
149     $self->{'HeaderTemplateNumber'}=2;
150     }
151     my @channelData=split('/',$3);
152 knops.gerd 13 my $channels={};
153 andrew.betts 32 my $channelName;
154     my $offset;
155     foreach my $chandef (@channelData)
156 knops.gerd 11 {
157 andrew.betts 32 if($chandef=~/^([a-z0-9]+)(.(r|b|h)([0-9]*))?$/)
158 knops.gerd 11 {
159 andrew.betts 32 $channelName = $1;
160     $channels->{$channelName}->{'startIndex'} = undef;
161     for ($3) {
162     $offset = $4;
163     /r/ && do { $channels->{$channelName}->{'startIndex'} = $offset; last; };
164     /b/ && do { $channels->{$channelName}->{'startIndex'} = -$offset; last; };
165     /h/ && do { $channels->{$channelName}->{'startIndex'} = 0; last; };
166 knops.gerd 13 }
167 knops.gerd 11 }
168     }
169    
170 knops.gerd 13 delete($self->{'headerBuffer'});
171    
172 andrew.betts 32 if($persist)
173 knops.gerd 11 {
174     $self->{'subscriberID'}=$subscriberID;
175     $self->deleteSubscriberWithID($subscriberID);
176     $PersistentConnections{$subscriberID}=$self;
177     }
178    
179 knops.gerd 13 if(scalar(keys %{$channels}))
180 knops.gerd 11 {
181     $self->emitOKHeader();
182 knops.gerd 17 $self->setChannels($channels,$persist);
183     $self->close(1) unless($persist);
184 knops.gerd 11 return;
185     }
186     }
187 andrew.betts 32 elsif($self->{'headerBuffer'}=~/GET\s+\/disconnect\/(\S+)/)
188     {
189     $self->deleteSubscriberWithID($1);
190     $self->emitOKHeader();
191     $self->close(1);
192     return;
193     }
194 knops.gerd 11 elsif($self->{'headerBuffer'}=~/GET\s+([^\s\?]+)/)
195     {
196     Meteor::Document->serveFileToClient($1,$self);
197     $self->close(1);
198     return;
199     }
200    
201     #
202     # If we fall through we did not understand the request
203     #
204     $self->emitErrorHeader();
205     }
206     }
207    
208 knops.gerd 13 sub setChannels {
209 knops.gerd 11 my $self=shift;
210 knops.gerd 13 my $channels=shift;
211 knops.gerd 17 my $persist=shift;
212 knops.gerd 11
213 knops.gerd 13 foreach my $channelName (keys %{$channels})
214     {
215     my $startIndex=$channels->{$channelName}->{'startIndex'};
216    
217     my $channel=Meteor::Channel->channelWithName($channelName);
218    
219     $self->{'channels'}->{$channelName}=$channel if($persist);
220    
221     $channel->addSubscriber($self,$startIndex,$persist);
222     }
223 knops.gerd 11 }
224    
225     sub emitOKHeader {
226     my $self=shift;
227    
228     $self->emitHeader('200 OK');
229     }
230    
231     sub emitErrorHeader {
232     my $self=shift;
233    
234     $self->emitHeader('404 Not Found');
235 knops.gerd 25 $::Statistics->{'errors_served'}++;
236 knops.gerd 11
237     # close up shop here!
238     $self->close();
239     }
240    
241     sub emitHeader {
242     my $self=shift;
243     my $status=shift;
244    
245     my $header=undef;
246     if(exists($self->{'HeaderTemplateNumber'}))
247     {
248     my $hn='HeaderTemplate'.$self->{'HeaderTemplateNumber'};
249    
250     $header=$::CONF{$hn};
251     }
252     $header=$::CONF{'HeaderTemplate'} unless(defined($header));
253    
254     $header=~s/~([^~]+)~/
255     if(!defined($1) || $1 eq '')
256     {
257     '~';
258     }
259     elsif($1 eq 'server')
260     {
261     $::PGM;
262     }
263     elsif($1 eq 'status')
264     {
265     $status;
266     }
267     elsif($1 eq 'servertime')
268     {
269     time;
270     }
271     else
272     {
273     '';
274     }
275     /gex;
276    
277     $self->write($header);
278     }
279    
280     sub sendMessage {
281     my $self=shift;
282     my $msg=shift;
283 knops.gerd 25 my $numMsgInThisBatch=shift;
284 knops.gerd 11
285 knops.gerd 25 $numMsgInThisBatch=1 unless(defined($numMsgInThisBatch));
286    
287 knops.gerd 11 $self->write($msg);
288    
289 knops.gerd 25 $::Statistics->{'messages_served'}+=$numMsgInThisBatch;
290    
291 knops.gerd 11 my $msgCount=++$self->{'MessageCount'};
292    
293     my $maxMsg=$::CONF{'MaxMessages'};
294     if(defined($maxMsg) && $maxMsg>0 && $msgCount>=$maxMsg)
295     {
296     $self->close(1);
297     }
298    
299     if($self->{'MaxMessageCount'}>0 && $msgCount>=$self->{'MaxMessageCount'})
300     {
301     $self->close(1);
302     }
303     }
304    
305 knops.gerd 13 sub closeChannel {
306     my $self=shift;
307     my $channelName=shift;
308    
309     return unless(exists($self->{'channels'}->{$channelName}));
310    
311     my $channel=$self->{'channels'}->{$channelName};
312     $channel->removeSubscriber($self);
313    
314     delete($self->{'channels'}->{$channelName});
315    
316     $self->close() if(scalar(keys %{$self->{'channels'}})==0);
317     }
318    
319 knops.gerd 11 sub close {
320     my $self=shift;
321     my $noShutdownMsg=shift;
322    
323 knops.gerd 13 foreach my $channelName (keys %{$self->{'channels'}})
324     {
325     my $channel=$self->{'channels'}->{$channelName};
326     $channel->removeSubscriber($self);
327     }
328     delete($self->{'channels'});
329 knops.gerd 11
330     if(exists($self->{'subscriberID'}))
331     {
332     delete($PersistentConnections{$self->{'subscriberID'}});
333     }
334    
335     #
336     # Send shutdown message unless remote closed or
337     # connection not yet established
338     #
339     unless($noShutdownMsg || $self->{'remoteClosed'} || exists($self->{'headerBuffer'}))
340     {
341     my $msg=$::CONF{'SubscriberShutdownMsg'};
342     if(defined($msg) && $msg ne '')
343     {
344     $self->write($msg);
345     }
346     }
347    
348 knops.gerd 25 $::Statistics->{'current_subscribers'}--;
349    
350 knops.gerd 11 $self->SUPER::close();
351     }
352    
353     sub checkForMaxTime {
354     my $self=shift;
355     my $time=shift;
356    
357     $self->close(1) if(exists($self->{'ConnectionTimeLimit'}) && $self->{'ConnectionTimeLimit'}<$time);
358     }
359    
360     1;
361 andrew.betts 3 ############################################################################EOF

  ViewVC Help
Powered by ViewVC 1.1.26