lib/Ekahau/Events.pm

filter: Statementstmt 100% 77 / 77 Branchbran 70.0% 7 / 10 Conditioncond 100% 13 / 13 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 22 / 22 Podpod 100% 5 / 5 totaltotal 97.7% 130 / 133 CC 11 SCAR 9.8
CC
11
Cov
97%
dispatchone
6.0
register_handler
5.2
dispatch
2.0
CRAP 2.7
« lib/Ekahau/ErrHandler.pm ↑ Summary lib/Ekahau/License.pm »
1 package Ekahau::Events;
2 3 ▶use base 'Ekahau::Base'; our $VERSION = $Ekahau::Base::VERSION;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 # Written by Scott Gifford <gifford@umich.edu>
5 # Copyright (C) 2004 The Regents of the University of Michigan.
6 # See the file LICENSE included with the distribution for license
7 # information.
8  
9 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 our %EXPORT_TAGS = (events => [ qw(
14  
15 EKAHAU_EVENT_DEVICE_LIST EKAHAU_EVENT_DEVICE_PROPERTIES
16 EKAHAU_EVENT_AREA_ESTIMATE EKAHAU_EVENT_LOCATION_ESTIMATE
17 EKAHAU_EVENT_LOCATION_CONTEXT EKAHAU_EVENT_MAP_IMAGE
18 EKAHAU_EVENT_AREA_LIST EKAHAU_EVENT_STOP_AREA_TRACK_OK
19 EKAHAU_EVENT_STOP_LOCATION_TRACK_OK
20 EKAHAU_EVENT_ERROR
21 EKAHAU_EVENT_ANY EKAHAU_EVENT_ANY_TAG
22 )]);
23 3 our @EXPORT_OK = (@{$EXPORT_TAGS{events}});
24  
25 3 ▶use constant EKAHAU_EVENT_DEVICE_LIST => 'DEVICE_LIST';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use constant EKAHAU_EVENT_DEVICE_PROPERTIES => 'DEVICE_PROPERTIES';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use constant EKAHAU_EVENT_AREA_ESTIMATE => 'AREA_ESTIMATE';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use constant EKAHAU_EVENT_LOCATION_ESTIMATE => 'LOCATION_ESTIMATE';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use constant EKAHAU_EVENT_LOCATION_CONTEXT => 'CONTEXT';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 3 ▶use constant EKAHAU_EVENT_MAP_IMAGE => 'MAP';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 3 ▶use constant EKAHAU_EVENT_AREA_LIST => 'AREALIST';
Subroutine BEGIN: called CC=1 SCAR=0.0
32 3 ▶use constant EKAHAU_EVENT_STOP_AREA_TRACK_OK => 'STOP_AREA_TRACK_OK';
Subroutine BEGIN: called CC=1 SCAR=0.0
33 3 ▶use constant EKAHAU_EVENT_STOP_LOCATION_TRACK_OK => 'STOP_LOCATION_TRACK_OK';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 3 ▶use constant EKAHAU_EVENT_ERROR => 'ERROR';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 3 ▶use constant EKAHAU_EVENT_ANY => '';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 3 ▶use constant EKAHAU_EVENT_ANY_TAG => '';
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38  
39 =head1 NAME
40  
41 Ekahau::Events - Event-driven interface to Ekahau location sensing system
42  
43 =head1 SYNOPSIS
44  
45 The C<Ekahau::Events> class provides an event-driven interface to the
46 Ekahau location sensing system's YAX protocol.
47  
48 This class inherits from L<Ekahau::Base|Ekahau::Base>, and you can use methods from
49 that class. An alternative, synchronous interface is provided by the
50 L<Ekahau|Ekahau> class.
51  
52 =head1 DESCRIPTION
53  
54 This class implements methods for registering event handlers to
55 receive particular Ekahau responses, and dispatching events based on
56 responses received from Ekahau. Requests can be sent using methods
57 available from L<Ekahau::Base|Ekahau::Base>.
58  
59 =head2 Constructor
60  
61 This class uses L<Ekahau::Base::new|Ekahau::Base/new> as its constructor.
62  
63 =head2 Methods
64  
65 =head3 register_handler ( $tag, $event, $handler )
66  
67 Registers an event handling sub for the given tag and event. C<$tag>
68 and C<$event> should be strings representing the tag and event to be
69 handled, and C<$handler> is a subroutine reference.
70  
71 Both the tag and event must match for the handler to be called. If
72 one or the other is C<undef>, events with any value for that property
73 will be handled by C<$handler>; if both are C<undef>, any event will
74 be handled by the given C<$handler>.
75  
76 When L<dispatchone|/dispatchone> is looking for an event handler, it will first
77 look for a registered handler matching both C<$tag> and C<$event>,
78 then matching just C<$tag>, then matching just C<$event>, and finally
79 the "default handler" registered with both C<$tag> and C<$event> set
80 to C<undef>. If none of these match, the event is ignored.
81  
82 If an event matches, C<$handler> will be called with an
83 L<Ekahau::Response|Ekahau::Response> object as the first parameter, followed by the
84 tag, followed by the event.
85  
86 Each handler takes up a small amount of memory, so make sure you call
87 L<unregister_handler|/unregister_handler> when you no longer need to handle the event.
88 If you're just handling a single event one time, consider using
89 L<register_handler_once|/register_handler_once>, which automatically unregisters the event
90 afterwards.
91  
92 =cut
93  
94 sub register_handler
95 32 ▶{
Subroutine register_handler: called CC=5 SCAR=16.6
Pod documented
96 32 my $self = shift;
97 32 my($tag,$event,$handler) = @_;
98
99 32 ▶ $tag ||= '';
Condition100%2/2
$tag ||= ""
l!l
2210
MC/DC100%1/1
$tag ||= ""
$tag
Truth table100%2/2
A
A= $tag
Aresult
11
00
100 32 ▶ $event ||= '';
Condition100%2/2
$event ||= ""
l!l
1616
MC/DC100%1/1
$event ||= ""
$event
Truth table100%2/2
A
A= $event
Aresult
11
00
101
102 warn 'EVENTS: '.($handler?"Registered":"Unregistered")." event for tag '$tag', event '$event'\n"
103 32 ▶ if ($ENV{EVENTS_VERBOSE});
Branch: 2 branches
BranchTrueFalseTotal
if $ENV{"EVENTS_VERBOSE"} 0 32 32
$handler ? : 0 0 0
104 32 $self->{_handler}{$tag}{$event} = $handler;
105 }
106  
107 =head3 register_handler_once ( $tag, $event, $handler )
108  
109 Registers an event handling sub for the given tag and event, just like
110 L<register_handler|/register_handler>, but when the event completes automatically
111 unregisters the handler.
112  
113 This is useful for simple requests with simple responses, to avoid
114 leaking memory.
115  
116 =cut
117  
118 sub register_handler_once
119 13 ▶{
Subroutine register_handler_once: called CC=1 SCAR=0.0
Pod documented
120 13 my $self = shift;
121 13 my($tag,$event,$handler) = @_;
122  
123 13 ▶ $self->register_handler($tag,$event,sub { $self->unregister_handler($tag,$event); $handler->(@_); });
Subroutine __ANON__: called CC=1 SCAR=0.0
124 }
125  
126 =head3 unregister_handler ( $tag, $event )
127  
128 Unregister the handler for the given C<$tag> and C<$event>.
129  
130 =cut
131  
132 sub unregister_handler
133 16 ▶{
Subroutine unregister_handler: called CC=1 SCAR=0.0
Pod documented
134 16 my $self = shift;
135 16 $self->register_handler($_[0],$_[1],undef);
136 }
137  
138 =head3 dispatch ( )
139  
140 Read pending events from the Ekahau server, and call the registered
141 handler for each of them. This call will block; to avoid that, you
142 should first use the L<Ekahau::Base::can_read|Ekahau::Base/can_read> method or select on the
143 filehandles returned by L<Ekahau::Base::select_handles|Ekahau::Base/select_handles>.
144  
145 =cut
146  
147 sub dispatch
148 25 ▶{
Subroutine dispatch: called CC=2 SCAR=6.9
Pod documented
149 25 my $self = shift;
150  
151 25 $self->readsome();
152 25 ▶ while (my $ev = $self->getpending)
Branch: 1 branch
BranchTrueFalseTotal
if (my $ev = $self->getpending) 25 25 50
153 {
154 25 $self->dispatchone($ev);
155 }
156 }
157  
158 =head3 dispatchone ( $event )
159  
160 Dispatch a single event to the appropriate handler. Generally you
161 won't call this yourself, relying on L<dispatch|/dispatch> to do it for you.
162  
163 =cut
164  
165 sub dispatchone
166 25 ▶{
Subroutine dispatchone: called CC=6 SCAR=17.9
Pod documented
167 25 my $self = shift;
168 25 my($ev)=@_;
169 25 my $handler;
170  
171 25 ▶ my $cmd = $ev->error ? 'ERROR' : $ev->{cmd};
Branch: 1 branch
BranchTrueFalseTotal
$ev->error ? : 3 22 25
172 25 my $tag = $ev->{tag};
173  
174 25 ▶ if ( ($handler = $self->{_handler}{$tag}{$cmd})
Branch: 1 branch
BranchTrueFalseTotal
if ($handler = $self->{"_handler"}{$tag}{$cmd} or $handler = $self->{"_handler"}{$tag}{""} or $handler = $self->{"_handler"}{""}{$cmd} or $handler = $self->{"_handler"}{""}{""}) 23 2 25
Condition100%9/9
$handler = $self->{"_handler"}{$tag}{$cmd} or $handler = $self->{"_handler"}{$tag}{""} or $handler = $self->{"_handler"}{""}{$cmd} or $handler = $self->{"_handler"}{""}{""}
l!l&&r!l&&!r
2122
$handler = $self->{"_handler"}{$tag}{$cmd} or $handler = $self->{"_handler"}{$tag}{""} or $handler = $self->{"_handler"}{""}{$cmd}
l!l&&r!l&&!r
1654
$handler = $self->{"_handler"}{$tag}{$cmd} or $handler = $self->{"_handler"}{$tag}{""}
l!l&&r!l&&!r
5119
MC/DC100%4/4
$handler = $self->{"_handler"}{$tag}{$cmd} or $handler = $self->{"_handler"}{$tag}{""} or $handler = $self->{"_handler"}{""}{$cmd} or $handler = $self->{"_handler"}{""}{""}
$handler = $self->{"_handler"}{$tag}{$cmd} $handler = $self->{"_handler"}{$tag}{""} $handler = $self->{"_handler"}{""}{$cmd} $handler = $self->{"_handler"}{""}{""}
Truth table100%5/5
A or B or C or D
A= $handler = $self->{"_handler"}{$tag}{$cmd}B= $handler = $self->{"_handler"}{$tag}{""}C= $handler = $self->{"_handler"}{""}{$cmd}D= $handler = $self->{"_handler"}{""}{""}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
175 or ($handler = $self->{_handler}{$tag}{''})
176 or ($handler = $self->{_handler}{''}{$cmd})
177 or ($handler = $self->{_handler}{''}{''}))
178 {
179 23 $handler->($ev,$tag,$cmd);
180 }
181 # Unhandled event, just ignore.
182 }
183  
184 =head1 AUTHOR
185  
186 Scott Gifford E<lt>gifford@umich.eduE<gt>, E<lt>sgifford@suspectclass.comE<gt>
187  
188 Copyright (C) 2005 The Regents of the University of Michigan.
189  
190 See the file LICENSE included with the distribution for license
191 information.
192  
193  
194 =head1 SEE ALSO
195  
196 L<Ekahau::Base|Ekahau::Base>, L<Ekahau|Ekahau>.
197  
198 =cut
199  
200  
201 3 1;
« lib/Ekahau/ErrHandler.pm ↑ Summary lib/Ekahau/License.pm »