]> git.basschouten.com Git - openhab-addons.git/blob
6ca28af2d8314a83c1ea816a66f1939c1db7c095
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 import java.util.Map;
23
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;
30
31 /**
32  * Class for communicating with the Rotel device through a IP connection or a serial over IP connection
33  *
34  * @author Laurent Garnier - Initial contribution
35  */
36 @NonNullByDefault
37 public class RotelIpConnector extends RotelConnector {
38
39     private final Logger logger = LoggerFactory.getLogger(RotelIpConnector.class);
40
41     private String address;
42     private int port;
43
44     private @Nullable Socket clientSocket;
45
46     /**
47      * Constructor
48      *
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
54      */
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);
58
59         this.address = address;
60         this.port = port;
61     }
62
63     @Override
64     public synchronized void open() throws RotelException {
65         logger.debug("Opening IP connection on IP {} port {}", this.address, this.port);
66         try {
67             Socket clientSocket = new Socket(this.address, this.port);
68             clientSocket.setSoTimeout(100);
69
70             dataOut = new DataOutputStream(clientSocket.getOutputStream());
71             dataIn = new DataInputStream(clientSocket.getInputStream());
72
73             Thread thread = new RotelReaderThread(this, readerThreadName);
74             setReaderThread(thread);
75             thread.start();
76
77             this.clientSocket = clientSocket;
78
79             setConnected(true);
80
81             logger.debug("IP connection opened");
82         } catch (IOException | SecurityException | IllegalArgumentException e) {
83             setConnected(false);
84             throw new RotelException("Opening IP connection failed", e);
85         }
86     }
87
88     @Override
89     public synchronized void close() {
90         logger.debug("Closing IP connection");
91         super.cleanup();
92         Socket clientSocket = this.clientSocket;
93         if (clientSocket != null) {
94             try {
95                 clientSocket.close();
96             } catch (IOException e) {
97             }
98             this.clientSocket = null;
99         }
100         setConnected(false);
101         logger.debug("IP connection closed");
102     }
103
104     /**
105      * Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes
106      * actually read is returned as an integer.
107      * In case of socket timeout, the returned value is 0.
108      *
109      * @param dataBuffer the buffer into which the data is read.
110      *
111      * @return the total number of bytes read into the buffer, or -1 if there is no more data because the end of the
112      *         stream has been reached.
113      *
114      * @throws RotelException - If the input stream is null, if the first byte cannot be read for any reason
115      *             other than the end of the file, if the input stream has been closed, or if some other I/O error
116      *             occurs.
117      * @throws InterruptedIOException - if the thread was interrupted during the reading of the input stream
118      */
119     @Override
120     protected int readInput(byte[] dataBuffer) throws RotelException, InterruptedIOException {
121         InputStream dataIn = this.dataIn;
122         if (dataIn == null) {
123             throw new RotelException("readInput failed: input stream is null");
124         }
125         try {
126             return dataIn.read(dataBuffer);
127         } catch (SocketTimeoutException e) {
128             return 0;
129         } catch (IOException e) {
130             logger.debug("readInput failed: {}", e.getMessage());
131             throw new RotelException("readInput failed", e);
132         }
133     }
134 }