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.config.StompBrokerRelayRegistration;
18 
19 import hunt.stomp.simp.broker.AbstractBrokerMessageHandler;
20 import hunt.stomp.simp.config.AbstractBrokerRegistration;
21 import hunt.stomp.simp.stomp.StompBrokerRelayMessageHandler;
22 import hunt.stomp.MessageChannel;
23 // import hunt.stomp.tcp.TcpOperations;
24 
25 import hunt.Exceptions;
26 
27 import std.array;
28 
29 /**
30  * Registration class for configuring a {@link StompBrokerRelayMessageHandler}.
31  *
32  * @author Rossen Stoyanchev
33  * @since 4.0
34  */
35 class StompBrokerRelayRegistration : AbstractBrokerRegistration {
36 
37 	private string relayHost = "127.0.0.1";
38 
39 	private int relayPort = 61613;
40 
41 	private string clientLogin = "guest";
42 
43 	private string clientPasscode = "guest";
44 
45 	private string systemLogin = "guest";
46 
47 	private string systemPasscode = "guest";
48 
49 	private long systemHeartbeatSendInterval;
50 
51 	private long systemHeartbeatReceiveInterval;
52 
53 	private string virtualHost;
54 
55 	
56 	// private TcpOperations!(byte[]) tcpClient;
57 
58 	private bool autoStartup = true;
59 
60 	
61 	private string userDestinationBroadcast;
62 
63 	
64 	private string userRegistryBroadcast;
65 
66 
67 	this(SubscribableChannel clientInboundChannel,
68 			MessageChannel clientOutboundChannel, string[] destinationPrefixes) {
69 
70 		super(clientInboundChannel, clientOutboundChannel, destinationPrefixes);
71 	}
72 
73 
74 	/**
75 	 * Set the STOMP message broker host.
76 	 */
77 	StompBrokerRelayRegistration setRelayHost(string relayHost) {
78 		assert(!relayHost.empty(), "relayHost must not be empty");
79 		this.relayHost = relayHost;
80 		return this;
81 	}
82 
83 	/**
84 	 * Set the STOMP message broker port.
85 	 */
86 	StompBrokerRelayRegistration setRelayPort(int relayPort) {
87 		this.relayPort = relayPort;
88 		return this;
89 	}
90 
91 	/**
92 	 * Set the login to use when creating connections to the STOMP broker on
93 	 * behalf of connected clients.
94 	 * <p>By default this is set to "guest".
95 	 */
96 	StompBrokerRelayRegistration setClientLogin(string login) {
97 		assert(!login.empty(), "clientLogin must not be empty");
98 		this.clientLogin = login;
99 		return this;
100 	}
101 
102 	/**
103 	 * Set the passcode to use when creating connections to the STOMP broker on
104 	 * behalf of connected clients.
105 	 * <p>By default this is set to "guest".
106 	 */
107 	StompBrokerRelayRegistration setClientPasscode(string passcode) {
108 		assert(!passcode.empty(), "clientPasscode must not be empty");
109 		this.clientPasscode = passcode;
110 		return this;
111 	}
112 
113 	/**
114 	 * Set the login for the shared "system" connection used to send messages to
115 	 * the STOMP broker from within the application, i.e. messages not associated
116 	 * with a specific client session (e.g. REST/HTTP request handling method).
117 	 * <p>By default this is set to "guest".
118 	 */
119 	StompBrokerRelayRegistration setSystemLogin(string login) {
120 		assert(!login.empty(), "systemLogin must not be empty");
121 		this.systemLogin = login;
122 		return this;
123 	}
124 
125 	/**
126 	 * Set the passcode for the shared "system" connection used to send messages to
127 	 * the STOMP broker from within the application, i.e. messages not associated
128 	 * with a specific client session (e.g. REST/HTTP request handling method).
129 	 * <p>By default this is set to "guest".
130 	 */
131 	StompBrokerRelayRegistration setSystemPasscode(string passcode) {
132 		assert(!passcode.empty(), "systemPasscode must not be empty");
133 		this.systemPasscode = passcode;
134 		return this;
135 	}
136 
137 	/**
138 	 * Set the interval, in milliseconds, at which the "system" relay session will,
139 	 * in the absence of any other data being sent, send a heartbeat to the STOMP broker.
140 	 * A value of zero will prevent heartbeats from being sent to the broker.
141 	 * <p>The default value is 10000.
142 	 */
143 	StompBrokerRelayRegistration setSystemHeartbeatSendInterval(long systemHeartbeatSendInterval) {
144 		this.systemHeartbeatSendInterval = systemHeartbeatSendInterval;
145 		return this;
146 	}
147 
148 	/**
149 	 * Set the maximum interval, in milliseconds, at which the "system" relay session
150 	 * expects, in the absence of any other data, to receive a heartbeat from the STOMP
151 	 * broker. A value of zero will configure the relay session to expect not to receive
152 	 * heartbeats from the broker.
153 	 * <p>The default value is 10000.
154 	 */
155 	StompBrokerRelayRegistration setSystemHeartbeatReceiveInterval(long heartbeatReceiveInterval) {
156 		this.systemHeartbeatReceiveInterval = heartbeatReceiveInterval;
157 		return this;
158 	}
159 
160 	/**
161 	 * Set the value of the "host" header to use in STOMP CONNECT frames. When this
162 	 * property is configured, a "host" header will be added to every STOMP frame sent to
163 	 * the STOMP broker. This may be useful for example in a cloud environment where the
164 	 * actual host to which the TCP connection is established is different from the host
165 	 * providing the cloud-based STOMP service.
166 	 * <p>By default this property is not set.
167 	 */
168 	StompBrokerRelayRegistration setVirtualHost(string virtualHost) {
169 		this.virtualHost = virtualHost;
170 		return this;
171 	}
172 
173 	/**
174 	 * Configure a TCP client for managing TCP connections to the STOMP broker.
175 	 * <p>By default {@code ReactorNettyTcpClient} is used.
176 	 * <p><strong>Note:</strong> when this property is used, any
177 	 * {@link #setRelayHost(string) host} or {@link #setRelayPort(int) port}
178 	 * specified are effectively ignored.
179 	 * @since 4.3.15
180 	 */
181 	// void setTcpClient(TcpOperations!(byte[]) tcpClient) {
182 	// 	this.tcpClient = tcpClient;
183 	// }
184 
185 	/**
186 	 * Configure whether the {@link StompBrokerRelayMessageHandler} should start
187 	 * automatically when the Spring ApplicationContext is refreshed.
188 	 * <p>The default setting is {@code true}.
189 	 */
190 	StompBrokerRelayRegistration setAutoStartup(bool autoStartup) {
191 		this.autoStartup = autoStartup;
192 		return this;
193 	}
194 
195 	/**
196 	 * Set a destination to broadcast messages to user destinations that remain
197 	 * unresolved because the user appears not to be connected. In a
198 	 * multi-application server scenario this gives other application servers
199 	 * a chance to try.
200 	 * <p>By default this is not set.
201 	 * @param destination the destination to broadcast unresolved messages to,
202 	 * e.g. "/topic/unresolved-user-destination"
203 	 */
204 	StompBrokerRelayRegistration setUserDestinationBroadcast(string destination) {
205 		this.userDestinationBroadcast = destination;
206 		return this;
207 	}
208 
209 	
210 	string getUserDestinationBroadcast() {
211 		return this.userDestinationBroadcast;
212 	}
213 
214 	/**
215 	 * Set a destination to broadcast the content of the local user registry to
216 	 * and to listen for such broadcasts from other servers. In a multi-application
217 	 * server scenarios this allows each server's user registry to be aware of
218 	 * users connected to other servers.
219 	 * <p>By default this is not set.
220 	 * @param destination the destination for broadcasting user registry details,
221 	 * e.g. "/topic/simp-user-registry".
222 	 */
223 	StompBrokerRelayRegistration setUserRegistryBroadcast(string destination) {
224 		this.userRegistryBroadcast = destination;
225 		return this;
226 	}
227 
228 	
229 	string getUserRegistryBroadcast() {
230 		return this.userRegistryBroadcast;
231 	}
232 
233 	override
234 	protected AbstractBrokerMessageHandler getMessageHandler(SubscribableChannel brokerChannel) {
235 
236 	// 	StompBrokerRelayMessageHandler handler = new StompBrokerRelayMessageHandler(
237 	// 			getClientInboundChannel(), getClientOutboundChannel(),
238 	// 			brokerChannel, getDestinationPrefixes());
239 
240 	// 	handler.setRelayHost(this.relayHost);
241 	// 	handler.setRelayPort(this.relayPort);
242 
243 	// 	handler.setClientLogin(this.clientLogin);
244 	// 	handler.setClientPasscode(this.clientPasscode);
245 
246 	// 	handler.setSystemLogin(this.systemLogin);
247 	// 	handler.setSystemPasscode(this.systemPasscode);
248 
249 	// 	if (this.systemHeartbeatSendInterval !is null) {
250 	// 		handler.setSystemHeartbeatSendInterval(this.systemHeartbeatSendInterval);
251 	// 	}
252 	// 	if (this.systemHeartbeatReceiveInterval !is null) {
253 	// 		handler.setSystemHeartbeatReceiveInterval(this.systemHeartbeatReceiveInterval);
254 	// 	}
255 	// 	if (this.virtualHost !is null) {
256 	// 		handler.setVirtualHost(this.virtualHost);
257 	// 	}
258 	// 	// if (this.tcpClient !is null) {
259 	// 	// 	handler.setTcpClient(this.tcpClient);
260 	// 	// }
261 
262 	// 	handler.setAutoStartup(this.autoStartup);
263 
264 	// 	return handler;
265 		implementationMissing(false);
266 		return null;
267 	}
268 
269 }