]> git.basschouten.com Git - openhab-addons.git/blob
ba0ae12ed08235465412c2de59c9c1e9f42ccdae
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.mihome.internal.socket;
14
15 import java.io.IOException;
16 import java.net.InetAddress;
17 import java.net.MulticastSocket;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Takes care of the multicast communication with the bridge.
25  *
26  * @author Dieter Schmidt - Initial contribution
27  *
28  */
29 @NonNullByDefault
30 public class XiaomiBridgeSocket extends XiaomiSocket {
31
32     private final Logger logger = LoggerFactory.getLogger(XiaomiBridgeSocket.class);
33
34     public XiaomiBridgeSocket(int port, String owner) {
35         super(port, owner);
36     }
37
38     /**
39      * Sets up the {@link XiaomiBridgeSocket}.
40      *
41      * Connects the socket to the specific multicast address and port.
42      */
43     @Override
44     protected synchronized void setupSocket() {
45         MulticastSocket socket = (MulticastSocket) getSocket();
46         if (socket != null) {
47             logger.debug("Socket already setup");
48             return;
49         }
50
51         try {
52             logger.debug("Setup socket");
53             socket = new MulticastSocket(getPort());
54             setSocket(socket); // must bind receive side
55             socket.joinGroup(InetAddress.getByName(MCAST_ADDR));
56             logger.debug("Initialized socket to {}:{} on {}:{}", socket.getRemoteSocketAddress(), socket.getPort(),
57                     socket.getLocalAddress(), socket.getLocalPort());
58         } catch (IOException e) {
59             logger.error("Setup socket error", e);
60         }
61     }
62 }