1 /*
2  * hunt-amqp: AMQP library for D programming language, based on hunt-net.
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 module hunt.amqp.ProtonLink;
12 
13 
14 import hunt.proton.amqp.Symbol;
15 import hunt.proton.amqp.UnsignedLong;
16 import hunt.proton.amqp.transport.ErrorCondition;
17 import hunt.proton.amqp.transport.Source;
18 import hunt.proton.amqp.transport.Target;
19 import hunt.amqp.Handler;
20 import hunt.collection.Map;
21 import hunt.amqp.ProtonQoS;
22 import hunt.amqp.ProtonSession;
23 import hunt.proton.engine.Record;
24 /**
25  * @author <a href="http://tfox.org">Tim Fox</a>
26  */
27 interface ProtonLink(T) {
28 
29   /**
30    * Opens the AMQP link, i.e. allows the Attach frame to be emitted. Typically used after any additional configuration
31    * is performed on the object.
32    *
33    * For locally initiated links, the {@link #openHandler(Handler)} may be used to handle the peer sending their Attach
34    * frame.
35    *
36    * @return the link
37    */
38   T open();
39 
40   /**
41    * Closes the AMQP link, i.e. allows the Detach frame to be emitted with closed=true set.
42    *
43    * If the closure is being locally initiated, the {@link #closeHandler(Handler)} should be used to handle the peer
44    * sending their Detach frame with closed=true (and {@link #detachHandler(Handler)} can be used to handle the peer
45    * sending their Detach frame with closed=false). When use of the link is complete, i.e it is locally and
46    * remotely closed, {@link #free()} must be called to ensure related resources can be tidied up.
47    *
48    * @return the link
49    */
50   T close();
51 
52   /**
53    * Detaches the AMQP link, i.e. allows the Detach frame to be emitted with closed=false.
54    *
55    * If the detach is being locally initiated, the {@link #detachHandler(Handler)} should be used to handle the peer
56    * sending their Detach frame with closed=false (and {@link #closeHandler(Handler)} can be used to handle the peer
57    * sending their Detach frame with closed=true). When use of the link is complete, i.e it is locally and
58    * remotely detached, {@link #free()} must be called to ensure related resources can be tidied up.
59    *
60    * @return the link
61    */
62   T detach();
63 
64   /**
65    * Sets a handler for when an AMQP Attach frame is received from the remote peer.
66    *
67    * Typically used by clients, servers rely on {@link ProtonConnection#senderOpenHandler(Handler)} and
68    * {@link ProtonConnection#receiverOpenHandler(Handler)}.
69    *
70    * @param remoteOpenHandler
71    *          the handler
72    * @return the link
73    */
74   T openHandler(Handler!T remoteOpenHandler);
75  // T openHandler();
76   /**
77    * Sets a handler for when an AMQP Detach frame with closed=true is received from the remote peer.
78    *
79    * @param remoteCloseHandler
80    *          the handler
81    * @return the link
82    */
83    T closeHandler(Handler!T remoteCloseHandler);
84  // T closeHandler();
85 
86   /**
87    * Sets a handler for when an AMQP Detach frame with closed=false is received from the remote peer.
88    *
89    * @param remoteDetachHandler
90    *          the handler
91    * @return the link
92    */
93   T detachHandler(Handler!T remoteDetachHandler);
94   //T detachHandler();
95 
96   /**
97    * Gets the local QOS config.
98    *
99    * @return the QOS config
100    */
101   ProtonQoS getQoS();
102 
103   /**
104    * Sets the local QOS config.
105    *
106    * @param qos
107    *          the QOS to configure
108    * @return the link
109    */
110   T setQoS(ProtonQoS qos);
111 
112   /**
113    * Gets the remote QOS config.
114    *
115    * @return the QOS config
116    */
117   ProtonQoS getRemoteQoS();
118 
119   /**
120    * Check whether the link is locally open.
121    *
122    * @return whether the link is locally open.
123    */
124   bool isOpen();
125 
126   /**
127    * Retrieves the attachments record, upon which application items can be set/retrieved.
128    *
129    * @return the attachments
130    */
131   Record attachments();
132 
133   /**
134    * Gets the current local target config.
135    *
136    * @return the target
137    */
138   Target getTarget();
139 
140   /**
141    * Sets the current local target config. Only useful to call before the link has locally opened.
142    *
143    * @param target
144    *          the target
145    * @return the link
146    */
147   T setTarget(Target target);
148 
149   /**
150    * Gets the current remote target config. Only useful to call after the link has remotely opened.
151    *
152    * @return the target
153    */
154   Target getRemoteTarget();
155 
156   /**
157    * Gets the current local source config.
158    *
159    * @return the source
160    */
161   Source getSource();
162 
163   /**
164    * Sets the current local source config. Only useful to call before the link has locally opened.
165    *
166    * @param source
167    *          the source
168    * @return the link
169    */
170   T setSource(Source source);
171 
172   /**
173    * Gets the current remote source config. Only useful to call after the link has remotely opened.
174    *
175    * @return the target
176    */
177   Source getRemoteSource();
178 
179   /**
180    * Gets the session this link is on.
181    * @return the session
182    */
183   ProtonSession getSession();
184 
185   /**
186    * Retrieves the remote address from the remote terminus (source for receivers, target for senders).
187    * Only useful to call after the link has remotely opened.
188    *
189    * @return the remote address, or null if there was none.
190    */
191   string getRemoteAddress();
192 
193   /**
194    * Sets the local ErrorCondition object.
195    *
196    * @param condition
197    *          the condition to set
198    * @return the link
199    */
200   T setCondition(ErrorCondition condition);
201 
202   /**
203    * Gets the local ErrorCondition object.
204    *
205    * @return the condition
206    */
207   ErrorCondition getCondition();
208 
209   /**
210    * Gets the remote ErrorCondition object.
211    *
212    * @return the condition
213    */
214   ErrorCondition getRemoteCondition();
215 
216   /**
217    * Retrieves the current amount of credit.
218    *
219    * For a receiver link, the value returned will still include the credits that will be used by any queued
220    * incoming messages, use {@link #getQueued()} to assess the number of credits that will be used by queued messages.
221    *
222    * @return the number of credits
223    */
224   int getCredit();
225 
226   /**
227    * Retrieves the current value of link 'drain' flag.
228    *
229    * @return when the link drain flag is set.
230    */
231   bool getDrain();
232 
233   /**
234    * Retrieves the current number of queued messages.
235    *
236    * For a receiver link, this is the number of messages that have already arrived locally but not yet been processed.
237    *
238    * @return the number of queues messages
239    */
240   int getQueued();
241 
242   /**
243    * Retrieves the link name
244    *
245    * @return  the link name
246    */
247   string getName();
248 
249   /**
250    * Sets the local link max message size, to be conveyed to the peer via the Attach frame
251    * when attaching the link to the session. Null or 0 means no limit.
252    *
253    * Must be called during link setup, i.e. before calling the {@link #open()} method.
254    *
255    * @param maxMessageSize
256    *            the local max message size value, or null to clear. 0 also means no limit.
257    */
258   void setMaxMessageSize(UnsignedLong maxMessageSize);
259 
260   /**
261    * Gets the local link max message size.
262    *
263    * @return the local max message size, or null if none was set. 0 also means no limit.
264    *
265    * @see #setMaxMessageSize(UnsignedLong)
266    */
267   UnsignedLong getMaxMessageSize();
268 
269   /**
270    * Gets the remote link max message size, as conveyed from the peer via the Attach frame
271    * when attaching the link to the session.
272    *
273    * @return the remote max message size conveyed by the peer, or null if none was set. 0 also means no limit.
274    */
275   UnsignedLong getRemoteMaxMessageSize();
276 
277 
278   /**
279    * Sets the link properties, to be conveyed to the peer via the Attach frame
280    * when attaching the link to the session.
281    *
282    * Must be called during link setup, i.e. before calling the {@link #open()} method.
283    *
284    * @param properties the properties of the link to be coveyed to the remote peer.
285    */
286   void setProperties(Map!(Symbol, Object) properties);
287 
288   /**
289    * Gets the remote link properties, as conveyed from the peer via the Attach frame
290    * when attaching the link to the session.
291    *
292    * @return the remote link properties conveyed by the peer, or null if none was set.
293    */
294   Map!(Symbol, Object) getRemoteProperties();
295 
296 
297   /**
298    * Sets the offered capabilities, to be conveyed to the peer via the Attach frame
299    * when attaching the link to the session.
300    *
301    * Must be called during link setup, i.e. before calling the {@link #open()} method.
302    *
303    * @param capabilities the capabilities offered to the remote peer.
304    */
305   void setOfferedCapabilities(Symbol[] capabilities);
306 
307   /**
308    * Gets the remote offered capabilities, as conveyed from the peer via the Attach frame
309    * when attaching the link to the session.
310    *
311    * @return the remote offered capabilities conveyed by the peer, or null if none was set.
312    */
313   Symbol[] getRemoteOfferedCapabilities();
314 
315   /**
316    * Sets the desired capabilities, to be conveyed to the peer via the Attach frame
317    * when attaching the link to the session.
318    *
319    * Must be called during link setup, i.e. before calling the {@link #open()} method.
320    *
321    * @param capabilities the capabilities desired of the remote peer.
322    */
323   void setDesiredCapabilities(Symbol[] capabilities);
324 
325   /**
326    * Gets the remote desired capabilities, as conveyed from the peer via the Attach frame
327    * when attaching the link to the session.
328    *
329    * @return the remote desired capabilities conveyed by the peer, or null if none was set.
330    */
331   Symbol[] getRemoteDesiredCapabilities();
332 
333   /**
334    * Tidies up related link resources when complete with use. Call only after the link is
335    * finished with, e.g. locally and remotely closed.
336    */
337   void free();
338 }