hallikpapa has asked for the wisdom of the Perl Monks concerning the following question:
And on the client side, here is just the connection part, most everything else is just how to handle the data it gets:use strict; use lib '/opt/nextone/lib/perl5/site_perl/5.8.3/'; use IO::Socket; use LogFile; use File::Tail; use Sys::Hostname; use Time::Local; use POSIX qw(strftime); use constant PORT => 9204; use constant PIDFILE => '/var/run/cdr_hup.pid'; use constant USER => 'root'; use constant GROUP => 'root'; use constant CDR_HOME => '/var/cdrs'; # signal handler for child die events $SIG{TERM} = $SIG{INT} = \&do_term; $SIG{HUP} = \&do_hup; our $connection; my $quit=0; my $port = $ARGV[0] || PORT; my @allow=('IP ADDRESS OF INCOMING CONNECTION'); my ($ip_addr, $valid, $log_file); $log_file ='/var/log/log_server'; init_log($log_file); log_notice "Start Server\n"; my $listen_socket = IO::Socket::INET->new(LocalPort => $port, Listen => 20, Proto => 'tcp', ReuseAddr => 1) or die "Can't bind : $@\n";; #log_notice "Can't create a listening socket: $@\n" unless $listen_soc +ket; #my $pid = init_server(PIDFILE,USER,GROUP,$port); log_notice "Server accepting connections on port $port\n"; while (!$quit) { next unless $connection = $listen_socket->accept; log_notice "Client connecting\n"; my $host = $connection->peerhost; foreach $ip_addr (@allow) { $valid=0; if($host == $ip_addr) { $valid=$ip_addr; last; } } if(!$valid) { $connection->close; log_warn "Connection attempt from invalid host : $host\n"; next; } $connection->setsockopt(SOL_SOCKET, SO_KEEPALIVE=>1); log_notice("Accepting a connection from $host\n"); interact($connection); log_notice("Connection from $host closed\n"); $connection->close; } # End While !quit sub interact { my $sock = shift; STDIN->fdopen($sock,"r") or die "Can't reopen STDIN: $!"; STDOUT->fdopen($sock,"w") or die "Can't reopen STDOUT: $!"; STDERR->fdopen($sock,"w") or die "Can't reopen STDERR: $!"; $| = 1; my ($cdr_path, $cdr_file, $record, $line, $file, $thishost, $filenam +e); $cdr_path="/var/cdrs"; $cdr_file=gen_name(); $record=0; $thishost=hostname; $filename=$cdr_path.'/'.$cdr_file; while ( <$sock> ) { until (-e $filename) { sleep 10; } next unless /\S/; # blank line log_notice "Tailing $filename\n"; $file=File::Tail->new(name=>$filename, maxinterval=>5, interval= +>1, tail=>-1, errmode=>\&do_exit); while ( defined($line=$file->read) ) { $record++; print $sock $record.";".$cdr_file.";".$thishost.";".$l +ine; } # End While # 2 &do_term($sock); } # End While sock } #End interact sub do_exit { log_notice("End Tail ... Closing Socket\n"); if (defined $connection) { $connection->shutdown(2); } sleep 2; exec '/usr/local/bin/logserver' || log_warn ("LOGSERVER : Could not exec $0\n"); exit 9; # Should not get here } # End do_exit sub do_term { log_notice("TERM signal received, terminating\n"); exit 0; }
Now I commented out the part where it does the is_recovery subroutine. The reason I did this is because the pipe between the client and server is never broken, but it just stops updating. If I restart the process, the recovery subroutine checks to see what line I was just on when the process was shut down and continues on from there. Because there seems to be some kind of timeout, or problem, I have it start from the beginning of the file to make sure it didn't miss anything. Also, I kept restarting the process and with the recovery part in there, and it wouldn't continue reading in files. May be a flaw there. Any ideas? I really need help on this one. Just doesn't make sense why few times a week now it just stops reading, but none of the processes die...CONNECT: $socket = IO::Socket::INET->new("$host:$port"); unless ($socket) { # Potential Problem here $retry++; if($retry==3) { log_die ("Client : Error connecting to server ... $@\n +"); } log_notice ("Client : Retry $retry : Sleeping $timeout Second +s\n"); sleep($timeout); # wait a minute; goto CONNECT; } $retry=0; #Reset in case we disconnect early $connected=1; #So that we know... #Start from beginning, uncomment if you don't want too $recover_from=is_recovery($dbh); $recover_from++; $rec_num=0; $stCnt->finish; if ($recover_mode) { log_notice ("Client : Begin recovery mode from record number $ +recover_from\n"); } $SIG{TERM}=$SIG{INT}=$SIG{PIPE}=$SIG{CHLD} = sub { my $sig=shift; if (defined $sth) { $sth->finish; } if (defined $dbh ) { $dbh->disconnect; } if (defined $socket) { $socket->shutdown(2); } sleep(5); do { $pid = waitpid(-1,&WNOHANG); } until $pid == -1; log_notice ("Client : Caught signal SIG$sig\n"); if($sig eq 'PIPE') { log_notice ("Connection Ended\n"); } if($sig eq 'TERM' || $sig eq 'HUP' || $sig eq 'INT') { log_notice ("User Cancelling?\n"); exit 1; #User cancelling job ? } if ($connected) { ############################################################# # First check to see if this is early termination # Should only terminate at midnight. If this is # an early termination the assumption is that the # system we were connected to has croaked. If the # current time is less than $midnight then we try # to exec to ourself (RESTART..) where, hopefully # we will reconnect and recover where we left off # or give up after three times the $timeout. ############################################################# $timestamp=time; if($timestamp < $midnight) { log_notice ("Client : Restarting $0\n"); log_error("End Processing $src_cdr_file\n"); exec '/home/$0' || log_warn ("Client : Could not exec $0\n"); exit 6; # Something is wrong if this exit is taken } # End if $timestamp log_notice ("Client : Normal Termination\n"); log_error("End Processing $src_cdr_file\n\n"); exec '/usr/bin/perl', '/home/client' || log_warn ("Client : Could not exec $0\n"); exit 7; # Something is wrong if this exit is taken } # End if $connected }; # End anonymous sub print $socket "tail\n"; # Rock n Roll
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Weird IO::Socket problem
by thparkth (Beadle) on Jan 14, 2008 at 18:30 UTC | |
by hallikpapa (Scribe) on Jan 14, 2008 at 20:12 UTC | |
by thparkth (Beadle) on Jan 15, 2008 at 00:01 UTC | |
by quester (Vicar) on Jan 15, 2008 at 05:44 UTC | |
by hallikpapa (Scribe) on Jan 14, 2008 at 19:23 UTC | |
|
Re: Weird IO::Socket problem
by roboticus (Chancellor) on Jan 14, 2008 at 17:04 UTC | |
by hallikpapa (Scribe) on Jan 14, 2008 at 17:07 UTC | |
by roboticus (Chancellor) on Jan 14, 2008 at 17:20 UTC | |
by hallikpapa (Scribe) on Jan 14, 2008 at 17:24 UTC | |
by roboticus (Chancellor) on Jan 14, 2008 at 17:31 UTC | |
| |
|
Re: Weird IO::Socket problem
by hallikpapa (Scribe) on Jan 15, 2008 at 17:20 UTC |