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