]> git.basschouten.com Git - openhab-addons.git/blob
83981240d47a405a4d4b2c703a945f0a42f90f5d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.souliss.internal;
14
15 import java.net.DatagramSocket;
16 import java.net.SocketException;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.slf4j.Logger;
21
22 /**
23  * The {@link SoulissDatagramSocketFactory} is responsible for creating datagramSocket object for trasmission e
24  * receiving.
25  *
26  * @author Tonino Fazio - Initial contribution
27  * @author Luca Calcaterra - Refactor for OH3
28  */
29 @NonNullByDefault
30 public class SoulissDatagramSocketFactory {
31
32     public static @Nullable DatagramSocket getSocketDatagram(Logger logger) {
33         return getSocketDatagram(0, logger);
34     }
35
36     public static @Nullable DatagramSocket getSocketDatagram(int socketPortNumber, Logger logger) {
37         // return DatagramSocket for packet trasmission
38         DatagramSocket soulissDatagramSocket = null;
39         logger.debug("Setup socket");
40         try {
41             if (socketPortNumber != 0) {
42                 soulissDatagramSocket = new DatagramSocket(socketPortNumber);
43             } else {
44                 soulissDatagramSocket = new DatagramSocket();
45             }
46             logger.debug("Datagram Socket Created on port {}", soulissDatagramSocket.getLocalPort());
47         } catch (SocketException e) {
48             logger.warn("Error on creation of Socket: {}", e.getMessage());
49         }
50
51         return soulissDatagramSocket;
52     }
53 }