Skip to content

Commit

Permalink
Add service publishing using avahi
Browse files Browse the repository at this point in the history
  • Loading branch information
amnonpaz committed Sep 4, 2024
1 parent 8debb42 commit 5386795
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 4 deletions.
2 changes: 1 addition & 1 deletion mrobot_controller/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def main():
logger.info("Starting controller...")
asyncio.run(controller.run())
except Exception as e:
logger.critical(f"Failed to start video streaming: {e}")
logger.critical(f"Failed to start controller: {e}")
controller.stop()
exit(1)

Expand Down
12 changes: 10 additions & 2 deletions mrobot_controller/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .websocket import WebSocketServer, WebSocketMessageHandler
from .serdes import deserialize, DeserializationError, serialize
from .video_streamer import VideoStreamer
from .dns_sd import ServicePublisher


class ControllerException(Exception):
Expand All @@ -20,6 +21,7 @@ def __init__(self, port: int, video_config: dict):
'localhost',
video_config['port'],
video_config['test'])
self.service_publisher = ServicePublisher('mrobot-server', video_config['port'])
self.event_loop = None
self.tasks = None

Expand Down Expand Up @@ -49,15 +51,21 @@ def handle_message(self, message):
async def run(self):
self.logger.debug('Starting server')

self.service_publisher.publish()

self.event_loop = asyncio.get_running_loop()
self.tasks = await asyncio.gather(
asyncio.to_thread(self.video_streamer.start),
asyncio.create_task(self.websocket_server.start())
)

def stop(self):
self.video_streamer.stop()
self.tasks.cancel()
if self.service_publisher:
self.service_publisher.unpublish()
if self.video_streamer:
self.video_streamer.stop()
if self.tasks:
self.tasks.cancel()

def video_start(self, parameters):
self.logger.info(f'Starting video')
Expand Down
1 change: 1 addition & 0 deletions mrobot_controller/dns_sd/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .snd_sd import ServicePublisher
32 changes: 32 additions & 0 deletions mrobot_controller/dns_sd/snd_sd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import socket
from zeroconf import ServiceInfo, Zeroconf
import logging


class ServicePublisher:

def __init__(self, service_name: str, port: int):
self.logger = logging.getLogger(f'{self.__class__.__name__}-{service_name}')
self.zeroconf = Zeroconf()
self.service_name = f'_{service_name}._websocket._tcp.local.'
self.port = port

local_ip = socket.gethostbyname(socket.gethostname())

self.info = ServiceInfo(
'_websocket._tcp.local.',
name=self.service_name,
addresses=[socket.inet_aton(local_ip)],
port=port,
properties={'path': '/ws'},
server="websocket-server.local."
)

def publish(self):
self.zeroconf.register_service(self.info)
self.logger.info(f"WebSocket service '{self.service_name}' published on port {self.port}")

def unpublish(self):
self.zeroconf.unregister_service(self.info)
self.zeroconf.close()
self.logger.info(f"WebSocket service '{self.service_name}' removed")
2 changes: 1 addition & 1 deletion mrobot_controller/video_streamer/video_streamer.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def pause(self):
self.logger.info('Pipeline state: PAUSED')
self.pipeline.set_state(Gst.State.PAUSED)

async def stop(self):
def stop(self):
# Clean up
self.pipeline.set_state(Gst.State.NULL)
self.logger.info('Pipeline stopped')
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ websockets
json
asyncio
msgpack
zeroconf

0 comments on commit 5386795

Please sign in to comment.