logicaffeine_system/
net.rs1#![cfg(any(all(not(target_arch = "wasm32"), feature = "relay"), target_arch = "wasm32"))]
16
17#[cfg(all(not(target_arch = "wasm32"), feature = "relay"))]
18mod imp {
19 use crate::relay::RelayClient;
20
21 pub struct Net {
23 client: RelayClient,
24 }
25
26 impl Net {
27 pub async fn connect(url: &str) -> Result<Net, String> {
28 Ok(Net { client: RelayClient::connect(url).await? })
29 }
30
31 pub async fn subscribe(&mut self, topic: &str) -> Result<(), String> {
32 self.client.subscribe(topic).await
33 }
34
35 pub fn publish(&self, topic: &str, data: Vec<u8>) -> Result<(), String> {
36 self.client.publish(topic, data)
37 }
38
39 pub fn drain(&mut self) -> Vec<(String, Vec<u8>)> {
41 self.client.drain_events()
42 }
43 }
44}
45
46#[cfg(target_arch = "wasm32")]
47mod imp {
48 use std::cell::RefCell;
49 use std::collections::VecDeque;
50 use std::rc::Rc;
51
52 use crate::relay_browser::RelayBrowserClient;
53
54 pub struct Net {
57 client: RelayBrowserClient,
58 events: Rc<RefCell<VecDeque<(String, Vec<u8>)>>>,
59 }
60
61 impl Net {
62 pub async fn connect(url: &str) -> Result<Net, String> {
63 let (open_tx, open_rx) = futures::channel::oneshot::channel::<()>();
64 let open_tx = Rc::new(RefCell::new(Some(open_tx)));
65 let events = Rc::new(RefCell::new(VecDeque::new()));
66
67 let opener = open_tx.clone();
68 let queue = events.clone();
69 let client = RelayBrowserClient::connect(
70 url,
71 move || {
72 if let Some(tx) = opener.borrow_mut().take() {
73 let _ = tx.send(());
74 }
75 },
76 move |topic, data| queue.borrow_mut().push_back((topic, data)),
77 )
78 .map_err(|e| format!("relay connect failed: {e:?}"))?;
79
80 open_rx.await.map_err(|_| "socket closed before it opened".to_string())?;
81 Ok(Net { client, events })
82 }
83
84 pub async fn subscribe(&mut self, topic: &str) -> Result<(), String> {
85 self.client.subscribe(topic).map_err(|e| format!("subscribe failed: {e:?}"))
88 }
89
90 pub fn publish(&self, topic: &str, data: Vec<u8>) -> Result<(), String> {
91 self.client.publish(topic, data).map_err(|e| format!("publish failed: {e:?}"))
92 }
93
94 pub fn drain(&mut self) -> Vec<(String, Vec<u8>)> {
95 self.events.borrow_mut().drain(..).collect()
96 }
97 }
98}
99
100pub use imp::Net;