1 |
#!/bin/perl |
2 |
|
3 |
#use strict; |
4 |
use strict qw ( vars refs ); |
5 |
use warnings; |
6 |
|
7 |
#sub POE::Kernel::ASSERT_DEFAULT () { 1 } |
8 |
#sub POE::Kernel::TRACE_DEFAULT () { 1 } |
9 |
#sub POE::Kernel::TRACE_SIGNALS () { 1 } |
10 |
use POE qw ( Session Wheel::Run Filter::Line Filter::Stream ); |
11 |
|
12 |
use Term::ReadKey; |
13 |
|
14 |
my $term; |
15 |
|
16 |
# ========= terminal handling session |
17 |
sub term_work { |
18 |
my ($kernel, $heap, $session) = @_[KERNEL, HEAP, SESSION ]; |
19 |
my $key = ReadKey(-1); |
20 |
#print "term_work", "\n"; |
21 |
if ($key) { |
22 |
if ($key eq "\r") { |
23 |
print "\n"; |
24 |
} |
25 |
print $key; |
26 |
} |
27 |
$kernel->post($session, "term_work"); |
28 |
#$kernel->delay("term_work", 1); |
29 |
} |
30 |
|
31 |
sub term_error { |
32 |
my ($heap, $session, $input, $param) = @_[HEAP, SESSION, ARG0, ARG1 ]; |
33 |
print "error: $input, $param", "\n"; |
34 |
} |
35 |
sub term_stdout { |
36 |
my ($heap, $input, $wheel_id) = @_[HEAP, ARG0, ARG1]; |
37 |
print "Child process in wheel $wheel_id wrote to STDOUT: $input\n"; |
38 |
} |
39 |
sub term_stderr { |
40 |
my ($heap, $session, $input) = @_[HEAP, SESSION, ARG0]; |
41 |
print "stderr: $input", "\n"; |
42 |
} |
43 |
sub term_start { |
44 |
my ($kernel, $heap, $session) = @_[KERNEL, HEAP, SESSION]; |
45 |
ReadMode 4; # Turn off controls keys |
46 |
$kernel->post($session, 'term_work'); |
47 |
} |
48 |
|
49 |
|
50 |
sub startTerminal { |
51 |
POE::Session->create( |
52 |
inline_states => { |
53 |
_start => \&term_start, |
54 |
term_work => \&term_work, |
55 |
term_error => \&term_error, |
56 |
term_stderr => \&term_stderr, |
57 |
term_stdout => \&term_stdout, |
58 |
} |
59 |
); |
60 |
} |
61 |
# ========= |
62 |
|
63 |
|
64 |
# ========= example worker session |
65 |
sub wt_start { |
66 |
my ($kernel, $session, $heap) = @_[ KERNEL, SESSION, HEAP ]; |
67 |
print "worker started", "\n"; |
68 |
$kernel->post($session, 'wt_work'); |
69 |
} |
70 |
|
71 |
sub wt_work { |
72 |
my ($kernel, $session, $heap) = @_[ KERNEL, SESSION, HEAP ]; |
73 |
print "w"; |
74 |
$kernel->delay('wt_work', 2); |
75 |
} |
76 |
|
77 |
sub startWorker { |
78 |
POE::Session->create( |
79 |
inline_states => { |
80 |
_start => \&wt_start, |
81 |
wt_work => \&wt_work, |
82 |
} |
83 |
); |
84 |
} |
85 |
# ========= |
86 |
|
87 |
|
88 |
|
89 |
# ========= main session |
90 |
sub _start { |
91 |
my ($kernel, $session, $heap) = @_[ KERNEL, SESSION, HEAP ]; |
92 |
print "main session started", "\n"; |
93 |
#my $term = |
94 |
startWorker(); |
95 |
startTerminal(); |
96 |
} |
97 |
|
98 |
sub _stop { |
99 |
my ($kernel, $session, $heap) = @_[ KERNEL, SESSION, HEAP ]; |
100 |
$kernel->signal($heap->{wheel}, "TERM"); |
101 |
delete $heap->{wheel}; |
102 |
print "main session stopped", "\n"; |
103 |
} |
104 |
|
105 |
POE::Session->create( |
106 |
inline_states => { |
107 |
_start => \&_start, |
108 |
_stop => \&_stop, |
109 |
} |
110 |
); |
111 |
# ========= |
112 |
|
113 |
$poe_kernel->run(); |
114 |
exit 0; |