Overview
TheChannelManager multiplexes multiple Channel implementations into a single unified message stream. It uses sequential polling: channels are polled in registration order, and the first channel with a message wins.
Location: crates/oneclaw-core/src/channel/manager.rs
ChannelManager Struct
- Sequential polling across registered channels
- Each channel is polled in order until a message is found
- If no channels have messages, returns
None(caller should sleep briefly) - Supports sending to channels by index or name
Methods
new()
Creates a new empty channel manager.
ChannelManager with no registered channels
Example:
add_channel()
Registers a channel with the manager.
channel: Box<dyn Channel>- Boxed channel implementation to register
- Channels are polled in registration order
- Logs an info message when channel is registered
- Channel name is obtained via
channel.name()
receive_any()
Polls all channels sequentially and returns the first message found.
Ok(Some((channel_idx, message)))- Message received from channel at indexchannel_idxOk(None)- No channel has a message ready (caller should sleep and retry)Err(OneClawError)- Fatal error (rare; channel errors are logged and skipped)
- Iterate through channels in registration order (index 0, 1, 2, …)
- Call
channel.receive().awaiton each channel - Return first
Ok(Some(msg))found along with channel index - Skip channels that return
Ok(None)orErr() - If all channels return
Noneor error, returnOk(None)
- Individual channel errors are logged (debug level) and skipped
- Allows remaining channels to continue functioning
- Only fatal errors propagate upward
send_to()
Sends a message via a specific channel index.
channel_idx: usize- Index of the channel (0-based, order of registration)msg: &OutgoingMessage- Message to send
Ok(())- Message sent successfullyErr(OneClawError::Channel)- Channel index out of range or send failed
send_by_name()
Sends a message to a channel by name.
name: &str- Name of the channel (e.g., “cli”, “tcp”, “telegram”)msg: &OutgoingMessage- Message to send
Ok(())- Message sent successfullyErr(OneClawError::Channel)- Channel not found or send failed
count()
Returns the number of registered channels.
list()
Returns a list of all registered channel names.
Usage Pattern
Basic Setup
Message Loop
Multi-Channel Coordination
Sequential Polling Behavior
Order matters: Channels registered first have priority. If multiple channels have messages ready, the first one in registration order wins.Error Handling
TheChannelManager is designed to be resilient:
- Individual channel errors: Logged and skipped (other channels continue)
- Channel index out of range: Returns
Err(OneClawError::Channel) - Channel not found by name: Returns
Err(OneClawError::Channel) - Fatal errors: Propagated upward (rare)
See Also
- Channel Trait - Core trait definition
- Built-in Channels - CLI, TCP, Telegram, MQTT