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
|
83
|
|
|
83
|
|
285928
|
use strict; |
|
83
|
|
|
|
|
212
|
|
|
83
|
|
|
|
|
2509
|
|
9
|
83
|
|
|
83
|
|
433
|
use warnings; |
|
83
|
|
|
|
|
163
|
|
|
83
|
|
|
|
|
4073
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.79'; |
12
|
|
|
|
|
|
|
|
13
|
83
|
|
|
83
|
|
552
|
use Carp; |
|
83
|
|
|
|
|
214
|
|
|
83
|
|
|
|
|
6883
|
|
14
|
83
|
|
|
83
|
|
565
|
use Scalar::Util qw( weaken ); |
|
83
|
|
|
|
|
166
|
|
|
83
|
|
|
|
|
5709
|
|
15
|
|
|
|
|
|
|
|
16
|
83
|
|
|
83
|
|
52096
|
use Future 0.26; # ->is_failed |
|
83
|
|
|
|
|
875222
|
|
|
83
|
|
|
|
|
2915
|
|
17
|
|
|
|
|
|
|
|
18
|
83
|
|
|
83
|
|
39557
|
use IO::Async::Debug; |
|
83
|
|
|
|
|
240
|
|
|
83
|
|
|
|
|
3656
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Perl 5.8.4 cannot do trampolines by modiying @_ then goto &$code |
21
|
83
|
|
|
83
|
|
588
|
use constant HAS_BROKEN_TRAMPOLINES => ( $] == "5.008004" ); |
|
83
|
|
|
|
|
192
|
|
|
83
|
|
|
|
|
202747
|
|
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
|
1417
|
|
|
1417
|
1
|
82617
|
my $class = shift; |
224
|
1417
|
|
|
|
|
16628
|
my %params = @_; |
225
|
|
|
|
|
|
|
|
226
|
1417
|
|
|
|
|
8682
|
my $self = bless {}, $class; |
227
|
|
|
|
|
|
|
|
228
|
1417
|
|
|
|
|
18131
|
$self->_init( \%params ); |
229
|
|
|
|
|
|
|
|
230
|
1417
|
|
|
|
|
11933
|
$self->configure( %params ); |
231
|
|
|
|
|
|
|
|
232
|
1416
|
|
|
|
|
11998
|
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
|
1760
|
|
|
1760
|
1
|
6101
|
my $self = shift; |
252
|
1760
|
|
|
|
|
5706
|
my %params = @_; |
253
|
|
|
|
|
|
|
|
254
|
1760
|
|
|
|
|
3947
|
foreach (qw( notifier_name on_error )) { |
255
|
3520
|
100
|
|
|
|
13407
|
$self->{"IO_Async_Notifier__$_"} = delete $params{$_} if exists $params{$_}; |
256
|
|
|
|
|
|
|
} |
257
|
|
|
|
|
|
|
|
258
|
1760
|
100
|
|
|
|
6676
|
$self->configure_unknown( %params ) if keys %params; |
259
|
|
|
|
|
|
|
} |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
sub configure_unknown |
262
|
|
|
|
|
|
|
{ |
263
|
1
|
|
|
1
|
1
|
2
|
my $self = shift; |
264
|
1
|
|
|
|
|
3
|
my %params = @_; |
265
|
|
|
|
|
|
|
|
266
|
1
|
|
|
|
|
3
|
my $class = ref $self; |
267
|
1
|
|
|
|
|
160
|
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
|
20326
|
|
|
20326
|
1
|
44390
|
my $self = shift; |
281
|
|
|
|
|
|
|
return $self->{IO_Async_Notifier__loop} |
282
|
20326
|
|
|
|
|
73687
|
} |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
*get_loop = \&loop; |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
# Only called by IO::Async::Loop, not external interface |
287
|
|
|
|
|
|
|
sub __set_loop |
288
|
|
|
|
|
|
|
{ |
289
|
3121
|
|
|
3121
|
|
5326
|
my $self = shift; |
290
|
3121
|
|
|
|
|
5638
|
my ( $loop ) = @_; |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
# early exit if no change |
293
|
3121
|
100
|
66
|
|
|
27302
|
return if !$loop and !$self->loop or |
|
|
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
294
|
|
|
|
|
|
|
$loop and $self->loop and $loop == $self->loop; |
295
|
|
|
|
|
|
|
|
296
|
2555
|
100
|
|
|
|
5572
|
$self->_remove_from_loop( $self->loop ) if $self->loop; |
297
|
|
|
|
|
|
|
|
298
|
2555
|
|
|
|
|
8990
|
$self->{IO_Async_Notifier__loop} = $loop; |
299
|
2555
|
|
|
|
|
10071
|
weaken( $self->{IO_Async_Notifier__loop} ); # To avoid a cycle |
300
|
|
|
|
|
|
|
|
301
|
2555
|
100
|
|
|
|
5121
|
$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
|
2178
|
my $self = shift; |
317
|
50
|
|
100
|
|
|
564
|
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
|
4760
|
my $self = shift; |
344
|
58
|
|
|
|
|
232
|
my ( $f ) = @_; |
345
|
|
|
|
|
|
|
|
346
|
58
|
|
|
|
|
223
|
my $fkey = "$f"; # stable stringification |
347
|
|
|
|
|
|
|
|
348
|
58
|
|
|
|
|
429
|
$self->{IO_Async_Notifier__futures}{$fkey} = $f; |
349
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
$f->on_ready( $self->_capture_weakself( sub { |
351
|
58
|
|
|
58
|
|
121
|
my $self = shift; |
352
|
58
|
|
|
|
|
130
|
my ( $f ) = @_; |
353
|
|
|
|
|
|
|
|
354
|
58
|
|
|
|
|
196
|
delete $self->{IO_Async_Notifier__futures}{$fkey}; |
355
|
|
|
|
|
|
|
|
356
|
58
|
100
|
|
|
|
244
|
$self->invoke_error( $f->failure ) if $f->is_failed; |
357
|
58
|
|
|
|
|
799
|
})); |
358
|
|
|
|
|
|
|
|
359
|
58
|
|
|
|
|
1406
|
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
|
29
|
my $self = shift; |
376
|
|
|
|
|
|
|
|
377
|
3
|
|
|
|
|
5
|
return values %{ $self->{IO_Async_Notifier__futures} }; |
|
3
|
|
|
|
|
22
|
|
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
|
3322
|
|
|
3322
|
1
|
5956
|
my $self = shift; |
401
|
3322
|
|
|
|
|
11147
|
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
|
3090
|
|
|
3090
|
1
|
5593
|
my $self = shift; |
415
|
3090
|
100
|
|
|
|
10337
|
return unless $self->{IO_Async_Notifier__children}; |
416
|
664
|
|
|
|
|
1159
|
return @{ $self->{IO_Async_Notifier__children} }; |
|
664
|
|
|
|
|
5028
|
|
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
|
2225
|
my $self = shift; |
434
|
649
|
|
|
|
|
1277
|
my ( $child ) = @_; |
435
|
|
|
|
|
|
|
|
436
|
649
|
100
|
|
|
|
1833
|
croak "Cannot add a child that already has a parent" if defined $child->{IO_Async_Notifier__parent}; |
437
|
|
|
|
|
|
|
|
438
|
648
|
50
|
|
|
|
1712
|
croak "Cannot add a child that is already a member of a loop" if defined $child->loop; |
439
|
|
|
|
|
|
|
|
440
|
648
|
100
|
|
|
|
1387
|
if( defined( my $loop = $self->loop ) ) { |
441
|
626
|
|
|
|
|
2894
|
$loop->add( $child ); |
442
|
|
|
|
|
|
|
} |
443
|
|
|
|
|
|
|
|
444
|
643
|
|
|
|
|
1234
|
push @{ $self->{IO_Async_Notifier__children} }, $child; |
|
643
|
|
|
|
|
3138
|
|
445
|
643
|
|
|
|
|
2030
|
$child->{IO_Async_Notifier__parent} = $self; |
446
|
643
|
|
|
|
|
2515
|
weaken( $child->{IO_Async_Notifier__parent} ); |
447
|
|
|
|
|
|
|
|
448
|
643
|
|
|
|
|
1727
|
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
|
361
|
|
|
361
|
1
|
1218
|
my $self = shift; |
464
|
361
|
|
|
|
|
781
|
my ( $child ) = @_; |
465
|
|
|
|
|
|
|
|
466
|
|
|
|
|
|
|
LOOP: { |
467
|
361
|
|
|
|
|
645
|
my $childrenref = $self->{IO_Async_Notifier__children}; |
|
361
|
|
|
|
|
704
|
|
468
|
361
|
|
|
|
|
2168
|
for my $i ( 0 .. $#$childrenref ) { |
469
|
394
|
100
|
|
|
|
1629
|
next unless $childrenref->[$i] == $child; |
470
|
361
|
|
|
|
|
1176
|
splice @$childrenref, $i, 1, (); |
471
|
361
|
|
|
|
|
1284
|
last LOOP; |
472
|
|
|
|
|
|
|
} |
473
|
|
|
|
|
|
|
|
474
|
0
|
|
|
|
|
0
|
croak "Cannot remove child from a parent that doesn't contain it"; |
475
|
|
|
|
|
|
|
} |
476
|
|
|
|
|
|
|
|
477
|
361
|
|
|
|
|
963
|
undef $child->{IO_Async_Notifier__parent}; |
478
|
|
|
|
|
|
|
|
479
|
361
|
100
|
|
|
|
1413
|
if( defined( my $loop = $self->loop ) ) { |
480
|
267
|
|
|
|
|
1704
|
$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
|
1644
|
my $self = shift; |
497
|
|
|
|
|
|
|
|
498
|
804
|
100
|
|
|
|
2518
|
if( my $parent = $self->parent ) { |
|
|
100
|
|
|
|
|
|
499
|
308
|
|
|
|
|
1629
|
$parent->remove_child( $self ); |
500
|
|
|
|
|
|
|
} |
501
|
|
|
|
|
|
|
elsif( my $loop = $self->loop ) { |
502
|
476
|
|
|
|
|
2869
|
$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
|
|
|
|
705
|
|
|
{ |
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
|
2203
|
|
|
2203
|
|
75421
|
my $self = shift; |
650
|
2203
|
|
|
|
|
5470
|
my ( $code ) = @_; # actually bare method names work too |
651
|
|
|
|
|
|
|
|
652
|
2203
|
100
|
|
|
|
6432
|
if( !ref $code ) { |
653
|
130
|
|
|
|
|
423
|
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
|
|
|
|
1155
|
$self->can( $code ) or |
658
|
|
|
|
|
|
|
croak qq(Can't locate object method "$code" via package "$class"); |
659
|
|
|
|
|
|
|
} |
660
|
|
|
|
|
|
|
|
661
|
2202
|
|
|
|
|
7795
|
weaken $self; |
662
|
|
|
|
|
|
|
|
663
|
|
|
|
|
|
|
return sub { |
664
|
2257
|
100
|
|
2257
|
|
68904
|
my $cv = ref( $code ) ? $code : $self->can( $code ); |
665
|
|
|
|
|
|
|
|
666
|
2257
|
|
|
|
|
5933
|
if( HAS_BROKEN_TRAMPOLINES ) { |
667
|
|
|
|
|
|
|
return $cv->( $self, @_ ); |
668
|
|
|
|
|
|
|
} |
669
|
|
|
|
|
|
|
else { |
670
|
2257
|
|
|
|
|
6270
|
unshift @_, $self; |
671
|
2257
|
|
|
|
|
17704
|
goto &$cv; |
672
|
|
|
|
|
|
|
} |
673
|
2202
|
|
|
|
|
33628
|
}; |
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
|
|
513
|
my $self = shift; |
710
|
185
|
|
|
|
|
370
|
my ( $code ) = @_; # actually bare method names work too |
711
|
|
|
|
|
|
|
|
712
|
185
|
100
|
|
|
|
492
|
if( !ref $code ) { |
713
|
|
|
|
|
|
|
# Don't save this coderef, see _capture_weakself for why |
714
|
14
|
|
|
|
|
29
|
my $class = ref $self; |
715
|
14
|
50
|
|
|
|
72
|
$self->can( $code ) or |
716
|
|
|
|
|
|
|
croak qq(Can't locate object method "$code" via package "$class"); |
717
|
|
|
|
|
|
|
} |
718
|
|
|
|
|
|
|
|
719
|
185
|
|
|
|
|
715
|
weaken $self; |
720
|
|
|
|
|
|
|
|
721
|
|
|
|
|
|
|
return sub { |
722
|
41
|
100
|
|
41
|
|
700
|
my $cv = ref( $code ) ? $code : $self->can( $code ); |
723
|
|
|
|
|
|
|
|
724
|
41
|
|
|
|
|
73
|
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
|
|
|
|
|
90
|
shift @_; |
730
|
41
|
|
|
|
|
85
|
unshift @_, $self; |
731
|
41
|
|
|
|
|
235
|
goto &$cv; |
732
|
|
|
|
|
|
|
} |
733
|
185
|
|
|
|
|
3337
|
}; |
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
|
5246
|
|
|
5246
|
1
|
10192
|
my $self = shift; |
749
|
5246
|
|
|
|
|
14566
|
my ( $event_name ) = @_; |
750
|
|
|
|
|
|
|
|
751
|
5246
|
|
100
|
|
|
44677
|
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
|
738
|
|
|
738
|
1
|
2115
|
my $self = shift; |
773
|
738
|
|
|
|
|
3457
|
my ( $event_name ) = @_; |
774
|
|
|
|
|
|
|
|
775
|
738
|
50
|
|
|
|
2004
|
my $code = $self->can_event( $event_name ) |
776
|
|
|
|
|
|
|
or croak "$self cannot handle $event_name event"; |
777
|
|
|
|
|
|
|
|
778
|
738
|
|
|
|
|
4525
|
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
|
738
|
50
|
|
|
|
5893
|
); |
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
|
813
|
my $self = shift; |
801
|
2
|
|
|
|
|
5
|
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
|
5229
|
my $self = shift; |
831
|
1388
|
|
|
|
|
5195
|
my ( $event_name, @args ) = @_; |
832
|
|
|
|
|
|
|
|
833
|
1388
|
50
|
|
|
|
4331
|
my $code = $self->can_event( $event_name ) |
834
|
|
|
|
|
|
|
or croak "$self cannot handle $event_name event"; |
835
|
|
|
|
|
|
|
|
836
|
1388
|
50
|
|
|
|
3529
|
$self->_debug_printf_event( scalar caller, $event_name ) if $IO::Async::Debug::DEBUG; |
837
|
1388
|
|
|
|
|
5627
|
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
|
4577
|
my $self = shift; |
855
|
1229
|
|
|
|
|
6264
|
my ( $event_name, @args ) = @_; |
856
|
|
|
|
|
|
|
|
857
|
1229
|
100
|
|
|
|
4396
|
my $code = $self->can_event( $event_name ) |
858
|
|
|
|
|
|
|
or return undef; |
859
|
|
|
|
|
|
|
|
860
|
73
|
50
|
|
|
|
359
|
$self->_debug_printf_event( scalar caller, $event_name ) if $IO::Async::Debug::DEBUG; |
861
|
73
|
|
|
|
|
403
|
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
|
3399
|
$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
|
1330
|
my $self = shift; |
966
|
5
|
|
|
|
|
14
|
my ( $message, $name, @details ) = @_; |
967
|
|
|
|
|
|
|
|
968
|
5
|
100
|
66
|
|
|
34
|
if( my $code = $self->{IO_Async_Notifier__on_error} || $self->can( "on_error" ) ) { |
969
|
3
|
|
|
|
|
14
|
return $code->( $self, $message, $name, @details ); |
970
|
|
|
|
|
|
|
} |
971
|
|
|
|
|
|
|
|
972
|
2
|
100
|
|
|
|
6
|
if( my $parent = $self->parent ) { |
973
|
1
|
|
|
|
|
7
|
return $parent->invoke_error( @_ ); |
974
|
|
|
|
|
|
|
} |
975
|
|
|
|
|
|
|
|
976
|
1
|
|
|
|
|
7
|
die "$message\n"; |
977
|
|
|
|
|
|
|
} |
978
|
|
|
|
|
|
|
|
979
|
|
|
|
|
|
|
=head1 AUTHOR |
980
|
|
|
|
|
|
|
|
981
|
|
|
|
|
|
|
Paul Evans |
982
|
|
|
|
|
|
|
|
983
|
|
|
|
|
|
|
=cut |
984
|
|
|
|
|
|
|
|
985
|
|
|
|
|
|
|
0x55AA; |