2 * Copyright (c) 2010-2020 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;
17 import org.openhab.binding.homematic.internal.common.HomematicConfig;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 * Server implementation for receiving messages via BIN-RPC from a Homematic gateway.
24 * @author Gerhard Riegler - Initial contribution
26 public class BinRpcServer implements RpcServer {
27 private final Logger logger = LoggerFactory.getLogger(BinRpcServer.class);
29 private Thread networkServiceThread;
30 private BinRpcNetworkService networkService;
31 private HomematicConfig config;
32 private RpcEventListener listener;
34 public BinRpcServer(RpcEventListener listener, HomematicConfig config) {
35 this.listener = listener;
40 public void start() throws IOException {
41 logger.debug("Initializing BIN-RPC server at port {}", config.getBinCallbackPort());
43 networkService = new BinRpcNetworkService(listener, config);
44 networkServiceThread = new Thread(networkService);
45 networkServiceThread.setName("HomematicRpcServer");
46 networkServiceThread.start();
50 public void shutdown() {
51 if (networkService != null) {
52 logger.debug("Stopping BIN-RPC server");
54 if (networkServiceThread != null) {
55 networkServiceThread.interrupt();
57 } catch (Exception e) {
58 logger.error("{}", e.getMessage(), e);
60 networkService.shutdown();
61 networkService = null;