Debian Codegraph @

lib/Sys/Virt/Event.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::Event - Event loop access
25
26 =head1 DESCRIPTION
27
28 The C<Sys::Virt::Event> 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 =head1 DEPRECATED FUNCTIONALITY
33
34 Event loop implementations used to be required to be instances of
35 sub-classes of C<Sys::Virt::Event>. Instead, they are now required to
36 inherit from C<Sys::Virt::EventImpl>. For backward compatibility with
37 versions 9.4 and older, inheriting from C<Sys::Virt::Event> is still
38 supported, but will be removed mid 2025.
39
40 =head1 METHODS
41
42 =over 4
43
44 =cut
45
46 package Sys::Virt::Event;
47
48 use strict;
49 use warnings;
50
51
52 #The line below exists for 9.4 backward compat; to be removed mid-2025
53 use parent 'Sys::Virt::EventImpl';
54
55
56 our $eventimpl = undef;
57
58 =item register_default()
59
60 Register the default libvirt event loop implementation
61
62 =item run_default()
63
64 Run a single iteration of the default event loop implementation
65
66 =item register($impl)
67
68 Register an event loop implementation. The implementation should be a
69 instance of a sub-class of the C<Sys::Virt::EventImpl> package. See the
70 section L</"EVENT LOOP IMPLEMENTATION"> below for more information.
71
72 =cut
73
74 sub register {
75 my $impl = shift;
76
77 if (!(ref($impl) &&
78 $impl->isa("Sys::Virt::EventImpl"))) {
79 die "event implementation must be a subclass of Sys::Virt::EventImpl";
80 }
81 if (ref($impl) &&
82 $impl->isa("Sys::Virt::Event")) {
83 warn "DECPRECATION WARNING: event implementation is a subclass of Sys::Virt::Event";
84 }
85
86 $eventimpl = $impl;
87
88 Sys::Virt::Event::_register_impl();
89 }
90
91
92
93 sub _add_handle {
94 $eventimpl->add_handle(@_);
95 }
96 sub _update_handle {
97 $eventimpl->update_handle(@_);
98 }
99 sub _remove_handle {
100 $eventimpl->remove_handle(@_);
101 }
102 sub _add_timeout {
103 $eventimpl->add_timeout(@_);
104 }
105 sub _update_timeout {
106 $eventimpl->update_timeout(@_);
107 }
108 sub _remove_timeout {
109 $eventimpl->remove_timeout(@_);
110 }
111
112 1;
113
114 =item my $watch = Sys::Virt::Event::add_handle($fd, $events, $coderef)
115
116 Adds a watch on the file descriptor C<$fd> for the events C<$events>
117 which is a mask of the FILE HANDLE EVENTS constants listed later.
118 The C<$coderef> parameter is a subroutine to invoke when an event
119 is triggered. The subroutine will be passed three parameters, the
120 watch identifier, the file descriptor and the event mask. This
121 method returns the watch identifier which can be used to update or
122 remove the watch
123
124 =item Sys::Virt::Event::update_handle($watch, $events)
125
126 Update the event mask for the file descriptor watch C<$watch>
127 to use the events C<$events>.
128
129 =item Sys::Virt::Event::remove_handle($watch)
130
131 Remove the event mask for the file descriptor watch C<$watch>.
132
133 =item my $timer = Sys::Virt::Event::add_timeout($frequency, $coderef, $opaque)
134
135 Adds a timeout to trigger with C<$frequency> milliseconds interval.
136 The C<$coderef> parameter is a subroutine to invoke when an event
137 is triggered. The subroutine will be passed one parameter, the
138 timer identifier. This method returns the timer identifier which
139 can be used to update or remove the timer
140
141 =item Sys::Virt::Event::update_timeout($timer, $frequency)
142
143 Update the timeout C<$timer> to have the frequency C<$frequency>
144 milliseconds. The values C<0> and C<-1> have special meaning. The value C<0>
145 wants the callback to be invoked on each event loop iteration, where
146 C<-1> stops the callback from being invoked.
147
148 =item Sys::Virt::Event::remove_timeout($timer)
149
150 Remove the timeout C<$timer>
151
152 =back
153
154 =head1 CONSTANTS
155
156 =head2 FILE HANDLE EVENTS
157
158 When integrating with an event loop the following constants
159 define the file descriptor events
160
161 =over 4
162
163 =item Sys::Virt::Event::HANDLE_READABLE
164
165 The file descriptor has data available for read without blocking
166
167 =item Sys::Virt::Event::HANDLE_WRITABLE
168
169 The file descriptor has ability to write data without blocking
170
171 =item Sys::Virt::Event::HANDLE_ERROR
172
173 An error occurred on the file descriptor
174
175 =item Sys::Virt::Event::HANDLE_HANGUP
176
177 The remote end of the file descriptor closed
178
179 =back
180
181 =head1 AUTHORS
182
183 Daniel P. Berrange <[email protected]>
184
185 =head1 COPYRIGHT
186
187 Copyright (C) 2006-2009 Red Hat
188 Copyright (C) 2006-2009 Daniel P. Berrange
189
190 =head1 LICENSE
191
192 This program is free software; you can redistribute it and/or modify
193 it under the terms of either the GNU General Public License as published
194 by the Free Software Foundation (either version 2 of the License, or at
195 your option any later version), or, the Artistic License, as specified
196 in the Perl README file.
197
198 =head1 SEE ALSO
199
200 L<Sys::Virt>, C<http://libvirt.org>
201
202 =cut