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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
use std::collections::BTreeMap;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::Ordering::Relaxed;
use std::sync::Arc;
use delegate::delegate;
use serde::{Deserialize, Serialize};
use smallbytes::SmallBytes;
use crate::{metadata::ToUnixNanos, ChannelBuilder, Encode, PartialMetadata, Schema, SinkId};
mod channel_descriptor;
mod lazy_channel;
mod raw_channel;
pub use channel_descriptor::ChannelDescriptor;
pub use lazy_channel::{LazyChannel, LazyRawChannel};
pub use raw_channel::RawChannel;
/// Stack buffer size to use for encoding messages.
const STACK_BUFFER_SIZE: usize = 256 * 1024;
/// Maximum integer that can be represented safely in a double floating point value (for JavaScript numbers)
const MAX_SAFE_DOUBLE_INTEGER_VALUE: u64 = 1 << 53;
/// Uniquely identifies a channel in the context of this program.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Deserialize, Serialize)]
pub struct ChannelId(u64);
impl ChannelId {
/// Returns a new ChannelId
pub fn new(id: u64) -> Self {
Self(id)
}
/// Allocates the next channel ID.
pub(crate) fn next() -> Self {
static NEXT_ID: AtomicU64 = AtomicU64::new(1);
let id = NEXT_ID.fetch_add(1, Relaxed);
// Panic if we exceed the maximum value we can safely represent in a double floating point number
// because the app in JavaScript currently parses these from JSON as numbers.
// It is assumed that nobody is able to actually trigger this in the real world.
assert!(
id < MAX_SAFE_DOUBLE_INTEGER_VALUE,
"ChannelId overflow, you win the prize!"
);
Self(id)
}
}
impl From<ChannelId> for u64 {
fn from(id: ChannelId) -> u64 {
id.0
}
}
impl From<u64> for ChannelId {
fn from(value: u64) -> Self {
ChannelId::new(value)
}
}
impl std::fmt::Display for ChannelId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
/// A channel for messages that implement [`Encode`].
///
/// Channels are immutable, returned as `Arc<Channel>` and can be shared between threads.
#[derive(Debug)]
pub struct Channel<T: Encode> {
inner: Arc<RawChannel>,
_phantom: std::marker::PhantomData<T>,
}
impl<T: Encode> Channel<T> {
/// Constructs a new typed channel with default settings.
///
/// If you want to override the channel configuration, use [`ChannelBuilder`].
///
/// You should choose a unique topic name per channel for compatibility with the Foxglove app.
pub fn new(topic: impl Into<String>) -> Self {
ChannelBuilder::new(topic).build()
}
/// Constructs a new typed channel from a raw channel.
///
/// This is intended for internal use only.
/// We're trusting the caller that the channel was created with the same type T as being used to call this.
#[doc(hidden)]
pub fn from_raw_channel(raw_channel: Arc<RawChannel>) -> Self {
Self {
inner: raw_channel,
_phantom: std::marker::PhantomData,
}
}
#[doc(hidden)]
pub fn into_inner(self) -> Arc<RawChannel> {
self.inner
}
delegate! { to self.inner {
/// Returns the channel ID.
pub fn id(&self) -> ChannelId;
/// Returns the topic name of the channel.
pub fn topic(&self) -> &str;
/// Returns the channel schema.
pub fn schema(&self) -> Option<&Schema>;
/// Returns the message encoding for this channel.
pub fn message_encoding(&self) -> &str;
/// Returns the metadata for this channel.
pub fn metadata(&self) -> &BTreeMap<String, String>;
/// Returns true if there's at least one sink subscribed to this channel.
pub fn has_sinks(&self) -> bool;
/// Closes the channel, removing it from the context.
///
/// You can use this to explicitly unadvertise the channel to sinks that subscribe to
/// channels dynamically, such as the [`WebSocketServer`][crate::WebSocketServer].
///
/// Attempts to log on a closed channel will elicit a throttled warning message.
pub fn close(&self);
} }
/// Encodes the message and logs it on the channel.
///
/// The buffering behavior depends on the log sink; see [`McapWriter`][crate::McapWriter] and
/// [`WebSocketServer`][crate::WebSocketServer] for details.
pub fn log(&self, msg: &T) {
self.log_with_meta(msg, PartialMetadata::default());
}
/// Encodes the message and logs it to a specific sink.
///
/// If a sink ID is provided, only that sink will receive the message.
/// Otherwise, the message will be sent to all subscribed sinks.
///
/// The buffering behavior depends on the log sink; see [`McapWriter`][crate::McapWriter] and
/// [`WebSocketServer`][crate::WebSocketServer] for details.
pub fn log_to_sink(&self, msg: &T, sink_id: Option<SinkId>) {
self.log_with_meta_to_sink(msg, PartialMetadata::default(), sink_id);
}
/// Encodes the message and logs it on the channel with additional metadata.
///
/// The buffering behavior depends on the log sink; see [`McapWriter`][crate::McapWriter] and
/// [`WebSocketServer`][crate::WebSocketServer] for details.
pub fn log_with_meta(&self, msg: &T, metadata: PartialMetadata) {
self.log_with_meta_to_sink(msg, metadata, None);
}
/// Encodes the message and logs it on the channel with additional metadata to a specific sink.
///
/// If a sink ID is provided, only that sink will receive the message.
/// Otherwise, the message will be sent to all subscribed sinks.
///
/// The buffering behavior depends on the log sink; see [`McapWriter`][crate::McapWriter] and
/// [`WebSocketServer`][crate::WebSocketServer] for details.
pub fn log_with_meta_to_sink(
&self,
msg: &T,
metadata: PartialMetadata,
sink_id: Option<SinkId>,
) {
if self.has_sinks() {
self.log_to_sinks(msg, metadata, sink_id);
} else {
self.inner.log_warn_if_closed();
}
}
/// Encodes the message and logs it on the channel with the given `timestamp`.
/// `timestamp` can be a u64 (nanoseconds since epoch), a foxglove [`Timestamp`][crate::schemas::Timestamp],
/// a [`SystemTime`][std::time::SystemTime], or anything else that implements [`ToUnixNanos`][crate::ToUnixNanos].
///
/// The buffering behavior depends on the log sink; see [`McapWriter`][crate::McapWriter] and
/// [`WebSocketServer`][crate::WebSocketServer] for details.
pub fn log_with_time(&self, msg: &T, timestamp: impl ToUnixNanos) {
self.log_with_meta(msg, PartialMetadata::with_log_time(timestamp))
}
fn log_to_sinks(&self, msg: &T, metadata: PartialMetadata, sink_id: Option<SinkId>) {
// Try to avoid heap allocation by using a stack buffer.
let mut buf: SmallBytes<STACK_BUFFER_SIZE> = SmallBytes::new();
if let Some(estimated_size) = msg.encoded_len() {
buf.reserve(estimated_size);
}
msg.encode(&mut buf).unwrap();
self.inner.log_to_sinks(&buf, metadata, sink_id);
}
}
#[cfg(test)]
mod test {
use crate::channel_builder::ChannelBuilder;
use crate::log_sink_set::ERROR_LOGGING_MESSAGE;
use crate::testutil::RecordingSink;
use crate::{Context, FoxgloveError, RawChannel, Schema, Sink};
use std::sync::Arc;
use tracing_test::traced_test;
fn new_test_channel(ctx: &Arc<Context>) -> Result<Arc<RawChannel>, FoxgloveError> {
ChannelBuilder::new("/topic")
.context(ctx)
.message_encoding("message_encoding")
.schema(Schema::new(
"name",
"encoding",
br#"{
"type": "object",
"properties": {
"msg": {"type": "string"},
"count": {"type": "number"},
},
}"#,
))
.metadata(maplit::btreemap! {"key".to_string() => "value".to_string()})
.build_raw()
}
#[test]
fn test_channel_new() {
let ctx = Context::new();
let topic = "topic";
let message_encoding = "message_encoding";
let schema = Schema::new("schema_name", "schema_encoding", &[1, 2, 3]);
let metadata = maplit::btreemap! {"key".to_string() => "value".to_string()};
let channel = ChannelBuilder::new(topic)
.message_encoding(message_encoding)
.schema(schema.clone())
.metadata(metadata.clone())
.context(&ctx)
.build_raw()
.expect("Failed to create channel");
assert!(u64::from(channel.id()) > 0);
assert_eq!(channel.topic(), topic);
assert_eq!(channel.message_encoding(), message_encoding);
assert_eq!(channel.schema(), Some(&schema));
assert_eq!(channel.metadata(), &metadata);
assert_eq!(ctx.get_channel_by_topic(topic), Some(channel));
}
#[traced_test]
#[test]
fn test_channel_log_msg() {
let ctx = Context::new();
let channel = new_test_channel(&ctx).unwrap();
let msg = vec![1, 2, 3];
channel.log(&msg);
assert!(!logs_contain(ERROR_LOGGING_MESSAGE));
}
#[traced_test]
#[test]
fn test_log_msg_success() {
let ctx = Context::new();
let recording_sink = Arc::new(RecordingSink::new());
assert!(ctx.add_sink(recording_sink.clone()));
let channel = new_test_channel(&ctx).unwrap();
let msg = b"test_message";
channel.log(msg);
assert!(!logs_contain(ERROR_LOGGING_MESSAGE));
let messages = recording_sink.take_messages();
assert_eq!(messages.len(), 1);
assert_eq!(messages[0].channel_id, channel.id());
assert_eq!(messages[0].msg, msg.to_vec());
assert!(messages[0].metadata.log_time > 1732847588055322395);
}
#[traced_test]
#[test]
fn test_channel_close() {
let ctx = Context::new();
let ch = new_test_channel(&ctx).unwrap();
ch.log(b"");
assert!(!logs_contain("Cannot log on closed channel for /topic"));
// Explicitly close the channel.
ch.close();
ch.log(b"");
assert!(logs_contain("Cannot log on closed channel for /topic"));
}
#[traced_test]
#[test]
fn test_channel_closed_by_context() {
let ctx = Context::new();
let ch = new_test_channel(&ctx).unwrap();
ch.log(b"");
assert!(!logs_contain("Cannot log on closed channel for /topic"));
// Drop the context, which effectively closes the channel.
drop(ctx);
ch.log(b"");
assert!(logs_contain("Cannot log on closed channel for /topic"));
}
#[traced_test]
#[test]
fn test_log_to_specific_sink() {
let ctx = Context::new();
// Create multiple recording sinks
let sink1 = Arc::new(RecordingSink::new());
let sink2 = Arc::new(RecordingSink::new());
let sink3 = Arc::new(RecordingSink::new());
// Add all sinks to context
assert!(ctx.add_sink(sink1.clone()));
assert!(ctx.add_sink(sink2.clone()));
assert!(ctx.add_sink(sink3.clone()));
// Create a raw channel
let channel = ChannelBuilder::new("/test_topic")
.context(&ctx)
.message_encoding("raw")
.build_raw()
.expect("Failed to create channel");
// Log a message to all sinks (default behavior)
let msg_all = b"message for all sinks";
channel.log(msg_all);
// Log a message to only sink2
let msg_sink2_only = b"message for sink2 only";
channel.log_to_sink(msg_sink2_only, Some(sink2.id()));
// Log a message to only sink3
let msg_sink3_only = b"message for sink3 only";
channel.log_to_sink(msg_sink3_only, Some(sink3.id()));
// Verify messages received by each sink
let sink1_messages = sink1.take_messages();
let sink2_messages = sink2.take_messages();
let sink3_messages = sink3.take_messages();
// Sink1 should only have received the "all sinks" message
assert_eq!(sink1_messages.len(), 1);
assert_eq!(sink1_messages[0].msg, msg_all.to_vec());
// Sink2 should have received the "all sinks" message and the sink2-specific message
assert_eq!(sink2_messages.len(), 2);
assert_eq!(sink2_messages[0].msg, msg_all.to_vec());
assert_eq!(sink2_messages[1].msg, msg_sink2_only.to_vec());
// Sink3 should have received the "all sinks" message and the sink3-specific message
assert_eq!(sink3_messages.len(), 2);
assert_eq!(sink3_messages[0].msg, msg_all.to_vec());
assert_eq!(sink3_messages[1].msg, msg_sink3_only.to_vec());
// Test logging to a non-existent sink ID (should not cause errors)
let non_existent_id = crate::SinkId::next();
channel.log_to_sink(b"message to nowhere", Some(non_existent_id));
// Verify no additional messages were received
assert_eq!(sink1.take_messages().len(), 0);
assert_eq!(sink2.take_messages().len(), 0);
assert_eq!(sink3.take_messages().len(), 0);
assert!(!logs_contain(ERROR_LOGGING_MESSAGE));
}
}