1 /*
2  * Copyright 2002-2018 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 module hunt.stomp.simp.stomp.StompSession;
18 
19 import hunt.stomp.simp.stomp.StompFrameHandler;
20 import hunt.stomp.simp.stomp.StompHeaders;
21 
22 import hunt.util.Common;
23 
24 
25 /**
26  * A handle to use to track receipts.
27  * @see #setAutoReceipt()
28  */
29 interface Receiptable {
30 
31     /**
32      * Return the receipt id, or {@code null} if the STOMP frame for which
33      * the handle was returned did not have a "receipt" header.
34      */
35     
36     string getReceiptId();
37 
38     /**
39      * Task to invoke when a receipt is received.
40      * @throws java.lang.IllegalArgumentException if the receiptId is {@code null}
41      */
42     void addReceiptTask(Runnable runnable);
43 
44     /**
45      * Task to invoke when a receipt is not received in the configured time.
46      * @throws java.lang.IllegalArgumentException if the receiptId is {@code null}
47      * @see hunt.stomp.simp.stomp.StompClientSupport#setReceiptTimeLimit(long)
48      */
49     void addReceiptLostTask(Runnable runnable);
50 }
51 
52 
53 /**
54  * A handle to use to unsubscribe or to track a receipt.
55  */
56 interface Subscription : Receiptable {
57 
58     /**
59      * Return the id for the subscription.
60      */
61     
62     string getSubscriptionId();
63 
64     /**
65      * Return the headers used on the SUBSCRIBE frame.
66      * @since 5.0
67      */
68     StompHeaders getSubscriptionHeaders();
69 
70     /**
71      * Remove the subscription by sending an UNSUBSCRIBE frame.
72      */
73     void unsubscribe();
74 
75     /**
76      * Alternative to {@link #unsubscribe()} with additional custom headers
77      * to send to the server.
78      * <p><strong>Note:</strong> There is no need to set the subscription id.
79      * @param headers the custom headers, if any
80      * @since 5.0
81      */
82     void unsubscribe(StompHeaders headers);
83 }
84 
85 
86 /**
87  * Represents a STOMP session with operations to send messages, create
88  * subscriptions and receive messages on those subscriptions.
89  *
90  * @author Rossen Stoyanchev
91  * @since 4.2
92  */
93 interface StompSession {
94 
95 	/**
96 	 * Return the id for the session.
97 	 */
98 	string getSessionId();
99 
100 	/**
101 	 * Whether the session is connected.
102 	 */
103 	bool isConnected();
104 
105 	/**
106 	 * When enabled, a receipt header is automatically added to future
107 	 * {@code send} and {@code subscribe} operations on this session, which
108 	 * causes the server to return a RECEIPT. An application can then use
109 	 * the {@link StompSession.Receiptable Receiptable} returned from the
110 	 * operation to track the receipt.
111 	 * <p>A receipt header can also be added manually through the overloaded
112 	 * methods that accept {@code StompHeaders}.
113 	 */
114 	void setAutoReceipt(bool enabled);
115 
116 	/**
117 	 * Send a message to the specified destination, converting the payload to a
118 	 * {@code byte[]} with the help of a
119 	 * {@link hunt.stomp.converter.MessageConverter MessageConverter}.
120 	 * @param destination the destination to send a message to
121 	 * @param payload the message payload
122 	 * @return a Receiptable for tracking receipts
123 	 */
124 	Receiptable send(string destination, Object payload);
125 
126 	/**
127 	 * An overloaded version of {@link #send(string, Object)} with full
128 	 * {@link StompHeaders} instead of just a destination. The headers must
129 	 * contain a destination and may also have other headers such as
130 	 * "content-type" or custom headers for the broker to propagate to
131 	 * subscribers, or broker-specific, non-standard headers..
132 	 * @param headers the message headers
133 	 * @param payload the message payload
134 	 * @return a Receiptable for tracking receipts
135 	 */
136 	Receiptable send(StompHeaders headers, Object payload);
137 
138 	/**
139 	 * Subscribe to the given destination by sending a SUBSCRIBE frame and handle
140 	 * received messages with the specified {@link StompFrameHandler}.
141 	 * @param destination the destination to subscribe to
142 	 * @param handler the handler for received messages
143 	 * @return a handle to use to unsubscribe and/or track receipts
144 	 */
145 	Subscription subscribe(string destination, StompFrameHandler handler);
146 
147 	/**
148 	 * An overloaded version of {@link #subscribe(string, StompFrameHandler)}
149 	 * with full {@link StompHeaders} instead of just a destination.
150 	 * @param headers the headers for the subscribe message frame
151 	 * @param handler the handler for received messages
152 	 * @return a handle to use to unsubscribe and/or track receipts
153 	 */
154 	Subscription subscribe(StompHeaders headers, StompFrameHandler handler);
155 
156 	/**
157 	 * Send an acknowledgement whether a message was consumed or not resulting
158 	 * in an ACK or NACK frame respectively.
159 	 * <p><strong>Note:</strong> to use this when subscribing you must set the
160 	 * {@link StompHeaders#setAck(string) ack} header to "client" or
161 	 * "client-individual" in order ot use this.
162 	 * @param messageId the id of the message
163 	 * @param consumed whether the message was consumed or not
164 	 * @return a Receiptable for tracking receipts
165 	 * @since 4.3
166 	 */
167 	Receiptable acknowledge(string messageId, bool consumed);
168 
169 	/**
170 	 * An overloaded version of {@link #acknowledge(string, )} with
171 	 * full {@link StompHeaders} instead of just a {@code messageId}.
172 	 * @param headers the headers for the ACK or NACK message frame
173 	 * @param consumed whether the message was consumed or not
174 	 * @return a Receiptable for tracking receipts
175 	 * @since 5.0.5
176 	 */
177 	Receiptable acknowledge(StompHeaders headers, bool consumed);
178 
179 	/**
180 	 * Disconnect the session by sending a DISCONNECT frame.
181 	 */
182 	void disconnect();
183 
184 }