1Net::DBus::Reactor(3) User Contributed Perl DocumentationNet::DBus::Reactor(3)
2
3
4
6 Net::DBus::Reactor - application event loop
7
9 Create and run an event loop:
10
11 use Net::DBus::Reactor;
12 my $reactor = Net::DBus::Reactor->main();
13
14 $reactor->run();
15
16 Manage some file handlers
17
18 $reactor->add_read($fd,
19 Net::DBus::Callback->new(method => sub {
20 my $fd = shift;
21 ...read some data...
22 }, args => [$fd]));
23
24 $reactor->add_write($fd,
25 Net::DBus::Callback->new(method => sub {
26 my $fd = shift;
27 ...write some data...
28 }, args => [$fd]));
29
30 Temporarily (dis|en)able a handle
31
32 # Disable
33 $reactor->toggle_read($fd, 0);
34 # Enable
35 $reactor->toggle_read($fd, 1);
36
37 Permanently remove a handle
38
39 $reactor->remove_read($fd);
40
41 Manage a regular timeout every 100 milliseconds
42
43 my $timer = $reactor->add_timeout(100,
44 Net::DBus::Callback->new(
45 method => sub {
46 ...process the alarm...
47 }));
48
49 Temporarily (dis|en)able a timer
50
51 # Disable
52 $reactor->toggle_timeout($timer, 0);
53 # Enable
54 $reactor->toggle_timeout($timer, 1);
55
56 Permanently remove a timer
57
58 $reactor->remove_timeout($timer);
59
60 Add a post-dispatch hook
61
62 my $hook = $reactor->add_hook(Net::DBus::Callback->new(
63 method => sub {
64 ... do some work...
65 }));
66
67 Remove a hook
68
69 $reactor->remove_hook($hook);
70
72 This class provides a general purpose event loop for the purposes of
73 multiplexing I/O events and timeouts in a single process. The
74 underlying implementation is done using the select system call. File
75 handles can be registered for monitoring on read, write and exception
76 (out-of-band data) events. Timers can be registered to expire with a
77 periodic frequency. These are implemented using the timeout parameter
78 of the select system call. Since this parameter merely represents an
79 upper bound on the amount of time the select system call is allowed to
80 sleep, the actual period of the timers may vary. Under normal load this
81 variance is typically 10 milliseconds. Finally, hooks may be
82 registered which will be invoked on each iteration of the event loop
83 (ie after processing the file events, or timeouts indicated by the
84 select system call returning).
85
87 my $reactor = Net::DBus::Reactor->new();
88 Creates a new event loop ready for monitoring file handles, or
89 generating timeouts. Except in very unusual circumstances (examples
90 of which I can't think up) it is not necessary or desriable to
91 explicitly create new reactor instances. Instead call the main
92 method to get a handle to the singleton instance.
93
94 $reactor = Net::DBus::Reactor->main;
95 Return a handle to the singleton instance of the reactor. This is
96 the recommended way of getting hold of a reactor, since it removes
97 the need for modules to pass around handles to their privately
98 created reactors.
99
100 $reactor->manage($connection);
101 $reactor->manage($server);
102 Registers a "Net::DBus::Binding::Connection" or
103 "Net::DBus::Binding::Server" object for management by the event
104 loop. This basically involves hooking up the watch & timeout
105 callbacks to the event loop. For connections it will also register
106 a hook to invoke the "dispatch" method periodically.
107
108 $reactor->run();
109 Starts the event loop monitoring any registered file handles and
110 timeouts. At least one file handle, or timer must have been
111 registered prior to running the reactor, otherwise it will
112 immediately exit. The reactor will run until all registered file
113 handles, or timeouts have been removed, or disabled. The reactor
114 can be explicitly stopped by calling the "shutdown" method.
115
116 $reactor->shutdown();
117 Explicitly shutdown the reactor after pending events have been
118 processed.
119
120 $reactor->step();
121 Perform one iteration of the event loop, going to sleep until an
122 event occurs on a registered file handle, or a timeout occurrs.
123 This method is generally not required in day-to-day use.
124
125 $reactor->add_read($fd, $callback[, $status]);
126 Registers a file handle for monitoring of read events. The
127 $callback parameter specifies either a code reference to a
128 subroutine, or an instance of the "Net::DBus::Callback" object to
129 invoke each time an event occurs. The optional $status parameter is
130 a boolean value to specify whether the watch is initially enabled.
131
132 $reactor->add_write($fd, $callback[, $status]);
133 Registers a file handle for monitoring of write events. The
134 $callback parameter specifies either a code reference to a
135 subroutine, or an instance of the "Net::DBus::Callback" object to
136 invoke each time an event occurs. The optional $status parameter is
137 a boolean value to specify whether the watch is initially enabled.
138
139 $reactor->add_exception($fd, $callback[, $status]);
140 Registers a file handle for monitoring of exception events. The
141 $callback parameter specifies either a code reference to a
142 subroutine, or an instance of the "Net::DBus::Callback" object to
143 invoke each time an event occurs. The optional $status parameter is
144 a boolean value to specify whether the watch is initially enabled.
145
146 my $id = $reactor->add_timeout($interval, $callback, $status);
147 Registers a new timeout to expire every $interval milliseconds. The
148 $callback parameter specifies either a code reference to a
149 subroutine, or an instance of the "Net::DBus::Callback" object to
150 invoke each time the timeout expires. The optional $status
151 parameter is a boolean value to specify whether the timeout is
152 initially enabled. The return parameter is a unique identifier
153 which can be used to later remove or disable the timeout.
154
155 $reactor->remove_timeout($id);
156 Removes a previously registered timeout specified by the $id
157 parameter.
158
159 $reactor->toggle_timeout($id, $status[, $interval]);
160 Updates the state of a previously registered timeout specified by
161 the $id parameter. The $status parameter specifies whether the
162 timeout is to be enabled or disabled, while the optional $interval
163 parameter can be used to change the period of the timeout.
164
165 my $id = $reactor->add_hook($callback[, $status]);
166 Registers a new hook to be fired on each iteration of the event
167 loop. The $callback parameter specifies either a code reference to
168 a subroutine, or an instance of the "Net::DBus::Callback" class to
169 invoke. The $status parameter determines whether the hook is
170 initially enabled, or disabled. The return parameter is a unique
171 id which should be used to later remove, or disable the hook.
172
173 $reactor->remove_hook($id)
174 Removes the previously registered hook identified by $id.
175
176 $reactor->toggle_hook($id, $status)
177 Updates the status of the previously registered hook identified by
178 $id. The $status parameter determines whether the hook is to be
179 enabled or disabled.
180
181 $reactor->remove_read($fd);
182 $reactor->remove_write($fd);
183 $reactor->remove_exception($fd);
184 Removes a watch on the file handle $fd.
185
186 $reactor->toggle_read($fd, $status);
187 $reactor->toggle_write($fd, $status);
188 $reactor->toggle_exception($fd, $status);
189 Updates the status of a watch on the file handle $fd. The $status
190 parameter species whether the watch is to be enabled or disabled.
191
193 Net::DBus::Callback, Net::DBus::Connection, Net::DBus::Server
194
196 Daniel Berrange <dan@berrange.com>
197
199 Copyright 2004-2011 by Daniel Berrange
200
201
202
203perl v5.32.0 2020-07-28 Net::DBus::Reactor(3)