use strict; use IO::Socket; use POSIX ":sys_wait_h"; use lib '/Some/path/to/modules'; use CT::Config; #still testing use Data::Dumper; my $pid = fork; exit if $pid; my $quit = 0; $SIG{INT} = $SIG{TERM} = $SIG{HUP} = sub { $quit++; }; $SIG{CHLD} = sub { while ((my $kid = waitpid(-1, WNOHANG)) > 0) {} }; # build hash of target-service socket objects my $pref_sockets = {}; my $ct = CT::Config->init(); my @envs = qw(UAT); foreach my $env ( @envs ) { my $ct_env = $ct->get_Environment( $env ); $pref_sockets->{$env} = IO::Socket::INET->new( PeerAddr => $ct_env->{_ip}, PeerPort => $ct_env->{_pref}, Proto => 'tcp', Timeout => 5, ); } # create listening socket my $l_socket = IO::Socket::INET->new( LocalPort => 32001, Listen => 10, Proto => 'tcp', Reuse => 1, Timeout => 60, ); # load a message my $msg; { local $/; open( MSG, "message" ) or die "No Message Loaded: $!\n"; $msg = ; } while ( !$quit ) { next unless my $m_socket = $l_socket->accept(); defined ( my $child = fork() ) or die "Can't Fork: $!\n"; if ( $child == 0 ) { $l_socket->close(); msocket_process( $m_socket ); exit 0; } $m_socket->close(); } sub msocket_process { my $sock = shift; my $in = <$sock>; my $resp; $pref_sockets->{$in}->send( $msg ); eval { local $SIG{ALRM} = sub { return ''; }; alarm 2; $pref_sockets->{$in}->recv( $resp, 500 ); alarm 0; }; $sock->send( $resp ) if $resp; $sock->send( 0 ) if !$resp; }