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