1 package Tak::STDIOSetup;
3 use Log::Contextual qw(:log);
4 use Log::Contextual::SimpleLogger;
5 use Tak::ConnectionService;
12 open my $stdin, '<&', \*STDIN or die "Duping stdin: $!";
13 open my $stdout, '>&', \*STDOUT or die "Duping stdout: $!";
14 $stdout->autoflush(1);
15 # if we don't re-open them then 0 and 1 get re-used - which is not
16 # only potentially bloody confusing but results in warnings like:
17 # "Filehandle STDOUT reopened as STDIN only for input"
18 close STDIN or die "Closing stdin: $!";
19 open STDIN, '<', '/dev/null' or die "Re-opening stdin: $!";
20 close STDOUT or die "Closing stdout: $!";
21 open STDOUT, '>', '/dev/null' or die "Re-opening stdout: $!";
22 my ($host, $level) = @ARGV;
23 my $sig = '<'.join ':', $host, $$.'> ';
24 Log::Contextual::set_logger(
25 Log::Contextual::SimpleLogger->new({
26 levels_upto => $level,
27 coderef => sub { print STDERR $sig, @_; }
31 my $connection = Tak::ConnectionService->new(
32 read_fh => $stdin, write_fh => $stdout,
33 listening_service => Tak::Router->new,
34 on_close => sub { $done = 1 }
36 $connection->receiver->service->register_weak(remote => $connection);
37 $0 = 'tak-stdio-node';
38 log_debug { "Node starting" };
39 # Tell the other end that we've finished messing around with file
40 # descriptors and that it's therefore safe to start sending requests.
41 print $stdout "Shere\n";
42 Tak->loop_until($done);
43 if (our $Next) { goto &$Next }