Skip to content

Commit 3c8b318

Browse files
SB-50 Remove interface address resolution
1 parent a95a3f0 commit 3c8b318

4 files changed

Lines changed: 19 additions & 64 deletions

File tree

examples/cameraServiceExample.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from uuid import uuid4
22

3+
from common_utility import InterfaceResolver
34
from context_logger import get_logger, setup_logging
45

56
from examples import setup_shutdown
6-
from hello import ServiceInfo, Hello, Group, GroupUrl
7+
from hello import ServiceInfo, Hello, Group
78

89
setup_logging('hello')
910

@@ -13,17 +14,16 @@
1314
def main() -> None:
1415
shutdown_event = setup_shutdown()
1516

16-
# Define the group URL optionally specifying the network interface
17-
url = GroupUrl(address='239.0.1.1', port=5555, interface='wlan0')
17+
# Resolve the address for the specified interface name (e.g., 'wlan0')
18+
if_address = InterfaceResolver().resolve('wlan0')
1819

1920
# Define the group to advertise the camera service on
20-
group = Group.create(name='effective-range/sniper', url=url)
21+
group = Group.create(name='effective-range/sniper', address='239.0.1.1', port=5555, if_address=if_address)
2122

2223
# Define the service information for the camera
23-
hostname = 'er-sniper-camera-1'
24-
info = ServiceInfo(uuid=uuid4(), name=hostname, role='camera', urls={
25-
'api': f'grpc://{hostname}.local:50051',
26-
'stream': f'http://{hostname}.local:8000'
24+
info = ServiceInfo(uuid=uuid4(), name='er-sniper-camera-1', role='camera', urls={
25+
'api': f'grpc://{if_address}:50051',
26+
'stream': f'http://{if_address}:8000/video_feed'
2727
})
2828

2929
# Use a scheduled advertizer to periodically announce the camera service

hello/group.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,6 @@
55
from dataclasses import dataclass
66
from enum import Enum
77

8-
import netifaces
9-
10-
11-
@dataclass
12-
class GroupUrl:
13-
address: str
14-
port: int
15-
protocol: str = 'udp'
16-
interface: str | None = None
17-
18-
def resolve(self) -> str:
19-
if self.interface and self.interface in netifaces.interfaces():
20-
inet_address = netifaces.ifaddresses(self.interface).get(netifaces.AF_INET)
21-
if inet_address and len(inet_address) > 0:
22-
address = inet_address[0].get('addr')
23-
return f'{self.protocol}://{address};{self.address}:{self.port}'
24-
25-
return f'{self.protocol}://{self.address}:{self.port}'
26-
278

289
class GroupPrefix(Enum):
2910
HELLO = 'hello'
@@ -42,8 +23,9 @@ def query(self) -> 'PrefixedGroup':
4223
return PrefixedGroup(GroupPrefix.QUERY, self)
4324

4425
@staticmethod
45-
def create(name: str, url: GroupUrl) -> 'Group':
46-
return Group(name, url.resolve())
26+
def create(name: str, address: str, port: int, protocol: str = 'udp', if_address: str | None = None) -> 'Group':
27+
if_address = f'{if_address};' if if_address else ''
28+
return Group(name, f'{protocol}://{if_address}{address}:{port}')
4729

4830

4931
@dataclass

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ maintainers = [
88
{ name = "Ferenc Nandor Janky & Attila Gombos", email = "info@effective-range.com" }
99
]
1010
dependencies = [
11-
"netifaces",
1211
"pyzmq @ git+https://github.com/EffectiveRange/pyzmq.git@v27.1.0+drafts1",
1312
"python-context-logger @ git+https://github.com/EffectiveRange/python-context-logger.git@latest",
1413
"python-common-utility @ git+https://github.com/EffectiveRange/python-common-utility.git@latest"
1514
]
1615
dynamic = ["version"]
1716

1817
[tool.setuptools]
19-
package-dir = {"" = "."}
18+
package-dir = { "" = "." }
2019
packages = ["hello", "examples"]
2120

2221
[tool.setuptools.package-data]

tests/groupTest.py

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from context_logger import setup_logging
55

6-
from hello import GroupUrl, Group
6+
from hello import Group
77

88

99
class GroupTest(TestCase):
@@ -15,45 +15,19 @@ def setUpClass(cls):
1515
def setUp(self):
1616
print()
1717

18-
def test_url_resolves_when_interface_is_not_provided(self):
19-
# Given
20-
group_url = GroupUrl(protocol='udp', address='239.0.1.1', port=5555)
21-
22-
# When
23-
url = group_url.resolve()
24-
25-
# Then
26-
self.assertEqual('udp://239.0.1.1:5555', url)
27-
28-
def test_url_resolves_when_interface_is_provided(self):
29-
# Given
30-
group_url = GroupUrl(protocol='udp', address='239.0.1.1', port=5555, interface='lo')
31-
18+
def test_group_created(self):
3219
# When
33-
url = group_url.resolve()
20+
group = Group.create(name='test-group', address='239.0.1.1', port=5555)
3421

3522
# Then
36-
self.assertEqual('udp://127.0.0.1;239.0.1.1:5555', url)
37-
38-
def test_url_resolves_when_interface_is_provided_but_not_exists(self):
39-
# Given
40-
group_url = GroupUrl(protocol='udp', address='239.0.1.1', port=5555, interface='nonexistent0')
41-
42-
# When
43-
url = group_url.resolve()
44-
45-
# Then
46-
self.assertEqual('udp://239.0.1.1:5555', url)
47-
48-
def test_group_created_with_resolved_url(self):
49-
# Given
50-
group_url = GroupUrl(address='239.0.1.1', port=5555, interface='lo')
23+
self.assertEqual(Group(name='test-group', url='udp://239.0.1.1:5555'), group)
5124

25+
def test_group_created_when_interface_address_is_provided(self):
5226
# When
53-
group = Group.create('test-group', group_url)
27+
group = Group.create(name='test-group', address='239.0.1.1', port=5555, if_address='192.168.0.100')
5428

5529
# Then
56-
self.assertEqual(Group(name='test-group', url='udp://127.0.0.1;239.0.1.1:5555'), group)
30+
self.assertEqual(Group(name='test-group', url='udp://192.168.0.100;239.0.1.1:5555'), group)
5731

5832
def test_prefixed_group_created_with_hello_prefix(self):
5933
# Given

0 commit comments

Comments
 (0)