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.homematic.internal.handler;
15 import java.util.ArrayList;
16 import java.util.List;
19 * A very simple pool implementation that handles free port numbers used by the RPC server if multiple bridges are
22 * @author Gerhard Riegler - Initial contribution
24 public class SimplePortPool {
25 private static int START_PORT = 9125;
27 private List<PortInfo> availablePorts = new ArrayList<>();
30 * Adds the specified port to the pool and mark it as in use.
32 public void setInUse(int port) {
33 PortInfo portInfo = new PortInfo();
35 portInfo.free = false;
36 availablePorts.add(portInfo);
40 * Returns the next free port number.
42 public synchronized int getNextPort() {
43 for (PortInfo portInfo : availablePorts) {
45 portInfo.free = false;
50 PortInfo portInfo = new PortInfo();
51 while (isPortInUse(START_PORT++)) {
53 portInfo.port = START_PORT - 1;
54 portInfo.free = false;
55 availablePorts.add(portInfo);
61 * Returns true, if the specified port is not in use.
63 private boolean isPortInUse(int port) {
64 for (PortInfo portInfo : availablePorts) {
65 if (portInfo.port == port) {
66 return !portInfo.free;
73 * Releases a unused port number.
75 public synchronized void release(int port) {
76 for (PortInfo portInfo : availablePorts) {
77 if (portInfo.port == port) {
83 private class PortInfo {