| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# You may distribute under the terms of either the GNU General Public License |
|
2
|
|
|
|
|
|
|
# or the Artistic License (the same terms as Perl itself) |
|
3
|
|
|
|
|
|
|
# |
|
4
|
|
|
|
|
|
|
# (C) Paul Evans, 2006-2015 -- leonerd@leonerd.org.uk |
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package IO::Async::Notifier; |
|
7
|
|
|
|
|
|
|
|
|
8
|
85
|
|
|
85
|
|
293336
|
use strict; |
|
|
85
|
|
|
|
|
230
|
|
|
|
85
|
|
|
|
|
2619
|
|
|
9
|
85
|
|
|
85
|
|
516
|
use warnings; |
|
|
85
|
|
|
|
|
175
|
|
|
|
85
|
|
|
|
|
4001
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.801'; |
|
12
|
|
|
|
|
|
|
|
|
13
|
85
|
|
|
85
|
|
561
|
use Carp; |
|
|
85
|
|
|
|
|
224
|
|
|
|
85
|
|
|
|
|
7586
|
|
|
14
|
85
|
|
|
85
|
|
654
|
use Scalar::Util qw( weaken ); |
|
|
85
|
|
|
|
|
209
|
|
|
|
85
|
|
|
|
|
5718
|
|
|
15
|
|
|
|
|
|
|
|
|
16
|
85
|
|
|
85
|
|
51224
|
use Future 0.26; # ->is_failed |
|
|
85
|
|
|
|
|
870657
|
|
|
|
85
|
|
|
|
|
2729
|
|
|
17
|
|
|
|
|
|
|
|
|
18
|
85
|
|
|
85
|
|
39693
|
use IO::Async::Debug; |
|
|
85
|
|
|
|
|
307
|
|
|
|
85
|
|
|
|
|
3874
|
|
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Perl 5.8.4 cannot do trampolines by modiying @_ then goto &$code |
|
21
|
85
|
|
|
85
|
|
596
|
use constant HAS_BROKEN_TRAMPOLINES => ( $] == "5.008004" ); |
|
|
85
|
|
|
|
|
191
|
|
|
|
85
|
|
|
|
|
205983
|
|
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 NAME |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
C - base class for L event objects |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
Usually not directly used by a program, but one valid use case may be: |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
use IO::Async::Notifier; |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
use IO::Async::Stream; |
|
34
|
|
|
|
|
|
|
use IO::Async::Signal; |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
use IO::Async::Loop; |
|
37
|
|
|
|
|
|
|
my $loop = IO::Async::Loop->new; |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
my $notifier = IO::Async::Notifier->new; |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
$notifier->add_child( |
|
42
|
|
|
|
|
|
|
IO::Async::Stream->new_for_stdin( |
|
43
|
|
|
|
|
|
|
on_read => sub { |
|
44
|
|
|
|
|
|
|
my $self = shift; |
|
45
|
|
|
|
|
|
|
my ( $buffref, $eof ) = @_; |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
while( $$buffref =~ s/^(.*)\n// ) { |
|
48
|
|
|
|
|
|
|
print "You said $1\n"; |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
return 0; |
|
52
|
|
|
|
|
|
|
}, |
|
53
|
|
|
|
|
|
|
) |
|
54
|
|
|
|
|
|
|
); |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
$notifier->add_child( |
|
57
|
|
|
|
|
|
|
IO::Async::Signal->new( |
|
58
|
|
|
|
|
|
|
name => 'INT', |
|
59
|
|
|
|
|
|
|
on_receipt => sub { |
|
60
|
|
|
|
|
|
|
print "Goodbye!\n"; |
|
61
|
|
|
|
|
|
|
$loop->stop; |
|
62
|
|
|
|
|
|
|
}, |
|
63
|
|
|
|
|
|
|
) |
|
64
|
|
|
|
|
|
|
); |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
$loop->add( $notifier ); |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
$loop->run; |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
This object class forms the basis for all the other event objects that an |
|
73
|
|
|
|
|
|
|
L program uses. It provides the lowest level of integration with a |
|
74
|
|
|
|
|
|
|
L container, and a facility to collect Notifiers together, in |
|
75
|
|
|
|
|
|
|
a tree structure, where any Notifier can contain a collection of children. |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
Normally, objects in this class would not be directly used by an end program, |
|
78
|
|
|
|
|
|
|
as it performs no actual IO work, and generates no actual events. These are all |
|
79
|
|
|
|
|
|
|
left to the various subclasses, such as: |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=over 4 |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=item * |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
L - event callbacks for a non-blocking file descriptor |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=item * |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
L - event callbacks and write bufering for a stream |
|
90
|
|
|
|
|
|
|
filehandle |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=item * |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
L - event callbacks and send buffering for a socket |
|
95
|
|
|
|
|
|
|
filehandle |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=item * |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
L - base class for Notifiers that use timed delays |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=item * |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
L - event callback on receipt of a POSIX signal |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=item * |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
L - event callback on exit of a child process |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=item * |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
L - start and manage a child process |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=back |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
For more detail, see the SYNOPSIS section in one of the above. |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
One case where this object class would be used, is when a library wishes to |
|
118
|
|
|
|
|
|
|
provide a sub-component which consists of multiple other C |
|
119
|
|
|
|
|
|
|
subclasses, such as Cs and C, but no particular object is |
|
120
|
|
|
|
|
|
|
suitable to be the root of a tree. In this case, a plain C object |
|
121
|
|
|
|
|
|
|
can be used as the tree root, and all the other notifiers added as children of |
|
122
|
|
|
|
|
|
|
it. |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=cut |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 AS A MIXIN |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
Rather than being used as a subclass this package also supports being used as |
|
129
|
|
|
|
|
|
|
a non-principle superclass for an object, as a mix-in. It still provides |
|
130
|
|
|
|
|
|
|
methods and satisfies an C test, even though the constructor is not |
|
131
|
|
|
|
|
|
|
directly called. This simply requires that the object be based on a normal |
|
132
|
|
|
|
|
|
|
blessed hash reference and include C somewhere in its |
|
133
|
|
|
|
|
|
|
C<@ISA> list. |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
The methods in this class all use only keys in the hash prefixed by |
|
136
|
|
|
|
|
|
|
C<"IO_Async_Notifier__"> for namespace purposes. |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
This is intended mainly for defining a subclass of some other object that is |
|
139
|
|
|
|
|
|
|
also an C, suitable to be added to an L. |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
package SomeEventSource::Async; |
|
142
|
|
|
|
|
|
|
use base qw( SomeEventSource IO::Async::Notifier ); |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub _add_to_loop |
|
145
|
|
|
|
|
|
|
{ |
|
146
|
|
|
|
|
|
|
my $self = shift; |
|
147
|
|
|
|
|
|
|
my ( $loop ) = @_; |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
# Code here to set up event handling on $loop that may be required |
|
150
|
|
|
|
|
|
|
} |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub _remove_from_loop |
|
153
|
|
|
|
|
|
|
{ |
|
154
|
|
|
|
|
|
|
my $self = shift; |
|
155
|
|
|
|
|
|
|
my ( $loop ) = @_; |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
# Code here to undo the event handling set up above |
|
158
|
|
|
|
|
|
|
} |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Since all the methods documented here will be available, the implementation |
|
161
|
|
|
|
|
|
|
may wish to use the C and C or C |
|
162
|
|
|
|
|
|
|
methods to implement its own event callbacks. |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=cut |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 EVENTS |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
The following events are invoked, either using subclass methods or CODE |
|
169
|
|
|
|
|
|
|
references in parameters: |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head2 on_error $message, $name, @details |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
Invoked by C. |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=cut |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head1 PARAMETERS |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
A specific subclass of C defines named parameters that |
|
180
|
|
|
|
|
|
|
control its behaviour. These may be passed to the C constructor, or to |
|
181
|
|
|
|
|
|
|
the C method. The documentation on each specific subclass will give |
|
182
|
|
|
|
|
|
|
details on the parameters that exist, and their uses. Some parameters may only |
|
183
|
|
|
|
|
|
|
support being set once at construction time, or only support being changed if |
|
184
|
|
|
|
|
|
|
the object is in a particular state. |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
The following parameters are supported by all Notifiers: |
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=over 8 |
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=item on_error => CODE |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
CODE reference for event handler. |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=item notifier_name => STRING |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
Optional string used to identify this particular Notifier. This value will be |
|
197
|
|
|
|
|
|
|
returned by the C method. |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=back |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=cut |
|
202
|
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=head1 CONSTRUCTOR |
|
204
|
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=cut |
|
206
|
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head2 new |
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
$notifier = IO::Async::Notifier->new( %params ) |
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
This function returns a new instance of a C object with |
|
212
|
|
|
|
|
|
|
the given initial values of the named parameters. |
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
Up until L version 0.19, this module used to implement the IO |
|
215
|
|
|
|
|
|
|
handle features now found in the L subclass. Code that |
|
216
|
|
|
|
|
|
|
needs to use any of C, C, C, |
|
217
|
|
|
|
|
|
|
C or C should use L instead. |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=cut |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
sub new |
|
222
|
|
|
|
|
|
|
{ |
|
223
|
1419
|
|
|
1419
|
1
|
89452
|
my $class = shift; |
|
224
|
1419
|
|
|
|
|
15922
|
my %params = @_; |
|
225
|
|
|
|
|
|
|
|
|
226
|
1419
|
|
|
|
|
8564
|
my $self = bless {}, $class; |
|
227
|
|
|
|
|
|
|
|
|
228
|
1419
|
|
|
|
|
16419
|
$self->_init( \%params ); |
|
229
|
|
|
|
|
|
|
|
|
230
|
1419
|
|
|
|
|
11772
|
$self->configure( %params ); |
|
231
|
|
|
|
|
|
|
|
|
232
|
1418
|
|
|
|
|
13143
|
return $self; |
|
233
|
|
|
|
|
|
|
} |
|
234
|
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=head1 METHODS |
|
236
|
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=cut |
|
238
|
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
=head2 configure |
|
240
|
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
$notifier->configure( %params ) |
|
242
|
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
Adjust the named parameters of the C as given by the C<%params> |
|
244
|
|
|
|
|
|
|
hash. |
|
245
|
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
=cut |
|
247
|
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
# for subclasses to override and call down to |
|
249
|
|
|
|
|
|
|
sub configure |
|
250
|
|
|
|
|
|
|
{ |
|
251
|
1762
|
|
|
1762
|
1
|
6129
|
my $self = shift; |
|
252
|
1762
|
|
|
|
|
6230
|
my %params = @_; |
|
253
|
|
|
|
|
|
|
|
|
254
|
1762
|
|
|
|
|
4132
|
foreach (qw( notifier_name on_error )) { |
|
255
|
3524
|
100
|
|
|
|
14643
|
$self->{"IO_Async_Notifier__$_"} = delete $params{$_} if exists $params{$_}; |
|
256
|
|
|
|
|
|
|
} |
|
257
|
|
|
|
|
|
|
|
|
258
|
1762
|
100
|
|
|
|
6702
|
$self->configure_unknown( %params ) if keys %params; |
|
259
|
|
|
|
|
|
|
} |
|
260
|
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
sub configure_unknown |
|
262
|
|
|
|
|
|
|
{ |
|
263
|
1
|
|
|
1
|
1
|
4
|
my $self = shift; |
|
264
|
1
|
|
|
|
|
3
|
my %params = @_; |
|
265
|
|
|
|
|
|
|
|
|
266
|
1
|
|
|
|
|
3
|
my $class = ref $self; |
|
267
|
1
|
|
|
|
|
1218
|
croak "Unrecognised configuration keys for $class - " . join( " ", keys %params ); |
|
268
|
|
|
|
|
|
|
} |
|
269
|
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
=head2 loop |
|
271
|
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
$loop = $notifier->loop |
|
273
|
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
Returns the L that this Notifier is a member of. |
|
275
|
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
=cut |
|
277
|
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
sub loop |
|
279
|
|
|
|
|
|
|
{ |
|
280
|
20340
|
|
|
20340
|
1
|
42151
|
my $self = shift; |
|
281
|
|
|
|
|
|
|
return $self->{IO_Async_Notifier__loop} |
|
282
|
20340
|
|
|
|
|
72129
|
} |
|
283
|
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
*get_loop = \&loop; |
|
285
|
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
# Only called by IO::Async::Loop, not external interface |
|
287
|
|
|
|
|
|
|
sub __set_loop |
|
288
|
|
|
|
|
|
|
{ |
|
289
|
3123
|
|
|
3123
|
|
5648
|
my $self = shift; |
|
290
|
3123
|
|
|
|
|
5312
|
my ( $loop ) = @_; |
|
291
|
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
# early exit if no change |
|
293
|
3123
|
100
|
66
|
|
|
19111
|
return if !$loop and !$self->loop or |
|
|
|
|
100
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
294
|
|
|
|
|
|
|
$loop and $self->loop and $loop == $self->loop; |
|
295
|
|
|
|
|
|
|
|
|
296
|
2557
|
100
|
|
|
|
5051
|
$self->_remove_from_loop( $self->loop ) if $self->loop; |
|
297
|
|
|
|
|
|
|
|
|
298
|
2557
|
|
|
|
|
8896
|
$self->{IO_Async_Notifier__loop} = $loop; |
|
299
|
2557
|
|
|
|
|
10103
|
weaken( $self->{IO_Async_Notifier__loop} ); # To avoid a cycle |
|
300
|
|
|
|
|
|
|
|
|
301
|
2557
|
100
|
|
|
|
5012
|
$self->_add_to_loop( $self->loop ) if $self->loop; |
|
302
|
|
|
|
|
|
|
} |
|
303
|
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
=head2 notifier_name |
|
305
|
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
$name = $notifier->notifier_name |
|
307
|
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
Returns the name to identify this Notifier. If a has not been set, it will |
|
309
|
|
|
|
|
|
|
return the empty string. Subclasses may wish to override this behaviour to |
|
310
|
|
|
|
|
|
|
return some more useful information, perhaps from configured parameters. |
|
311
|
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
=cut |
|
313
|
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
sub notifier_name |
|
315
|
|
|
|
|
|
|
{ |
|
316
|
50
|
|
|
50
|
1
|
1814
|
my $self = shift; |
|
317
|
50
|
|
100
|
|
|
491
|
return $self->{IO_Async_Notifier__notifier_name} || ""; |
|
318
|
|
|
|
|
|
|
} |
|
319
|
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
=head2 adopt_future |
|
321
|
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
$f = $notifier->adopt_future( $f ) |
|
323
|
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
Stores a reference to the L instance within the notifier itself, so |
|
325
|
|
|
|
|
|
|
the reference doesn't get lost. This reference will be dropped when the future |
|
326
|
|
|
|
|
|
|
becomes ready (either by success or failure). Additionally, if the future |
|
327
|
|
|
|
|
|
|
failed the notifier's C method will be informed. |
|
328
|
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
This means that if the notifier does not provide an C handler, nor |
|
330
|
|
|
|
|
|
|
is there one anywhere in the parent chain, this will be fatal to the caller of |
|
331
|
|
|
|
|
|
|
C<< $f->fail >>. To avoid this being fatal if the failure is handled |
|
332
|
|
|
|
|
|
|
elsewhere, use the C method on the future to obtain a sequence one |
|
333
|
|
|
|
|
|
|
that never fails. |
|
334
|
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
$notifier->adopt_future( $f->else_done() ) |
|
336
|
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
The future itself is returned. |
|
338
|
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
=cut |
|
340
|
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
sub adopt_future |
|
342
|
|
|
|
|
|
|
{ |
|
343
|
58
|
|
|
58
|
1
|
4551
|
my $self = shift; |
|
344
|
58
|
|
|
|
|
126
|
my ( $f ) = @_; |
|
345
|
|
|
|
|
|
|
|
|
346
|
58
|
|
|
|
|
176
|
my $fkey = "$f"; # stable stringification |
|
347
|
|
|
|
|
|
|
|
|
348
|
58
|
|
|
|
|
456
|
$self->{IO_Async_Notifier__futures}{$fkey} = $f; |
|
349
|
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
$f->on_ready( $self->_capture_weakself( sub { |
|
351
|
58
|
|
|
58
|
|
154
|
my $self = shift; |
|
352
|
58
|
|
|
|
|
157
|
my ( $f ) = @_; |
|
353
|
|
|
|
|
|
|
|
|
354
|
58
|
|
|
|
|
245
|
delete $self->{IO_Async_Notifier__futures}{$fkey}; |
|
355
|
|
|
|
|
|
|
|
|
356
|
58
|
100
|
|
|
|
281
|
$self->invoke_error( $f->failure ) if $f->is_failed; |
|
357
|
58
|
|
|
|
|
765
|
})); |
|
358
|
|
|
|
|
|
|
|
|
359
|
58
|
|
|
|
|
1357
|
return $f; |
|
360
|
|
|
|
|
|
|
} |
|
361
|
|
|
|
|
|
|
|
|
362
|
|
|
|
|
|
|
=head2 adopted_futures |
|
363
|
|
|
|
|
|
|
|
|
364
|
|
|
|
|
|
|
@f = $notifier->adopted_futures |
|
365
|
|
|
|
|
|
|
|
|
366
|
|
|
|
|
|
|
I |
|
367
|
|
|
|
|
|
|
|
|
368
|
|
|
|
|
|
|
Returns a list of all the adopted and still-pending futures, in no particular |
|
369
|
|
|
|
|
|
|
order. |
|
370
|
|
|
|
|
|
|
|
|
371
|
|
|
|
|
|
|
=cut |
|
372
|
|
|
|
|
|
|
|
|
373
|
|
|
|
|
|
|
sub adopted_futures |
|
374
|
|
|
|
|
|
|
{ |
|
375
|
3
|
|
|
3
|
1
|
26
|
my $self = shift; |
|
376
|
|
|
|
|
|
|
|
|
377
|
3
|
|
|
|
|
5
|
return values %{ $self->{IO_Async_Notifier__futures} }; |
|
|
3
|
|
|
|
|
20
|
|
|
378
|
|
|
|
|
|
|
} |
|
379
|
|
|
|
|
|
|
|
|
380
|
|
|
|
|
|
|
=head1 CHILD NOTIFIERS |
|
381
|
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
During the execution of a program, it may be the case that certain IO handles |
|
383
|
|
|
|
|
|
|
cause other handles to be created; for example, new sockets that have been |
|
384
|
|
|
|
|
|
|
Ced from a listening socket. To facilitate these, a notifier may |
|
385
|
|
|
|
|
|
|
contain child notifier objects, that are automatically added to or removed |
|
386
|
|
|
|
|
|
|
from the L that manages their parent. |
|
387
|
|
|
|
|
|
|
|
|
388
|
|
|
|
|
|
|
=cut |
|
389
|
|
|
|
|
|
|
|
|
390
|
|
|
|
|
|
|
=head2 parent |
|
391
|
|
|
|
|
|
|
|
|
392
|
|
|
|
|
|
|
$parent = $notifier->parent |
|
393
|
|
|
|
|
|
|
|
|
394
|
|
|
|
|
|
|
Returns the parent of the notifier, or C if does not have one. |
|
395
|
|
|
|
|
|
|
|
|
396
|
|
|
|
|
|
|
=cut |
|
397
|
|
|
|
|
|
|
|
|
398
|
|
|
|
|
|
|
sub parent |
|
399
|
|
|
|
|
|
|
{ |
|
400
|
3320
|
|
|
3320
|
1
|
5650
|
my $self = shift; |
|
401
|
3320
|
|
|
|
|
11228
|
return $self->{IO_Async_Notifier__parent}; |
|
402
|
|
|
|
|
|
|
} |
|
403
|
|
|
|
|
|
|
|
|
404
|
|
|
|
|
|
|
=head2 children |
|
405
|
|
|
|
|
|
|
|
|
406
|
|
|
|
|
|
|
@children = $notifier->children |
|
407
|
|
|
|
|
|
|
|
|
408
|
|
|
|
|
|
|
Returns a list of the child notifiers contained within this one. |
|
409
|
|
|
|
|
|
|
|
|
410
|
|
|
|
|
|
|
=cut |
|
411
|
|
|
|
|
|
|
|
|
412
|
|
|
|
|
|
|
sub children |
|
413
|
|
|
|
|
|
|
{ |
|
414
|
3092
|
|
|
3092
|
1
|
5579
|
my $self = shift; |
|
415
|
3092
|
100
|
|
|
|
10400
|
return unless $self->{IO_Async_Notifier__children}; |
|
416
|
664
|
|
|
|
|
1125
|
return @{ $self->{IO_Async_Notifier__children} }; |
|
|
664
|
|
|
|
|
4873
|
|
|
417
|
|
|
|
|
|
|
} |
|
418
|
|
|
|
|
|
|
|
|
419
|
|
|
|
|
|
|
=head2 add_child |
|
420
|
|
|
|
|
|
|
|
|
421
|
|
|
|
|
|
|
$notifier->add_child( $child ) |
|
422
|
|
|
|
|
|
|
|
|
423
|
|
|
|
|
|
|
Adds a child notifier. This notifier will be added to the containing loop, if |
|
424
|
|
|
|
|
|
|
the parent has one. Only a notifier that does not currently have a parent and |
|
425
|
|
|
|
|
|
|
is not currently a member of any loop may be added as a child. If the child |
|
426
|
|
|
|
|
|
|
itself has grandchildren, these will be recursively added to the containing |
|
427
|
|
|
|
|
|
|
loop. |
|
428
|
|
|
|
|
|
|
|
|
429
|
|
|
|
|
|
|
=cut |
|
430
|
|
|
|
|
|
|
|
|
431
|
|
|
|
|
|
|
sub add_child |
|
432
|
|
|
|
|
|
|
{ |
|
433
|
649
|
|
|
649
|
1
|
2030
|
my $self = shift; |
|
434
|
649
|
|
|
|
|
1281
|
my ( $child ) = @_; |
|
435
|
|
|
|
|
|
|
|
|
436
|
649
|
100
|
|
|
|
1886
|
croak "Cannot add a child that already has a parent" if defined $child->{IO_Async_Notifier__parent}; |
|
437
|
|
|
|
|
|
|
|
|
438
|
648
|
50
|
|
|
|
1499
|
croak "Cannot add a child that is already a member of a loop" if defined $child->loop; |
|
439
|
|
|
|
|
|
|
|
|
440
|
648
|
100
|
|
|
|
1370
|
if( defined( my $loop = $self->loop ) ) { |
|
441
|
626
|
|
|
|
|
2201
|
$loop->add( $child ); |
|
442
|
|
|
|
|
|
|
} |
|
443
|
|
|
|
|
|
|
|
|
444
|
643
|
|
|
|
|
1031
|
push @{ $self->{IO_Async_Notifier__children} }, $child; |
|
|
643
|
|
|
|
|
3070
|
|
|
445
|
643
|
|
|
|
|
2364
|
$child->{IO_Async_Notifier__parent} = $self; |
|
446
|
643
|
|
|
|
|
2543
|
weaken( $child->{IO_Async_Notifier__parent} ); |
|
447
|
|
|
|
|
|
|
|
|
448
|
643
|
|
|
|
|
1795
|
return; |
|
449
|
|
|
|
|
|
|
} |
|
450
|
|
|
|
|
|
|
|
|
451
|
|
|
|
|
|
|
=head2 remove_child |
|
452
|
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
$notifier->remove_child( $child ) |
|
454
|
|
|
|
|
|
|
|
|
455
|
|
|
|
|
|
|
Removes a child notifier. The child will be removed from the containing loop, |
|
456
|
|
|
|
|
|
|
if the parent has one. If the child itself has grandchildren, these will be |
|
457
|
|
|
|
|
|
|
recurively removed from the loop. |
|
458
|
|
|
|
|
|
|
|
|
459
|
|
|
|
|
|
|
=cut |
|
460
|
|
|
|
|
|
|
|
|
461
|
|
|
|
|
|
|
sub remove_child |
|
462
|
|
|
|
|
|
|
{ |
|
463
|
358
|
|
|
358
|
1
|
984
|
my $self = shift; |
|
464
|
358
|
|
|
|
|
787
|
my ( $child ) = @_; |
|
465
|
|
|
|
|
|
|
|
|
466
|
|
|
|
|
|
|
LOOP: { |
|
467
|
358
|
|
|
|
|
582
|
my $childrenref = $self->{IO_Async_Notifier__children}; |
|
|
358
|
|
|
|
|
897
|
|
|
468
|
358
|
|
|
|
|
2251
|
for my $i ( 0 .. $#$childrenref ) { |
|
469
|
420
|
100
|
|
|
|
1625
|
next unless $childrenref->[$i] == $child; |
|
470
|
358
|
|
|
|
|
1076
|
splice @$childrenref, $i, 1, (); |
|
471
|
358
|
|
|
|
|
984
|
last LOOP; |
|
472
|
|
|
|
|
|
|
} |
|
473
|
|
|
|
|
|
|
|
|
474
|
0
|
|
|
|
|
0
|
croak "Cannot remove child from a parent that doesn't contain it"; |
|
475
|
|
|
|
|
|
|
} |
|
476
|
|
|
|
|
|
|
|
|
477
|
358
|
|
|
|
|
921
|
undef $child->{IO_Async_Notifier__parent}; |
|
478
|
|
|
|
|
|
|
|
|
479
|
358
|
100
|
|
|
|
994
|
if( defined( my $loop = $self->loop ) ) { |
|
480
|
263
|
|
|
|
|
1641
|
$loop->remove( $child ); |
|
481
|
|
|
|
|
|
|
} |
|
482
|
|
|
|
|
|
|
} |
|
483
|
|
|
|
|
|
|
|
|
484
|
|
|
|
|
|
|
=head2 remove_from_parent |
|
485
|
|
|
|
|
|
|
|
|
486
|
|
|
|
|
|
|
$notifier->remove_from_parent |
|
487
|
|
|
|
|
|
|
|
|
488
|
|
|
|
|
|
|
Removes this notifier object from its parent (either another notifier object |
|
489
|
|
|
|
|
|
|
or the containing loop) if it has one. If the notifier is not a child of |
|
490
|
|
|
|
|
|
|
another notifier nor a member of a loop, this method does nothing. |
|
491
|
|
|
|
|
|
|
|
|
492
|
|
|
|
|
|
|
=cut |
|
493
|
|
|
|
|
|
|
|
|
494
|
|
|
|
|
|
|
sub remove_from_parent |
|
495
|
|
|
|
|
|
|
{ |
|
496
|
804
|
|
|
804
|
1
|
1669
|
my $self = shift; |
|
497
|
|
|
|
|
|
|
|
|
498
|
804
|
100
|
|
|
|
2192
|
if( my $parent = $self->parent ) { |
|
|
|
100
|
|
|
|
|
|
|
499
|
305
|
|
|
|
|
1540
|
$parent->remove_child( $self ); |
|
500
|
|
|
|
|
|
|
} |
|
501
|
|
|
|
|
|
|
elsif( my $loop = $self->loop ) { |
|
502
|
476
|
|
|
|
|
2164
|
$loop->remove( $self ); |
|
503
|
|
|
|
|
|
|
} |
|
504
|
|
|
|
|
|
|
} |
|
505
|
|
|
|
|
|
|
|
|
506
|
|
|
|
|
|
|
=head1 SUBCLASS METHODS |
|
507
|
|
|
|
|
|
|
|
|
508
|
|
|
|
|
|
|
C is a base class provided so that specific subclasses of |
|
509
|
|
|
|
|
|
|
it provide more specific behaviour. The base class provides a number of |
|
510
|
|
|
|
|
|
|
methods that subclasses may wish to override. |
|
511
|
|
|
|
|
|
|
|
|
512
|
|
|
|
|
|
|
If a subclass implements any of these, be sure to invoke the superclass method |
|
513
|
|
|
|
|
|
|
at some point within the code. |
|
514
|
|
|
|
|
|
|
|
|
515
|
|
|
|
|
|
|
=cut |
|
516
|
|
|
|
|
|
|
|
|
517
|
|
|
|
|
|
|
=head2 _init |
|
518
|
|
|
|
|
|
|
|
|
519
|
|
|
|
|
|
|
$notifier->_init( $paramsref ) |
|
520
|
|
|
|
|
|
|
|
|
521
|
|
|
|
|
|
|
This method is called by the constructor just before calling C. |
|
522
|
|
|
|
|
|
|
It is passed a reference to the HASH storing the constructor arguments. |
|
523
|
|
|
|
|
|
|
|
|
524
|
|
|
|
|
|
|
This method may initialise internal details of the Notifier as required, |
|
525
|
|
|
|
|
|
|
possibly by using parameters from the HASH. If any parameters are |
|
526
|
|
|
|
|
|
|
construction-only they should be Cd from the hash. |
|
527
|
|
|
|
|
|
|
|
|
528
|
|
|
|
|
|
|
=cut |
|
529
|
|
|
|
|
|
|
|
|
530
|
|
|
|
|
|
|
sub _init |
|
531
|
|
|
|
707
|
|
|
{ |
|
532
|
|
|
|
|
|
|
# empty default |
|
533
|
|
|
|
|
|
|
} |
|
534
|
|
|
|
|
|
|
|
|
535
|
|
|
|
|
|
|
=head2 configure |
|
536
|
|
|
|
|
|
|
|
|
537
|
|
|
|
|
|
|
$notifier->configure( %params ) |
|
538
|
|
|
|
|
|
|
|
|
539
|
|
|
|
|
|
|
This method is called by the constructor to set the initial values of named |
|
540
|
|
|
|
|
|
|
parameters, and by users of the object to adjust the values once constructed. |
|
541
|
|
|
|
|
|
|
|
|
542
|
|
|
|
|
|
|
This method should C from the C<%params> hash any keys it has dealt |
|
543
|
|
|
|
|
|
|
with, then pass the remaining ones to the C. The base |
|
544
|
|
|
|
|
|
|
class implementation will throw an exception if there are any unrecognised |
|
545
|
|
|
|
|
|
|
keys remaining. |
|
546
|
|
|
|
|
|
|
|
|
547
|
|
|
|
|
|
|
=cut |
|
548
|
|
|
|
|
|
|
|
|
549
|
|
|
|
|
|
|
=head2 configure_unknown |
|
550
|
|
|
|
|
|
|
|
|
551
|
|
|
|
|
|
|
$notifier->configure_unknown( %params ) |
|
552
|
|
|
|
|
|
|
|
|
553
|
|
|
|
|
|
|
This method is called by the base class C method, for any remaining |
|
554
|
|
|
|
|
|
|
parameters that are not recognised. The default implementation throws an |
|
555
|
|
|
|
|
|
|
exception using C that lists the unrecognised keys. This method is |
|
556
|
|
|
|
|
|
|
provided to allow subclasses to override the behaviour, perhaps to store |
|
557
|
|
|
|
|
|
|
unrecognised keys, or to otherwise inspect the left-over arguments for some |
|
558
|
|
|
|
|
|
|
other purpose. |
|
559
|
|
|
|
|
|
|
|
|
560
|
|
|
|
|
|
|
=cut |
|
561
|
|
|
|
|
|
|
|
|
562
|
|
|
|
|
|
|
=head2 _add_to_loop |
|
563
|
|
|
|
|
|
|
|
|
564
|
|
|
|
|
|
|
$notifier->_add_to_loop( $loop ) |
|
565
|
|
|
|
|
|
|
|
|
566
|
|
|
|
|
|
|
This method is called when the Notifier has been added to a Loop; either |
|
567
|
|
|
|
|
|
|
directly, or indirectly through being a child of a Notifer already in a loop. |
|
568
|
|
|
|
|
|
|
|
|
569
|
|
|
|
|
|
|
This method may be used to perform any initial startup activity required for |
|
570
|
|
|
|
|
|
|
the Notifier to be fully functional but which requires a Loop to do so. |
|
571
|
|
|
|
|
|
|
|
|
572
|
|
|
|
|
|
|
=cut |
|
573
|
|
|
|
|
|
|
|
|
574
|
|
|
|
|
|
|
sub _add_to_loop |
|
575
|
|
|
|
514
|
|
|
{ |
|
576
|
|
|
|
|
|
|
# empty default |
|
577
|
|
|
|
|
|
|
} |
|
578
|
|
|
|
|
|
|
|
|
579
|
|
|
|
|
|
|
=head2 _remove_from_loop |
|
580
|
|
|
|
|
|
|
|
|
581
|
|
|
|
|
|
|
$notifier->_remove_from_loop( $loop ) |
|
582
|
|
|
|
|
|
|
|
|
583
|
|
|
|
|
|
|
This method is called when the Notifier has been removed from a Loop; either |
|
584
|
|
|
|
|
|
|
directly, or indirectly through being a child of a Notifier removed from the |
|
585
|
|
|
|
|
|
|
loop. |
|
586
|
|
|
|
|
|
|
|
|
587
|
|
|
|
|
|
|
This method may be used to undo the effects of any setup that the |
|
588
|
|
|
|
|
|
|
C<_add_to_loop> method had originally done. |
|
589
|
|
|
|
|
|
|
|
|
590
|
|
|
|
|
|
|
=cut |
|
591
|
|
|
|
|
|
|
|
|
592
|
|
|
|
|
|
|
sub _remove_from_loop |
|
593
|
|
|
|
454
|
|
|
{ |
|
594
|
|
|
|
|
|
|
# empty default |
|
595
|
|
|
|
|
|
|
} |
|
596
|
|
|
|
|
|
|
|
|
597
|
|
|
|
|
|
|
=head1 UTILITY METHODS |
|
598
|
|
|
|
|
|
|
|
|
599
|
|
|
|
|
|
|
=cut |
|
600
|
|
|
|
|
|
|
|
|
601
|
|
|
|
|
|
|
=head2 _capture_weakself |
|
602
|
|
|
|
|
|
|
|
|
603
|
|
|
|
|
|
|
$mref = $notifier->_capture_weakself( $code ) |
|
604
|
|
|
|
|
|
|
|
|
605
|
|
|
|
|
|
|
Returns a new CODE ref which, when invoked, will invoke the originally-passed |
|
606
|
|
|
|
|
|
|
ref, with additionally a reference to the Notifier as its first argument. The |
|
607
|
|
|
|
|
|
|
Notifier reference is stored weakly in C<$mref>, so this CODE ref may be |
|
608
|
|
|
|
|
|
|
stored in the Notifier itself without creating a cycle. |
|
609
|
|
|
|
|
|
|
|
|
610
|
|
|
|
|
|
|
For example, |
|
611
|
|
|
|
|
|
|
|
|
612
|
|
|
|
|
|
|
my $mref = $notifier->_capture_weakself( sub { |
|
613
|
|
|
|
|
|
|
my ( $notifier, $arg ) = @_; |
|
614
|
|
|
|
|
|
|
print "Notifier $notifier got argument $arg\n"; |
|
615
|
|
|
|
|
|
|
} ); |
|
616
|
|
|
|
|
|
|
|
|
617
|
|
|
|
|
|
|
$mref->( 123 ); |
|
618
|
|
|
|
|
|
|
|
|
619
|
|
|
|
|
|
|
This is provided as a utility for Notifier subclasses to use to build a |
|
620
|
|
|
|
|
|
|
callback CODEref to pass to a Loop method, but which may also want to store |
|
621
|
|
|
|
|
|
|
the CODE ref internally for efficiency. |
|
622
|
|
|
|
|
|
|
|
|
623
|
|
|
|
|
|
|
The C<$code> argument may also be a plain string, which will be used as a |
|
624
|
|
|
|
|
|
|
method name; the returned CODE ref will then invoke that method on the object. |
|
625
|
|
|
|
|
|
|
In this case the method name is stored symbolically in the returned CODE |
|
626
|
|
|
|
|
|
|
reference, and dynamically dispatched each time the reference is invoked. This |
|
627
|
|
|
|
|
|
|
allows it to follow code reloading, dynamic replacement of class methods, or |
|
628
|
|
|
|
|
|
|
other similar techniques. |
|
629
|
|
|
|
|
|
|
|
|
630
|
|
|
|
|
|
|
If the C<$mref> CODE reference is being stored in some object other than the |
|
631
|
|
|
|
|
|
|
one it refers to, remember that since the Notifier is only weakly captured, it |
|
632
|
|
|
|
|
|
|
is possible that it has been destroyed by the time the code runs, and so the |
|
633
|
|
|
|
|
|
|
reference will be passed as C. This should be protected against by the |
|
634
|
|
|
|
|
|
|
code body. |
|
635
|
|
|
|
|
|
|
|
|
636
|
|
|
|
|
|
|
$other_object->{on_event} = $notifier->_capture_weakself( sub { |
|
637
|
|
|
|
|
|
|
my $notifier = shift or return; |
|
638
|
|
|
|
|
|
|
my ( @event_args ) = @_; |
|
639
|
|
|
|
|
|
|
... |
|
640
|
|
|
|
|
|
|
} ); |
|
641
|
|
|
|
|
|
|
|
|
642
|
|
|
|
|
|
|
For stand-alone generic implementation of this behaviour, see also L |
|
643
|
|
|
|
|
|
|
and C. |
|
644
|
|
|
|
|
|
|
|
|
645
|
|
|
|
|
|
|
=cut |
|
646
|
|
|
|
|
|
|
|
|
647
|
|
|
|
|
|
|
sub _capture_weakself |
|
648
|
|
|
|
|
|
|
{ |
|
649
|
2205
|
|
|
2205
|
|
72800
|
my $self = shift; |
|
650
|
2205
|
|
|
|
|
5225
|
my ( $code ) = @_; # actually bare method names work too |
|
651
|
|
|
|
|
|
|
|
|
652
|
2205
|
100
|
|
|
|
6578
|
if( !ref $code ) { |
|
653
|
130
|
|
|
|
|
367
|
my $class = ref $self; |
|
654
|
|
|
|
|
|
|
# Don't save this coderef, or it will break dynamic method dispatch, |
|
655
|
|
|
|
|
|
|
# which means code reloading, dynamic replacement, or other funky |
|
656
|
|
|
|
|
|
|
# techniques stop working |
|
657
|
130
|
100
|
|
|
|
806
|
$self->can( $code ) or |
|
658
|
|
|
|
|
|
|
croak qq(Can't locate object method "$code" via package "$class"); |
|
659
|
|
|
|
|
|
|
} |
|
660
|
|
|
|
|
|
|
|
|
661
|
2204
|
|
|
|
|
7746
|
weaken $self; |
|
662
|
|
|
|
|
|
|
|
|
663
|
|
|
|
|
|
|
return sub { |
|
664
|
2266
|
100
|
|
2266
|
|
68778
|
my $cv = ref( $code ) ? $code : $self->can( $code ); |
|
665
|
|
|
|
|
|
|
|
|
666
|
2266
|
|
|
|
|
3544
|
if( HAS_BROKEN_TRAMPOLINES ) { |
|
667
|
|
|
|
|
|
|
return $cv->( $self, @_ ); |
|
668
|
|
|
|
|
|
|
} |
|
669
|
|
|
|
|
|
|
else { |
|
670
|
2266
|
|
|
|
|
5980
|
unshift @_, $self; |
|
671
|
2266
|
|
|
|
|
17464
|
goto &$cv; |
|
672
|
|
|
|
|
|
|
} |
|
673
|
2204
|
|
|
|
|
34323
|
}; |
|
674
|
|
|
|
|
|
|
} |
|
675
|
|
|
|
|
|
|
|
|
676
|
|
|
|
|
|
|
=head2 _replace_weakself |
|
677
|
|
|
|
|
|
|
|
|
678
|
|
|
|
|
|
|
$mref = $notifier->_replace_weakself( $code ) |
|
679
|
|
|
|
|
|
|
|
|
680
|
|
|
|
|
|
|
Returns a new CODE ref which, when invoked, will invoke the originally-passed |
|
681
|
|
|
|
|
|
|
ref, with a reference to the Notifier replacing its first argument. The |
|
682
|
|
|
|
|
|
|
Notifier reference is stored weakly in C<$mref>, so this CODE ref may be |
|
683
|
|
|
|
|
|
|
stored in the Notifier itself without creating a cycle. |
|
684
|
|
|
|
|
|
|
|
|
685
|
|
|
|
|
|
|
For example, |
|
686
|
|
|
|
|
|
|
|
|
687
|
|
|
|
|
|
|
my $mref = $notifier->_replace_weakself( sub { |
|
688
|
|
|
|
|
|
|
my ( $notifier, $arg ) = @_; |
|
689
|
|
|
|
|
|
|
print "Notifier $notifier got argument $arg\n"; |
|
690
|
|
|
|
|
|
|
} ); |
|
691
|
|
|
|
|
|
|
|
|
692
|
|
|
|
|
|
|
$mref->( $object, 123 ); |
|
693
|
|
|
|
|
|
|
|
|
694
|
|
|
|
|
|
|
This is provided as a utility for Notifier subclasses to use for event |
|
695
|
|
|
|
|
|
|
callbacks on other objects, where the delegated object is passed in the |
|
696
|
|
|
|
|
|
|
function's arguments. |
|
697
|
|
|
|
|
|
|
|
|
698
|
|
|
|
|
|
|
The C<$code> argument may also be a plain string, which will be used as a |
|
699
|
|
|
|
|
|
|
method name; the returned CODE ref will then invoke that method on the object. |
|
700
|
|
|
|
|
|
|
As with C<_capture_weakself> this is stored symbolically. |
|
701
|
|
|
|
|
|
|
|
|
702
|
|
|
|
|
|
|
As with C<_capture_weakself>, care should be taken against Notifier |
|
703
|
|
|
|
|
|
|
destruction if the C<$mref> CODE reference is stored in some other object. |
|
704
|
|
|
|
|
|
|
|
|
705
|
|
|
|
|
|
|
=cut |
|
706
|
|
|
|
|
|
|
|
|
707
|
|
|
|
|
|
|
sub _replace_weakself |
|
708
|
|
|
|
|
|
|
{ |
|
709
|
185
|
|
|
185
|
|
627
|
my $self = shift; |
|
710
|
185
|
|
|
|
|
372
|
my ( $code ) = @_; # actually bare method names work too |
|
711
|
|
|
|
|
|
|
|
|
712
|
185
|
100
|
|
|
|
533
|
if( !ref $code ) { |
|
713
|
|
|
|
|
|
|
# Don't save this coderef, see _capture_weakself for why |
|
714
|
14
|
|
|
|
|
27
|
my $class = ref $self; |
|
715
|
14
|
50
|
|
|
|
84
|
$self->can( $code ) or |
|
716
|
|
|
|
|
|
|
croak qq(Can't locate object method "$code" via package "$class"); |
|
717
|
|
|
|
|
|
|
} |
|
718
|
|
|
|
|
|
|
|
|
719
|
185
|
|
|
|
|
679
|
weaken $self; |
|
720
|
|
|
|
|
|
|
|
|
721
|
|
|
|
|
|
|
return sub { |
|
722
|
41
|
100
|
|
41
|
|
565
|
my $cv = ref( $code ) ? $code : $self->can( $code ); |
|
723
|
|
|
|
|
|
|
|
|
724
|
41
|
|
|
|
|
80
|
if( HAS_BROKEN_TRAMPOLINES ) { |
|
725
|
|
|
|
|
|
|
return $cv->( $self, @_[1..$#_] ); |
|
726
|
|
|
|
|
|
|
} |
|
727
|
|
|
|
|
|
|
else { |
|
728
|
|
|
|
|
|
|
# Don't assign to $_[0] directly or we will change caller's first argument |
|
729
|
41
|
|
|
|
|
75
|
shift @_; |
|
730
|
41
|
|
|
|
|
83
|
unshift @_, $self; |
|
731
|
41
|
|
|
|
|
262
|
goto &$cv; |
|
732
|
|
|
|
|
|
|
} |
|
733
|
185
|
|
|
|
|
2706
|
}; |
|
734
|
|
|
|
|
|
|
} |
|
735
|
|
|
|
|
|
|
|
|
736
|
|
|
|
|
|
|
=head2 can_event |
|
737
|
|
|
|
|
|
|
|
|
738
|
|
|
|
|
|
|
$code = $notifier->can_event( $event_name ) |
|
739
|
|
|
|
|
|
|
|
|
740
|
|
|
|
|
|
|
Returns a C reference if the object can perform the given event name, |
|
741
|
|
|
|
|
|
|
either by a configured C reference parameter, or by implementing a |
|
742
|
|
|
|
|
|
|
method. If the object is unable to handle this event, C is returned. |
|
743
|
|
|
|
|
|
|
|
|
744
|
|
|
|
|
|
|
=cut |
|
745
|
|
|
|
|
|
|
|
|
746
|
|
|
|
|
|
|
sub can_event |
|
747
|
|
|
|
|
|
|
{ |
|
748
|
5250
|
|
|
5250
|
1
|
9632
|
my $self = shift; |
|
749
|
5250
|
|
|
|
|
14660
|
my ( $event_name ) = @_; |
|
750
|
|
|
|
|
|
|
|
|
751
|
5250
|
|
100
|
|
|
42258
|
return $self->{$event_name} || $self->can( $event_name ); |
|
752
|
|
|
|
|
|
|
} |
|
753
|
|
|
|
|
|
|
|
|
754
|
|
|
|
|
|
|
=head2 make_event_cb |
|
755
|
|
|
|
|
|
|
|
|
756
|
|
|
|
|
|
|
$callback = $notifier->make_event_cb( $event_name ) |
|
757
|
|
|
|
|
|
|
|
|
758
|
|
|
|
|
|
|
Returns a C reference which, when invoked, will execute the given event |
|
759
|
|
|
|
|
|
|
handler. Event handlers may either be subclass methods, or parameters given to |
|
760
|
|
|
|
|
|
|
the C or C method. |
|
761
|
|
|
|
|
|
|
|
|
762
|
|
|
|
|
|
|
The event handler can be passed extra arguments by giving them to the C |
|
763
|
|
|
|
|
|
|
reference; the first parameter received will be a reference to the notifier |
|
764
|
|
|
|
|
|
|
itself. This is stored weakly in the closure, so it is safe to store the |
|
765
|
|
|
|
|
|
|
resulting C reference in the object itself without causing a reference |
|
766
|
|
|
|
|
|
|
cycle. |
|
767
|
|
|
|
|
|
|
|
|
768
|
|
|
|
|
|
|
=cut |
|
769
|
|
|
|
|
|
|
|
|
770
|
|
|
|
|
|
|
sub make_event_cb |
|
771
|
|
|
|
|
|
|
{ |
|
772
|
740
|
|
|
740
|
1
|
1799
|
my $self = shift; |
|
773
|
740
|
|
|
|
|
2948
|
my ( $event_name ) = @_; |
|
774
|
|
|
|
|
|
|
|
|
775
|
740
|
50
|
|
|
|
2046
|
my $code = $self->can_event( $event_name ) |
|
776
|
|
|
|
|
|
|
or croak "$self cannot handle $event_name event"; |
|
777
|
|
|
|
|
|
|
|
|
778
|
740
|
|
|
|
|
4158
|
my $caller = caller; |
|
779
|
|
|
|
|
|
|
|
|
780
|
|
|
|
|
|
|
return $self->_capture_weakself( |
|
781
|
|
|
|
|
|
|
!$IO::Async::Debug::DEBUG ? $code : sub { |
|
782
|
0
|
|
|
0
|
|
0
|
my $self = $_[0]; |
|
783
|
0
|
|
|
|
|
0
|
$self->_debug_printf_event( $caller, $event_name ); |
|
784
|
0
|
|
|
|
|
0
|
goto &$code; |
|
785
|
|
|
|
|
|
|
} |
|
786
|
740
|
50
|
|
|
|
4995
|
); |
|
787
|
|
|
|
|
|
|
} |
|
788
|
|
|
|
|
|
|
|
|
789
|
|
|
|
|
|
|
=head2 maybe_make_event_cb |
|
790
|
|
|
|
|
|
|
|
|
791
|
|
|
|
|
|
|
$callback = $notifier->maybe_make_event_cb( $event_name ) |
|
792
|
|
|
|
|
|
|
|
|
793
|
|
|
|
|
|
|
Similar to C but will return C if the object cannot |
|
794
|
|
|
|
|
|
|
handle the named event, rather than throwing an exception. |
|
795
|
|
|
|
|
|
|
|
|
796
|
|
|
|
|
|
|
=cut |
|
797
|
|
|
|
|
|
|
|
|
798
|
|
|
|
|
|
|
sub maybe_make_event_cb |
|
799
|
|
|
|
|
|
|
{ |
|
800
|
2
|
|
|
2
|
1
|
771
|
my $self = shift; |
|
801
|
2
|
|
|
|
|
6
|
my ( $event_name ) = @_; |
|
802
|
|
|
|
|
|
|
|
|
803
|
2
|
100
|
|
|
|
6
|
my $code = $self->can_event( $event_name ) |
|
804
|
|
|
|
|
|
|
or return undef; |
|
805
|
|
|
|
|
|
|
|
|
806
|
1
|
|
|
|
|
4
|
my $caller = caller; |
|
807
|
|
|
|
|
|
|
|
|
808
|
|
|
|
|
|
|
return $self->_capture_weakself( |
|
809
|
|
|
|
|
|
|
!$IO::Async::Debug::DEBUG ? $code : sub { |
|
810
|
0
|
|
|
0
|
|
0
|
my $self = $_[0]; |
|
811
|
0
|
|
|
|
|
0
|
$self->_debug_printf_event( $caller, $event_name ); |
|
812
|
0
|
|
|
|
|
0
|
goto &$code; |
|
813
|
|
|
|
|
|
|
} |
|
814
|
1
|
50
|
|
|
|
7
|
); |
|
815
|
|
|
|
|
|
|
} |
|
816
|
|
|
|
|
|
|
|
|
817
|
|
|
|
|
|
|
=head2 invoke_event |
|
818
|
|
|
|
|
|
|
|
|
819
|
|
|
|
|
|
|
@ret = $notifier->invoke_event( $event_name, @args ) |
|
820
|
|
|
|
|
|
|
|
|
821
|
|
|
|
|
|
|
Invokes the given event handler, passing in the given arguments. Event |
|
822
|
|
|
|
|
|
|
handlers may either be subclass methods, or parameters given to the C or |
|
823
|
|
|
|
|
|
|
C method. Returns whatever the underlying method or CODE reference |
|
824
|
|
|
|
|
|
|
returned. |
|
825
|
|
|
|
|
|
|
|
|
826
|
|
|
|
|
|
|
=cut |
|
827
|
|
|
|
|
|
|
|
|
828
|
|
|
|
|
|
|
sub invoke_event |
|
829
|
|
|
|
|
|
|
{ |
|
830
|
1388
|
|
|
1388
|
1
|
3661
|
my $self = shift; |
|
831
|
1388
|
|
|
|
|
5242
|
my ( $event_name, @args ) = @_; |
|
832
|
|
|
|
|
|
|
|
|
833
|
1388
|
50
|
|
|
|
4349
|
my $code = $self->can_event( $event_name ) |
|
834
|
|
|
|
|
|
|
or croak "$self cannot handle $event_name event"; |
|
835
|
|
|
|
|
|
|
|
|
836
|
1388
|
50
|
|
|
|
3543
|
$self->_debug_printf_event( scalar caller, $event_name ) if $IO::Async::Debug::DEBUG; |
|
837
|
1388
|
|
|
|
|
5222
|
return $code->( $self, @args ); |
|
838
|
|
|
|
|
|
|
} |
|
839
|
|
|
|
|
|
|
|
|
840
|
|
|
|
|
|
|
=head2 maybe_invoke_event |
|
841
|
|
|
|
|
|
|
|
|
842
|
|
|
|
|
|
|
$retref = $notifier->maybe_invoke_event( $event_name, @args ) |
|
843
|
|
|
|
|
|
|
|
|
844
|
|
|
|
|
|
|
Similar to C but will return C if the object cannot |
|
845
|
|
|
|
|
|
|
handle the name event, rather than throwing an exception. In order to |
|
846
|
|
|
|
|
|
|
distinguish this from an event-handling function that simply returned |
|
847
|
|
|
|
|
|
|
C, if the object does handle the event, the list that it returns will |
|
848
|
|
|
|
|
|
|
be returned in an ARRAY reference. |
|
849
|
|
|
|
|
|
|
|
|
850
|
|
|
|
|
|
|
=cut |
|
851
|
|
|
|
|
|
|
|
|
852
|
|
|
|
|
|
|
sub maybe_invoke_event |
|
853
|
|
|
|
|
|
|
{ |
|
854
|
1229
|
|
|
1229
|
1
|
4319
|
my $self = shift; |
|
855
|
1229
|
|
|
|
|
5318
|
my ( $event_name, @args ) = @_; |
|
856
|
|
|
|
|
|
|
|
|
857
|
1229
|
100
|
|
|
|
3700
|
my $code = $self->can_event( $event_name ) |
|
858
|
|
|
|
|
|
|
or return undef; |
|
859
|
|
|
|
|
|
|
|
|
860
|
73
|
50
|
|
|
|
382
|
$self->_debug_printf_event( scalar caller, $event_name ) if $IO::Async::Debug::DEBUG; |
|
861
|
73
|
|
|
|
|
282
|
return [ $code->( $self, @args ) ]; |
|
862
|
|
|
|
|
|
|
} |
|
863
|
|
|
|
|
|
|
|
|
864
|
|
|
|
|
|
|
=head1 DEBUGGING SUPPORT |
|
865
|
|
|
|
|
|
|
|
|
866
|
|
|
|
|
|
|
=cut |
|
867
|
|
|
|
|
|
|
|
|
868
|
|
|
|
|
|
|
=head2 debug_printf |
|
869
|
|
|
|
|
|
|
|
|
870
|
|
|
|
|
|
|
$notifier->debug_printf( $format, @args ) |
|
871
|
|
|
|
|
|
|
|
|
872
|
|
|
|
|
|
|
Conditionally print a debugging message to C if debugging is enabled. |
|
873
|
|
|
|
|
|
|
If such a message is printed, it will be printed using C using the |
|
874
|
|
|
|
|
|
|
given format and arguments. The message will be prefixed with a string, in |
|
875
|
|
|
|
|
|
|
square brackets, to help identify the C<$notifier> instance. This string will |
|
876
|
|
|
|
|
|
|
be the class name of the notifier, and any parent notifiers it is contained |
|
877
|
|
|
|
|
|
|
by, joined by an arrow C<< <- >>. To ensure this string does not grow too |
|
878
|
|
|
|
|
|
|
long, certain prefixes are abbreviated: |
|
879
|
|
|
|
|
|
|
|
|
880
|
|
|
|
|
|
|
IO::Async::Protocol:: => IaP: |
|
881
|
|
|
|
|
|
|
IO::Async:: => Ia: |
|
882
|
|
|
|
|
|
|
Net::Async:: => Na: |
|
883
|
|
|
|
|
|
|
|
|
884
|
|
|
|
|
|
|
Finally, each notifier that has a name defined using the C |
|
885
|
|
|
|
|
|
|
parameter has that name appended in braces. |
|
886
|
|
|
|
|
|
|
|
|
887
|
|
|
|
|
|
|
For example, invoking |
|
888
|
|
|
|
|
|
|
|
|
889
|
|
|
|
|
|
|
$stream->debug_printf( "EVENT on_read" ) |
|
890
|
|
|
|
|
|
|
|
|
891
|
|
|
|
|
|
|
On an L instance reading and writing a file descriptor |
|
892
|
|
|
|
|
|
|
whose C is 4, which is a child of an L, |
|
893
|
|
|
|
|
|
|
will produce a line of output: |
|
894
|
|
|
|
|
|
|
|
|
895
|
|
|
|
|
|
|
[Ia:Stream{rw=4}<-IaP:Stream] EVENT on_read |
|
896
|
|
|
|
|
|
|
|
|
897
|
|
|
|
|
|
|
=cut |
|
898
|
|
|
|
|
|
|
|
|
899
|
|
|
|
|
|
|
sub debug_printf |
|
900
|
|
|
|
|
|
|
{ |
|
901
|
1092
|
50
|
|
1092
|
1
|
3884
|
$IO::Async::Debug::DEBUG or return; |
|
902
|
|
|
|
|
|
|
|
|
903
|
0
|
|
|
|
|
0
|
my $self = shift; |
|
904
|
0
|
|
|
|
|
0
|
my ( $format, @args ) = @_; |
|
905
|
|
|
|
|
|
|
|
|
906
|
0
|
|
|
|
|
0
|
my @id; |
|
907
|
0
|
|
|
|
|
0
|
while( $self ) { |
|
908
|
0
|
|
|
|
|
0
|
push @id, ref $self; |
|
909
|
|
|
|
|
|
|
|
|
910
|
0
|
|
|
|
|
0
|
my $name = $self->notifier_name; |
|
911
|
0
|
0
|
0
|
|
|
0
|
$id[-1] .= "{$name}" if defined $name and length $name; |
|
912
|
|
|
|
|
|
|
|
|
913
|
0
|
|
|
|
|
0
|
$self = $self->parent; |
|
914
|
|
|
|
|
|
|
} |
|
915
|
|
|
|
|
|
|
|
|
916
|
|
|
|
|
|
|
s/^IO::Async::Protocol::/IaP:/, |
|
917
|
|
|
|
|
|
|
s/^IO::Async::/Ia:/, |
|
918
|
0
|
|
|
|
|
0
|
s/^Net::Async::/Na:/ for @id; |
|
919
|
|
|
|
|
|
|
|
|
920
|
0
|
|
|
|
|
0
|
IO::Async::Debug::logf "[%s] $format\n", join("<-", @id), @args; |
|
921
|
|
|
|
|
|
|
} |
|
922
|
|
|
|
|
|
|
|
|
923
|
|
|
|
|
|
|
sub _debug_printf_event |
|
924
|
|
|
|
|
|
|
{ |
|
925
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
|
926
|
0
|
|
|
|
|
0
|
my ( $caller, $event_name ) = @_; |
|
927
|
|
|
|
|
|
|
|
|
928
|
0
|
|
|
|
|
0
|
my $class = ref $self; |
|
929
|
|
|
|
|
|
|
|
|
930
|
0
|
0
|
0
|
|
|
0
|
if( $IO::Async::Debug::DEBUG > 1 or $class eq $caller ) { |
|
931
|
|
|
|
|
|
|
s/^IO::Async::Protocol::/IaP:/, |
|
932
|
|
|
|
|
|
|
s/^IO::Async::/Ia:/, |
|
933
|
0
|
|
|
|
|
0
|
s/^Net::Async::/Na:/ for my $str_caller = $caller; |
|
934
|
|
|
|
|
|
|
|
|
935
|
0
|
0
|
|
|
|
0
|
$self->debug_printf( "EVENT %s", |
|
936
|
|
|
|
|
|
|
( $class eq $caller ? $event_name : "${str_caller}::$event_name" ) |
|
937
|
|
|
|
|
|
|
); |
|
938
|
|
|
|
|
|
|
} |
|
939
|
|
|
|
|
|
|
} |
|
940
|
|
|
|
|
|
|
|
|
941
|
|
|
|
|
|
|
=head2 invoke_error |
|
942
|
|
|
|
|
|
|
|
|
943
|
|
|
|
|
|
|
$notifier->invoke_error( $message, $name, @details ) |
|
944
|
|
|
|
|
|
|
|
|
945
|
|
|
|
|
|
|
Invokes the stored C event handler, passing in the given arguments. |
|
946
|
|
|
|
|
|
|
If no handler is defined, it will be passed up to the containing parent |
|
947
|
|
|
|
|
|
|
notifier, if one exists. If no parent exists, the error message will be thrown |
|
948
|
|
|
|
|
|
|
as an exception by using C and this method will not return. |
|
949
|
|
|
|
|
|
|
|
|
950
|
|
|
|
|
|
|
If a handler is found to handle this error, the method will return as normal. |
|
951
|
|
|
|
|
|
|
However, as the expected use-case is to handle "fatal" errors that now render |
|
952
|
|
|
|
|
|
|
the notifier unsuitable to continue, code should be careful not to perform any |
|
953
|
|
|
|
|
|
|
further work after invoking it. Specifically, sockets may become disconnected, |
|
954
|
|
|
|
|
|
|
or the entire notifier may now be removed from its containing loop. |
|
955
|
|
|
|
|
|
|
|
|
956
|
|
|
|
|
|
|
The C<$name> and C<@details> list should follow similar semantics to L |
|
957
|
|
|
|
|
|
|
failures. That is, the C<$name> should be a string giving a category of |
|
958
|
|
|
|
|
|
|
failure, and the C<@details> list should contain additional arguments that |
|
959
|
|
|
|
|
|
|
relate to that kind of failure. |
|
960
|
|
|
|
|
|
|
|
|
961
|
|
|
|
|
|
|
=cut |
|
962
|
|
|
|
|
|
|
|
|
963
|
|
|
|
|
|
|
sub invoke_error |
|
964
|
|
|
|
|
|
|
{ |
|
965
|
5
|
|
|
5
|
1
|
1249
|
my $self = shift; |
|
966
|
5
|
|
|
|
|
13
|
my ( $message, $name, @details ) = @_; |
|
967
|
|
|
|
|
|
|
|
|
968
|
5
|
100
|
66
|
|
|
45
|
if( my $code = $self->{IO_Async_Notifier__on_error} || $self->can( "on_error" ) ) { |
|
969
|
3
|
|
|
|
|
9
|
return $code->( $self, $message, $name, @details ); |
|
970
|
|
|
|
|
|
|
} |
|
971
|
|
|
|
|
|
|
|
|
972
|
2
|
100
|
|
|
|
9
|
if( my $parent = $self->parent ) { |
|
973
|
1
|
|
|
|
|
8
|
return $parent->invoke_error( @_ ); |
|
974
|
|
|
|
|
|
|
} |
|
975
|
|
|
|
|
|
|
|
|
976
|
1
|
|
|
|
|
8
|
die "$message\n"; |
|
977
|
|
|
|
|
|
|
} |
|
978
|
|
|
|
|
|
|
|
|
979
|
|
|
|
|
|
|
=head1 AUTHOR |
|
980
|
|
|
|
|
|
|
|
|
981
|
|
|
|
|
|
|
Paul Evans |
|
982
|
|
|
|
|
|
|
|
|
983
|
|
|
|
|
|
|
=cut |
|
984
|
|
|
|
|
|
|
|
|
985
|
|
|
|
|
|
|
0x55AA; |