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.EOFException;
16 import java.io.IOException;
17 import java.net.Socket;
19 import org.openhab.binding.homematic.internal.common.HomematicConfig;
20 import org.openhab.binding.homematic.internal.communicator.message.BinRpcMessage;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * Reads a BIN-RPC message from the socket and handles the method call.
27 * @author Gerhard Riegler - Initial contribution
29 public class BinRpcResponseHandler implements Runnable {
30 private final Logger logger = LoggerFactory.getLogger(BinRpcResponseHandler.class);
32 private Socket socket;
33 private RpcResponseHandler<byte[]> rpcResponseHandler;
34 private HomematicConfig config;
37 public BinRpcResponseHandler(Socket socket, RpcResponseHandler<byte[]> rpcResponseHandler, HomematicConfig config) {
39 this.rpcResponseHandler = rpcResponseHandler;
41 this.created = System.currentTimeMillis();
45 * Reads the event from the Homematic gateway and handles the method call.
50 boolean isMaxAliveReached;
52 BinRpcMessage message = new BinRpcMessage(socket.getInputStream(), true, config.getEncoding());
53 logger.trace("Event BinRpcMessage: {}", message);
54 byte[] returnValue = rpcResponseHandler.handleMethodCall(message.getMethodName(),
55 message.getResponseData());
56 if (returnValue != null) {
57 socket.getOutputStream().write(returnValue);
59 isMaxAliveReached = System.currentTimeMillis() - created > (config.getSocketMaxAlive() * 1000);
60 } while (!isMaxAliveReached);
62 } catch (EOFException eof) {
64 } catch (Exception e) {
65 logger.warn("{}", e.getMessage(), e);
69 } catch (IOException ioe) {