File Coverage

blib/lib/Ekahau/Events.pm
Criterion Covered Total %
statement 3 3 100.0
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 4 4 100.0


line stmt bran cond sub pod time code
1             package Ekahau::Events;
2 2     2   26130 use base 'Ekahau::Base'; our $VERSION = $Ekahau::Base::VERSION;
  2         4  
  2         244  
3              
4             # Written by Scott Gifford
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             use warnings;
10             use strict;
11              
12             use base 'Exporter';
13             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             our @EXPORT_OK = (@{$EXPORT_TAGS{events}});
24              
25             use constant EKAHAU_EVENT_DEVICE_LIST => 'DEVICE_LIST';
26             use constant EKAHAU_EVENT_DEVICE_PROPERTIES => 'DEVICE_PROPERTIES';
27             use constant EKAHAU_EVENT_AREA_ESTIMATE => 'AREA_ESTIMATE';
28             use constant EKAHAU_EVENT_LOCATION_ESTIMATE => 'LOCATION_ESTIMATE';
29             use constant EKAHAU_EVENT_LOCATION_CONTEXT => 'CONTEXT';
30             use constant EKAHAU_EVENT_MAP_IMAGE => 'MAP';
31             use constant EKAHAU_EVENT_AREA_LIST => 'AREALIST';
32             use constant EKAHAU_EVENT_STOP_AREA_TRACK_OK => 'STOP_AREA_TRACK_OK';
33             use constant EKAHAU_EVENT_STOP_LOCATION_TRACK_OK => 'STOP_LOCATION_TRACK_OK';
34             use constant EKAHAU_EVENT_ERROR => 'ERROR';
35             use constant EKAHAU_EVENT_ANY => '';
36             use constant EKAHAU_EVENT_ANY_TAG => '';
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 class provides an event-driven interface to the
46             Ekahau location sensing system's YAX protocol.
47              
48             This class inherits from L, and you can use methods from
49             that class. An alternative, synchronous interface is provided by the
50             L 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.
58              
59             =head2 Constructor
60              
61             This class uses L 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, events with any value for that property
73             will be handled by C<$handler>; if both are C, any event will
74             be handled by the given C<$handler>.
75              
76             When L 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. If none of these match, the event is ignored.
81              
82             If an event matches, C<$handler> will be called with an
83             L 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 when you no longer need to handle the event.
88             If you're just handling a single event one time, consider using
89             L, which automatically unregisters the event
90             afterwards.
91              
92             =cut
93              
94             sub register_handler
95             {
96             my $self = shift;
97             my($tag,$event,$handler) = @_;
98            
99             $tag ||= '';
100             $event ||= '';
101            
102             warn 'EVENTS: '.($handler?"Registered":"Unregistered")." event for tag '$tag', event '$event'\n"
103             if ($ENV{EVENTS_VERBOSE});
104             $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, 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             {
120             my $self = shift;
121             my($tag,$event,$handler) = @_;
122              
123             $self->register_handler($tag,$event,sub { $self->unregister_handler($tag,$event); $handler->(@_); });
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             {
134             my $self = shift;
135             $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 method or select on the
143             filehandles returned by L.
144              
145             =cut
146              
147             sub dispatch
148             {
149             my $self = shift;
150              
151             $self->readsome();
152             while (my $ev = $self->getpending)
153             {
154             $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 to do it for you.
162              
163             =cut
164              
165             sub dispatchone
166             {
167             my $self = shift;
168             my($ev)=@_;
169             my $handler;
170              
171             my $cmd = $ev->error ? 'ERROR' : $ev->{cmd};
172             my $tag = $ev->{tag};
173              
174             if ( ($handler = $self->{_handler}{$tag}{$cmd})
175             or ($handler = $self->{_handler}{$tag}{''})
176             or ($handler = $self->{_handler}{''}{$cmd})
177             or ($handler = $self->{_handler}{''}{''}))
178             {
179             $handler->($ev,$tag,$cmd);
180             }
181             # Unhandled event, just ignore.
182             }
183              
184             =head1 AUTHOR
185              
186             Scott Gifford Egifford@umich.eduE, Esgifford@suspectclass.comE
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, L.
197              
198             =cut
199              
200              
201             1;