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.rotel.internal.communication;
15 import java.io.DataInputStream;
16 import java.io.DataOutputStream;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.InterruptedIOException;
20 import java.net.Socket;
21 import java.net.SocketTimeoutException;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.rotel.internal.RotelException;
27 import org.openhab.binding.rotel.internal.RotelModel;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Class for communicating with the Rotel device through a IP connection or a serial over IP connection
34 * @author Laurent Garnier - Initial contribution
37 public class RotelIpConnector extends RotelConnector {
39 private final Logger logger = LoggerFactory.getLogger(RotelIpConnector.class);
41 private String address;
44 private @Nullable Socket clientSocket;
49 * @param address the IP address of the projector
50 * @param port the TCP port to be used
51 * @param model the projector model in use
52 * @param protocol the protocol to be used
53 * @param readerThreadName the name of thread to be created
55 public RotelIpConnector(String address, Integer port, RotelModel model, RotelProtocol protocol,
56 Map<RotelSource, String> sourcesLabels, String readerThreadName) {
57 super(model, protocol, sourcesLabels, false, readerThreadName);
59 this.address = address;
64 public synchronized void open() throws RotelException {
65 logger.debug("Opening IP connection on IP {} port {}", this.address, this.port);
67 Socket clientSocket = new Socket(this.address, this.port);
68 clientSocket.setSoTimeout(100);
70 dataOut = new DataOutputStream(clientSocket.getOutputStream());
71 dataIn = new DataInputStream(clientSocket.getInputStream());
73 Thread thread = new RotelReaderThread(this, readerThreadName);
74 setReaderThread(thread);
77 this.clientSocket = clientSocket;
81 logger.debug("IP connection opened");
82 } catch (IOException | SecurityException | IllegalArgumentException e) {
84 logger.debug("Opening IP connection failed: {}", e.getMessage());
85 throw new RotelException("Opening IP connection failed: " + e.getMessage());
90 public synchronized void close() {
91 logger.debug("Closing IP connection");
93 Socket clientSocket = this.clientSocket;
94 if (clientSocket != null) {
97 } catch (IOException e) {
99 this.clientSocket = null;
102 logger.debug("IP connection closed");
106 * Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes
107 * actually read is returned as an integer.
108 * In case of socket timeout, the returned value is 0.
110 * @param dataBuffer the buffer into which the data is read.
112 * @return the total number of bytes read into the buffer, or -1 if there is no more data because the end of the
113 * stream has been reached.
115 * @throws RotelException - If the input stream is null, if the first byte cannot be read for any reason
116 * other than the end of the file, if the input stream has been closed, or if some other I/O error
118 * @throws InterruptedIOException - if the thread was interrupted during the reading of the input stream
121 protected int readInput(byte[] dataBuffer) throws RotelException, InterruptedIOException {
122 InputStream dataIn = this.dataIn;
123 if (dataIn == null) {
124 throw new RotelException("readInput failed: input stream is null");
127 return dataIn.read(dataBuffer);
128 } catch (SocketTimeoutException e) {
130 } catch (IOException e) {
131 logger.debug("readInput failed: {}", e.getMessage());
132 throw new RotelException("readInput failed: " + e.getMessage());