Pilot/EasyEvent.pm

filter: Statementstmt 100% 66 / 66 Branchbran 80.0% 8 / 10 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 13 / 13 Podpod 50.0% 2 / 4 totaltotal 95.8% 92 / 96 CC 9 SCAR 8.9
CC
9
Cov
97%
send_event
5.0
add_event
3.0
_convert_time_units
2.0
CRAP 2.4
« Pilot/Logger.pm ↑ Summary Pilot/Mock/Commands.pm »
1 # Copyright (c) 2014 Timm Murray
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are met:
6 #
7 # * Redistributions of source code must retain the above copyright notice,
8 # this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution.
12 #
13 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23 # POSSIBILITY OF SUCH DAMAGE.
24 package UAV::Pilot::EasyEvent;
25 2 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 #with 'MooseX::Clone';
30  
31  
32 use constant {
33 2 UNITS_MILLISECOND => 0,
34 2 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 2 has 'condvar' => (
37 is => 'ro',
38 isa => 'AnyEvent::CondVar',
39 );
40 has '_timers' => (
41 traits => [ 'Array' ],
42 is => 'ro',
43 isa => 'ArrayRef[HashRef[Any]]',
44 5 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 2 handles => {
46 _add_timer => 'push',
47 },
48 );
49 has '_events' => (
50 traits => [ 'Hash' ],
51 is => 'ro',
52 isa => 'HashRef[ArrayRef[HashRef[Item]]]',
53 5 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
54 2 handles => {
55 '_set_event_callbacks' => 'set',
56 '_event_type_exists' => 'exists',
57 '_get_event_callbacks' => 'get',
58 },
59 );
60  
61  
62 sub add_timer
63 {
64 4 ▶ my ($self, $args) = @_;
Subroutine add_timer: called CC=1 SCAR=0.0
Pod documented
65 4 my $duration = $$args{duration};
66 4 my $duration_units = $$args{duration_units};
67 4 my $callback = $$args{cb};
68  
69 4 my $true_time = $self->_convert_time_units( $duration, $duration_units );
70 4 my $new_self = ref($self)->new({
71 condvar => $self->condvar,
72 });
73  
74 4 $self->_add_timer({
75 time => $true_time,
76 cb => $callback,
77 child_events => $new_self,
78 });
79  
80 4 return $new_self;
81 }
82  
83 sub add_event
84 {
85 4 ▶ my ($self, $name, $callback, $is_oneoff) = @_;
Subroutine add_event: called CC=3 SCAR=11.0
Pod undocumented
86 4 ▶ $is_oneoff //= 0;
Condition100%2/2
$is_oneoff //= 0
l!l
13
MC/DC100%1/1
$is_oneoff //= 0
$is_oneoff
Truth table100%2/2
A
A= $is_oneoff
Aresult
11
00
87  
88 4 my @callbacks;
89 4 ▶ if( $self->_event_type_exists( $name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_event_type_exists($name)) { } 2 2 4
90 2 @callbacks = @{ $self->_get_event_callbacks( $name ) };
91 }
92 else {
93 2 @callbacks = ();
94 }
95  
96 4 push @callbacks, {
97 callback => $callback,
98 is_one_off => $is_oneoff,
99 };
100 4 $self->_set_event_callbacks( $name => \@callbacks );
101  
102 4 return 1;
103 }
104  
105 sub send_event
106 {
107 3 ▶ my ($self, $name, @args) = @_;
Subroutine send_event: called CC=5 SCAR=16.1
Pod undocumented
108 3 my $callbacks = $self->_get_event_callbacks( $name );
109 3 ▶ return 1 unless defined $callbacks;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $callbacks 0 3 3
110 3 my @callbacks = (@$callbacks);
111 3 my $is_callbacks_changed = 0;
112  
113 3 foreach my $i (0 .. $#callbacks) {
114 # Always modify the *original* arrayref $callbacks here, not the
115 # copy @callbacks. If we splice out a one-off, @callbacks will be
116 # changed and the index will be off.
117 6 my $cb = $callbacks->[$i]{callback};
118 6 my $is_one_off = $callbacks->[$i]{is_one_off};
119 6 $cb->(@args);
120  
121 6 ▶ if( $is_one_off ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_one_off) 1 5 6
122 1 splice @callbacks, $i, 1;
123 1 $is_callbacks_changed = 1;
124 }
125 }
126  
127 3 ▶ $self->_set_event_callbacks( $name => \@callbacks) if $is_callbacks_changed;
Branch: 1 branch
BranchTrueFalseTotal
if $is_callbacks_changed 1 2 3
128 3 return 1;
129 }
130  
131 sub init_event_loop
132 {
133 5 ▶ my ($self) = @_;
Subroutine init_event_loop: called CC=2 SCAR=6.9
Pod documented
134  
135 5 foreach my $timer_def (@{ $self->_timers }) {
136 4 my $timer; $timer = AnyEvent->timer(
137 after => $timer_def->{time},
138 cb => sub {
139 4 ▶ $timer_def->{cb}->();
Subroutine __ANON__: called CC=1 SCAR=0.0
140 4 $timer_def->{child_events}->init_event_loop;
141 4 $timer;
142 },
143 4 );
144 }
145  
146 5 return 1;
147 }
148  
149  
150 sub _convert_time_units
151 {
152 4 ▶ my ($self, $time, $unit) = @_;
Subroutine _convert_time_units: called CC=2 SCAR=7.0
153  
154 4 ▶ if( $self->UNITS_MILLISECOND == $unit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->UNITS_MILLISECOND == $unit) 4 0 4
155 4 $time /= 1000;
156 }
157  
158 4 return $time;
159 }
160  
161  
162 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
163 2 __PACKAGE__->meta->make_immutable;
164 2 1;
165 __END__
166  
167  
168 =head1 NAME
169  
170 UAV::Pilot::EasyEvent
171  
172 =head1 SYNOPSIS
173  
174 my $cv = AnyEvent->condvar;
175 my $event = UAV::Pilot::EasyEvent->new({
176 condvar => $cv,
177 });
178
179 my @events;
180 my $event2 = $event->add_timer({
181 duration => 100,
182 duration_units => $event->UNITS_MILLISECOND,
183 cb => sub {
184 push @events => 'First event',
185 },
186 })->add_timer({
187 duration => 10,
188 duration_units => $event->UNITS_MILLISECOND,
189 cb => sub {
190 push @events => 'Second event',
191 },
192 });
193
194 $event2->add_timer({
195 duration => 50,
196 duration_units => $event->UNITS_MILLISECOND,
197 cb => sub {
198 push @events => 'Fourth event',
199 $cv->send;
200 },
201 });
202 $event2->add_timer({
203 duration => 10,
204 duration_units => $event->UNITS_MILLISECOND,
205 cb => sub {
206 push @events => 'Third event',
207 },
208 });
209
210
211 $event->init_event_loop;
212 $cv->recv;
213
214 # After time passes, prints:
215 # First event
216 # Second event
217 # Third event
218 # Fourth event
219 #
220 say $_ for @events;
221  
222 =head1 DESCRIPTION
223  
224 C<AnyEvent> is the standard event framework used for C<UAV::Pilot>. However, its
225 interface isn't convenient for some of the typical things done for UAV piloting. For
226 instance, to put the code into plain English, we might want to say:
227  
228 Takeoff, wait 5 seconds, then pitch forward for 2 seconds, then pitch backwards
229 for 2 seconds, then land
230  
231 In the usual C<AnyEvent> interface, this requires building the timers inside the callbacks
232 of other timers, which leads to several levels of indentation. C<UAV::Pilot::EasyEvent>
233 simplifies the handling and syntax of this kind of event workflow.
234  
235 =head1 METHODS
236  
237 =head2 new
238  
239 new({
240 condvar => $cv,
241 })
242  
243 Constructor. The C<condvar> argument should be an C<AnyEvent::CondVar>.
244  
245 =head2 add_timer
246  
247 add_timer({
248 duration => 100,
249 duration_units => $event->UNITS_MILLISECOND,
250 cb => sub { ... },
251 })
252  
253 Add a timer to run in the event loop. It will run after C<duration> units of time, with
254 the units specified by C<duration_units>. The C<cb> parameter is a reference to a
255 subroutine to use as a callback.
256  
257 Returns a child C<EasyEvent> object. When the timer above has finished, any timers on
258 child objects will be setup for execution. This makes it easy to chain timers to run
259 after each other.
260  
261 =head2 init_event_loop
262  
263 This method must be called after running a series of C<add_timer()> calls. You only need
264 to call this on the root object, not the children.
265  
266 You must call C<recv> on the C<condvar> yourself.
267  
268 =head1 add_event
269  
270 add_event( 'foo', sub {...}, 0 )
271  
272 Add a subref that will be called when the named event is fired off. The
273 first parameter is the name of the event, and the second is the subref.
274  
275 The third is optional, and specifies if the call will be a "one-off" or not.
276 If it's a one-off, then after the first call to the sub, it will be removed
277 from list of callbacks. Defaults to false.
278  
279 The callback will receive the arguments that were passed to C<send_event()>
280 when the event is triggered.
281  
282 =head1 send_event
283  
284 send_event( 'foo', @args )
285  
286 Trigger an event with the given name. The first arg is the name of the event.
287 All subsequent args will be passed to the callbacks attached to that event
288 name.
289  
290 =cut
« Pilot/Logger.pm ↑ Summary Pilot/Mock/Commands.pm »