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.homematic.internal.communicator.server;
15 import java.io.IOException;
16 import java.net.InetSocketAddress;
17 import java.net.ServerSocket;
18 import java.net.Socket;
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;
26 * Waits for a message from the Homematic gateway and starts the RpcCallbackHandler to handle the message.
28 * @author Gerhard Riegler - Initial contribution
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' };
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;
43 * Creates the socket for listening to events from the Homematic gateway.
45 public BinRpcNetworkService(RpcEventListener listener, HomematicConfig config) throws IOException {
48 serverSocket = new ServerSocket();
49 serverSocket.setReuseAddress(true);
50 serverSocket.bind(new InetSocketAddress(config.getBinCallbackPort()));
52 this.rpcResponseHandler = new RpcResponseHandler<byte[]>(listener) {
55 protected byte[] getEmptyStringResult() {
56 return BIN_EMPTY_STRING;
60 protected byte[] getEmptyEventListResult() {
61 return BIN_EMPTY_EVENT_LIST;
65 protected byte[] getEmptyArrayResult() {
66 return BIN_EMPTY_ARRAY;
70 protected RpcRequest<byte[]> createRpcRequest() {
71 return new BinRpcMessage(null, BinRpcMessage.TYPE.RESPONSE, config.getEncoding());
77 * Listening for events and starts the callbackHandler if an event received.
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) {
93 * Stops the listening.
95 public void shutdown() {
99 } catch (IOException ioe) {