Book HomeMastering Perl/TkSearch this book

19.4. The IPADM Helper, ipadmh

The helper completes the communications link between ipadm and ipadmd by opening a socket. By far the easiest way of doing this is to use the IO::Socket module. ipadm provides the @ARGV values for the PeerAddr and PeerPort parameters, which specify the remote machine and port to contact. The connect status is piped to the Perl/Tk client by writing a message to STDOUT.

The helper then enters its main loop, transferring client IPADM commands over the socket to the daemon and piping responses back.

Notice that the helper is free to use signals, in particular SIGALRM, so it can timeout network reads and inform the client.

sub timeout {print "1 Socket Timeout\n$EOFn"; $SIG{ALRM} = \&timeout}
$SIG{PIPE} = sub {print "2 Pipe Error.\n$EOF\n"};

my $sock = IO::Socket::INET->new( PeerAddr => $ARGV[0],
                  Proto => 'tcp', PeerPort => $ARGV[1]);
print +((defined $sock) ? "0 Connect OK" : "3 Connect Failed"), "\n$EOF\n";

while (1) {

    while(<STDIN>) {
        print $sock $_;         # send parent's command/data to daemon
        last if /^$EOF$/;
    }

    my(@data) = ( );
    $SIG{ALRM} = \&timeout;     # reset handler
    alarm 60;

    while (<$sock>) {
        push @data, $_;         # accumulate command's reply
        last if /^$EOF$/;
    }

    alarm 0;
    print (/^$EOF$/ ? @data : "4 Daemon Failure\n$EOF\n");

} # whilend


Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.