--- googlecode.com/svn/trunk/meteord 2006/11/20 17:59:30 3 +++ googlecode.com/svn/trunk/meteord 2009/01/19 11:19:41 64 @@ -1,43 +1,51 @@ #!/usr/bin/perl -w ############################################################################### -# Copyright 2006 BITart Gerd Knops, All rights reserved. +# Meteor +# An HTTP server for the 2.0 web +# Copyright (c) 2006 contributing authors # -# Project : Meteor -# File : meteord -# Author : Gerd Knops gerti@BITart.com +# The Meteor daemon # -############################################################################### -# -# History: -# 060821 Creation of file +# Main program should call Meteor::Config::setCommandLineParameters(@ARGV),. +# Afterwards anybody can access $::CONF{}, where +# is any valid parameter (except 'Help') listed in the +# @DEFAULTS array below. # ############################################################################### # -# Description: -# The Meteor daemon -# -# $Id:$ +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the Free +# Software Foundation; either version 2 of the License, or (at your option) +# any later version. # -############################################################################### +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. # -# DISCLAIMER +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # -# BITart and Gerd Knops make no warranties, representations or commitments -# with regard to the contents of this software. BITart and Gerd Knops -# specifically disclaim any and all warranties, whether express, implied or -# statutory, including, but not limited to, any warranty of merchantability -# or fitness for a particular purpose, and non-infringement. Under no -# circumstances will BITart or Gerd Knops be liable for loss of data, -# special, incidental or consequential damages out of the use of this -# software, even if those damages were foreseeable, or BITart or Gerd Knops -# was informed of their potential. +# For more information visit www.meteorserver.org # ############################################################################### + +############################################################################### +# meterod version +################################################################################ + + $::VERSION='1.06.04'; + $::RELEASE_DATE='not yet released'; + +############################################################################### # Configuration ############################################################################### use strict; + use Socket; + use Meteor::Syslog; use Meteor::Socket; @@ -57,12 +65,20 @@ our $AGE_CHECK_INTERVALL=60; our $MAX_EXIT_DELAY=120; + + our $UDP_MAX_MESSAGE_SIZE=8192; ############################################################################### # Main ############################################################################### # + # Record startup time + # + $::STARTUP_TIME=time; + $::STARTUP_TIME+=0; # avoid warning + + # # Program name # $::PGM=$0; @@ -90,7 +106,9 @@ { $0="$::PGM daemon"; - unless($::CONF{'Debug'}) + my $facility=$::CONF{'SyslogFacility'} || $Meteor::Syslog::DEFAULT_FACILITY; + + unless($::CONF{'Debug'} || $facility eq 'none') { # close standard file descriptors close(STDIN); @@ -98,7 +116,9 @@ close(STDERR); chdir("/"); umask(0); + # fork and exit parent + print STDERR "Exit: Fork\n"; exit if fork; setpgrp(0, $$) if defined $SIG{TTOU}; $SIG{TTOU}='ignore' if defined $SIG{TTOU}; @@ -106,14 +126,14 @@ # Avoid 'stdin reopened for output' warning with newer perls open(NULL,'/dev/null'); if(0); - open(OUT,">/var/run/$::PGM.pid"); print OUT "$$\n"; close(OUT); + } else { - print "$::PGM PID: $$\n"; + &::syslog('info',"PID\t%s",$$); } } @@ -125,7 +145,8 @@ $SIG{'TERM'}=sub{$::TERM=1}; $SIG{'USR1'}=sub{$::USR1=1}; $SIG{'USR2'}=sub{$::USR2=1}; - + $SIG{'PIPE'}=sub{&::syslog('info',"Signal PIPE received and ignored\n");}; + # # Run server # @@ -146,9 +167,22 @@ ); my $subscriberServerFN=$subscriberServer->fileno(); + my $udpServer=undef; + my $udpPort=$::CONF{'UDPPort'}; + my $udpServerFN=undef; + if($udpPort && $udpPort>0) + { + $udpServer=Meteor::Socket->newUDPServer( + $udpPort, + $::CONF{'UDPIP'} + ); + $udpServerFN=$udpServer->fileno(); + } + my $serverVector=''; vec($serverVector,$controlServerFN,1)=1; vec($serverVector,$subscriberServerFN,1)=1; + vec($serverVector,$udpServerFN,1)=1 if(defined($udpServerFN)); my $lastAgeCheck=time; @@ -164,14 +198,16 @@ { while(!$::TERM) { + + my $rVec=$serverVector; my $wVec=''; my $eVec=''; - + my $rout; my $wout; my $eout; - + Meteor::Connection->addAllHandleBits(\$rVec,\$wVec,\$eVec); my $timeout=$MAIN_LOOP_TIMEOUT; @@ -196,6 +232,10 @@ { Meteor::Subscriber->newFromServer($subscriberServer); } + if(defined($udpServerFN) && vec($rout,$udpServerFN,1)) + { + &handleUPD($udpServer); + } Meteor::Connection->checkAllHandleBits($rout,$wout,$eout); } @@ -225,7 +265,7 @@ Meteor::Channel->clearAllBuffers(); } - + if($::USR2) { $::USR2=0; @@ -234,7 +274,7 @@ Meteor::Document->clearDocuments() } - + my $t=time; if($t>$lastAgeCheck+$AGE_CHECK_INTERVALL) { @@ -244,6 +284,7 @@ $t=$lastAgeCheck; Meteor::Subscriber->checkPersistentConnectionsForMaxTime(); + Meteor::Connection->destroyBadRequests(); } if(defined($nextPing) && $nextPing<=$t) @@ -306,15 +347,44 @@ { &::syslog('info',"$cnt client(s) unresponsive, will shutdown anyway"); + print STDERR "Exit: TERM Shutdown (unresponsive clients)\n"; exit(1); } &::syslog('info',"shutdown succeeded"); + print STDERR "Exit: TERM Shutdown (clean)\n"; exit(0); } &::syslog('emerg',"$::PGM loop exited"); +############################################################################### +# Subroutines +############################################################################### +sub handleUPD { + $udpServer=shift; + + my $line; + my $hispaddr=recv($udpServer->{'handle'},$line,$::UDP_MAX_MESSAGE_SIZE,0); + + &::syslog('debug',"udp message received: %s",$line); + + return unless($line=~s/^(\S+)\s//); + + my $cmd=$1; + + if($cmd eq 'ADDMESSAGE') + { + return unless($line=~s/^(\S+)\s//); + + my $channelName=$1; + my $channel=Meteor::Channel->channelWithName($channelName); + my $msg=$channel->addMessage($line); + my $msgID=$msg->id(); + &::syslog('debug',"udp: new message added, ID %s",$msgID); + } +} + 1; -############################################################################EOF \ No newline at end of file +############################################################################EOF