--- projects/cms/source/host/ihost-perl/ihost.pl 2001/02/28 12:25:48 1.21 +++ projects/cms/source/host/ihost-perl/ihost.pl 2001/03/14 18:00:16 1.27 @@ -10,7 +10,7 @@ # - pjm2@ukc.ac.uk # # $Author: pjm2 $ -# $Id: ihost.pl,v 1.21 2001/02/28 12:25:48 pjm2 Exp $ +# $Id: ihost.pl,v 1.27 2001/03/14 18:00:16 pjm2 Exp $ #------------------------------------------------------------ $| = 1; @@ -33,6 +33,7 @@ use vars qw ( $filter_addr $file_list $fqdn + $pidfile ); if (@ARGV != 2) { @@ -44,6 +45,10 @@ $filter_manager_port = $ARGV[1]; $seq_no = 1; +# write our PID to a file +$pidfile = "/var/tmp/ihost.pid"; +&write_pid(); + &tcp_configure(); &send_udp_packet(); @@ -71,6 +76,8 @@ while (1) { `sleep $delay`; } +# we'll probably never get here... +`rm -f $pidfile`; exit(0); @@ -151,9 +158,17 @@ sub tcp_configure() { print $sock "FILTER\n"; $response = <$sock>; + unless (defined($response)) { + print "Failed: Could not get a filter address from the filter manager.\n"; + exit(1); + } chop $response; $response =~ /(.*);(.*);(.*)/; ($filter_addr, $udp_port, $tcp_port) = ($1, $2, $3); + unless (defined($filter_addr) && defined($udp_port) && defined($tcp_port)) { + print "Failed: Filter address response from server did not make sense: $response\n"; + exit(1); + } print "Got filter data ($filter_addr, $udp_port, $tcp_port)\n"; @@ -296,6 +311,7 @@ sub send_tcp_heartbeat() { ) or return; if (!defined $sock) { print "IHOST WARNING: Failed to deliver a heartbeat to the i-scream filter.\n"; + &tcp_configure(); return; } @@ -304,56 +320,80 @@ sub send_tcp_heartbeat() { print $sock "HEARTBEAT\n"; $response = <$sock>; + return if (!defined $response); chop $response; if (!$response eq "OK") { close($sock); print "Server gave wrong response to HEARTBEAT: $response\n"; + &tcp_configure(); return; } print $sock "CONFIG\n"; $response = <$sock>; + return if (!defined $response); chop $response; if (!$response eq "OK") { close($sock); print "Server gave wrong response to CONFIG: $response\n"; + &tcp_configure(); return; } print $sock "$file_list\n"; $response = <$sock>; + return if (!defined $response); chop $response; if (!$response eq "OK") { close($sock); print "Server gave wrong response to file list: $response\n"; + &tcp_configure(); return; } print $sock "$last_modified\n"; $response = <$sock>; + return if (!defined $response); chop $response; if ($response eq "ERROR") { close($sock); + print "Server configuration changed. Reconfiguring with filter manager.\n"; &tcp_configure(); return; } if (!$response eq "OK") { close($sock); print "Server gave wrong response to HEARTBEAT: $response\n"; + &tcp_configure(); return; } print $sock "ENDHEARTBEAT\n"; $response = <$sock>; + return if (!defined $response); chop $response; if (!$response eq "OK") { close($sock); print "Server gave wrong response to ENDHEARTBEAT: $response\n"; + &tcp_configure(); return; } close($sock); print "^"; + + return; +} + +#----------------------------------------------------------------------- +# write_pid +# Writes the PID (process ID) of this instance to $pidfile. +# This is then used by a seperate script to check (and restart) ihost. +#----------------------------------------------------------------------- +sub write_pid() { + open PID, ">$pidfile"; + print PID $$; + close PID; return; }