/[meteor]/googlecode.com/svn/trunk/Meteor/Controller.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/Controller.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 62 - (hide annotations)
Thu Nov 27 00:33:21 2008 UTC (15 years, 4 months ago) by andrew.betts
File size: 5990 byte(s)
1 Fixed: Added SIGPIPE handler.  We noticed that under heavy load
Meteor receives SIGPIPEs from the OS, suspected to relate to clients
that have just disconnected the moment Meteor attempts to write to the
socket.  This caused Meteor to crash.
2 Fixed: Long polling multiple channels no longer causes the loop to
die and restart when some channels have messages queued for delivery.
3 Fixed: Over time, Meteor 'collected' connections from clients that
never got disconnected even if MaxTime was set.  This happened if the
client concerned sent a header with no terminating blank line.  Meteor
kept waiting for the rest of the header, which never arrived, and
therefore the client remained in limbo, never subjected to the MaxTime
time limit because it had not yet become a subscriber.  Clients are
now allowed 30 seconds to send a valid request header.
4 Fixed: If only one message existed on the server, the JS client
would continue to request it again and again, because it has message
ID 0, and the JS client considered this an invalid message ID.
5 Fixed: Corrected some comments in file headers

6 Changed: MaxMessages has been renamed to CloseOnEvent and functions
in a similar, but not quite identical way.  Thanks to Matthew Haak,
who pointed out the extreme confusingness of MaxMessages and a bug
that has resulted in Fix 2 above.  Setting CloseOnEvent to any value
that evaluates to true will cause Meteor to close subscriber
connections after at least one message has been sent and there are no
further messages pending.  This is identical to MaxMessages for values
of 0 and 1, but where MaxMessages is set to a value higher than one,
replacing it with CloseOnEvent with the same value will act as though
it were set to one.  The intent of MaxMessages was to enable long-
polling (and it is used by the JS client in that way), and
CloseonEvent is a drop in replacement for that behaviour.
7 Changed: Meteor JS client now uses dynamic <SCRIPT> tags for all
polling behaviours, rather than XHR.  This enables it to make poll
requests cross-domain (see 13)
8 Changed: Meteor JS client now abstracts timestamp lookups to a
dedicated method.
9 Changed: Default HeaderTemplates no longer include cache busting
headers, since all meteor requests contain a millisecond timestamp and
so no client makes the same request twice.  These were therefore
simply chewing up bandwidth.
10 Changed: Date strings used for logging debug messages are cached to
avoid numerous expensive lookups to localtime().
11 Changed: Channel info is only sent in a response if the client does
not request a restart from a specified ID.  The logic being that if
the client knows the ID they want to start from, they have already
made previous requests and have the channel information they need.
Bandwidth saving measure.

