Refactored ZMQ context implementation #16
@@ -17,8 +17,7 @@ async def receive_command(command: SpeechCommand, request: Request):
|
|||||||
# Validate and retrieve data.
|
# Validate and retrieve data.
|
||||||
SpeechCommand.model_validate(command)
|
SpeechCommand.model_validate(command)
|
||||||
topic = b"command"
|
topic = b"command"
|
||||||
pub_socket = Context.instance().socket(zmq.PUB)
|
pub_socket = request.app.state.endpoints_pub_socket
|
||||||
pub_socket.connect(settings.zmq_settings.internal_pub_address)
|
|
||||||
await pub_socket.send_multipart([topic, command.model_dump_json().encode()])
|
await pub_socket.send_multipart([topic, command.model_dump_json().encode()])
|
||||||
|
|
||||||
return {"status": "Command received"}
|
return {"status": "Command received"}
|
||||||
|
|||||||
@@ -1,10 +1,7 @@
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
import zmq
|
|
||||||
from fastapi import APIRouter, Request
|
from fastapi import APIRouter, Request
|
||||||
from zmq.asyncio import Context
|
|
||||||
|
|
||||||
from control_backend.core.config import settings
|
|
||||||
from control_backend.schemas.message import Message
|
from control_backend.schemas.message import Message
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -19,8 +16,7 @@ async def receive_message(message: Message, request: Request):
|
|||||||
topic = b"message"
|
topic = b"message"
|
||||||
body = message.model_dump_json().encode("utf-8")
|
body = message.model_dump_json().encode("utf-8")
|
||||||
|
|
||||||
pub_socket = Context.instance().socket(zmq.PUB)
|
pub_socket = request.app.state.endpoints_pub_socket
|
||||||
pub_socket.bind(settings.zmq_settings.internal_pub_address)
|
|
||||||
await pub_socket.send_multipart([topic, body])
|
await pub_socket.send_multipart([topic, body])
|
||||||
|
|
||||||
return {"status": "Message received"}
|
return {"status": "Message received"}
|
||||||
|
|||||||
@@ -28,14 +28,14 @@ def setup_sockets():
|
|||||||
context = Context.instance()
|
context = Context.instance()
|
||||||
|
|
||||||
internal_pub_socket = context.socket(zmq.XPUB)
|
internal_pub_socket = context.socket(zmq.XPUB)
|
||||||
internal_pub_socket.bind(settings.zmq_settings.internal_pub_address)
|
internal_pub_socket.bind(settings.zmq_settings.internal_sub_address)
|
||||||
logger.debug("Internal publishing socket bound to %s", internal_pub_socket)
|
logger.debug("Internal publishing socket bound to %s", internal_pub_socket)
|
||||||
|
|
||||||
internal_sub_socket = context.socket(zmq.XSUB)
|
internal_sub_socket = context.socket(zmq.XSUB)
|
||||||
internal_sub_socket.bind(settings.zmq_settings.internal_sub_address)
|
internal_sub_socket.bind(settings.zmq_settings.internal_pub_address)
|
||||||
logger.debug("Internal subscribing socket bound to %s", internal_sub_socket)
|
logger.debug("Internal subscribing socket bound to %s", internal_sub_socket)
|
||||||
try:
|
try:
|
||||||
zmq.proxy(internal_pub_socket, internal_sub_socket)
|
zmq.proxy(internal_sub_socket, internal_pub_socket)
|
||||||
except zmq.ZMQError:
|
except zmq.ZMQError:
|
||||||
logger.warning("Error while handling PUB/SUB proxy. Closing sockets.")
|
logger.warning("Error while handling PUB/SUB proxy. Closing sockets.")
|
||||||
finally:
|
finally:
|
||||||
@@ -51,6 +51,12 @@ async def lifespan(app: FastAPI):
|
|||||||
proxy_thread.daemon = True
|
proxy_thread.daemon = True
|
||||||
proxy_thread.start()
|
proxy_thread.start()
|
||||||
|
|
||||||
|
context = Context.instance()
|
||||||
|
|
||||||
|
endpoints_pub_socket = context.socket(zmq.PUB)
|
||||||
|
endpoints_pub_socket.connect(settings.zmq_settings.internal_pub_address)
|
||||||
|
app.state.endpoints_pub_socket = endpoints_pub_socket
|
||||||
|
|
||||||
# Initiate agents
|
# Initiate agents
|
||||||
ri_communication_agent = RICommunicationAgent(
|
ri_communication_agent = RICommunicationAgent(
|
||||||
settings.agent_settings.ri_communication_agent_name + "@" + settings.agent_settings.host,
|
settings.agent_settings.ri_communication_agent_name + "@" + settings.agent_settings.host,
|
||||||
|
|||||||
Reference in New Issue
Block a user