/[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 47 by knops.gerd, Mon Feb 4 21:06:42 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.04';
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                                            my $facility=$::CONF{'SyslogFacility'} || $Meteor::Syslog::DEFAULT_FACILITY;
106                          # Avoid 'stdin reopened for output' warning with newer perls                  
107                          open(NULL,'/dev/null');                  unless($::CONF{'Debug'} || $facility eq 'none')
108                          <NULL> if(0);                  {
109                                                    # close standard file descriptors
110                          open(OUT,">/var/run/$::PGM.pid");                          close(STDIN);
111                          print OUT "$$\n";                          close(STDOUT);
112                          close(OUT);                          close(STDERR);
113                  }                          chdir("/");
114                  else                          umask(0);
115                  {                          # fork and exit parent
116                          print "$::PGM PID: $$\n";                          exit if fork;
117                  }                          setpgrp(0, $$) if defined $SIG{TTOU};
118          }                          $SIG{TTOU}='ignore' if defined $SIG{TTOU};
119                                    
120          #                          # Avoid 'stdin reopened for output' warning with newer perls
121          # Signal handlers                          open(NULL,'/dev/null');
122          #                          <NULL> if(0);
123          $::HUP=$::TERM=$::USR1=$::USR2=0;                          
124          $SIG{'HUP'}=sub{$::HUP=1};                          open(OUT,">/var/run/$::PGM.pid");
125          $SIG{'TERM'}=sub{$::TERM=1};                          print OUT "$$\n";
126          $SIG{'USR1'}=sub{$::USR1=1};                          close(OUT);
127          $SIG{'USR2'}=sub{$::USR2=1};                  }
128                            else
129          #                  {
130          # Run server                          &::syslog('info',"PID\t%s",$$);
131          #                  }
132          my $con_counter=0;          }
133          my $con;          
134                    #
135          my $controlServer=Meteor::Socket->newServer(          # Signal handlers
136                  $::CONF{'ControllerPort'},          #
137                  $CONTROL_QUEUE_SIZE,          $::HUP=$::TERM=$::USR1=$::USR2=0;
138                  $::CONF{'ControllerIP'}          $SIG{'HUP'}=sub{$::HUP=1};
139          );          $SIG{'TERM'}=sub{$::TERM=1};
140          my $controlServerFN=$controlServer->fileno();          $SIG{'USR1'}=sub{$::USR1=1};
141                    $SIG{'USR2'}=sub{$::USR2=1};
142          my $subscriberServer=Meteor::Socket->newServer(          
143                  $::CONF{'SubscriberPort'},          #
144                  $SUBSCRIBER_QUEUE_SIZE,          # Run server
145                  $::CONF{'SubscriberIP'}          #
146          );          my $con_counter=0;
147          my $subscriberServerFN=$subscriberServer->fileno();          my $con;
148                    
149          my $serverVector='';          my $controlServer=Meteor::Socket->newServer(
150          vec($serverVector,$controlServerFN,1)=1;                  $::CONF{'ControllerPort'},
151          vec($serverVector,$subscriberServerFN,1)=1;                  $CONTROL_QUEUE_SIZE,
152                            $::CONF{'ControllerIP'}
153          my $lastAgeCheck=time;          );
154                    my $controlServerFN=$controlServer->fileno();
155          my $nextPing=undef;          
156          if(exists($::CONF{'PingInterval'}) && $::CONF{'PingInterval'}>2)          my $subscriberServer=Meteor::Socket->newServer(
157          {                  $::CONF{'SubscriberPort'},
158                  $nextPing=$::CONF{'PingInterval'}+$lastAgeCheck;                  $SUBSCRIBER_QUEUE_SIZE,
159          }                  $::CONF{'SubscriberIP'}
160                    );
161          while(!$::TERM)          my $subscriberServerFN=$subscriberServer->fileno();
162          {          
163                  eval          my $serverVector='';
164                  {          vec($serverVector,$controlServerFN,1)=1;
165                          while(!$::TERM)          vec($serverVector,$subscriberServerFN,1)=1;
166                          {          
167                                  my $rVec=$serverVector;          my $lastAgeCheck=time;
168                                  my $wVec='';          
169                                  my $eVec='';          my $nextPing=undef;
170                            if(exists($::CONF{'PingInterval'}) && $::CONF{'PingInterval'}>2)
171                                  my $rout;          {
172                                  my $wout;                  $nextPing=$::CONF{'PingInterval'}+$lastAgeCheck;
173                                  my $eout;          }
174                            
175                                  Meteor::Connection->addAllHandleBits(\$rVec,\$wVec,\$eVec);          while(!$::TERM)
176                                            {
177                                  my $timeout=$MAIN_LOOP_TIMEOUT;                  eval
178                                  if(defined($nextPing))                  {
179                                  {                          while(!$::TERM)
180                                          $timeout=$nextPing-time;                          {
181                                  }                                  my $rVec=$serverVector;
182                                                                    my $wVec='';
183                                  my $result=0;                                  my $eVec='';
184                                  if($timeout>0)                          
185                                  {                                  my $rout;
186                                          $result=&Meteor::Socket::sselect($rout=$rVec,$wout=$wVec,$eout=$eVec,$timeout);                                  my $wout;
187                                  }                                  my $eout;
188                                                            
189                                  if($result>0)                                  Meteor::Connection->addAllHandleBits(\$rVec,\$wVec,\$eVec);
190                                  {                                  
191                                          if(vec($rout,$controlServerFN,1))                                  my $timeout=$MAIN_LOOP_TIMEOUT;
192                                          {                                  if(defined($nextPing))
193                                                  Meteor::Controller->newFromServer($controlServer);                                  {
194                                          }                                          $timeout=$nextPing-time;
195                                          if(vec($rout,$subscriberServerFN,1))                                  }
196                                          {                                  
197                                                  Meteor::Subscriber->newFromServer($subscriberServer);                                  my $result=0;
198                                          }                                  if($timeout>0)
199                                                                            {
200                                          Meteor::Connection->checkAllHandleBits($rout,$wout,$eout);                                          $result=&Meteor::Socket::sselect($rout=$rVec,$wout=$wVec,$eout=$eVec,$timeout);
201                                  }                                  }
202                                  elsif($result<0)                                  
203                                  {                                  if($result>0)
204                                          &::syslog('crit',"Select failed: $!");                                  {
205                                          sleep(30);                                          if(vec($rout,$controlServerFN,1))
206                                  }                                          {
207                                                                                    Meteor::Controller->newFromServer($controlServer);
208                                  if($::HUP)                                          }
209                                  {                                          if(vec($rout,$subscriberServerFN,1))
210                                          $::HUP=0;                                          {
211                                                                                            Meteor::Subscriber->newFromServer($subscriberServer);
212                                          &::syslog('info',"Received SIGHUP, re-reading config and clearing document cache!");                                          }
213                                                                                    
214                                          Meteor::Config->readConfig();                                          Meteor::Connection->checkAllHandleBits($rout,$wout,$eout);
215                                          Meteor::Config->updateConfig();                                  }
216                                                                            elsif($result<0)
217                                          Meteor::Document->clearDocuments()                                  {
218                                  }                                          &::syslog('crit',"Select failed: $!");
219                                                                            sleep(30);
220                                  if($::USR1)                                  }
221                                  {                                  
222                                          $::USR1=0;                                  if($::HUP)
223                                                                            {
224                                          &::syslog('info',"Received SIGUSR1, clearing channel buffers!");                                          $::HUP=0;
225                                                                                    
226                                          Meteor::Channel->clearAllBuffers();                                          &::syslog('info',"Received SIGHUP, re-reading config and clearing document cache!");
227                                  }                                          
228                                                                            Meteor::Config->readConfig();
229                                  if($::USR2)                                          Meteor::Config->updateConfig();
230                                  {                                          
231                                          $::USR2=0;                                          Meteor::Document->clearDocuments()
232                                                                            }
233                                          &::syslog('info',"Received SIGUSR2, clearing document cache!");                                  
234                                                                            if($::USR1)
235                                          Meteor::Document->clearDocuments()                                  {
236                                  }                                          $::USR1=0;
237                                                                            
238                                  my $t=time;                                          &::syslog('info',"Received SIGUSR1, clearing channel buffers!");
239                                  if($t>$lastAgeCheck+$AGE_CHECK_INTERVALL)                                          
240                                  {                                          Meteor::Channel->clearAllBuffers();
241                                          my $minTimeStap=time-$::CONF{'MaxMessageAge'};                                  }
242                                          Meteor::Channel->trimMessageStoresByTimestamp($minTimeStap);                                  
243                                          $lastAgeCheck=time;                                  if($::USR2)
244                                          $t=$lastAgeCheck;                                  {
245                                                                                    $::USR2=0;
246                                          Meteor::Subscriber->checkPersistentConnectionsForMaxTime();                                          
247                                  }                                          &::syslog('info',"Received SIGUSR2, clearing document cache!");
248                                                                            
249                                  if(defined($nextPing) && $nextPing<=$t)                                          Meteor::Document->clearDocuments()
250                                  {                                  }
251                                          $nextPing=undef;                                  
252                                                                            my $t=time;
253                                          Meteor::Subscriber->pingPersistentConnections();                                  if($t>$lastAgeCheck+$AGE_CHECK_INTERVALL)
254                                                                            {
255                                          if(exists($::CONF{'MaxMessageAge'}) && $::CONF{'MaxMessageAge'}>2)                                          my $minTimeStap=time-$::CONF{'MaxMessageAge'};
256                                          {                                          Meteor::Channel->trimMessageStoresByTimestamp($minTimeStap);
257                                                  $nextPing=$::CONF{'PingInterval'}+time;                                          $lastAgeCheck=time;
258                                          }                                          $t=$lastAgeCheck;
259                                  }                                          
260                          }                                          Meteor::Subscriber->checkPersistentConnectionsForMaxTime();
261                  };                                  }
262                  unless($::TERM)                                  
263                  {                                  if(defined($nextPing) && $nextPing<=$t)
264                          &::syslog('alert',"$::PGM loop died (will restart in 2 seconds): $@");                                  {
265                          sleep(2);                                          $nextPing=undef;
266                  }                                          
267          }                                          Meteor::Subscriber->pingPersistentConnections();
268                                                    
269          #                                          if(exists($::CONF{'MaxMessageAge'}) && $::CONF{'MaxMessageAge'}>2)
270          # Proper shutdown                                          {
271          #                                                  $nextPing=$::CONF{'PingInterval'}+time;
272          if($::TERM)                                          }
273          {                                  }
274                  &::syslog('info',"Received SIGTERM, begin shutdown!");                          }
275                                    };
276                  $subscriberServer->close();                  unless($::TERM)
277                  $controlServer->close();                  {
278                                            &::syslog('alert',"$::PGM loop died (will restart in 2 seconds): $@");
279                  unlink("/var/run/$::PGM.pid") unless($::CONF{'Debug'});                          sleep(2);
280                                    }
281                  Meteor::Connection->closeAllConnections();          }
282                            
283                  my $timoutAt=time+$MAX_EXIT_DELAY;          #
284                            # Proper shutdown
285                  while(Meteor::Connection->connectionCount() && time<$timoutAt)          #
286                  {          if($::TERM)
287                          my $rVec='';          {
288                          my $wVec='';                  &::syslog('info',"Received SIGTERM, begin shutdown!");
289                          my $eVec='';                  
290                                            $subscriberServer->close();
291                          my $rout;                  $controlServer->close();
292                          my $wout;                  
293                          my $eout;                  unlink("/var/run/$::PGM.pid") unless($::CONF{'Debug'});
294                                            
295                          Meteor::Connection->addAllHandleBits(\$rVec,\$wVec,\$eVec);                  Meteor::Connection->closeAllConnections();
296                                            
297                          my $result=&Meteor::Socket::sselect($rout=$rVec,$wout=$wVec,$eout=$eVec,$timoutAt-time);                  my $timoutAt=time+$MAX_EXIT_DELAY;
298                                            
299                          if($result>0)                  while(Meteor::Connection->connectionCount() && time<$timoutAt)
300                          {                  {
301                                  Meteor::Connection->checkAllHandleBits($rout,$wout,$eout);                          my $rVec='';
302                          }                          my $wVec='';
303                  }                          my $eVec='';
304                                            
305                  if(my $cnt=Meteor::Connection->connectionCount())                          my $rout;
306                  {                          my $wout;
307                          &::syslog('info',"$cnt client(s) unresponsive, will shutdown anyway");                          my $eout;
308                                                    
309                          exit(1);                          Meteor::Connection->addAllHandleBits(\$rVec,\$wVec,\$eVec);
310                  }                          
311                                            my $result=&Meteor::Socket::sselect($rout=$rVec,$wout=$wVec,$eout=$eVec,$timoutAt-time);
312                  &::syslog('info',"shutdown succeeded");                          
313                                            if($result>0)
314                  exit(0);                          {
315          }                                  Meteor::Connection->checkAllHandleBits($rout,$wout,$eout);
316                                    }
317          &::syslog('emerg',"$::PGM loop exited");                  }
318                    
319  1;                  if(my $cnt=Meteor::Connection->connectionCount())
320                    {
321                            &::syslog('info',"$cnt client(s) unresponsive, will shutdown anyway");
322                            
323                            exit(1);
324                    }
325                    
326                    &::syslog('info',"shutdown succeeded");
327                    
328                    exit(0);
329            }
330            
331            &::syslog('emerg',"$::PGM loop exited");
332    
333    1;
334  ############################################################################EOF  ############################################################################EOF

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

  ViewVC Help
Powered by ViewVC 1.1.26