Debian Codegraph @

proton-j/src/main/java/org/apache/qpid/proton/reactor/Selectable.java

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 package org.apache.qpid.proton.reactor;
23
24 import java.nio.channels.SelectableChannel;
25
26 import org.apache.qpid.proton.engine.Collector;
27 import org.apache.qpid.proton.engine.Extendable;
28
29 /**
30 * An entity that can be multiplexed using a {@link Selector}.
31 * <p>
32 * Every selectable is associated with exactly one {@link SelectableChannel}.
33 * Selectables may be interested in three kinds of events: read events, write
34 * events, and timer events. A selectable will express its interest in these
35 * events through the {@link #isReading()}, {@link #isWriting()}, and
36 * {@link #getDeadline()} methods.
37 * <p>
38 * When a read, write, or timer event occurs, the selectable must be notified by
39 * calling {@link #readable()}, {@link #writeable()}, or {@link #expired()} as
40 * appropriate.
41 *
42 * Once a selectable reaches a terminal state (see {@link #isTerminal()}, it
43 * will never be interested in events of any kind. When this occurs it should be
44 * removed from the Selector and discarded using {@link #free()}.
45 */
46 public interface Selectable extends ReactorChild, Extendable {
47
48 /**
49 * A callback that can be passed to the various "on" methods of the
50 * selectable - to allow code to be run when the selectable becomes ready
51 * for the associated operation.
52 */
53 interface Callback {
54 void run(Selectable selectable);
55 }
56
57 /**
58 * @return <code>true</code> if the selectable is interested in receiving
59 * notification (via the {@link #readable()} method that indicate
60 * that the associated {@link SelectableChannel} has data ready
61 * to be read from it.
62 */
63 boolean isReading();
64
65 /**
66 * @return <code>true</code> if the selectable is interested in receiving
67 * notifications (via the {@link #writeable()} method that indicate
68 * that the associated {@link SelectableChannel} is ready to be
69 * written to.
70 */
71 boolean isWriting();
72
73 /**
74 * @return a deadline after which this selectable can expect to receive
75 * a notification (via the {@link #expired()} method that indicates
76 * that the deadline has past. The deadline is expressed in the
77 * same format as {@link System#currentTimeMillis()}. Returning
78 * a deadline of zero (or a negative number) indicates that the
79 * selectable does not wish to be notified of expiry.
80 */
81 long getDeadline();
82
83 /**
84 * Sets the value that will be returned by {@link #isReading()}.
85 * @param reading
86 */
87 void setReading(boolean reading);
88
89 /**
90 * Sets the value that will be returned by {@link #isWriting()}.
91 * @param writing
92 */
93 void setWriting(boolean writing);
94
95 /**
96 * Sets the value that will be returned by {@link #getDeadline()}.
97 * @param deadline
98 */
99 void setDeadline(long deadline);
100
101 /**
102 * Registers a callback that will be run when the selectable becomes ready
103 * for reading.
104 * @param runnable the callback to register. Any previously registered
105 * callback will be replaced.
106 */
107 void onReadable(Callback runnable);
108
109 /**
110 * Registers a callback that will be run when the selectable becomes ready
111 * for writing.
112 * @param runnable the callback to register. Any previously registered
113 * callback will be replaced.
114 */
115 void onWritable(Callback runnable);
116
117 /**
118 * Registers a callback that will be run when the selectable expires.
119 * @param runnable the callback to register. Any previously registered
120 * callback will be replaced.
121 */
122 void onExpired(Callback runnable);
123
124 /**
125 * Registers a callback that will be run when the selectable is notified of
126 * an error.
127 * @param runnable the callback to register. Any previously registered
128 * callback will be replaced.
129 */
130 void onError(Callback runnable);
131
132 /**
133 * Registers a callback that will be run when the selectable is notified
134 * that it has been released.
135 * @param runnable the callback to register. Any previously registered
136 * callback will be replaced.
137 */
138 void onRelease(Callback runnable);
139
140 /**
141 * Registers a callback that will be run when the selectable is notified
142 * that it has been free'd.
143 * @param runnable the callback to register. Any previously registered
144 * callback will be replaced.
145 */
146 void onFree(Callback runnable);
147
148 /**
149 * Notify the selectable that the underlying {@link SelectableChannel} is
150 * ready for a read operation.
151 */
152 void readable();
153
154 /**
155 * Notify the selectable that the underlying {@link SelectableChannel} is
156 * ready for a write operation.
157 */
158 void writeable();
159
160 /** Notify the selectable that it has expired. */
161 void expired();
162
163 /** Notify the selectable that an error has occurred. */
164 void error();
165
166 /** Notify the selectable that it has been released. */
167 void release();
168
169 /** Notify the selectable that it has been free'd. */
170 @Override
171 void free();
172
173 /**
174 * Associates a {@link SelectableChannel} with this selector.
175 * @param channel
176 */
177 void setChannel(SelectableChannel channel); // This is the equivalent to pn_selectable_set_fd(...)
178
179 /** @return the {@link SelectableChannel} associated with this selector. */
180 SelectableChannel getChannel(); // This is the equivalent to pn_selectable_get_fd(...)
181
182 /**
183 * Check if a selectable is registered. This can be used for tracking
184 * whether a given selectable has been registerd with an external event
185 * loop.
186 * <p>
187 * <em>Note:</em> the reactor code, currently, does not use this flag.
188 * @return <code>true</code>if the selectable is registered.
189 */
190 boolean isRegistered(); // XXX: unused in C reactor code
191
192 /**
193 * Set the registered flag for a selectable.
194 * <p>
195 * <em>Note:</em> the reactor code, currently, does not use this flag.
196 * @param registered the value returned by {@link #isRegistered()}
197 */
198 void setRegistered(boolean registered); // XXX: unused in C reactor code
199
200 /**
201 * Configure a selectable with a set of callbacks that emit readable,
202 * writable, and expired events into the supplied collector.
203 * @param collector
204 */
205 void setCollector(final Collector collector);
206
207 /** @return the reactor to which this selectable is a child. */
208 Reactor getReactor() ;
209
210 /**
211 * Terminates the selectable. Once a selectable reaches a terminal state
212 * it will never be interested in events of any kind.
213 */
214 public void terminate() ;
215
216 /**
217 * @return <code>true</code> if the selectable has reached a terminal state.
218 */
219 boolean isTerminal();
220
221 }