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.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * The {@link IpNode} represents a sending or receiving node with address and port
28 * @author Jan N. Klug - Initial contribution
32 protected static final Pattern ADDR_PATTERN = Pattern.compile("([\\w.-]+):?(\\d*)");
34 private final Logger logger = LoggerFactory.getLogger(IpNode.class);
36 protected int port = 0;
37 protected InetAddress address = null;
46 * constructor with address
48 * @param addrString address in format address[:port]
50 public IpNode(String addrString) {
51 Matcher addrMatcher = ADDR_PATTERN.matcher(addrString);
52 if (addrMatcher.matches()) {
53 setInetAddress(addrMatcher.group(1));
54 if (!addrMatcher.group(2).isEmpty()) {
55 setPort(Integer.valueOf(addrMatcher.group(2)));
58 logger.warn("invalid format {}, returning empty UdpNode", addrString);
63 * constructor with address and port
65 * @param addrString domain name or IP address as string representation
66 * @param port UDP port of the receiver node
68 public IpNode(String addrString, int port) {
70 setInetAddress(addrString);
74 * sets the node address
76 * @param addrString domain name or IP address as string representation
78 public void setInetAddress(String addrString) {
80 this.address = InetAddress.getByName(addrString);
81 } catch (UnknownHostException e) {
83 logger.warn("could not set address from {}", addrString);
88 * set the node address
90 * @param address inet address
92 public void setInetAddress(InetAddress address) {
93 this.address = address;
99 * @param port UDP port of the receiver node
101 public void setPort(int port) {
106 * get this nodes port
110 public int getPort() {
115 * get this nodes address
117 * @return address as InetAddress
119 public InetAddress getAddress() {
123 public String getAddressString() {
124 String addrString = address.getHostAddress();
129 * convert node to String
131 * @return string representation of this node (address:port)
134 public String toString() {
135 if (this.address == null) {
136 return "(null):" + String.valueOf(this.port);
138 return this.address.toString() + ":" + String.valueOf(this.port);
142 * create list of nodes from string
144 * @param addrString input string, format: address1[:port],address2
145 * @param defaultPort default port if none is given in the string
146 * @return a List of IpNodes
148 public static List<IpNode> fromString(String addrString, int defaultPort) throws IllegalArgumentException {
149 List<IpNode> ipNodes = new ArrayList<>();
152 for (String singleAddrString : addrString.split(",")) {
153 Matcher addrMatch = ADDR_PATTERN.matcher(singleAddrString);
154 if (addrMatch.matches()) {
155 port = (addrMatch.group(2).isEmpty()) ? defaultPort : Integer.valueOf(addrMatch.group(2));
156 ipNodes.add(new IpNode(addrMatch.group(1), port));
158 throw new IllegalArgumentException(String.format("Node definition {} is not valid.", singleAddrString));