ctb_workspace_ipc/services/parent/
channel.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! Channel-based implementations for the parent service.
21//!
22//! These are small adapters for workspace-style event loops that prefer to
23//! receive child-to-parent messages over tokio channels.
24
25#[allow(
26    unused_imports,
27    clippy::wildcard_imports,
28    reason = "Standard workspace module prelude"
29)]
30use crate::utilities::*;
31
32use tokio::sync::mpsc;
33
34use crate::error::Error;
35use crate::services::parent::api::{
36    MessageToParentResponse, ParentMessage, ParentMessenger,
37    ParentRequestContext, ProxyCallRequest, ProxyCallResponse,
38    SpawnChildRequest, SpawnChildResponse,
39};
40
41/// A message event delivered to a workspace loop, including sender context.
42#[derive(Debug)]
43pub struct ParentMessageEvent {
44    pub ctx: ParentRequestContext,
45    pub message: ParentMessage,
46}
47
48/// A spawn request with a response channel for synchronous spawning.
49#[derive(Debug)]
50pub struct SpawnRequestWithResponse {
51    pub ctx: ParentRequestContext,
52    pub request: SpawnChildRequest,
53    pub response_tx: tokio::sync::oneshot::Sender<SpawnChildResponse>,
54}
55
56/// A proxy-call request with a response channel.
57#[derive(Debug)]
58pub struct ProxyCallWithResponse {
59    pub ctx: ParentRequestContext,
60    pub request: ProxyCallRequest,
61    pub response_tx: tokio::sync::oneshot::Sender<ProxyCallResponse>,
62}
63
64/// Parent messenger implementation that forwards messages to a workspace loop.
65#[derive(Clone)]
66pub struct ChannelParentMessenger {
67    messages_tx: mpsc::Sender<ParentMessageEvent>,
68    spawn_requests_tx: mpsc::Sender<SpawnRequestWithResponse>,
69    proxy_calls_tx: mpsc::Sender<ProxyCallWithResponse>,
70}
71
72impl std::fmt::Debug for ChannelParentMessenger {
73    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
74        f.debug_struct("ChannelParentMessenger").finish()
75    }
76}
77
78impl ChannelParentMessenger {
79    /// Create a new parent messenger with the given channels.
80    pub fn new(
81        messages_tx: mpsc::Sender<ParentMessageEvent>,
82        spawn_requests_tx: mpsc::Sender<SpawnRequestWithResponse>,
83        proxy_calls_tx: mpsc::Sender<ProxyCallWithResponse>,
84    ) -> Self {
85        Self {
86            messages_tx,
87            spawn_requests_tx,
88            proxy_calls_tx,
89        }
90    }
91}
92
93#[async_trait::async_trait]
94impl ParentMessenger for ChannelParentMessenger {
95    async fn send_message(
96        &self,
97        ctx: ParentRequestContext,
98        message: ParentMessage,
99    ) -> Result<MessageToParentResponse, Error> {
100        self.messages_tx
101            .send(ParentMessageEvent { ctx, message })
102            .await
103            .map_err(|e| {
104                Error::Internal(format!("channel send failed: {e}"))
105            })?;
106        Ok(MessageToParentResponse {
107            accepted: true,
108            response: None,
109        })
110    }
111
112    async fn request_spawn_child(
113        &self,
114        ctx: ParentRequestContext,
115        request: SpawnChildRequest,
116    ) -> Result<SpawnChildResponse, Error> {
117        let (response_tx, response_rx) = tokio::sync::oneshot::channel();
118        self.spawn_requests_tx
119            .send(SpawnRequestWithResponse {
120                ctx,
121                request,
122                response_tx,
123            })
124            .await
125            .map_err(|e| {
126                Error::Internal(format!("channel send failed: {e}"))
127            })?;
128
129        response_rx.await.map_err(|e| {
130            Error::Internal(format!("spawn response channel closed: {e}"))
131        })
132    }
133
134    async fn proxy_call(
135        &self,
136        ctx: ParentRequestContext,
137        request: ProxyCallRequest,
138    ) -> Result<ProxyCallResponse, Error> {
139        let (response_tx, response_rx) = tokio::sync::oneshot::channel();
140        self.proxy_calls_tx
141            .send(ProxyCallWithResponse {
142                ctx,
143                request,
144                response_tx,
145            })
146            .await
147            .map_err(|e| {
148                Error::Internal(format!("channel send failed: {e}"))
149            })?;
150
151        response_rx.await.map_err(|e| {
152            Error::Internal(format!("proxy response channel closed: {e}"))
153        })
154    }
155}