You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.rs 1.9 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. use dora_node_api::{self, DoraNode, Input, Receiver};
  2. #[cxx::bridge]
  3. mod ffi {
  4. struct DoraInput {
  5. end_of_input: bool,
  6. id: String,
  7. data: Vec<u8>,
  8. }
  9. struct DoraResult {
  10. error: String,
  11. }
  12. extern "Rust" {
  13. type Inputs;
  14. type OutputSender<'a>;
  15. fn next_input(inputs: &mut Inputs) -> DoraInput;
  16. fn send_output(output_sender: &mut OutputSender, id: String, data: &[u8]) -> DoraResult;
  17. }
  18. unsafe extern "C++" {
  19. include!("cxx-dataflow-example-node-rust-api/src/main.h");
  20. fn cxx_main(inputs: &mut Inputs, output_sender: &mut OutputSender);
  21. }
  22. }
  23. pub struct Inputs(Receiver<Input>);
  24. fn next_input(inputs: &mut Inputs) -> ffi::DoraInput {
  25. match inputs.0.recv() {
  26. Ok(input) => {
  27. let id = input.id.clone().into();
  28. let data = input.data();
  29. ffi::DoraInput {
  30. end_of_input: false,
  31. id,
  32. data: data.into_owned(),
  33. }
  34. }
  35. Err(_) => ffi::DoraInput {
  36. end_of_input: true,
  37. id: String::new(),
  38. data: Vec::new(),
  39. },
  40. }
  41. }
  42. pub struct OutputSender<'a>(&'a mut DoraNode);
  43. fn send_output(sender: &mut OutputSender, id: String, data: &[u8]) -> ffi::DoraResult {
  44. let result = sender
  45. .0
  46. .send_output(&id.into(), &Default::default(), data.len(), |out| {
  47. out.copy_from_slice(data)
  48. });
  49. let error = match result {
  50. Ok(()) => String::new(),
  51. Err(err) => format!("{err:?}"),
  52. };
  53. ffi::DoraResult { error }
  54. }
  55. fn main() -> eyre::Result<()> {
  56. let mut node = DoraNode::init_from_env()?;
  57. let input_stream = node.inputs()?;
  58. let mut inputs = Inputs(input_stream);
  59. let mut outputs = OutputSender(&mut node);
  60. ffi::cxx_main(&mut inputs, &mut outputs);
  61. Ok(())
  62. }

DORA (Dataflow-Oriented Robotic Architecture) is middleware designed to streamline and simplify the creation of AI-based robotic applications. It offers low latency, composable, and distributed datafl