]> git.basschouten.com Git - openhab-addons.git/blob
d8cb216738c2a8896b286124ce615bca2642c19b
[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.dmx.internal.dmxoverethernet;
14
15 import java.net.InetAddress;
16 import java.net.UnknownHostException;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * The {@link IpNode} represents a sending or receiving node with address and port
29  *
30  * @author Jan N. Klug - Initial contribution
31  *
32  */
33 @NonNullByDefault
34 public class IpNode {
35     protected static final Pattern ADDR_PATTERN = Pattern.compile("([\\w.-]+):?(\\d*)");
36
37     private final Logger logger = LoggerFactory.getLogger(IpNode.class);
38
39     protected int port = 0;
40     protected @Nullable InetAddress address = null;
41
42     /**
43      * default constructor
44      */
45     public IpNode() {
46     }
47
48     /**
49      * constructor with address
50      *
51      * @param addrString address in format address[:port]
52      */
53     public IpNode(String addrString) {
54         Matcher addrMatcher = ADDR_PATTERN.matcher(addrString);
55         if (addrMatcher.matches()) {
56             setInetAddress(addrMatcher.group(1));
57             if (!addrMatcher.group(2).isEmpty()) {
58                 setPort(Integer.parseInt(addrMatcher.group(2)));
59             }
60         } else {
61             logger.warn("invalid format {}, returning empty UdpNode", addrString);
62         }
63     }
64
65     /**
66      * constructor with address and port
67      *
68      * @param addrString domain name or IP address as string representation
69      * @param port UDP port of the receiver node
70      */
71     public IpNode(String addrString, int port) {
72         setPort(port);
73         setInetAddress(addrString);
74     }
75
76     /**
77      * sets the node address
78      *
79      * @param addrString domain name or IP address as string representation
80      */
81     public void setInetAddress(String addrString) {
82         try {
83             this.address = InetAddress.getByName(addrString);
84         } catch (UnknownHostException e) {
85             this.address = null;
86             logger.warn("could not set address from {}", addrString);
87         }
88     }
89
90     /**
91      * set the node address
92      *
93      * @param address inet address
94      */
95     public void setInetAddress(InetAddress address) {
96         this.address = address;
97     }
98
99     /**
100      * sets the node port
101      *
102      * @param port UDP port of the receiver node
103      */
104     public void setPort(int port) {
105         this.port = port;
106     }
107
108     /**
109      * get this nodes port
110      *
111      * @return UDP port
112      */
113     public int getPort() {
114         return this.port;
115     }
116
117     /**
118      * get this nodes address
119      *
120      * @return address as InetAddress
121      */
122     public @Nullable InetAddress getAddress() {
123         return address;
124     }
125
126     public @Nullable String getAddressString() {
127         InetAddress address = this.address;
128         return address != null ? address.getHostAddress() : null;
129     }
130
131     /**
132      * convert node to String
133      *
134      * @return string representation of this node (address:port)
135      */
136     @Override
137     public String toString() {
138         InetAddress address = this.address;
139         return address != null ? address.toString() + ":" + this.port : "(null):" + this.port;
140     }
141
142     /**
143      * create list of nodes from string
144      *
145      * @param addrString input string, format: address1[:port],address2
146      * @param defaultPort default port if none is given in the string
147      * @return a List of IpNodes
148      */
149     public static List<IpNode> fromString(String addrString, int defaultPort) throws IllegalArgumentException {
150         List<IpNode> ipNodes = new ArrayList<>();
151         int port;
152
153         for (String singleAddrString : addrString.split(",")) {
154             Matcher addrMatch = ADDR_PATTERN.matcher(singleAddrString);
155             if (addrMatch.matches()) {
156                 port = (addrMatch.group(2).isEmpty()) ? defaultPort : Integer.valueOf(addrMatch.group(2));
157                 ipNodes.add(new IpNode(addrMatch.group(1), port));
158             } else {
159                 throw new IllegalArgumentException(String.format("Node definition {} is not valid.", singleAddrString));
160             }
161         }
162         return ipNodes;
163     }
164 }