| 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::Events; | ||
| 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 | 2 | ▶ | use AnyEvent; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 29 | 2 | ▶ | use UAV::Pilot::EventHandler; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 30 | |||
| 31 | |||
| 32 | 2 | ▶ | use constant TIMER_INTERVAL => 1 / 60; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 33 | |||
| 34 | |||
| 35 | 2 | has 'condvar' => ( | |
| 36 | is => 'ro', | ||
| 37 | isa => 'AnyEvent::CondVar', | ||
| 38 | ); | ||
| 39 | has '_handlers' => ( | ||
| 40 | traits => ['Array'], | ||
| 41 | is => 'ro', | ||
| 42 | isa => 'ArrayRef[UAV::Pilot::EventHandler]', | ||
| 43 | 1 | ▶ | default => sub {[]}, |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 44 | 2 | handles => { | |
| 45 | register => 'push', | ||
| 46 | }, | ||
| 47 | ); | ||
| 48 | |||
| 49 | |||
| 50 | sub init_event_loop | ||
| 51 | { | ||
| 52 | 1 | ▶ | my ($self) = @_; |
|
Subroutine
init_event_loop: called CC=1 SCAR=0.0
Pod
documented
| |||
| 53 | |||
| 54 | 1 | my $timer; $timer = AnyEvent->timer( | |
| 55 | after => 1, | ||
| 56 | interval => $self->TIMER_INTERVAL, | ||
| 57 | cb => sub { | ||
| 58 | 1 | ▶ | $_->process_events for @{ $self->_handlers }; |
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||
| 59 | 1 | $timer; | |
| 60 | }, | ||
| 61 | 1 | ); | |
| 62 | |||
| 63 | 1 | return 1; | |
| 64 | } | ||
| 65 | |||
| 66 | |||
| 67 | 2 | ▶ | no Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 68 | 2 | __PACKAGE__->meta->make_immutable; | |
| 69 | 2 | 1; | |
| 70 | __END__ | ||
| 71 | |||
| 72 | |||
| 73 | =head1 NAME | ||
| 74 | |||
| 75 | UAV::Pilot::Events | ||
| 76 | |||
| 77 | =head1 SYNOPSIS | ||
| 78 | |||
| 79 | my $condvar = AnyEvent->condvar; | ||
| 80 | my $events = UAV::Pilot::Events->new({ | ||
| 81 | condvar => $condvar, | ||
| 82 | }); | ||
| 83 | $events->register( ... ); | ||
| 84 | $events->init_event_loop; | ||
| 85 | $condvar->recv; | ||
| 86 | |||
| 87 | =head1 DESCRIPTION | ||
| 88 | |||
| 89 | Handles event loops on a regular timer. | ||
| 90 | |||
| 91 | =head1 METHODS | ||
| 92 | |||
| 93 | =head2 new | ||
| 94 | |||
| 95 | new({ | ||
| 96 | condvar => $cv, | ||
| 97 | }) | ||
| 98 | |||
| 99 | Constructor. The C<condvar> argument is an C<AnyEvent::Condvar>. | ||
| 100 | |||
| 101 | =head2 register | ||
| 102 | |||
| 103 | register( $event_handler ) | ||
| 104 | |||
| 105 | Adds a object that does the C<UAV::Pilot::EventHandler> role to the list. The | ||
| 106 | C<process_events> method on that object will be called each time the event loop runs. | ||
| 107 | |||
| 108 | =head2 init_event_loop | ||
| 109 | |||
| 110 | Sets up the event loop. Note that you must still call C<recv> on the C<AnyEvent::Condvar> | ||
| 111 | to start the loop running. | ||
| 112 | |||
| 113 | =cut | ||