Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ those entries into the versioned section when a release is created.

### Changed

- Split CLI command handling, parameter protocol types, and TUI parameter input handling into focused modules.
- Centralize talos-agent supported ROS message type subscription wiring in a registry for easier message support additions.
- Split the agent server implementation into focused UDS, QUIC, request, graph, and control modules without changing the public server API.
- Cache mdBook tooling in the Docs workflow to reduce CI time.
Expand Down
79 changes: 79 additions & 0 deletions talos-cli/src/commands/echo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use talos_common::protocol::types::DynValue;
use talos_common::session::ProtocolClient;

pub async fn run<C: ProtocolClient>(
client: &mut C,
topic: String,
count: usize,
) -> Result<(), Box<dyn std::error::Error>> {
match client.subscribe(&[topic.clone()]).await {
Ok(subs) if subs.is_empty() => {
eprintln!("warning: agent did not confirm subscription to '{topic}'");
eprintln!("(the agent may not be subscribed to this topic)");
}
Err(e) => {
return Err(format!("failed to subscribe to '{topic}': {e}").into());
}
_ => {}
}

let mut received = 0usize;
loop {
let (recv_topic, frame) = client.recv_data().await?;
if recv_topic == topic {
print_dynvalue(&frame.data, 0);
println!("---");
received += 1;
if count > 0 && received >= count {
break;
}
}
}

if received == 0 {
eprintln!("no data received for topic '{topic}'");
eprintln!("(the agent may not be subscribed to this topic)");
}

Ok(())
}

fn print_dynvalue(value: &DynValue, indent: usize) {
let pad = " ".repeat(indent);
match value {
DynValue::Bool(v) => println!("{pad}{v}"),
DynValue::I8(v) => println!("{pad}{v}"),
DynValue::U8(v) => println!("{pad}{v}"),
DynValue::I16(v) => println!("{pad}{v}"),
DynValue::U16(v) => println!("{pad}{v}"),
DynValue::I32(v) => println!("{pad}{v}"),
DynValue::U32(v) => println!("{pad}{v}"),
DynValue::I64(v) => println!("{pad}{v}"),
DynValue::U64(v) => println!("{pad}{v}"),
DynValue::F32(v) => println!("{pad}{v}"),
DynValue::F64(v) => println!("{pad}{v}"),
DynValue::String(v) => println!("{pad}\"{v}\""),
DynValue::Bytes(v) => println!("{pad}[{} bytes]", v.len()),
DynValue::Array(arr) => {
println!("{pad}[");
for item in arr {
print_dynvalue(item, indent + 1);
}
println!("{pad}]");
}
DynValue::Struct { type_name, fields } => {
println!("{pad}{type_name} {{");
for (name, val) in fields {
print!("{pad} {name}: ");
match val {
DynValue::Struct { .. } | DynValue::Array(_) => {
println!();
print_dynvalue(val, indent + 2);
}
_ => print_dynvalue(val, 0),
}
}
println!("{pad}}}");
}
}
}
4 changes: 4 additions & 0 deletions talos-cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod echo;
pub mod nodes;
pub mod parameters;
pub mod topics;
35 changes: 35 additions & 0 deletions talos-cli/src/commands/nodes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use talos_common::protocol::messages::{Request, Response};
use talos_common::session::ProtocolClient;

pub async fn list<C: ProtocolClient>(client: &mut C) -> Result<(), Box<dyn std::error::Error>> {
let response = client.request(Request::ListNodes).await?;
handle_response(response)
}

