]> git.basschouten.com Git - openhab-addons.git/blob
e88d492834f4bc37ed92e250c49c8d1aa7e0831e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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
17 import org.openhab.binding.homematic.internal.common.HomematicConfig;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Server implementation for receiving messages via BIN-RPC from a Homematic gateway.
23  *
24  * @author Gerhard Riegler - Initial contribution
25  */
26 public class BinRpcServer implements RpcServer {
27     private final Logger logger = LoggerFactory.getLogger(BinRpcServer.class);
28
29     private Thread networkServiceThread;
30     private BinRpcNetworkService networkService;
31     private HomematicConfig config;
32     private RpcEventListener listener;
33
34     public BinRpcServer(RpcEventListener listener, HomematicConfig config) {
35         this.listener = listener;
36         this.config = config;
37     }
38
39     @Override
40     public void start() throws IOException {
41         logger.debug("Initializing BIN-RPC server at port {}", config.getBinCallbackPort());
42
43         networkService = new BinRpcNetworkService(listener, config);
44         networkServiceThread = new Thread(networkService);
45         networkServiceThread.setName("HomematicRpcServer");
46         networkServiceThread.start();
47     }
48
49     @Override
50     public void shutdown() {
51         if (networkService != null) {
52             logger.debug("Stopping BIN-RPC server");
53             try {
54                 if (networkServiceThread != null) {
55                     networkServiceThread.interrupt();
56                 }
57             } catch (Exception e) {
58                 logger.error("{}", e.getMessage(), e);
59             }
60             networkService.shutdown();
61             networkService = null;
62         }
63     }
64 }