]> git.basschouten.com Git - openhab-addons.git/blob
b70cf61a65bdd4b15a5296cd02c8e06ee29e00c9
[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.pioneeravr.internal.protocol.ip;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
18 import java.net.InetSocketAddress;
19 import java.net.Socket;
20
21 import org.openhab.binding.pioneeravr.internal.protocol.StreamAvrConnection;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  *
27  * A class that wraps the communication to a Pioneer AVR devices using an IP connection.
28  *
29  * @author Antoine Besnard - Initial contribution
30  */
31 public class IpAvrConnection extends StreamAvrConnection {
32
33     private final Logger logger = LoggerFactory.getLogger(IpAvrConnection.class);
34
35     /** default port for IP communication **/
36     public static final int DEFAULT_IPCONTROL_PORT = 8102;
37
38     /** Connection timeout in milliseconds **/
39     private static final int CONNECTION_TIMEOUT = 3000;
40
41     /** Socket read timeout in milliseconds **/
42     private static final int SOCKET_READ_TIMEOUT = 1000;
43
44     private int receiverPort;
45     private String receiverHost;
46
47     private Socket ipControlSocket;
48
49     public IpAvrConnection(String receiverHost) {
50         this(receiverHost, null);
51     }
52
53     public IpAvrConnection(String receiverHost, Integer ipControlPort) {
54         this.receiverHost = receiverHost;
55         this.receiverPort = ipControlPort != null && ipControlPort >= 1 ? ipControlPort : DEFAULT_IPCONTROL_PORT;
56     }
57
58     @Override
59     protected void openConnection() throws IOException {
60         ipControlSocket = new Socket();
61
62         // Set this timeout to unblock a blocking read when no data is received. It is useful to check if the
63         // reading thread has to be stopped (it implies a latency of SOCKET_READ_TIMEOUT at most before the
64         // thread is really stopped)
65         ipControlSocket.setSoTimeout(SOCKET_READ_TIMEOUT);
66
67         // Enable tcpKeepAlive to detect premature disconnection
68         // and prevent disconnection because of inactivity
69         ipControlSocket.setKeepAlive(true);
70
71         // Connect to the AVR with a connection timeout.
72         ipControlSocket.connect(new InetSocketAddress(receiverHost, receiverPort), CONNECTION_TIMEOUT);
73
74         logger.debug("Connected to {}:{}", receiverHost, receiverPort);
75     }
76
77     @Override
78     public boolean isConnected() {
79         return ipControlSocket != null && ipControlSocket.isConnected() && !ipControlSocket.isClosed();
80     }
81
82     @Override
83     public void close() {
84         super.close();
85         try {
86             if (ipControlSocket != null) {
87                 ipControlSocket.close();
88                 ipControlSocket = null;
89                 logger.debug("Closed socket!");
90             }
91         } catch (IOException ioException) {
92             logger.error("Closing connection throws an exception!", ioException);
93         }
94     }
95
96     @Override
97     public String getConnectionName() {
98         return receiverHost + ":" + receiverPort;
99     }
100
101     @Override
102     protected InputStream getInputStream() throws IOException {
103         return ipControlSocket.getInputStream();
104     }
105
106     @Override
107     protected OutputStream getOutputStream() throws IOException {
108         return ipControlSocket.getOutputStream();
109     }
110 }