12 Added: JS client now has a Meteor.isSupportedBrowser() method,
which you can call to detemine whether Meteor will run in the user's
browser version.
13 Added: JS client can now use different hosts for polling and
streaming.  This is only really useful if your website is on a domain
that has a lot of cookies, and you don't want to send them in every
poll request.  Removing cookies from request headers can reduce the
size of the request significantly.  We find that with cookies included
Meteor poll requests are usually larger than the responses.  To use,
set Meteor.pollhost.  Meteor.pollhost can be any domain, while
Meteor.host must be a subdomain of your website hostname.
14 Added: Config file now supports new 'FooterTemplate' parameter, for
a string to send just before the connection to the subscriber is
closed.  This is in support of change 7.
15 Added: Better inline documentation for ChannelInfoTemplate config
parameter
16 Added: Log output includes connection IDs corresponding to the file
inode for each connection
17 Added: New controller command LISTCONNECTIONS, produces a newline
delimited list of all currently connected clients, and for each one
displaying "ConnectionID IPAddress ClientType [SubscriberID]"
18 Added: New controller command DESCRIBE, takes a ConnectionID as a
parameter, and outputs numerous statistics about that particular
client, including number of messages sent/received, user agent, IP
address, time connected, time remaining until MaxTime etc.
19 Added: New controller comment LISTSUBSCRIBERS, produces a newline
delimited list of all currently connected streaming subscribers, and
for each one displaying "SubscriberID IPAddress Starttime TimeLimit
TimeRemaining MessageCount UserAgent"
20 Added: SHOWSTATS command produces the following additional stats:
connection_count: total current connections, real_subscribers: total
of number of currently connected streaming subscribers plus the number
of unique polling connections seen in the last 60 seconds.
21 Added: STDERR outputs prior to every exit() for debugging purposes
22 Added: The UDP server is now considered stable, and is the best way
of broadcasting messages to lots of Meteor nodes simultaneously and
efficiently. 


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 Controller
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::Controller;
33     ###############################################################################
34     # Configuration
35     ###############################################################################
36    
37     use strict;
38    
39     use Meteor::Connection;
40     use Meteor::Channel;
41 knops.gerd 25 use Meteor::Subscriber;
42 knops.gerd 11
43     @Meteor::Controller::ISA=qw(Meteor::Connection);
44    
45     ###############################################################################
46 knops.gerd 25 # Factory methods
47     ###############################################################################
48     sub newFromServer {
49     my $class=shift;
50    
51     my $self=$class->SUPER::newFromServer(shift);
52    
53     $::Statistics->{'current_controllers'}++;
54     $::Statistics->{'controller_connections_accepted'}++;
55    
56     $self;
57     }
58    
59     ###############################################################################
60 knops.gerd 11 # Instance methods
61     ###############################################################################
62     sub processLine {
63     my $self=shift;
64     my $line=shift;
65    
66     # ADDMESSAGE channel1 Message text
67     # < OK
68     # ADDMESSAGE
69     # < ERR Invalid command syntax
70     # COUNTSUBSCRIBERS channel1
71     # < OK 344
72    
73 andrew.betts 62 unless($line=~s/^(ADDMESSAGE|COUNTSUBSCRIBERS|LISTCHANNELS|LISTSUBSCRIBERS|LISTCONNECTIONS|DESCRIBE|SHOWSTATS|QUIT)//)
74 knops.gerd 11 {
75     $self->write("ERR Invalid command syntax$::CRLF");
76    
77     return;
78     }
79    
80     my $cmd=$1;
81    
82     if($cmd eq 'ADDMESSAGE')
83     {
84     unless($line=~s/^\s+(\S+)\s//)
85     {
86     $self->write("ERR Invalid command syntax$::CRLF");
87 knops.gerd 25
88 knops.gerd 11 return;
89     }
90    
91     my $channelName=$1;
92     my $channel=Meteor::Channel->channelWithName($channelName);
93 knops.gerd 46 my $msg=$channel->addMessage($line);
94     my $msgID=$msg->id();
95     $self->write("OK\t$msgID$::CRLF");
96 knops.gerd 11 }
97     elsif($cmd eq 'COUNTSUBSCRIBERS')
98     {
99     unless($line=~s/^\s+(\S+)$//)
100     {
101     $self->write("ERR Invalid command syntax$::CRLF");
102     return;
103     }
104    
105     my $channelName=$1;
106     my $numSubscribers=0;
107     my $channel=Meteor::Channel->channelWithName($channelName,1);
108     $numSubscribers=$channel->subscriberCount() if($channel);
109    
110     $self->write("OK $numSubscribers$::CRLF");
111     }
112     elsif($cmd eq 'LISTCHANNELS')
113     {
114     unless($line eq '')
115     {
116     $self->write("ERR Invalid command syntax$::CRLF");
117     return;
118     }
119    
120     my $txt="OK$::CRLF".Meteor::Channel->listChannels()."--EOT--$::CRLF";
121    
122     $self->write($txt);
123     }
124 andrew.betts 62 elsif($cmd eq 'LISTSUBSCRIBERS')
125     {
126     unless($line eq '')
127     {
128     $self->write("ERR Invalid command syntax$::CRLF");
129     return;
130     }
131    
132     my $txt="OK$::CRLF".Meteor::Subscriber->listSubscribers()."--EOT--$::CRLF";
133    
134     $self->write($txt);
135     }
136     elsif($cmd eq 'LISTCONNECTIONS')
137     {
138     unless($line eq '')
139     {
140     $self->write("ERR Invalid command syntax$::CRLF");
141     return;
142     }
143    
144     my $txt="OK$::CRLF".Meteor::Connection->listConnections()."--EOT--$::CRLF";
145    
146     $self->write($txt);
147     }
148     elsif($cmd eq 'DESCRIBE')
149     {
150     unless($line=~s/^\s+(\S+)$//)
151     {
152     $self->write("ERR Invalid command syntax$::CRLF");
153     return;
154     }
155    
156     my $filenum=$1;
157     my $condesc=Meteor::Connection->describeConnWithFileNum($filenum);
158     if ($condesc != -1) {
159     $self->write("OK$::CRLF");
160     $self->write($condesc);
161     $self->write("--EOT--$::CRLF");
162    
163     } else {
164     $self->write("ERR Unknown client$::CRLF");
165     }
166     }
167 knops.gerd 25 elsif($cmd eq 'SHOWSTATS')
168     {
169     # uptime
170     my $uptime=time-$::STARTUP_TIME;
171 knops.gerd 43 my $txt="OK$::CRLF"."uptime: $uptime$::CRLF";
172 knops.gerd 25
173     # channel_count
174     my $numChannels=Meteor::Channel->numChannels();
175     $txt.="channel_count: $numChannels$::CRLF";
176    
177 andrew.betts 62 # connection_count
178     my $numConnections=Meteor::Connection->connectionCount();
179     $txt.="connection_count: $numConnections$::CRLF";
180    
181     # subscriber count = current_subscribers + number of pollers in last minute
182     my $now = time; my $numpoll = 0;
183     foreach my $key (keys %{$::Pollers}) {
184     if($::Pollers->{$key} < ($now-60) || Meteor::Subscriber->subscriberExists($key)) {
185     delete $::Pollers->{$key};
186     } else {
187     $numpoll++;
188     }
189     }
190     my $numsub = 0;
191     if (exists($::Statistics->{'current_subscribers'})) {
192     $numsub = $::Statistics->{'current_subscribers'};
193     }
194     $txt.="real_subscribers: ".($numpoll+$numsub)."$::CRLF";
195    
196 knops.gerd 25 foreach my $key (keys %{$::Statistics})
197     {
198     $txt.=$key.': '.$::Statistics->{$key}.$::CRLF;
199     }
200    
201 knops.gerd 43 $txt.="--EOT--$::CRLF";
202    
203 knops.gerd 25 $self->write($txt);
204     }
205 knops.gerd 11 elsif($cmd eq 'QUIT')
206     {
207     unless($line eq '')
208     {
209     $self->write("ERR Invalid command syntax$::CRLF");
210 knops.gerd 25
211 knops.gerd 11 return;
212     }
213    
214     $self->write("OK$::CRLF");
215     $self->close(1);
216     }
217     else
218     {
219     # Should never get here
220     die("Unknown command '$cmd'");
221     }
222     }
223    
224     sub close {
225     my $self=shift;
226     my $noShutdownMsg=shift;
227    
228     unless($noShutdownMsg || $self->{'remoteClosed'})
229     {
230     my $msg=$::CONF{'ControllerShutdownMsg'};
231     if(defined($msg) && $msg ne '')
232     {
233     $self->write($msg);
234     }
235     }
236    
237     $self->SUPER::close();
238     }
239    
240 knops.gerd 37 sub didClose {
241    
242     $::Statistics->{'current_controllers'}--;
243     }
244    
245 knops.gerd 11 1;
246 andrew.betts 3 ############################################################################EOF

  ViewVC Help
Powered by ViewVC 1.1.26