]>
Dogcows Code - chaz/chatty/blob - extra/cometd/meteord
2394c9443187168d8fe212ad37ba3448e7e941e2
2 ###############################################################################
4 # An HTTP server for the 2.0 web
5 # Copyright (c) 2006 contributing authors
9 # Main program should call Meteor::Config::setCommandLineParameters(@ARGV),.
10 # Afterwards anybody can access $::CONF{<parameterName>}, where
11 # <parameterName> is any valid parameter (except 'Help') listed in the
12 # @DEFAULTS array below.
14 ###############################################################################
16 # This program is free software; you can redistribute it and/or modify it
17 # under the terms of the GNU General Public License as published by the Free
18 # Software Foundation; either version 2 of the License, or (at your option)
21 # This program is distributed in the hope that it will be useful, but WITHOUT
22 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
26 # You should have received a copy of the GNU General Public License along
27 # with this program; if not, write to the Free Software Foundation, Inc.,
28 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 # For more information visit www.meteorserver.org
32 ###############################################################################
34 ###############################################################################
36 ################################################################################
39 $::RELEASE_DATE
='2008-03-02';
41 ###############################################################################
43 ###############################################################################
52 use Meteor
::Connection
;
53 use Meteor
::Controller
;
54 use Meteor
::Subscriber
;
59 $::CRLF
="\r\n"; # Line separator to be used throughout all modules
61 our $CONTROL_QUEUE_SIZE=5;
62 our $SUBSCRIBER_QUEUE_SIZE=20;
64 our $MAIN_LOOP_TIMEOUT=60;
65 our $AGE_CHECK_INTERVALL=60;
67 our $MAX_EXIT_DELAY=120;
69 our $UDP_MAX_MESSAGE_SIZE=8192;
71 ###############################################################################
73 ###############################################################################
79 $::STARTUP_TIME
+=0; # avoid warning
88 # Handle command line options and config file
90 Meteor
::Config-
>setCommandLineParameters(@ARGV);
93 # Do something about warn and die
95 unless($::CONF
{'Debug'})
97 $SIG{'__WARN__'}=\
&Meteor
::Syslog
::myWarn
;
98 $SIG{'__DIE__'}=\
&Meteor
::Syslog
::myDie
;
101 &::syslog
('info',"$::PGM launched!");
109 my $facility=$::CONF
{'SyslogFacility'} || $Meteor::Syslog
::DEFAULT_FACILITY
;
111 unless($::CONF
{'Debug'} || $facility eq 'none')
113 # close standard file descriptors
119 # fork and exit parent
121 setpgrp(0, $$) if defined $SIG{TTOU
};
122 $SIG{TTOU
}='ignore' if defined $SIG{TTOU
};
124 # Avoid 'stdin reopened for output' warning with newer perls
125 open(NULL
,'/dev/null');
128 open(OUT
,">/var/run/$::PGM.pid");
134 &::syslog
('info',"PID\t%s",$$);
141 $::HUP
=$::TERM
=$::USR1
=$::USR2
=0;
142 $SIG{'HUP'}=sub{$::HUP
=1};
143 $SIG{'TERM'}=sub{$::TERM
=1};
144 $SIG{'USR1'}=sub{$::USR1
=1};
145 $SIG{'USR2'}=sub{$::USR2
=1};
153 my $controlServer=Meteor
::Socket-
>newServer(
154 $::CONF
{'ControllerPort'},
156 $::CONF
{'ControllerIP'}
158 my $controlServerFN=$controlServer->fileno();
160 my $subscriberServer=Meteor
::Socket-
>newServer(
161 $::CONF
{'SubscriberPort'},
162 $SUBSCRIBER_QUEUE_SIZE,
163 $::CONF
{'SubscriberIP'}
165 my $subscriberServerFN=$subscriberServer->fileno();
168 my $udpPort=$::CONF
{'UDPPort'};
169 my $udpServerFN=undef;
170 if($udpPort && $udpPort>0)
172 $udpServer=Meteor
::Socket-
>newUDPServer(
176 $udpServerFN=$udpServer->fileno();
180 vec($serverVector,$controlServerFN,1)=1;
181 vec($serverVector,$subscriberServerFN,1)=1;
182 vec($serverVector,$udpServerFN,1)=1 if(defined($udpServerFN));
184 my $lastAgeCheck=time;
187 if(exists($::CONF
{'PingInterval'}) && $::CONF
{'PingInterval'}>2)
189 $nextPing=$::CONF
{'PingInterval'}+$lastAgeCheck;
198 my $rVec=$serverVector;
206 Meteor
::Connection-
>addAllHandleBits(\
$rVec,\
$wVec,\
$eVec);
208 my $timeout=$MAIN_LOOP_TIMEOUT;
209 if(defined($nextPing))
211 $timeout=$nextPing-time
;
217 $result=&Meteor
::Socket
::sselect
($rout=$rVec,$wout=$wVec,$eout=$eVec,$timeout);
222 if(vec($rout,$controlServerFN,1))
224 Meteor
::Controller-
>newFromServer($controlServer);
226 if(vec($rout,$subscriberServerFN,1))
228 Meteor
::Subscriber-
>newFromServer($subscriberServer);
230 if(defined($udpServerFN) && vec($rout,$udpServerFN,1))
232 &handleUPD
($udpServer);
235 Meteor
::Connection-
>checkAllHandleBits($rout,$wout,$eout);
239 &::syslog
('crit',"Select failed: $!");
247 &::syslog
('info',"Received SIGHUP, re-reading config and clearing document cache!");
249 Meteor
::Config-
>readConfig();
250 Meteor
::Config-
>updateConfig();
252 Meteor
::Document-
>clearDocuments()
259 &::syslog
('info',"Received SIGUSR1, clearing channel buffers!");
261 Meteor
::Channel-
>clearAllBuffers();
268 &::syslog
('info',"Received SIGUSR2, clearing document cache!");
270 Meteor
::Document-
>clearDocuments()
274 if($t>$lastAgeCheck+$AGE_CHECK_INTERVALL)
276 my $minTimeStap=time-
$::CONF
{'MaxMessageAge'};
277 Meteor
::Channel-
>trimMessageStoresByTimestamp($minTimeStap);
281 Meteor
::Subscriber-
>checkPersistentConnectionsForMaxTime();
284 if(defined($nextPing) && $nextPing<=$t)
288 Meteor
::Subscriber-
>pingPersistentConnections();
290 if(exists($::CONF
{'MaxMessageAge'}) && $::CONF
{'MaxMessageAge'}>2)
292 $nextPing=$::CONF
{'PingInterval'}+time;
299 &::syslog
('alert',"$::PGM loop died (will restart in 2 seconds): $@");
309 &::syslog
('info',"Received SIGTERM, begin shutdown!");
311 $subscriberServer->close();
312 $controlServer->close();
314 unlink("/var/run/$::PGM.pid") unless($::CONF
{'Debug'});
316 Meteor
::Connection-
>closeAllConnections();
318 my $timoutAt=time+$MAX_EXIT_DELAY;
320 while(Meteor
::Connection-
>connectionCount() && time<$timoutAt)
330 Meteor
::Connection-
>addAllHandleBits(\
$rVec,\
$wVec,\
$eVec);
332 my $result=&Meteor
::Socket
::sselect
($rout=$rVec,$wout=$wVec,$eout=$eVec,$timoutAt-time
);
336 Meteor
::Connection-
>checkAllHandleBits($rout,$wout,$eout);
340 if(my $cnt=Meteor
::Connection-
>connectionCount())
342 &::syslog
('info',"$cnt client(s) unresponsive, will shutdown anyway");
347 &::syslog
('info',"shutdown succeeded");
352 &::syslog
('emerg',"$::PGM loop exited");
354 ###############################################################################
356 ###############################################################################
361 my $hispaddr=recv($udpServer->{'handle'},$line,$::UDP_MAX_MESSAGE_SIZE
,0);
363 &::syslog
('debug',"udp message received: %s",$line);
365 return unless($line=~s/^(\S+)\s//);
369 if($cmd eq 'ADDMESSAGE')
371 return unless($line=~s/^(\S+)\s//);
374 my $channel=Meteor
::Channel-
>channelWithName($channelName);
375 my $msg=$channel->addMessage($line);
376 my $msgID=$msg->id();
377 &::syslog
('debug',"udp: new message added, ID %s",$msgID);
382 ############################################################################EOF
This page took 0.063385 seconds and 4 git commands to generate.