"""fleetmesh-protocol-rhio: Rhio cluster sync protocol package for Fleetmesh.
Conforms to ProtocolPackage (fleetmesh_core.wire.interfaces.ProtocolPackage).
Mounts Rhio cluster blob sync, NATS JetStream bridge, and Bao tree verification
onto ProtocolHostRouter and provides RhioRung for the TransportRouter ladder.
"""
from __future__ import annotations
import base64
import json
import logging
import os
import urllib.parse
from typing import Any, Mapping, Optional
from fleetmesh_core.wire.interfaces import (
ProtocolHealth,
ProtocolHostRouter,
ProtocolIdentityAdapter,
)
from fleetmesh_node.protocols.base import BaseProtocolPackage
from fleetmesh_node.protocols.p2panda.identity import P2PandaIdentityAdapter
from .rung import RhioRung
from .store import RhioBlobSync
__all__ = [
'RhioProtocolPackage',
]
log = logging.getLogger(__name__)
[docs]class RhioProtocolPackage(BaseProtocolPackage):
"""The Rhio cluster synchronization suite for Fleetmesh."""
name: str = 'rhio'
version: str = '0.1.0'
standards: tuple[str, ...] = (
'rhio-nats-v1',
'p2panda-v0.4.0',
'bao-tree-hash',
)
[docs] def __init__(
self,
node_dir: Optional[str] = None,
keystore: Optional[Any] = None,
blob_store: Optional[Any] = None,
store: Optional[RhioBlobSync] = None,
sidecar_client: Optional[Any] = None,
auto_start_sidecar: bool = False,
**kwargs: Any,
) -> None:
self.node_dir = node_dir
self.keystore = keystore
self.blob_store = blob_store
self.auto_start_sidecar = auto_start_sidecar
self.config: Mapping[str, Any] = {}
self._enabled: bool = False
self._running: bool = False
self._messages_in: int = 0
self._messages_out: int = 0
self._daemon: Optional[Any] = None
# Identity adapter (p2panda author identity used by Rhio)
if keystore is not None:
self.identity: Optional[P2PandaIdentityAdapter] = P2PandaIdentityAdapter.from_keystore(keystore)
else:
self.identity = None
# Blob & Tree Hash Store
if store is not None:
self.store: RhioBlobSync = store
elif node_dir:
db_path = os.path.join(node_dir, "rhio_blobs.db")
cas_dir = os.path.join(node_dir, "rhio_blobs")
self.store = RhioBlobSync(db_path=db_path, cas_dir=cas_dir)
else:
self.store = RhioBlobSync(":memory:")
# Outbound Transport Rung
self.socket_path: Optional[str] = os.path.join(node_dir, "rhio.sock") if node_dir else None
self.rung: RhioRung = RhioRung(
sidecar_client=sidecar_client,
socket_path=self.socket_path,
enabled=self._enabled,
)
self._host: Optional[ProtocolHostRouter] = None
[docs] def identity_adapter(self, keystore: Any) -> Optional[ProtocolIdentityAdapter]:
if keystore is not None:
return P2PandaIdentityAdapter.from_keystore(keystore)
return self.identity
[docs] def mount_ingress(self, host: ProtocolHostRouter) -> None:
if not self._enabled:
return
self._host = host
from fleetmesh_node.transport import ws
# 1. GET /rhio/status
def _handle_status(conn: Any, req: Any) -> bool:
body_dict = {
'status': 'online' if self._running else 'configured',
'name': self.name,
'version': self.version,
'network_id': self.config.get('network_id', 'rhio-default-network-1'),
'nats_endpoint': self.config.get('nats_endpoint', 'localhost:4222'),
's3_endpoint': self.config.get('s3_endpoint', 'http://localhost:9000'),
'blobs_count': self.store.count_blobs(),
}
body = json.dumps(body_dict, indent=2).encode('utf-8')
ws.http_response(conn, "200 OK", body, "application/json")
return True
# 2. GET & POST /rhio/blobs
def _handle_blobs(conn: Any, req: Any) -> bool:
method = getattr(req, 'method', 'GET').upper()
uri = getattr(req, 'path', '')
parsed = urllib.parse.urlparse(uri)
if method == 'GET':
qparams = urllib.parse.parse_qs(parsed.query)
blob_hash = qparams.get('hash', [None])[0]
target_route = parsed.path
if not blob_hash and target_route.startswith('/rhio/blobs/'):
tail = target_route[len('/rhio/blobs/'):].strip('/')
if tail:
blob_hash = tail
if blob_hash:
blob = self.store.get_blob(blob_hash)
if blob is None and self.blob_store is not None and hasattr(self.blob_store, 'get'):
try:
blob = self.blob_store.get(blob_hash)
except Exception:
blob = None
if blob is None:
ws.http_response(conn, "404 Not Found", b'{"error":"not_found"}', "application/json")
return True
ws.http_response(conn, "200 OK", blob, "application/octet-stream")
return True
else:
blobs = self.store.list_blobs(limit=50)
body = json.dumps({'blobs': blobs, 'total': self.store.count_blobs()}).encode('utf-8')
ws.http_response(conn, "200 OK", body, "application/json")
return True
elif method == 'POST':
try:
payload = ws.read_body(conn, req)
except Exception as e:
ws.http_response(conn, "400 Bad Request", json.dumps({'error': str(e)}).encode('utf-8'))
return True
headers = getattr(req, 'headers', {})
content_type = headers.get(b'content-type', b'').decode('utf-8', errors='ignore').lower()
# If JSON payload with data_b64
if 'json' in content_type:
try:
obj = json.loads(payload.decode('utf-8'))
blob_bytes = base64.b64decode(obj['data_b64'])
except Exception as e:
ws.http_response(conn, "400 Bad Request", json.dumps({'error': str(e)}).encode('utf-8'))
return True
else:
blob_bytes = payload
blob_hash, tree_root, size = self.store.store_blob(blob_bytes)
if self.blob_store is not None and hasattr(self.blob_store, 'put'):
try:
self.blob_store.put(blob_bytes)
except Exception:
pass
self._messages_in += 1
resp_data = {
'hash': blob_hash,
'tree_root': tree_root,
'size': size,
}
ws.http_response(conn, "201 Created", json.dumps(resp_data).encode('utf-8'), "application/json")
return True
return False
# 3. GET /rhio/sync
def _handle_sync(conn: Any, req: Any) -> bool:
body = json.dumps({
'network_id': self.config.get('network_id', 'rhio-default-network-1'),
'synced': True,
'blobs_stored': self.store.count_blobs(),
}).encode('utf-8')
ws.http_response(conn, "200 OK", body, "application/json")
return True
host.mount_route('/rhio/status', _handle_status, methods=('GET',), owner=self.name)
host.mount_route('/rhio/blobs', _handle_blobs, methods=('GET', 'POST'), owner=self.name)
host.mount_route('/rhio/sync', _handle_sync, methods=('GET',), owner=self.name)
[docs] def transport_rung(self, context: Any) -> Optional[Any]:
if not self._enabled:
return None
return self.rung
[docs] def compartments(self) -> tuple[type[Any], ...]:
return ()
[docs] def start(self) -> None:
if not self._enabled:
return
if self.auto_start_sidecar and self._daemon is None and self.node_dir:
from fleetmesh_node.sidecar.fleetmesh_rhio import RhioSidecarClient, RhioSidecarDaemon
self._daemon = RhioSidecarDaemon(
node_dir=self.node_dir,
uds_path=self.socket_path,
network_id=str(self.config.get('network_id', 'rhio-default-network-1')),
nats_endpoint=str(self.config.get('nats_endpoint', 'localhost:4222')),
s3_endpoint=str(self.config.get('s3_endpoint', 'http://localhost:9000')),
)
self._daemon.start()
client = RhioSidecarClient(self._daemon.uds_path)
client.connect()
self.rung._client = client
self._running = True
[docs] def stop(self) -> None:
self._running = False
if self._daemon is not None:
if self.rung._client is not None:
try:
self.rung._client.close()
except Exception:
pass
self.rung._client = None
self._daemon.stop()
self._daemon = None
if self.store is not None:
self.store.close()
[docs] def health(self) -> ProtocolHealth:
if not self._enabled:
return ProtocolHealth(
healthy=True,
status="disabled",
connections_active=0,
messages_in=0,
messages_out=0,
errors=(),
)
return ProtocolHealth(
healthy=True,
status="serving" if self._running else "configured",
connections_active=0,
messages_in=self._messages_in,
messages_out=self._messages_out,
errors=(),
)
[docs] def descriptor_capabilities(self) -> tuple[tuple[str, str], ...]:
if not self._enabled:
return ()
return (('c', 'rhio'), ('c', 'nats-jetstream'), ('c', 'bao'))