--- projects/cms/source/host/ihost-perl/ihost.pl 2001/02/28 10:53:13 1.20 +++ projects/cms/source/host/ihost-perl/ihost.pl 2001/03/12 23:51:15 1.26 @@ -10,7 +10,7 @@ # - pjm2@ukc.ac.uk # # $Author: tdb $ -# $Id: ihost.pl,v 1.20 2001/02/28 10:53:13 tdb Exp $ +# $Id: ihost.pl,v 1.26 2001/03/12 23:51:15 tdb 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); @@ -86,7 +93,7 @@ sub tcp_configure() { PeerAddr => $filter_manager_addr, PeerPort => $filter_manager_port, Proto => 'tcp' - ); + ) or die "Cannot connect!"; if (!defined $sock) { print "IHOST ERROR: Could not connect to $filter_manager_addr:$filter_manager_port.\n"; print "Please check that there is an i-scream server at this address.\n"; @@ -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"; @@ -269,7 +284,7 @@ EOF PeerPort => $udp_port, PeerAddr => $filter_addr, Proto => 'udp' - ) or die "Socket: $!\n"; + ) or die "Could not send UDP: $!\n"; print $sock $xml or die "Could not send UDP packet: $!\n"; close($sock); @@ -293,7 +308,7 @@ sub send_tcp_heartbeat() { PeerAddr => $filter_addr, PeerPort => $tcp_port, Proto => 'tcp' - ); + ) or return; if (!defined $sock) { print "IHOST WARNING: Failed to deliver a heartbeat to the i-scream filter.\n"; return; @@ -304,6 +319,7 @@ sub send_tcp_heartbeat() { print $sock "HEARTBEAT\n"; $response = <$sock>; + return if (!defined $response); chop $response; if (!$response eq "OK") { close($sock); @@ -313,6 +329,7 @@ sub send_tcp_heartbeat() { print $sock "CONFIG\n"; $response = <$sock>; + return if (!defined $response); chop $response; if (!$response eq "OK") { close($sock); @@ -322,6 +339,7 @@ sub send_tcp_heartbeat() { print $sock "$file_list\n"; $response = <$sock>; + return if (!defined $response); chop $response; if (!$response eq "OK") { close($sock); @@ -331,6 +349,7 @@ sub send_tcp_heartbeat() { print $sock "$last_modified\n"; $response = <$sock>; + return if (!defined $response); chop $response; if ($response eq "ERROR") { close($sock); @@ -345,6 +364,7 @@ sub send_tcp_heartbeat() { print $sock "ENDHEARTBEAT\n"; $response = <$sock>; + return if (!defined $response); chop $response; if (!$response eq "OK") { close($sock); @@ -354,6 +374,19 @@ sub send_tcp_heartbeat() { 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; }