2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.atlona.internal.net;
15 import java.io.IOException;
16 import java.net.InetSocketAddress;
17 import java.nio.ByteBuffer;
18 import java.nio.channels.AsynchronousCloseException;
19 import java.nio.channels.SocketChannel;
20 import java.util.List;
21 import java.util.concurrent.ArrayBlockingQueue;
22 import java.util.concurrent.BlockingQueue;
23 import java.util.concurrent.CopyOnWriteArrayList;
24 import java.util.concurrent.CountDownLatch;
25 import java.util.concurrent.TimeUnit;
26 import java.util.concurrent.atomic.AtomicBoolean;
27 import java.util.concurrent.atomic.AtomicReference;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Represents a restartable socket connection to the underlying telnet session. Commands can be sent via
34 * {@link #sendCommand(String)} and responses will be received on any {@link SocketSessionListener}. This implementation
35 * of {@link SocketSession} communicates using a {@link SocketChannel} connection.
37 * @author Tim Roberts - Initial contribution
39 public class SocketChannelSession implements SocketSession {
40 private final Logger logger = LoggerFactory.getLogger(SocketChannelSession.class);
43 * The host/ip address to connect to
45 private final String host;
48 * The port to connect to
50 private final int port;
53 * The actual socket being used. Will be null if not connected
55 private final AtomicReference<SocketChannel> socketChannel = new AtomicReference<>();
58 * The {@link ResponseReader} that will be used to read from {@link #_readBuffer}
60 private final ResponseReader responseReader = new ResponseReader();
63 * The responses read from the {@link #responseReader}
65 private final BlockingQueue<Object> responses = new ArrayBlockingQueue<>(50);
68 * The dispatcher of responses from {@link #responses}
70 private final Dispatcher dispatcher = new Dispatcher();
73 * The {@link SocketSessionListener} that the {@link #dispatcher} will call
75 private List<SocketSessionListener> listeners = new CopyOnWriteArrayList<>();
78 * Creates the socket session from the given host and port
80 * @param host a non-null, non-empty host/ip address
81 * @param port the port number between 1 and 65535
83 public SocketChannelSession(String host, int port) {
84 if (host == null || host.trim().length() == 0) {
85 throw new IllegalArgumentException("Host cannot be null or empty");
88 if (port < 1 || port > 65535) {
89 throw new IllegalArgumentException("Port must be between 1 and 65535");
96 public void addListener(SocketSessionListener listener) {
97 if (listener == null) {
98 throw new IllegalArgumentException("listener cannot be null");
100 listeners.add(listener);
104 public void clearListeners() {
109 public boolean removeListener(SocketSessionListener listener) {
110 return listeners.remove(listener);
114 public void connect() throws IOException {
117 final SocketChannel channel = SocketChannel.open();
118 channel.configureBlocking(true);
120 logger.debug("Connecting to {}:{}", host, port);
121 channel.connect(new InetSocketAddress(host, port));
123 logger.debug("Waiting for connect");
124 while (!channel.finishConnect()) {
127 } catch (InterruptedException e) {
131 socketChannel.set(channel);
132 new Thread(dispatcher).start();
133 new Thread(responseReader).start();
137 public void disconnect() throws IOException {
139 logger.debug("Disconnecting from {}:{}", host, port);
141 final SocketChannel channel = socketChannel.getAndSet(null);
144 dispatcher.stopRunning();
145 responseReader.stopRunning();
152 public boolean isConnected() {
153 final SocketChannel channel = socketChannel.get();
154 return channel != null && channel.isConnected();
158 public synchronized void sendCommand(String command) throws IOException {
159 if (command == null) {
160 throw new IllegalArgumentException("command cannot be null");
163 if (!isConnected()) {
164 throw new IOException("Cannot send message - disconnected");
167 ByteBuffer toSend = ByteBuffer.wrap((command + "\r\n").getBytes());
169 final SocketChannel channel = socketChannel.get();
170 if (channel == null) {
171 logger.debug("Cannot send command '{}' - socket channel was closed", command);
173 logger.debug("Sending Command: '{}'", command);
174 channel.write(toSend);
179 * This is the runnable that will read from the socket and add messages to the responses queue (to be processed by
182 * @author Tim Roberts
185 private class ResponseReader implements Runnable {
188 * Whether the reader is currently running
190 private final AtomicBoolean isRunning = new AtomicBoolean(false);
193 * Locking to allow proper shutdown of the reader
195 private final CountDownLatch running = new CountDownLatch(1);
198 * Stops the reader. Will wait 5 seconds for the runnable to stop
200 public void stopRunning() {
201 if (isRunning.getAndSet(false)) {
203 if (!running.await(5, TimeUnit.SECONDS)) {
204 logger.warn("Waited too long for response reader to finish");
206 } catch (InterruptedException e) {
213 * Runs the logic to read from the socket until {@link #isRunning} is false. A 'response' is anything that ends
214 * with a carriage-return/newline combo. Additionally, the special "Login: " and "Password: " prompts are
215 * treated as responses for purposes of logging in.
219 final StringBuilder sb = new StringBuilder(100);
220 final ByteBuffer readBuffer = ByteBuffer.allocate(1024);
225 while (isRunning.get()) {
227 // if reader is null, sleep and try again
228 if (readBuffer == null) {
233 final SocketChannel channel = socketChannel.get();
234 if (channel == null) {
236 isRunning.set(false);
240 int bytesRead = channel.read(readBuffer);
241 if (bytesRead == -1) {
242 responses.put(new IOException("server closed connection"));
243 isRunning.set(false);
245 } else if (bytesRead == 0) {
251 while (readBuffer.hasRemaining()) {
252 final char ch = (char) readBuffer.get();
254 if (ch == '\n' || ch == ' ') {
255 final String str = sb.toString();
256 if (str.endsWith("\r\n") || str.endsWith("Login: ") || str.endsWith("Password: ")) {
258 final String response = str.substring(0, str.length() - 2);
259 responses.put(response);
265 } catch (InterruptedException e) {
266 // Do nothing - probably shutting down
267 } catch (AsynchronousCloseException e) {
268 // socket was definitely closed by another thread
269 } catch (IOException e) {
271 isRunning.set(false);
273 } catch (InterruptedException e1) {
274 // Do nothing - probably shutting down
284 * The dispatcher runnable is responsible for reading the response queue and dispatching it to the current callable.
285 * Since the dispatcher is ONLY started when a callable is set, responses may pile up in the queue and be dispatched
286 * when a callable is set. Unlike the socket reader, this can be assigned to another thread (no state outside of the
289 * @author Tim Roberts
291 private class Dispatcher implements Runnable {
294 * Whether the dispatcher is running or not
296 private final AtomicBoolean isRunning = new AtomicBoolean(false);
299 * Locking to allow proper shutdown of the reader
301 private final CountDownLatch running = new CountDownLatch(1);
304 * Whether the dispatcher is currently processing a message
306 private final AtomicReference<Thread> processingThread = new AtomicReference<>();
309 * Stops the reader. Will wait 5 seconds for the runnable to stop (should stop within 1 second based on the poll
312 public void stopRunning() {
313 if (isRunning.getAndSet(false)) {
314 // only wait if stopRunning didn't get called as part of processing a message
315 // (which would happen if we are processing an exception that forced a session close)
316 final Thread processingThread = this.processingThread.get();
317 if (processingThread != null && Thread.currentThread() != processingThread) {
319 if (!running.await(5, TimeUnit.SECONDS)) {
320 logger.warn("Waited too long for dispatcher to finish");
322 } catch (InterruptedException e) {
330 * Runs the logic to dispatch any responses to the current listeners until {@link #isRunning} is false.
334 processingThread.set(Thread.currentThread());
337 while (isRunning.get()) {
339 // if no listeners, we don't want to start dispatching yet.
340 if (listeners.size() == 0) {
345 final Object response = responses.poll(1, TimeUnit.SECONDS);
347 if (response != null) {
348 if (response instanceof String) {
350 logger.debug("Dispatching response: {}", response);
351 final SocketSessionListener[] listeners = SocketChannelSession.this.listeners
352 .toArray(new SocketSessionListener[0]);
353 for (SocketSessionListener listener : listeners) {
354 listener.responseReceived((String) response);
356 } catch (Exception e) {
357 logger.warn("Exception occurred processing the response '{}': ", response, e);
359 } else if (response instanceof Exception) {
360 logger.debug("Dispatching exception: {}", response);
361 final SocketSessionListener[] listeners = SocketChannelSession.this.listeners
362 .toArray(new SocketSessionListener[0]);
363 for (SocketSessionListener listener : listeners) {
364 listener.responseException((Exception) response);
367 logger.warn("Unknown response class: {}", response);
370 } catch (InterruptedException e) {
372 } catch (Exception e) {
373 logger.debug("Uncaught exception {}", e.getMessage(), e);
377 isRunning.set(false);
378 processingThread.set(null);