Complete implementation of the DCOS package including: - 40+ Python source files across agents, communication, core, learning, memory, protocols, utils - pyproject.toml build configuration - 102 unit tests across all subsystems - Fixed flake.nix (Python 3.12, proper dependencies, pyproject build) - Fixed .woodpecker.yml (correct paths, removed silent-fail flags) - Added .gitignore - Cleared stale pytest cache
47 lines
1.7 KiB
Python
47 lines
1.7 KiB
Python
"""
|
|
Communication network — manages agent connectivity and message delivery.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
from typing import Any, Dict, List, Optional
|
|
from .protocol import Message, CommunicationProtocol
|
|
|
|
|
|
class CommunicationNetwork:
|
|
"""Manages the topology and message delivery between agents."""
|
|
|
|
def __init__(self) -> None:
|
|
self._protocol = CommunicationProtocol()
|
|
self._connections: Dict[str, List[str]] = {} # agent_id -> [peer_ids]
|
|
self._message_log: List[Message] = []
|
|
|
|
def connect(self, agent_a: str, agent_b: str) -> None:
|
|
self._connections.setdefault(agent_a, []).append(agent_b)
|
|
self._connections.setdefault(agent_b, []).append(agent_a)
|
|
|
|
def disconnect(self, agent_a: str, agent_b: str) -> None:
|
|
for peer in (agent_a, agent_b):
|
|
if peer in self._connections:
|
|
self._connections[peer] = [
|
|
p for p in self._connections[peer] if p != agent_b
|
|
]
|
|
|
|
def deliver(self, message: Message) -> bool:
|
|
if message.recipient in self._connections.get(message.sender, []):
|
|
self._message_log.append(message)
|
|
return self._protocol.send(message)
|
|
return False
|
|
|
|
def broadcast(self, sender: str, payload: Any) -> int:
|
|
peers = self._connections.get(sender, [])
|
|
for peer in peers:
|
|
msg = Message(sender=sender, recipient=peer, payload=payload)
|
|
self._message_log.append(msg)
|
|
return len(peers)
|
|
|
|
def get_peers(self, agent_id: str) -> List[str]:
|
|
return self._connections.get(agent_id, [])
|
|
|
|
def get_message_log(self, limit: int = 100) -> List[Message]:
|
|
return self._message_log[-limit:]
|