]> git.basschouten.com Git - openhab-addons.git/blob
7a8056818c28dcbf3c1eea681f27d818d7e306f1
[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.homematic.internal.communicator.server;
14
15 import java.io.IOException;
16 import java.net.InetSocketAddress;
17 import java.net.ServerSocket;
18 import java.net.Socket;
19
20 import org.openhab.binding.homematic.internal.common.HomematicConfig;
21 import org.openhab.binding.homematic.internal.communicator.message.BinRpcMessage;
22 import org.openhab.binding.homematic.internal.communicator.message.RpcRequest;
23 import org.openhab.core.common.ThreadPoolManager;
24
25 /**
26  * Waits for a message from the Homematic gateway and starts the RpcCallbackHandler to handle the message.
27  *
28  * @author Gerhard Riegler - Initial contribution
29  */
30 public class BinRpcNetworkService implements Runnable {
31     private static final byte[] BIN_EMPTY_STRING = { 'B', 'i', 'n', 1, 0, 0, 0, 8, 0, 0, 0, 3, 0, 0, 0, 0 };
32     private static final byte[] BIN_EMPTY_ARRAY = { 'B', 'i', 'n', 1, 0, 0, 0, 8, 0, 0, 1, 0, 0, 0, 0, 0 };
33     private static final byte[] BIN_EMPTY_EVENT_LIST = { 'B', 'i', 'n', 1, 0, 0, 0, 21, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
34             3, 0, 0, 0, 5, 'e', 'v', 'e', 'n', 't' };
35
36     private static final String RPC_POOL_NAME = "homematicRpc";
37     private ServerSocket serverSocket;
38     private boolean accept = true;
39     private HomematicConfig config;
40     private RpcResponseHandler<byte[]> rpcResponseHandler;
41
42     /**
43      * Creates the socket for listening to events from the Homematic gateway.
44      */
45     public BinRpcNetworkService(RpcEventListener listener, HomematicConfig config) throws IOException {
46         this.config = config;
47
48         serverSocket = new ServerSocket();
49         serverSocket.setReuseAddress(true);
50         serverSocket.bind(new InetSocketAddress(config.getBinCallbackPort()));
51
52         this.rpcResponseHandler = new RpcResponseHandler<byte[]>(listener) {
53
54             @Override
55             protected byte[] getEmptyStringResult() {
56                 return BIN_EMPTY_STRING;
57             }
58
59             @Override
60             protected byte[] getEmptyEventListResult() {
61                 return BIN_EMPTY_EVENT_LIST;
62             }
63
64             @Override
65             protected byte[] getEmptyArrayResult() {
66                 return BIN_EMPTY_ARRAY;
67             }
68
69             @Override
70             protected RpcRequest<byte[]> createRpcRequest() {
71                 return new BinRpcMessage(null, BinRpcMessage.TYPE.RESPONSE, config.getEncoding());
72             }
73         };
74     }
75
76     /**
77      * Listening for events and starts the callbackHandler if an event received.
78      */
79     @Override
80     public void run() {
81         while (accept) {
82             try {
83                 Socket cs = serverSocket.accept();
84                 BinRpcResponseHandler rpcHandler = new BinRpcResponseHandler(cs, rpcResponseHandler, config);
85                 ThreadPoolManager.getPool(RPC_POOL_NAME).execute(rpcHandler);
86             } catch (IOException ex) {
87                 // ignore
88             }
89         }
90     }
91
92     /**
93      * Stops the listening.
94      */
95     public void shutdown() {
96         accept = false;
97         try {
98             serverSocket.close();
99         } catch (IOException ioe) {
100             // ignore
101         }
102     }
103 }