1 /* 2 * Copyright 2002-2017 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.StompClientSupport; 18 19 // import java.util.Arrays; 20 // import java.util.concurrent.TimeUnit; 21 22 23 // import hunt.stomp.converter.MessageConverter; 24 // import hunt.stomp.converter.SimpleMessageConverter; 25 // import hunt.framework.task.TaskScheduler; 26 27 28 // /** 29 // * Base class for STOMP client implementations. 30 // * 31 // * <p>Subclasses can connect over WebSocket or TCP using any library. When creating 32 // * a new connection, a subclass can create an instance of @link DefaultStompSession} 33 // * which extends {@link hunt.stomp.tcp.TcpConnectionHandler} 34 // * whose lifecycle methods the subclass must then invoke. 35 // * 36 // * <p>In effect, {@code TcpConnectionHandler} and {@code TcpConnection} are the 37 // * contracts that any subclass must adapt to while using {@link StompEncoder} 38 // * and {@link StompDecoder} to encode and decode STOMP messages. 39 // * 40 // * @author Rossen Stoyanchev 41 // * @since 4.2 42 // */ 43 // abstract class StompClientSupport { 44 45 // private MessageConverter messageConverter = new SimpleMessageConverter(); 46 47 48 // private TaskScheduler taskScheduler; 49 50 // private long[] defaultHeartbeat = [10000, 10000]; 51 52 // private long receiptTimeLimit = TimeUnit.SECONDS.toMillis(15); 53 54 55 // /** 56 // * Set the {@link MessageConverter} to use to convert the payload of incoming 57 // * and outgoing messages to and from {@code byte[]} based on object type 58 // * and the "content-type" header. 59 // * <p>By default, {@link SimpleMessageConverter} is configured. 60 // * @param messageConverter the message converter to use 61 // */ 62 // public void setMessageConverter(MessageConverter messageConverter) { 63 // assert(messageConverter, "MessageConverter must not be null"); 64 // this.messageConverter = messageConverter; 65 // } 66 67 // /** 68 // * Return the configured {@link MessageConverter}. 69 // */ 70 // public MessageConverter getMessageConverter() { 71 // return this.messageConverter; 72 // } 73 74 // /** 75 // * Configure a scheduler to use for heartbeats and for receipt tracking. 76 // * <p><strong>Note:</strong> Some transports have built-in support to work 77 // * with heartbeats and therefore do not require a TaskScheduler. 78 // * Receipts however, if needed, do require a TaskScheduler to be configured. 79 // * <p>By default, this is not set. 80 // */ 81 // public void setTaskScheduler(TaskScheduler taskScheduler) { 82 // this.taskScheduler = taskScheduler; 83 // } 84 85 // /** 86 // * The configured TaskScheduler. 87 // */ 88 89 // public TaskScheduler getTaskScheduler() { 90 // return this.taskScheduler; 91 // } 92 93 // /** 94 // * Configure the default value for the "heart-beat" header of the STOMP 95 // * CONNECT frame. The first number represents how often the client will write 96 // * or send a heart-beat. The second is how often the server should write. 97 // * A value of 0 means no heart-beats. 98 // * <p>By default this is set to "10000,10000" but subclasses may override 99 // * that default and for example set it to "0,0" if they require a 100 // * TaskScheduler to be configured first. 101 // * @param heartbeat the value for the CONNECT "heart-beat" header 102 // * @see <a href="http://stomp.github.io/stomp-specification-1.2.html#Heart-beating"> 103 // * http://stomp.github.io/stomp-specification-1.2.html#Heart-beating</a> 104 // */ 105 // public void setDefaultHeartbeat(long[] heartbeat) { 106 // if (heartbeat.length != 2 || heartbeat[0] < 0 || heartbeat[1] < 0) { 107 // throw new IllegalArgumentException("Invalid heart-beat: " ~ Arrays.toString(heartbeat)); 108 // } 109 // this.defaultHeartbeat = heartbeat; 110 // } 111 112 // /** 113 // * Return the configured default heart-beat value (never {@code null}). 114 // */ 115 // public long[] getDefaultHeartbeat() { 116 // return this.defaultHeartbeat; 117 // } 118 119 // /** 120 // * Determine whether heartbeats are enabled. 121 // * <p>Returns {@code false} if {@link #setDefaultHeartbeat defaultHeartbeat} 122 // * is set to "0,0", and {@code true} otherwise. 123 // */ 124 // bool isDefaultHeartbeatEnabled() { 125 // long[] heartbeat = getDefaultHeartbeat(); 126 // return (heartbeat[0] != 0 && heartbeat[1] != 0); 127 // } 128 129 // /** 130 // * Configure the number of milliseconds before a receipt is considered expired. 131 // * <p>By default set to 15,000 (15 seconds). 132 // */ 133 // public void setReceiptTimeLimit(long receiptTimeLimit) { 134 // assert(receiptTimeLimit > 0, "Receipt time limit must be larger than zero"); 135 // this.receiptTimeLimit = receiptTimeLimit; 136 // } 137 138 // /** 139 // * Return the configured receipt time limit. 140 // */ 141 // public long getReceiptTimeLimit() { 142 // return this.receiptTimeLimit; 143 // } 144 145 146 // /** 147 // * Factory method for create and configure a new session. 148 // * @param connectHeaders headers for the STOMP CONNECT frame 149 // * @param handler the handler for the STOMP session 150 // * @return the created session 151 // */ 152 // protected ConnectionHandlingStompSession createSession( 153 // StompHeaders connectHeaders, StompSessionHandler handler) { 154 155 // connectHeaders = processConnectHeaders(connectHeaders); 156 // DefaultStompSession session = new DefaultStompSession(handler, connectHeaders); 157 // session.setMessageConverter(getMessageConverter()); 158 // session.setTaskScheduler(getTaskScheduler()); 159 // session.setReceiptTimeLimit(getReceiptTimeLimit()); 160 // return session; 161 // } 162 163 // /** 164 // * Further initialize the StompHeaders, for example setting the heart-beat 165 // * header if necessary. 166 // * @param connectHeaders the headers to modify 167 // * @return the modified headers 168 // */ 169 // protected StompHeaders processConnectHeaders(StompHeaders connectHeaders) { 170 // connectHeaders = (connectHeaders !is null ? connectHeaders : new StompHeaders()); 171 // if (connectHeaders.getHeartbeat() is null) { 172 // connectHeaders.setHeartbeat(getDefaultHeartbeat()); 173 // } 174 // return connectHeaders; 175 // } 176 177 // }