fn handle_response(response: Response) -> Result<(), Box<dyn std::error::Error>> {
match response {
Response::NodeList(nodes) => {
println!("{:<30} {:<20}", "NODE", "NAMESPACE");
println!("{}", "-".repeat(52));
for n in &nodes {
println!("{:<30} {:<20}", n.name, n.namespace);
}
println!("\n{} node(s)", nodes.len());
Ok(())
}
Response::Error(e) => Err(e.into()),
_ => Err("unexpected response".into()),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn error_response_returns_error() {
let err = handle_response(Response::Error("missing graph".into()))
.expect_err("node list error response should fail");
assert_eq!(err.to_string(), "missing graph");
}
}
110 changes: 110 additions & 0 deletions talos-cli/src/commands/parameters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
use talos_common::protocol::messages::{Request, Response};
use talos_common::protocol::types::ParamValue;
use talos_common::session::ProtocolClient;

pub async fn list<C: ProtocolClient>(
client: &mut C,
node: String,
) -> Result<(), Box<dyn std::error::Error>> {
let response = client
.request(Request::ListParameters { node: node.clone() })
.await?;
handle_list_params_response(response)
}

pub async fn get<C: ProtocolClient>(
client: &mut C,
node: String,
names: Vec<String>,
) -> Result<(), Box<dyn std::error::Error>> {
let response = client
.request(Request::GetParameters {
node: node.clone(),
names: names.clone(),
})
.await?;
handle_get_param_response(response)
}

pub async fn set<C: ProtocolClient>(
client: &mut C,
node: String,
name: String,
value: String,
) -> Result<(), Box<dyn std::error::Error>> {
let parsed = ParamValue::parse(&value);
let response = client
.request(Request::SetParameter {
node: node.clone(),
name: name.clone(),
value: parsed,
})
.await?;
match response {
Response::ParameterSet {
node,
name,
successful: true,
..
} => {
println!("set {node} {name}");
Ok(())
}
Response::ParameterSet {
name,
successful: false,
reason,
..
} => Err(format!("failed to set '{name}': {reason}").into()),
Response::Error(e) => Err(e.into()),
_ => Err("unexpected response".into()),
}
}

fn handle_list_params_response(response: Response) -> Result<(), Box<dyn std::error::Error>> {
match response {
Response::Parameters { node, parameters } => {
println!("{:<40} {:<14} VALUE", "PARAMETER", "TYPE");
println!("{}", "-".repeat(80));
for p in &parameters {
println!("{:<40} {:<14} {}", p.name, p.value.type_name(), p.value);
}
println!("\n{} parameter(s) on {node}", parameters.len());
Ok(())
}
Response::Error(e) => Err(e.into()),
_ => Err("unexpected response".into()),
}
}

fn handle_get_param_response(response: Response) -> Result<(), Box<dyn std::error::Error>> {
match response {
Response::Parameters { parameters, .. } => {
for p in &parameters {
println!("{}: {} ({})", p.name, p.value, p.value.type_name());
}
Ok(())
}
Response::Error(e) => Err(e.into()),
_ => Err("unexpected response".into()),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn list_params_error_response_returns_error() {
let err = handle_list_params_response(Response::Error("boom".into()))
.expect_err("list params error response should fail");
assert_eq!(err.to_string(), "boom");
}

#[test]
fn get_param_error_response_returns_error() {
let err = handle_get_param_response(Response::Error("missing".into()))
.expect_err("get param error response should fail");
assert_eq!(err.to_string(), "missing");
}
}
38 changes: 38 additions & 0 deletions talos-cli/src/commands/topics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use talos_common::protocol::messages::{Request, Response};
use talos_common::session::ProtocolClient;

pub async fn list<C: ProtocolClient>(client: &mut C) -> Result<(), Box<dyn std::error::Error>> {
let response = client.request(Request::ListTopics).await?;
handle_response(response)
}

fn handle_response(response: Response) -> Result<(), Box<dyn std::error::Error>> {
match response {
Response::TopicList(topics) => {
println!("{:<30} {:<35} {:>4} {:>4}", "TOPIC", "TYPE", "PUB", "SUB");
println!("{}", "-".repeat(75));
for t in &topics {
println!(
"{:<30} {:<35} {:>4} {:>4}",
t.name, t.type_name, t.publisher_count, t.subscriber_count
);
}
println!("\n{} topic(s)", topics.len());
Ok(())
}
Response::Error(e) => Err(e.into()),
_ => Err("unexpected response".into()),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn error_response_returns_error() {
let err = handle_response(Response::Error("boom".into()))
.expect_err("topic list error response should fail");
assert_eq!(err.to_string(), "boom");
}
}
Loading