2 * Copyright (c) 2010-2023 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.pioneeravr.internal.protocol.ip;
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;
21 import org.openhab.binding.pioneeravr.internal.protocol.StreamAvrConnection;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
27 * A class that wraps the communication to a Pioneer AVR devices using an IP connection.
29 * @author Antoine Besnard - Initial contribution
31 public class IpAvrConnection extends StreamAvrConnection {
33 private final Logger logger = LoggerFactory.getLogger(IpAvrConnection.class);
35 /** default port for IP communication **/
36 public static final int DEFAULT_IPCONTROL_PORT = 8102;
38 /** Connection timeout in milliseconds **/
39 private static final int CONNECTION_TIMEOUT = 3000;
41 /** Socket read timeout in milliseconds **/
42 private static final int SOCKET_READ_TIMEOUT = 1000;
44 private int receiverPort;
45 private String receiverHost;
47 private Socket ipControlSocket;
49 public IpAvrConnection(String receiverHost) {
50 this(receiverHost, null);
53 public IpAvrConnection(String receiverHost, Integer ipControlPort) {
54 this.receiverHost = receiverHost;
55 this.receiverPort = ipControlPort != null && ipControlPort >= 1 ? ipControlPort : DEFAULT_IPCONTROL_PORT;
59 protected void openConnection() throws IOException {
60 ipControlSocket = new Socket();
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);
67 // Enable tcpKeepAlive to detect premature disconnection
68 // and prevent disconnection because of inactivity
69 ipControlSocket.setKeepAlive(true);
71 // Connect to the AVR with a connection timeout.
72 ipControlSocket.connect(new InetSocketAddress(receiverHost, receiverPort), CONNECTION_TIMEOUT);
74 logger.debug("Connected to {}:{}", receiverHost, receiverPort);
78 public boolean isConnected() {
79 return ipControlSocket != null && ipControlSocket.isConnected() && !ipControlSocket.isClosed();
86 if (ipControlSocket != null) {
87 ipControlSocket.close();
88 ipControlSocket = null;
89 logger.debug("Closed socket!");
91 } catch (IOException ioException) {
92 logger.error("Closing connection throws an exception!", ioException);
97 public String getConnectionName() {
98 return receiverHost + ":" + receiverPort;
102 protected InputStream getInputStream() throws IOException {
103 return ipControlSocket.getInputStream();
107 protected OutputStream getOutputStream() throws IOException {
108 return ipControlSocket.getOutputStream();