1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//! Type-safe and raw publish/subscribe methods.
#![cfg(feature = "messaging")]
use super::{Context, Receiver};
use crate::error::{Mecha10Error, Result};
use crate::messages::Message;
use crate::topics::Topic;
use anyhow::Context as _;
use serde::{de::DeserializeOwned, Serialize};
impl Context {
/// Subscribe to a topic with type-safe message handling
///
/// # Arguments
///
/// * `topic` - Type-safe topic to subscribe to
///
/// # Returns
///
/// A receiver that will yield messages of type `T`
///
/// # Example
///
/// ```rust
/// use mecha10_core::prelude::*;
/// use mecha10_core::topics::sensor;
///
/// # async fn example(ctx: &Context) -> Result<()> {
/// // Type is inferred as Receiver<Image>
/// let mut images = ctx.subscribe(sensor::CAMERA_RGB).await?;
///
/// while let Some(image) = images.recv().await {
/// println!("Image: {}x{}", image.width, image.height);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn subscribe<T>(&self, topic: Topic<T>) -> Result<Receiver<T>>
where
T: Message + DeserializeOwned + Send + 'static,
{
let path = topic.path();
self.subscribe_raw(path).await
}
/// Publish a message to a specific topic
///
/// # Arguments
///
/// * `topic` - Type-safe topic to publish to
/// * `message` - Message to publish (type checked against topic)
///
/// # Example
///
/// ```rust
/// use mecha10_core::prelude::*;
/// use mecha10_core::topics::sensor;
/// use mecha10_core::messages::Image;
///
/// # async fn example(ctx: &Context) -> Result<()> {
/// let image = Image {
/// timestamp: now_micros(),
/// width: 640,
/// height: 480,
/// encoding: "rgb8".to_string(),
/// data: vec![],
/// frame_id: None,
/// };
///
/// // Type checked: image must be of type Image
/// ctx.publish_to(sensor::CAMERA_RGB, &image).await?;
/// # Ok(())
/// # }
/// ```
pub async fn publish_to<T>(&self, topic: Topic<T>, message: &T) -> Result<()>
where
T: Message + Serialize,
{
self.publish_raw(topic.path(), message).await
}
/// Publish a message to a dynamic topic path (for runtime-generated topics like camera streams)
///
/// This is useful when the topic path is determined at runtime and cannot be a `&'static str`.
///
/// # Arguments
/// * `topic_path` - The topic path as a String (e.g., "robot/sensors/camera/front/compressed")
/// * `message` - The message to publish
pub async fn publish_to_path<T>(&self, topic_path: &str, message: &T) -> Result<()>
where
T: Message + Serialize,
{
self.publish_raw(topic_path, message).await
}
/// Publish a message to an instance-scoped topic
///
/// This automatically appends the context's instance name to the topic path.
/// If no instance is set, this behaves like `publish_to()`.
///
/// # Arguments
///
/// * `topic` - Type-safe topic to publish to
/// * `message` - Message to publish (type checked against topic)
///
/// # Example
///
/// ```rust
/// use mecha10_core::prelude::*;
/// use mecha10_core::topics::sensor;
/// use mecha10_core::messages::Image;
///
/// # async fn example() -> std::result::Result<(), Box<dyn std::error::Error>> {
/// let ctx = Context::new("camera_fake").await?.with_instance("left");
///
/// let image = Image {
/// timestamp: now_micros(),
/// width: 640,
/// height: 480,
/// encoding: "rgb8".to_string(),
/// data: vec![],
/// frame_id: None,
/// };
///
/// // Publishes to "/sensor/camera/rgb/left"
/// ctx.publish_to_scoped(sensor::CAMERA_RGB, &image).await?;
/// # Ok(())
/// # }
/// ```
pub async fn publish_to_scoped<T>(&self, topic: Topic<T>, message: &T) -> Result<()>
where
T: Message + Serialize,
{
let scoped_path = if let Some(instance) = &self.instance {
format!("{}/{}", topic.path(), instance)
} else {
topic.path().to_string()
};
self.publish_raw(&scoped_path, message).await
}
/// Publish a message to a topic with an explicit instance suffix
///
/// This allows publishing to a specific instance's topic regardless of
/// the context's own instance name.
///
/// # Arguments
///
/// * `topic` - Type-safe topic to publish to
/// * `instance` - Instance name to append to topic path
/// * `message` - Message to publish (type checked against topic)
///
/// # Example
///
/// ```rust
/// use mecha10_core::prelude::*;
/// use mecha10_core::topics::actuator;
///
/// # async fn example(ctx: &Context) -> Result<()> {
/// // Velocity + torque PWM values for this motor
/// let cmd = vec![1.0, 0.0];
///
/// // Send to specific motor instances
/// ctx.publish_to_instance(actuator::MOTOR_CMD, "front_left", &cmd).await?;
/// ctx.publish_to_instance(actuator::MOTOR_CMD, "front_right", &cmd).await?;
/// # Ok(())
/// # }
/// ```
pub async fn publish_to_instance<T>(&self, topic: Topic<T>, instance: &str, message: &T) -> Result<()>
where
T: Message + Serialize,
{
let instance_path = format!("{}/{}", topic.path(), instance);
self.publish_raw(&instance_path, message).await
}
/// Subscribe to an instance-scoped topic
///
/// This automatically appends the context's instance name to the topic path.
/// If no instance is set, this behaves like `subscribe()`.
///
/// # Example
///
/// ```rust
/// use mecha10_core::prelude::*;
/// use mecha10_core::topics::sensor;
///
/// # async fn example() -> std::result::Result<(), Box<dyn std::error::Error>> {
/// let ctx = Context::new("vision_node").await?;
///
/// // Subscribe to left camera (topic: "/sensor/camera/rgb/left")
/// let mut left_images = ctx.subscribe_scoped(sensor::CAMERA_RGB, "left").await?;
///
/// // Subscribe to right camera (topic: "/sensor/camera/rgb/right")
/// let mut right_images = ctx.subscribe_scoped(sensor::CAMERA_RGB, "right").await?;
/// # Ok(())
/// # }
/// ```
pub async fn subscribe_scoped<T>(&self, topic: Topic<T>, instance: &str) -> Result<Receiver<T>>
where
T: Message + DeserializeOwned + Send + 'static,
{
let scoped_path = format!("{}/{}", topic.path(), instance);
self.subscribe_raw(&scoped_path).await
}
/// Subscribe to a topic using a raw string path (for RPC and advanced use cases)
///
/// # Arguments
///
/// * `topic_path` - Raw topic path string
///
/// # Returns
///
/// A receiver that will yield messages of type `T`
///
/// # Note
///
/// This is a low-level method. Prefer using `subscribe()` with type-safe topics
/// for normal use cases. This method is primarily for RPC and dynamic topic handling.
pub async fn subscribe_raw<T>(&self, topic_path: &str) -> Result<Receiver<T>>
where
T: Message + DeserializeOwned + Send + 'static,
{
let mut bus = self.message_bus.write().await;
let consumer_group = format!("{}-{}", self.node_id, topic_path.replace('/', "-"));
let subscriber = bus
.subscribe::<T>(topic_path, &consumer_group)
.await
.with_context(|| {
format!(
"Failed to subscribe to raw topic '{}' with consumer group '{}' from node '{}'",
topic_path, consumer_group, self.node_id
)
})
.map_err(|e| Mecha10Error::MessagingError {
message: format!("{:#}", e),
suggestion: format!("Check that Redis is running and topic '{}' is valid", topic_path),
})?;
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
tokio::spawn(async move {
let mut sub = subscriber;
while let Some(msg) = sub.recv().await {
if let Err(e) = msg.ack().await {
tracing::warn!(
topic = %msg.topic,
message_id = %msg.id,
error = %e,
"Failed to acknowledge message (will be redelivered on reconnect)"
);
}
if tx.send(msg.payload).is_err() {
break;
}
}
});
Ok(Receiver::from(rx))
}
/// Publish a message to a topic using a raw string path (for RPC and advanced use cases)
///
/// # Arguments
///
/// * `topic_path` - Raw topic path string
/// * `message` - Message to publish
///
/// # Note
///
/// This is a low-level method. Prefer using `publish_to()` with type-safe topics
/// for normal use cases. This method is primarily for RPC and dynamic topic handling.
pub async fn publish_raw<T>(&self, topic_path: &str, message: &T) -> Result<()>
where
T: Message + Serialize,
{
let mut bus = self.message_bus.write().await;
bus.publish(topic_path, message)
.await
.with_context(|| {
format!(
"Failed to publish to raw topic '{}' from node '{}'",
topic_path, self.node_id
)
})
.map_err(|e| Mecha10Error::MessagingError {
message: format!("{:#}", e),
suggestion: "Check that Redis is running and accepting connections".to_string(),
})?;
Ok(())
}
}