2 * Copyright (c) 2010-2023 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.dmx.internal.dmxoverethernet;
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;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * The {@link IpNode} represents a sending or receiving node with address and port
30 * @author Jan N. Klug - Initial contribution
35 protected static final Pattern ADDR_PATTERN = Pattern.compile("([\\w.-]+):?(\\d*)");
37 private final Logger logger = LoggerFactory.getLogger(IpNode.class);
39 protected int port = 0;
40 protected @Nullable InetAddress address = null;
49 * constructor with address
51 * @param addrString address in format address[:port]
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)));
61 logger.warn("invalid format {}, returning empty UdpNode", addrString);
66 * constructor with address and port
68 * @param addrString domain name or IP address as string representation
69 * @param port UDP port of the receiver node
71 public IpNode(String addrString, int port) {
73 setInetAddress(addrString);
77 * sets the node address
79 * @param addrString domain name or IP address as string representation
81 public void setInetAddress(String addrString) {
83 this.address = InetAddress.getByName(addrString);
84 } catch (UnknownHostException e) {
86 logger.warn("could not set address from {}", addrString);
91 * set the node address
93 * @param address inet address
95 public void setInetAddress(InetAddress address) {
96 this.address = address;
102 * @param port UDP port of the receiver node
104 public void setPort(int port) {
109 * get this nodes port
113 public int getPort() {
118 * get this nodes address
120 * @return address as InetAddress
122 public @Nullable InetAddress getAddress() {
126 public @Nullable String getAddressString() {
127 InetAddress address = this.address;
128 return address != null ? address.getHostAddress() : null;
132 * convert node to String
134 * @return string representation of this node (address:port)
137 public String toString() {
138 InetAddress address = this.address;
139 return address != null ? address.toString() + ":" + this.port : "(null):" + this.port;
143 * create list of nodes from string
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
149 public static List<IpNode> fromString(String addrString, int defaultPort) throws IllegalArgumentException {
150 List<IpNode> ipNodes = new ArrayList<>();
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));
159 throw new IllegalArgumentException(String.format("Node definition {} is not valid.", singleAddrString));