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::ReadLine; |
13 |
|
14 |
my $term; |
15 |
|
16 |
# ========= terminal handling session |
17 |
sub term_work { |
18 |
|
19 |
if (!$term) { |
20 |
$term = new Term::ReadLine 'jash'; |
21 |
} |
22 |
|
23 |
my $prompt = "#> "; |
24 |
#my $OUT = $term->OUT || STDOUT; |
25 |
|
26 |
while ( defined ($_ = $term->readline($prompt)) ) { |
27 |
#$res = eval($_), "\n"; |
28 |
|
29 |
#my $res = execCmd($_); |
30 |
my $res = "issued: $_"; |
31 |
|
32 |
#warn $@ if $@; |
33 |
#print $OUT $res, "\n" unless $@; |
34 |
|
35 |
#$res && print $OUT $res, "\n"; |
36 |
$res && print $res, "\n"; |
37 |
|
38 |
$term->addhistory($_) if /\S/; |
39 |
} |
40 |
|
41 |
} |
42 |
|
43 |
sub term_error { |
44 |
my ($heap, $session, $input, $param) = @_[HEAP, SESSION, ARG0, ARG1 ]; |
45 |
print "error: $input, $param", "\n"; |
46 |
} |
47 |
sub term_stdout { |
48 |
my ($heap, $input, $wheel_id) = @_[HEAP, ARG0, ARG1]; |
49 |
print "Child process in wheel $wheel_id wrote to STDOUT: $input\n"; |
50 |
} |
51 |
sub term_stderr { |
52 |
my ($heap, $session, $input) = @_[HEAP, SESSION, ARG0]; |
53 |
print "stderr: $input", "\n"; |
54 |
} |
55 |
sub term_start { |
56 |
my ($heap, $session) = @_[HEAP, SESSION]; |
57 |
#$program = '/usr/bin/cat -'; |
58 |
#my $program = [ 'terminal_readline.pl', '-' ]; |
59 |
|
60 |
#my $program = 'terminal_readline.pl'; |
61 |
my $program = \&term_work; |
62 |
|
63 |
$heap->{wheel} = POE::Wheel::Run->new( |
64 |
Program => $program, |
65 |
#Priority => +5, # Adjust priority. May need to be root. |
66 |
#User => getpwnam('nobody'), # Adjust UID. May need to be root. |
67 |
#Group => getgrnam('nobody'), # Adjust GID. May need to be root. |
68 |
ErrorEvent => 'term_error', # Event to emit on errors. |
69 |
|
70 |
#StdinEvent => 'stdin', # Event to emit when stdin is flushed to child. |
71 |
StdoutEvent => 'term_stdout', # Event to emit with child stdout information. |
72 |
StderrEvent => 'term_stderr', # Event to emit with child stderr information. |
73 |
|
74 |
# Identify the child process' I/O type. |
75 |
Filter => POE::Filter::Line->new(), # Or some other filter. |
76 |
#Filter => POE::Filter::Stream->new(), # Or some other filter. |
77 |
); |
78 |
|
79 |
|
80 |
#print "Unique wheel ID is : ", $wheel->ID; |
81 |
#print "Wheel's child PID is: ", $wheel->PID; |
82 |
|
83 |
# Send something to the child's STDIN. |
84 |
#$wheel->put( 'input for the child' ); |
85 |
|
86 |
# Kill the child. |
87 |
#$wheel->kill(); |
88 |
#$wheel->kill( -9 ); |
89 |
|
90 |
} |
91 |
|
92 |
|
93 |
sub startTerminal { |
94 |
POE::Session->create( |
95 |
inline_states => { |
96 |
_start => \&term_start, |
97 |
term_error => \&term_error, |
98 |
term_stderr => \&term_stderr, |
99 |
term_stdout => \&term_stdout, |
100 |
} |
101 |
); |
102 |
} |
103 |
# ========= |
104 |
|
105 |
|
106 |
# ========= example worker session |
107 |
sub wt_start { |
108 |
my ($kernel, $session, $heap) = @_[ KERNEL, SESSION, HEAP ]; |
109 |
print "worker started", "\n"; |
110 |
$kernel->post($session, 'wt_work'); |
111 |
} |
112 |
|
113 |
sub wt_work { |
114 |
my ($kernel, $session, $heap) = @_[ KERNEL, SESSION, HEAP ]; |
115 |
print "w"; |
116 |
$kernel->delay('wt_work', 2); |
117 |
} |
118 |
|
119 |
sub startWorker { |
120 |
POE::Session->create( |
121 |
inline_states => { |
122 |
_start => \&wt_start, |
123 |
wt_work => \&wt_work, |
124 |
} |
125 |
); |
126 |
} |
127 |
# ========= |
128 |
|
129 |
|
130 |
|
131 |
# ========= main session |
132 |
sub _start { |
133 |
my ($kernel, $session, $heap) = @_[ KERNEL, SESSION, HEAP ]; |
134 |
print "main session started", "\n"; |
135 |
#my $term = |
136 |
startWorker(); |
137 |
startTerminal(); |
138 |
} |
139 |
|
140 |
sub _stop { |
141 |
my ($kernel, $session, $heap) = @_[ KERNEL, SESSION, HEAP ]; |
142 |
$kernel->signal($heap->{wheel}, "TERM"); |
143 |
delete $heap->{wheel}; |
144 |
print "main session stopped", "\n"; |
145 |
} |
146 |
|
147 |
POE::Session->create( |
148 |
inline_states => { |
149 |
_start => \&_start, |
150 |
_stop => \&_stop, |
151 |
} |
152 |
); |
153 |
# ========= |
154 |
|
155 |
$poe_kernel->run(); |
156 |
exit 0; |