]> git.basschouten.com Git - openhab-addons.git/blob
ea971e754c707964d50bb3609dd7402df9a47c0a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.rotel.internal.communication;
14
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;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.rotel.internal.RotelException;
26 import org.openhab.binding.rotel.internal.protocol.RotelAbstractProtocolHandler;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Class for communicating with the Rotel device through an IP connection or a serial over IP connection
32  *
33  * @author Laurent Garnier - Initial contribution
34  */
35 @NonNullByDefault
36 public class RotelIpConnector extends RotelConnector {
37
38     private final Logger logger = LoggerFactory.getLogger(RotelIpConnector.class);
39
40     private String address;
41     private int port;
42
43     private @Nullable Socket clientSocket;
44
45     /**
46      * Constructor
47      *
48      * @param address the IP address of the projector
49      * @param port the TCP port to be used
50      * @param protocolHandler the protocol handler
51      * @param readerThreadName the name of thread to be created
52      */
53     public RotelIpConnector(String address, Integer port, RotelAbstractProtocolHandler protocolHandler,
54             String readerThreadName) {
55         super(protocolHandler, false, readerThreadName);
56
57         this.address = address;
58         this.port = port;
59     }
60
61     @Override
62     public synchronized void open() throws RotelException {
63         logger.debug("Opening IP connection on IP {} port {}", this.address, this.port);
64         try {
65             Socket clientSocket = new Socket(this.address, this.port);
66             clientSocket.setSoTimeout(100);
67
68             dataOut = new DataOutputStream(clientSocket.getOutputStream());
69             dataIn = new DataInputStream(clientSocket.getInputStream());
70
71             startReaderThread();
72
73             this.clientSocket = clientSocket;
74
75             setConnected(true);
76
77             logger.debug("IP connection opened");
78         } catch (IOException | SecurityException | IllegalArgumentException e) {
79             setConnected(false);
80             throw new RotelException("Opening IP connection failed", e);
81         }
82     }
83
84     @Override
85     public synchronized void close() {
86         logger.debug("Closing IP connection");
87         super.cleanup();
88         Socket clientSocket = this.clientSocket;
89         if (clientSocket != null) {
90             try {
91                 clientSocket.close();
92             } catch (IOException e) {
93             }
94             this.clientSocket = null;
95         }
96         setConnected(false);
97         logger.debug("IP connection closed");
98     }
99
100     /**
101      * Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes
102      * actually read is returned as an integer.
103      * In case of socket timeout, the returned value is 0.
104      *
105      * @param dataBuffer the buffer into which the data is read.
106      *
107      * @return the total number of bytes read into the buffer, or -1 if there is no more data because the end of the
108      *         stream has been reached.
109      *
110      * @throws RotelException - If the input stream is null, if the first byte cannot be read for any reason
111      *             other than the end of the file, if the input stream has been closed, or if some other I/O error
112      *             occurs.
113      * @throws InterruptedIOException - if the thread was interrupted during the reading of the input stream
114      */
115     @Override
116     protected int readInput(byte[] dataBuffer) throws RotelException, InterruptedIOException {
117         InputStream dataIn = this.dataIn;
118         if (dataIn == null) {
119             throw new RotelException("readInput failed: input stream is null");
120         }
121         try {
122             return dataIn.read(dataBuffer);
123         } catch (SocketTimeoutException e) {
124             return 0;
125         } catch (IOException e) {
126             logger.debug("readInput failed: {}", e.getMessage());
127             throw new RotelException("readInput failed", e);
128         }
129     }
130 }