Skip to content
This repository was archived by the owner on Dec 16, 2025. It is now read-only.

Commit fd65d97

Browse files
committed
added lsiten to port 7447 if its not already allocated for in contianer usecases
1 parent 0ad78bb commit fd65d97

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

make87/interfaces/zenoh/interface.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
from typing import Any, Callable, Optional, Union
44
import zenoh
5+
import socket
56
from functools import cached_property
67
from make87.interfaces.base import InterfaceBase
78
from make87.interfaces.zenoh.model import (
@@ -11,6 +12,7 @@
1112
ZenohProviderConfig,
1213
)
1314

15+
logger = logging.getLogger(__name__)
1416

1517
class ZenohInterface(InterfaceBase):
1618
"""
@@ -21,6 +23,10 @@ class ZenohInterface(InterfaceBase):
2123
@cached_property
2224
def zenoh_config(self) -> zenoh.Config:
2325
cfg = zenoh.Config()
26+
27+
if not is_port_in_use(7447):
28+
cfg.insert_json5("listen/endpoints", json.dumps(["tcp/0.0.0.0:7447"]))
29+
2430
endpoints = {
2531
f"tcp/{x.vpn_ip}:{x.vpn_port}"
2632
for x in list(self.interface_config.requesters.values()) + list(self.interface_config.subscribers.values())
@@ -125,3 +131,14 @@ def get_server(self, name: str) -> Any:
125131
Zenoh does not have a server concept, so this method is not implemented.
126132
"""
127133
raise NotImplementedError("Zenoh does not support server interfaces.")
134+
135+
136+
def is_port_in_use(port: int, host: str = "0.0.0.0") -> bool:
137+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
138+
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
139+
try:
140+
sock.bind((host, port))
141+
return False # Not in use
142+
except OSError:
143+
logger.info(f"Port {port} is already in use on {host}.")
144+
return True # Already bound

tests/integration/zenoh/test_pub_sub.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_pub_sub_combination(priority, reliability, congestion_control, express,
5656
handler_type=handler_type,
5757
capacity=handler_capacity,
5858
),
59-
vpn_ip="172.0.0.1",
59+
vpn_ip="localhost",
6060
vpn_port=7447,
6161
same_node=True,
6262
),
@@ -129,19 +129,19 @@ def test_pub_sub_combination(priority, reliability, congestion_control, express,
129129
}
130130
)
131131

132-
# Start subscriber
133-
subscriber_proc = subprocess.Popen(
134-
[sys.executable, str(subscriber_path)], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=subscriber_env
135-
)
136-
137-
time.sleep(1) # Let subscriber boot up
138132

139133
# Start publisher (non-blocking)
140134
publisher_proc = subprocess.Popen(
141135
[sys.executable, str(publisher_path)], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=publisher_env
142136
)
143137

144138
time.sleep(1) # Let publisher publish + subscriber receive
139+
# Start subscriber
140+
subscriber_proc = subprocess.Popen(
141+
[sys.executable, str(subscriber_path)], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=subscriber_env
142+
)
143+
144+
time.sleep(1) # Let subscriber boot up
145145

146146
# Kill publisher
147147
publisher_proc.terminate()
@@ -189,7 +189,7 @@ def test_defaults_only():
189189
topic_key="my_topic_key",
190190
protocol="zenoh",
191191
message_type="make87_messages.text.text_plain.PlainText",
192-
vpn_ip="127.0.0.1",
192+
vpn_ip="localhost",
193193
vpn_port=7447,
194194
same_node=True,
195195
),
@@ -258,24 +258,24 @@ def test_defaults_only():
258258
}
259259
)
260260

261-
# Start subscriber
262-
subscriber_proc = subprocess.Popen(
263-
[sys.executable, str(subscriber_path)], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=subscriber_env
264-
)
265-
266-
time.sleep(1) # Let subscriber boot up
267261

268262
# Start publisher (non-blocking)
269263
publisher_proc = subprocess.Popen(
270264
[sys.executable, str(publisher_path)], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=publisher_env
271265
)
272266

273267
time.sleep(1) # Let publisher publish + subscriber receive
268+
# Start subscriber
269+
subscriber_proc = subprocess.Popen(
270+
[sys.executable, str(subscriber_path)], stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=subscriber_env
271+
)
272+
273+
time.sleep(1) # Let subscriber boot up
274274

275275
# Kill publisher
276276
publisher_proc.terminate()
277277
try:
278-
publisher_proc.communicate(timeout=5)
278+
pub_stdout, pub_stderr = publisher_proc.communicate(timeout=5)
279279
except subprocess.TimeoutExpired:
280280
publisher_proc.kill()
281281
publisher_proc.communicate()

0 commit comments

Comments
 (0)