Debian Codegraph @

lib/Sys/Virt/EventImpl.pm

raw
1 # -*- perl -*-
2 #
3 # Copyright (C) 2006 Red Hat
4 # Copyright (C) 2006-2007 Daniel P. Berrange
5 #
6 # This program is free software; You can redistribute it and/or modify
7 # it under either:
8 #
9 # a) the GNU General Public License as published by the Free
10 # Software Foundation; either version 2, or (at your option) any
11 # later version,
12 #
13 # or
14 #
15 # b) the "Artistic License"
16 #
17 # The file "LICENSE" distributed along with this file provides full
18 # details of the terms and conditions of the two licenses.
19
20 =pod
21
22 =head1 NAME
23
24 Sys::Virt::EventImpl - Event loop implementation parent class
25
26 =head1 DESCRIPTION
27
28 The C<Sys::Virt::EventImpl> module represents the contract for integrating
29 libvirt with an event loop. This package is abstract and intended to
30 be subclassed to provide an actual implementation.
31
32 This module is new in 9.5.0. To use this module while also supporting pre-9.5.0
33 versions, could may consider the code below to set up inheritance.
34
35
36 package YourImpl;
37
38 use strict;
39 use warnings;
40
41 BEGIN {
42 if (require Sys::Virt::EventImpl) {
43 eval "use parent 'Sys::Virt::EventImpl';";
44 }
45 else {
46 eval "use parent 'Sys::Virt::Event';";
47 }
48 }
49
50 =cut
51
52 package Sys::Virt::EventImpl;
53
54 use strict;
55 use warnings;
56
57
58 =head1 EVENT LOOP IMPLEMENTATION
59
60 When implementing an event loop, the implementation must be a sub-class
61 of the C<Sys::Virt::EventImpl> class. It must override these functions:
62
63
64 =over 4
65
66 =item my $watch_id = $self->add_handle( $fd, $events, $callback, $opaque, $ff )
67
68 Registers C<$callback> to be invoked for C<$events> on C<$fd>. Use
69 C<_run_handle_callback> to do the actual invocation from the event
70 loop (see L</"Callback helpers"> below).
71
72 Next to the events explicitly indicated by C<$events>,
73 C<Sys::Virt::Event::HANDLE_ERROR> and C<Sys::Virt::Event::HANDLE_HANGUP>
74 should I<always> trigger the callback.
75
76 Returns a positive integer C<$watch_id> or C<-1> on error.
77
78 =item $self->update_handle($watch_id, $events)
79
80 Replaces the events currencly triggering C<$watch_id> with C<$events>.
81
82 =item my $ret = $self->remove_handle($watch_id)
83
84 Removes the C<$callback> from the C<$fd>.
85
86 Returns C<0> on success or C<-1> on failure.
87
88 B<IMPORTANT> This should also make sure that C<_free_callback_opaque> is
89 called I<after> this function has been executed: not doing so will prevent
90 the connection from being garbage collected.
91
92 =item my $timer_id = $self->add_timeout($frequency, $callback, $opaque, $ff)
93
94 =item $self->update_timeout($timer_id, $frequency)
95
96 Replaces the interval on the timer with C<$frequency>.
97
98 =item my $ret = $self->remove_timeout($timer_id)
99
100 Discards the timer.
101
102 Returns C<0> on success or C<-1> on failure.
103
104 B<IMPORTANT> This should also make sure that C<_free_callback_opaque> is
105 called I<after> this function has been executed: not doing so will prevent
106 the connection from being garbage collected.
107
108 =back
109
110 =cut
111
112 sub add_handle {
113 my $self = shift;
114 die ref($self) . " must provide an impl of 'add_handle'";
115 }
116 sub update_handle {
117 my $self = shift;
118 die ref($self) . " must provide an impl of 'update_handle'";
119 }
120 sub remove_handle {
121 my $self = shift;
122 die ref($self) . " must provide an impl of 'remove_handle'";
123 }
124 sub add_timeout {
125 my $self = shift;
126 die ref($self) . " must provide an impl of 'add_timeout'";
127 }
128 sub update_timeout {
129 my $self = shift;
130 die ref($self) . " must provide an impl of 'update_timeout'";
131 }
132 sub remove_timeout {
133 my $self = shift;
134 die ref($self) . " must provide an impl of 'remove_timeout'";
135 }
136
137 =head2 Callback helpers
138
139 =over 4
140
141 =item $self->_run_handle_callback($watch_id, $fd, $events, $cb, $opaque)
142
143 A helper method for executing a callback in response to one of more
144 C<$events> on the file handle C<$fd>. The C<$watch> number is the
145 unique identifier associated with the file descriptor. The C<$cb>
146 and C<$opaque> parameters are the callback and data registered for
147 the handle.
148
149 =cut
150
151 sub _run_handle_callback {
152 my $self = shift;
153 my $watch = shift;
154 my $fd = shift;
155 my $events = shift;
156 my $cb = shift;
157 my $opaque = shift;
158 _run_handle_callback_helper($watch, $fd, $events, $cb, $opaque);
159 }
160
161 =item $self->_run_timeout_callback($timer_id, $cb, $opaque)
162
163 A helper method for executing a callback in response to the
164 expiry of a timeout identified by C<$timer>. The C<$cb>
165 and C<$opaque> parameters are the callback and data registered for
166 the timeout.
167
168 =cut
169
170 sub _run_timeout_callback {
171 my $self = shift;
172 my $timer = shift;
173 my $cb = shift;
174 my $opaque = shift;
175 _run_timeout_callback_helper($timer, $cb, $opaque);
176 }
177
178 =item $self->_free_callback_opaque($ff, $opaque)
179
180 A helper method for freeing the data associated with a callback.
181 The C<$ff> and C<$opaque> parameters are the callback and data
182 registered for the handle/timeout.
183
184 B<IMPORTANT> This helper must be called outside of any callbacks; that is
185 I<after> the C<remove_handle> or C<remove_timeout> callbacks complete.
186
187 =cut
188
189 sub _free_callback_opaque {
190 my $self = shift;
191 my $ff = shift;
192 my $opaque = shift;
193 _free_callback_opaque_helper($ff, $opaque);
194 }
195
196 1;
197
198 =back
199
200 =head1 AUTHORS
201
202 Daniel P. Berrange <[email protected]>
203
204 =head1 COPYRIGHT
205
206 Copyright (C) 2006-2009 Red Hat
207 Copyright (C) 2006-2009 Daniel P. Berrange
208
209 =head1 LICENSE
210
211 This program is free software; you can redistribute it and/or modify
212 it under the terms of either the GNU General Public License as published
213 by the Free Software Foundation (either version 2 of the License, or at
214 your option any later version), or, the Artistic License, as specified
215 in the Perl README file.
216
217 =head1 SEE ALSO
218
219 L<Sys::Virt>, C<http://libvirt.org>
220
221 =cut