]> git.basschouten.com Git - openhab-addons.git/blob
792e449d0153d2e7ff4f111dc73c8a98efdcd6c5
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.insteon.internal.driver;
14
15 import java.io.IOException;
16 import java.net.Socket;
17 import java.net.UnknownHostException;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Implements IOStream for the older hubs (pre 2014).
26  * Also works for serial ports exposed via tcp, eg. ser2net
27  *
28  * @author Bernd Pfrommer - Initial contribution
29  * @author Rob Nielsen - Port to openHAB 2 insteon binding
30  *
31  */
32 @NonNullByDefault
33 @SuppressWarnings("null")
34 public class TcpIOStream extends IOStream {
35     private final Logger logger = LoggerFactory.getLogger(TcpIOStream.class);
36
37     private @Nullable String host = null;
38     private int port = -1;
39     private @Nullable Socket socket = null;
40
41     /**
42      * Constructor
43      *
44      * @param host host name of hub device
45      * @param port port to connect to
46      */
47     public TcpIOStream(String host, int port) {
48         this.host = host;
49         this.port = port;
50     }
51
52     @Override
53     public boolean open() {
54         if (host == null || port < 0) {
55             logger.warn("tcp connection to hub not properly configured!");
56             return (false);
57         }
58         try {
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);
64             return (false);
65         } catch (IOException e) {
66             logger.warn("cannot open connection to {} port {}: {}", host, port, e.getMessage());
67             return (false);
68         }
69         return true;
70     }
71
72     @Override
73     public void close() {
74         if (in != null) {
75             try {
76                 in.close();
77             } catch (IOException e) {
78                 logger.warn("failed to close input stream", e);
79             }
80             in = null;
81         }
82
83         if (out != null) {
84             try {
85                 out.close();
86             } catch (IOException e) {
87                 logger.warn("failed to close output stream", e);
88             }
89             out = null;
90         }
91
92         if (socket != null) {
93             try {
94                 socket.close();
95             } catch (IOException e) {
96                 logger.warn("failed to close the socket", e);
97             }
98             socket = null;
99         }
100     }
101 }