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