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.insteon.internal.driver;
15 import java.io.IOException;
16 import java.net.Socket;
17 import java.net.UnknownHostException;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * Implements IOStream for the older hubs (pre 2014).
26 * Also works for serial ports exposed via tcp, eg. ser2net
28 * @author Bernd Pfrommer - Initial contribution
29 * @author Rob Nielsen - Port to openHAB 2 insteon binding
33 @SuppressWarnings("null")
34 public class TcpIOStream extends IOStream {
35 private final Logger logger = LoggerFactory.getLogger(TcpIOStream.class);
37 private @Nullable String host = null;
38 private int port = -1;
39 private @Nullable Socket socket = null;
44 * @param host host name of hub device
45 * @param port port to connect to
47 public TcpIOStream(String host, int port) {
53 public boolean open() {
54 if (host == null || port < 0) {
55 logger.warn("tcp connection to hub not properly configured!");
59 socket = new Socket(host, port);
60 in = socket.getInputStream();
61 out = socket.getOutputStream();
62 } catch (UnknownHostException e) {
63 logger.warn("unknown host name: {}", host);
65 } catch (IOException e) {
66 logger.warn("cannot open connection to {} port {}: {}", host, port, e.getMessage());
77 } catch (IOException e) {
78 logger.warn("failed to close input stream", e);
86 } catch (IOException e) {
87 logger.warn("failed to close output stream", e);
95 } catch (IOException e) {
96 logger.warn("failed to close the socket", e);