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.

lib.rs 2.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. use dora_node_api::{self, Event, EventStream};
  2. use eyre::bail;
  3. #[cxx::bridge]
  4. #[allow(clippy::needless_lifetimes)]
  5. mod ffi {
  6. struct DoraNode {
  7. events: Box<Events>,
  8. send_output: Box<OutputSender>,
  9. }
  10. pub enum DoraEventType {
  11. Stop,
  12. Input,
  13. InputClosed,
  14. Error,
  15. Unknown,
  16. AllInputsClosed,
  17. }
  18. struct DoraInput {
  19. id: String,
  20. data: Vec<u8>,
  21. }
  22. struct DoraResult {
  23. error: String,
  24. }
  25. extern "Rust" {
  26. type Events;
  27. type OutputSender;
  28. type DoraEvent<'a>;
  29. fn init_dora_node() -> Result<DoraNode>;
  30. fn next_event(inputs: &mut Box<Events>) -> Box<DoraEvent<'_>>;
  31. fn event_type(event: &Box<DoraEvent>) -> DoraEventType;
  32. fn event_as_input(event: Box<DoraEvent>) -> Result<DoraInput>;
  33. fn send_output(
  34. output_sender: &mut Box<OutputSender>,
  35. id: String,
  36. data: &[u8],
  37. ) -> DoraResult;
  38. }
  39. }
  40. fn init_dora_node() -> eyre::Result<ffi::DoraNode> {
  41. let (node, events) = dora_node_api::DoraNode::init_from_env()?;
  42. let events = Events(events);
  43. let send_output = OutputSender(node);
  44. Ok(ffi::DoraNode {
  45. events: Box::new(events),
  46. send_output: Box::new(send_output),
  47. })
  48. }
  49. pub struct Events(EventStream);
  50. fn next_event(events: &mut Box<Events>) -> Box<DoraEvent> {
  51. Box::new(DoraEvent(events.0.recv()))
  52. }
  53. pub struct DoraEvent<'a>(Option<Event<'a>>);
  54. fn event_type(event: &DoraEvent) -> ffi::DoraEventType {
  55. match &event.0 {
  56. Some(event) => match event {
  57. Event::Stop => ffi::DoraEventType::Stop,
  58. Event::Input { .. } => ffi::DoraEventType::Input,
  59. Event::InputClosed { .. } => ffi::DoraEventType::InputClosed,
  60. Event::Error(_) => ffi::DoraEventType::Error,
  61. _ => ffi::DoraEventType::Unknown,
  62. },
  63. None => ffi::DoraEventType::AllInputsClosed,
  64. }
  65. }
  66. fn event_as_input(event: Box<DoraEvent>) -> eyre::Result<ffi::DoraInput> {
  67. let Some(Event::Input { id, metadata: _, data }) = event.0 else {
  68. bail!("not an input event");
  69. };
  70. Ok(ffi::DoraInput {
  71. id: id.into(),
  72. data: data.map(|d| d.to_owned()).unwrap_or_default(),
  73. })
  74. }
  75. pub struct OutputSender(dora_node_api::DoraNode);
  76. fn send_output(sender: &mut Box<OutputSender>, id: String, data: &[u8]) -> ffi::DoraResult {
  77. let result = sender
  78. .0
  79. .send_output(id.into(), Default::default(), data.len(), |out| {
  80. out.copy_from_slice(data)
  81. });
  82. let error = match result {
  83. Ok(()) => String::new(),
  84. Err(err) => format!("{err:?}"),
  85. };
  86. ffi::DoraResult { error }
  87. }

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