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

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

  ViewVC Help
Powered by ViewVC 1.1.26