line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Process::Async::Worker; |
2
|
|
|
|
|
|
|
$Process::Async::Worker::VERSION = '0.003'; |
3
|
1
|
|
|
1
|
|
931
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
30
|
|
4
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
22
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
3
|
use parent qw(IO::Async::Notifier); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
3
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Process::Async::Worker - base class for IO::Async::Loop-using subprocess |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 VERSION |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
version 0.003 |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 DESCRIPTION |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
Provides the base class for a worker implementation. |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=cut |
21
|
|
|
|
|
|
|
|
22
|
1
|
|
|
1
|
|
522
|
use curry; |
|
1
|
|
|
|
|
171
|
|
|
1
|
|
|
|
|
268
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 METHODS |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=cut |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=head2 run |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
Subclasses must provide this method. |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub run { |
33
|
|
|
|
|
|
|
my ($self, $loop) = @_; |
34
|
|
|
|
|
|
|
$self->send_command('started'); |
35
|
|
|
|
|
|
|
$loop->add(my $ua = Net::Async::HTTP->new); |
36
|
|
|
|
|
|
|
$ua->GET('http://example.com')->get; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=cut |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head2 stdio |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
Accessor for the STDIO L. |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=cut |
46
|
|
|
|
|
|
|
|
47
|
0
|
|
|
0
|
1
|
|
sub stdio { shift->{stdio} } |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub send_command { |
50
|
0
|
|
|
0
|
0
|
|
my ($self, $cmd, @data) = @_; |
51
|
0
|
|
|
|
|
|
$self->stdio->write(join(" ", $cmd, @data) . "\n") |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head2 on_stdio_read |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Handler for incoming STDIN events. |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
By default, this extracts lines and dispatches the first word as C< cmd_$word > |
59
|
|
|
|
|
|
|
method, if available, or calls L if not found. |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
Subclasses should override this to provide custom behaviour. |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=cut |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub on_stdio_read { |
66
|
0
|
|
|
0
|
1
|
|
my ($self, $stream, $buffref, $eof) = @_; |
67
|
0
|
|
|
|
|
|
while($$buffref =~ s/^(.*)\n//) { |
68
|
0
|
|
|
|
|
|
my ($k, $data) = split ' ', $1, 2; |
69
|
0
|
0
|
|
|
|
|
if(my $method = $self->can('cmd_' . $k)) { |
|
|
0
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
$method->($self, $data); |
71
|
|
|
|
|
|
|
} elsif(my $on_command = $self->can('on_command')) { |
72
|
0
|
|
|
|
|
|
$on_command->($self, $k, $data); |
73
|
|
|
|
|
|
|
} else { |
74
|
0
|
|
|
|
|
|
$self->debug_printf("No handler for [%s]", $k); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
} |
77
|
0
|
0
|
0
|
|
|
|
$self->on_eof if $eof && $self->can('on_eof'); |
78
|
0
|
|
|
|
|
|
0 |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head2 _add_to_loop |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
Sets up an L for STDIO when we're added to the event loop. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=cut |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub _add_to_loop { |
88
|
0
|
|
|
0
|
|
|
my ($self, $loop) = @_; |
89
|
0
|
|
|
|
|
|
$self->add_child( |
90
|
|
|
|
|
|
|
$self->{stdio} = my $stdio = IO::Async::Stream->new_for_stdio( |
91
|
|
|
|
|
|
|
on_read => $self->curry::weak::on_stdio_read, |
92
|
|
|
|
|
|
|
) |
93
|
|
|
|
|
|
|
); |
94
|
0
|
|
|
|
|
|
$self->debug_printf("Worker has been added to the event loop"); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
1; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
__END__ |