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

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 42 by knops.gerd, Sun Feb 3 23:10:24 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  #   The Meteor daemon  #   The Meteor daemon
8  #  #
9  #       Main program should call Meteor::Config::setCommandLineParameters(@ARGV),.  #       Main program should call Meteor::Config::setCommandLineParameters(@ARGV),.
10  #       Afterwards anybody can access $::CONF{<parameterName>}, where  #       Afterwards anybody can access $::CONF{<parameterName>}, where
11  #       <parameterName> is any valid parameter (except 'Help') listed in the  #       <parameterName> is any valid parameter (except 'Help') listed in the
12  #       @DEFAULTS array below.  #       @DEFAULTS array below.
13  #  #
14  ###############################################################################  ###############################################################################
15  #  #
16  #   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
17  #   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
18  #   Software Foundation; either version 2 of the License, or (at your option)  #   Software Foundation; either version 2 of the License, or (at your option)
19  #   any later version.  #   any later version.
20  #  #
21  #   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
22  #   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or  #   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23  #   FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for  #   FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
24  #   more details.  #   more details.
25  #  #
26  #   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
27  #   with this program; if not, write to the Free Software Foundation, Inc.,  #   with this program; if not, write to the Free Software Foundation, Inc.,
28  #   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA  #   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29  #  #
30  #   For more information visit www.meteorserver.org  #   For more information visit www.meteorserver.org
31  #  #
32  ###############################################################################  ###############################################################################
33    
34    ###############################################################################
35  ###############################################################################  # meterod version
36  # Configuration  ################################################################################
37  ###############################################################################          
38                    $::VERSION='1.05.01';
39          use strict;  
40            ###############################################################################
41          use Meteor::Syslog;  # Configuration
42            ###############################################################################
43          use Meteor::Socket;          
44          use Meteor::Connection;          use strict;
45          use Meteor::Controller;          
46          use Meteor::Subscriber;          use Meteor::Syslog;
47          use Meteor::Channel;          
48          use Meteor::Document;          use Meteor::Socket;
49          use Meteor::Config;          use Meteor::Connection;
50                    use Meteor::Controller;
51          $::CRLF="\r\n";         # Line separator to be used throughout all modules          use Meteor::Subscriber;
52                    use Meteor::Channel;
53          our $CONTROL_QUEUE_SIZE=5;          use Meteor::Document;
54          our $SUBSCRIBER_QUEUE_SIZE=20;          use Meteor::Config;
55                    
56          our $MAIN_LOOP_TIMEOUT=60;          $::CRLF="\r\n";         # Line separator to be used throughout all modules
57          our $AGE_CHECK_INTERVALL=60;          
58                    our $CONTROL_QUEUE_SIZE=5;
59          our $MAX_EXIT_DELAY=120;          our $SUBSCRIBER_QUEUE_SIZE=20;
60            
61  ###############################################################################          our $MAIN_LOOP_TIMEOUT=60;
62  # Main          our $AGE_CHECK_INTERVALL=60;
63  ###############################################################################          
64                    our $MAX_EXIT_DELAY=120;
65          #  
66          # Program name  ###############################################################################
67          #  # Main
68          $::PGM=$0;  ###############################################################################
69          $::PGM=~s/^.*\///;          
70                    #
71          #          # Record startup time
72          # Handle command line options and config file          #
73          #          $::STARTUP_TIME=time;
74          Meteor::Config->setCommandLineParameters(@ARGV);          $::STARTUP_TIME+=0; # avoid warning
75                    
76          #          #
77          # Do something about warn and die          # Program name
78          #          #
79          unless($::CONF{'Debug'})          $::PGM=$0;
80          {          $::PGM=~s/^.*\///;
81                  $SIG{'__WARN__'}=\&Meteor::Syslog::myWarn;          
82                  $SIG{'__DIE__'}=\&Meteor::Syslog::myDie;          #
83          }          # Handle command line options and config file
84                    #
85          &::syslog('info',"$::PGM launched!");          Meteor::Config->setCommandLineParameters(@ARGV);
86                    
87          #          #
88          # Daemonize          # Do something about warn and die
89          #          #
90          {          unless($::CONF{'Debug'})
91                  $0="$::PGM daemon";          {
92                                    $SIG{'__WARN__'}=\&Meteor::Syslog::myWarn;
93                  unless($::CONF{'Debug'})                  $SIG{'__DIE__'}=\&Meteor::Syslog::myDie;
94                  {          }
95                          # close standard file descriptors          
96                          close(STDIN);          &::syslog('info',"$::PGM launched!");
97                          close(STDOUT);          
98                          close(STDERR);          #
99                          chdir("/");          # Daemonize
100                          umask(0);          #
101                          # fork and exit parent          {
102                          exit if fork;                  $0="$::PGM daemon";
103                          setpgrp(0, $$) if defined $SIG{TTOU};                  
104                          $SIG{TTOU}='ignore' if defined $SIG{TTOU};                  unless($::CONF{'Debug'})
105                                            {
106                          # Avoid 'stdin reopened for output' warning with newer perls                          # close standard file descriptors
107                          open(NULL,'/dev/null');                          close(STDIN);
108                          <NULL> if(0);                          close(STDOUT);
109                                                    close(STDERR);
110                          open(OUT,">/var/run/$::PGM.pid");                          chdir("/");
111                          print OUT "$$\n";                          umask(0);
112                          close(OUT);                          # fork and exit parent
113                  }                          exit if fork;
114                  else                          setpgrp(0, $$) if defined $SIG{TTOU};
115                  {                          $SIG{TTOU}='ignore' if defined $SIG{TTOU};
116                          print "$::PGM PID: $$\n";                          
117                  }                          # Avoid 'stdin reopened for output' warning with newer perls
118          }                          open(NULL,'/dev/null');
119                                    <NULL> if(0);
120          #                          
121          # Signal handlers                          open(OUT,">/var/run/$::PGM.pid");
122          #                          print OUT "$$\n";
123          $::HUP=$::TERM=$::USR1=$::USR2=0;                          close(OUT);
124          $SIG{'HUP'}=sub{$::HUP=1};                  }
125          $SIG{'TERM'}=sub{$::TERM=1};                  else
126          $SIG{'USR1'}=sub{$::USR1=1};                  {
127          $SIG{'USR2'}=sub{$::USR2=1};                          print "$::PGM PID: $$\n";
128                            }
129          #          }
130          # Run server          
131          #          #
132          my $con_counter=0;          # Signal handlers
133          my $con;          #
134                    $::HUP=$::TERM=$::USR1=$::USR2=0;
135          my $controlServer=Meteor::Socket->newServer(          $SIG{'HUP'}=sub{$::HUP=1};
136                  $::CONF{'ControllerPort'},          $SIG{'TERM'}=sub{$::TERM=1};
137                  $CONTROL_QUEUE_SIZE,          $SIG{'USR1'}=sub{$::USR1=1};
138                  $::CONF{'ControllerIP'}          $SIG{'USR2'}=sub{$::USR2=1};
139          );          
140          my $controlServerFN=$controlServer->fileno();          #
141                    # Run server
142          my $subscriberServer=Meteor::Socket->newServer(          #
143                  $::CONF{'SubscriberPort'},          my $con_counter=0;
144                  $SUBSCRIBER_QUEUE_SIZE,          my $con;
145                  $::CONF{'SubscriberIP'}          
146          );          my $controlServer=Meteor::Socket->newServer(
147          my $subscriberServerFN=$subscriberServer->fileno();                  $::CONF{'ControllerPort'},
148                            $CONTROL_QUEUE_SIZE,
149          my $serverVector='';                  $::CONF{'ControllerIP'}
150          vec($serverVector,$controlServerFN,1)=1;          );
151          vec($serverVector,$subscriberServerFN,1)=1;          my $controlServerFN=$controlServer->fileno();
152                    
153          my $lastAgeCheck=time;          my $subscriberServer=Meteor::Socket->newServer(
154                            $::CONF{'SubscriberPort'},
155          my $nextPing=undef;                  $SUBSCRIBER_QUEUE_SIZE,
156          if(exists($::CONF{'PingInterval'}) && $::CONF{'PingInterval'}>2)                  $::CONF{'SubscriberIP'}
157          {          );
158                  $nextPing=$::CONF{'PingInterval'}+$lastAgeCheck;          my $subscriberServerFN=$subscriberServer->fileno();
159          }          
160                    my $serverVector='';
161          while(!$::TERM)          vec($serverVector,$controlServerFN,1)=1;
162          {          vec($serverVector,$subscriberServerFN,1)=1;
163                  eval          
164                  {          my $lastAgeCheck=time;
165                          while(!$::TERM)          
166                          {          my $nextPing=undef;
167                                  my $rVec=$serverVector;          if(exists($::CONF{'PingInterval'}) && $::CONF{'PingInterval'}>2)
168                                  my $wVec='';          {
169                                  my $eVec='';                  $nextPing=$::CONF{'PingInterval'}+$lastAgeCheck;
170                            }
171                                  my $rout;          
172                                  my $wout;          while(!$::TERM)
173                                  my $eout;          {
174                                    eval
175                                  Meteor::Connection->addAllHandleBits(\$rVec,\$wVec,\$eVec);                  {
176                                                            while(!$::TERM)
177                                  my $timeout=$MAIN_LOOP_TIMEOUT;                          {
178                                  if(defined($nextPing))                                  my $rVec=$serverVector;
179                                  {                                  my $wVec='';
180                                          $timeout=$nextPing-time;                                  my $eVec='';
181                                  }                  
182                                                                    my $rout;
183                                  my $result=0;                                  my $wout;
184                                  if($timeout>0)                                  my $eout;
185                                  {                  
186                                          $result=&Meteor::Socket::sselect($rout=$rVec,$wout=$wVec,$eout=$eVec,$timeout);                                  Meteor::Connection->addAllHandleBits(\$rVec,\$wVec,\$eVec);
187                                  }                                  
188                                                                    my $timeout=$MAIN_LOOP_TIMEOUT;
189                                  if($result>0)                                  if(defined($nextPing))
190                                  {                                  {
191                                          if(vec($rout,$controlServerFN,1))                                          $timeout=$nextPing-time;
192                                          {                                  }
193                                                  Meteor::Controller->newFromServer($controlServer);                                  
194                                          }                                  my $result=0;
195                                          if(vec($rout,$subscriberServerFN,1))                                  if($timeout>0)
196                                          {                                  {
197                                                  Meteor::Subscriber->newFromServer($subscriberServer);                                          $result=&Meteor::Socket::sselect($rout=$rVec,$wout=$wVec,$eout=$eVec,$timeout);
198                                          }                                  }
199                                                                            
200                                          Meteor::Connection->checkAllHandleBits($rout,$wout,$eout);                                  if($result>0)
201                                  }                                  {
202                                  elsif($result<0)                                          if(vec($rout,$controlServerFN,1))
203                                  {                                          {
204                                          &::syslog('crit',"Select failed: $!");                                                  Meteor::Controller->newFromServer($controlServer);
205                                          sleep(30);                                          }
206                                  }                                          if(vec($rout,$subscriberServerFN,1))
207                                                                            {
208                                  if($::HUP)                                                  Meteor::Subscriber->newFromServer($subscriberServer);
209                                  {                                          }
210                                          $::HUP=0;                                          
211                                                                                    Meteor::Connection->checkAllHandleBits($rout,$wout,$eout);
212                                          &::syslog('info',"Received SIGHUP, re-reading config and clearing document cache!");                                  }
213                                                                            elsif($result<0)
214                                          Meteor::Config->readConfig();                                  {
215                                          Meteor::Config->updateConfig();                                          &::syslog('crit',"Select failed: $!");
216                                                                                    sleep(30);
217                                          Meteor::Document->clearDocuments()                                  }
218                                  }                                  
219                                                                    if($::HUP)
220                                  if($::USR1)                                  {
221                                  {                                          $::HUP=0;
222                                          $::USR1=0;                                          
223                                                                                    &::syslog('info',"Received SIGHUP, re-reading config and clearing document cache!");
224                                          &::syslog('info',"Received SIGUSR1, clearing channel buffers!");                                          
225                                                                                    Meteor::Config->readConfig();
226                                          Meteor::Channel->clearAllBuffers();                                          Meteor::Config->updateConfig();
227                                  }                                          
228                                                                            Meteor::Document->clearDocuments()
229                                  if($::USR2)                                  }
230                                  {                                  
231                                          $::USR2=0;                                  if($::USR1)
232                                                                            {
233                                          &::syslog('info',"Received SIGUSR2, clearing document cache!");                                          $::USR1=0;
234                                                                                    
235                                          Meteor::Document->clearDocuments()                                          &::syslog('info',"Received SIGUSR1, clearing channel buffers!");
236                                  }                                          
237                                                                            Meteor::Channel->clearAllBuffers();
238                                  my $t=time;                                  }
239                                  if($t>$lastAgeCheck+$AGE_CHECK_INTERVALL)                                  
240                                  {                                  if($::USR2)
241                                          my $minTimeStap=time-$::CONF{'MaxMessageAge'};                                  {
242                                          Meteor::Channel->trimMessageStoresByTimestamp($minTimeStap);                                          $::USR2=0;
243                                          $lastAgeCheck=time;                                          
244                                          $t=$lastAgeCheck;                                          &::syslog('info',"Received SIGUSR2, clearing document cache!");
245                                                                                    
246                                          Meteor::Subscriber->checkPersistentConnectionsForMaxTime();                                          Meteor::Document->clearDocuments()
247                                  }                                  }
248                                                                    
249                                  if(defined($nextPing) && $nextPing<=$t)                                  my $t=time;
250                                  {                                  if($t>$lastAgeCheck+$AGE_CHECK_INTERVALL)
251                                          $nextPing=undef;                                  {
252                                                                                    my $minTimeStap=time-$::CONF{'MaxMessageAge'};
253                                          Meteor::Subscriber->pingPersistentConnections();                                          Meteor::Channel->trimMessageStoresByTimestamp($minTimeStap);
254                                                                                    $lastAgeCheck=time;
255                                          if(exists($::CONF{'MaxMessageAge'}) && $::CONF{'MaxMessageAge'}>2)                                          $t=$lastAgeCheck;
256                                          {                                          
257                                                  $nextPing=$::CONF{'PingInterval'}+time;                                          Meteor::Subscriber->checkPersistentConnectionsForMaxTime();
258                                          }                                  }
259                                  }                                  
260                          }                                  if(defined($nextPing) && $nextPing<=$t)
261                  };                                  {
262                  unless($::TERM)                                          $nextPing=undef;
263                  {                                          
264                          &::syslog('alert',"$::PGM loop died (will restart in 2 seconds): $@");                                          Meteor::Subscriber->pingPersistentConnections();
265                          sleep(2);                                          
266                  }                                          if(exists($::CONF{'MaxMessageAge'}) && $::CONF{'MaxMessageAge'}>2)
267          }                                          {
268                                                            $nextPing=$::CONF{'PingInterval'}+time;
269          #                                          }
270          # Proper shutdown                                  }
271          #                          }
272          if($::TERM)                  };
273          {                  unless($::TERM)
274                  &::syslog('info',"Received SIGTERM, begin shutdown!");                  {
275                                            &::syslog('alert',"$::PGM loop died (will restart in 2 seconds): $@");
276                  $subscriberServer->close();                          sleep(2);
277                  $controlServer->close();                  }
278                            }
279                  unlink("/var/run/$::PGM.pid") unless($::CONF{'Debug'});          
280                            #
281                  Meteor::Connection->closeAllConnections();          # Proper shutdown
282                            #
283                  my $timoutAt=time+$MAX_EXIT_DELAY;          if($::TERM)
284                            {
285                  while(Meteor::Connection->connectionCount() && time<$timoutAt)                  &::syslog('info',"Received SIGTERM, begin shutdown!");
286                  {                  
287                          my $rVec='';                  $subscriberServer->close();
288                          my $wVec='';                  $controlServer->close();
289                          my $eVec='';                  
290                                            unlink("/var/run/$::PGM.pid") unless($::CONF{'Debug'});
291                          my $rout;                  
292                          my $wout;                  Meteor::Connection->closeAllConnections();
293                          my $eout;                  
294                                            my $timoutAt=time+$MAX_EXIT_DELAY;
295                          Meteor::Connection->addAllHandleBits(\$rVec,\$wVec,\$eVec);                  
296                                            while(Meteor::Connection->connectionCount() && time<$timoutAt)
297                          my $result=&Meteor::Socket::sselect($rout=$rVec,$wout=$wVec,$eout=$eVec,$timoutAt-time);                  {
298                                                    my $rVec='';
299                          if($result>0)                          my $wVec='';
300                          {                          my $eVec='';
301                                  Meteor::Connection->checkAllHandleBits($rout,$wout,$eout);                          
302                          }                          my $rout;
303                  }                          my $wout;
304                                            my $eout;
305                  if(my $cnt=Meteor::Connection->connectionCount())                          
306                  {                          Meteor::Connection->addAllHandleBits(\$rVec,\$wVec,\$eVec);
307                          &::syslog('info',"$cnt client(s) unresponsive, will shutdown anyway");                          
308                                                    my $result=&Meteor::Socket::sselect($rout=$rVec,$wout=$wVec,$eout=$eVec,$timoutAt-time);
309                          exit(1);                          
310                  }                          if($result>0)
311                                            {
312                  &::syslog('info',"shutdown succeeded");                                  Meteor::Connection->checkAllHandleBits($rout,$wout,$eout);
313                                            }
314                  exit(0);                  }
315          }                  
316                            if(my $cnt=Meteor::Connection->connectionCount())
317          &::syslog('emerg',"$::PGM loop exited");                  {
318                            &::syslog('info',"$cnt client(s) unresponsive, will shutdown anyway");
319  1;                          
320                            exit(1);
321                    }
322                    
323                    &::syslog('info',"shutdown succeeded");
324                    
325                    exit(0);
326            }
327            
328            &::syslog('emerg',"$::PGM loop exited");
329    
330    1;
331  ############################################################################EOF  ############################################################################EOF

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

  ViewVC Help
Powered by ViewVC 1.1.26