Commit | Line | Data |
77bf1d9b |
1 | package Tak::STDIOSetup; |
2 | |
7b71b06e |
3 | use Log::Contextual qw(:log); |
4 | use Log::Contextual::SimpleLogger; |
77bf1d9b |
5 | use Tak::ConnectionService; |
6 | use Tak::Router; |
7 | use Tak; |
3e883325 |
8 | use IO::Handle; |
77bf1d9b |
9 | use strictures 1; |
10 | |
11 | sub run { |
7b71b06e |
12 | open my $stdin, '<&', \*STDIN or die "Duping stdin: $!"; |
13 | open my $stdout, '>&', \*STDOUT or die "Duping stdout: $!"; |
3e883325 |
14 | $stdout->autoflush(1); |
2791fd73 |
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" |
7b71b06e |
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, @_; } |
28 | }) |
29 | ); |
986f5290 |
30 | my $done; |
77bf1d9b |
31 | my $connection = Tak::ConnectionService->new( |
32 | read_fh => $stdin, write_fh => $stdout, |
986f5290 |
33 | listening_service => Tak::Router->new, |
34 | on_close => sub { $done = 1 } |
77bf1d9b |
35 | ); |
2791fd73 |
36 | $connection->receiver->service->register_weak(remote => $connection); |
a2a6ec17 |
37 | $0 = 'tak-stdio-node'; |
7b71b06e |
38 | log_debug { "Node starting" }; |
9df46eb8 |
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 "Ssyshere\n"; |
986f5290 |
42 | Tak->loop_until($done); |
a2a6ec17 |
43 | if (our $Next) { goto &$Next } |
77bf1d9b |
44 | } |
45 | |
46 | 1; |