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