Skip to content

Commit

Permalink
Refine blockwise stream initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
j-devel committed Feb 13, 2024
1 parent 8c83515 commit e5438f2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
8 changes: 5 additions & 3 deletions examples/xbd-net/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,11 @@ async fn xbd_main() {
//panic!("ok"); // !!!!

// WIP multiple-static blockwise streams !!
let mut bs = Xbd::async_gcoap_get_blockwise(addr_self, "/const/song.txt"); // FIXME AlreadyInit
let mut bs = Xbd::async_gcoap_get_blockwise(addr_self, "/const/song.txt");
while let Some(req) = bs.next().await {
panic!("{:?}", req);
let out = req.await;
println!("@@ out: {:?}", out);
panic!("!!");
}
}
}
Expand All @@ -190,4 +192,4 @@ async fn xbd_main() {
let out = Xbd::async_gcoap_get(req_external_native.0, req_external_native.1).await;
println!("@@ out: {:?}", out);
}
}
}
6 changes: 4 additions & 2 deletions examples/xbd-net/src/xbd/blockwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ pub fn add_blockwise_req(req: ReqInner) {
pub struct BlockwiseStream(XbdStream<ReqInner>);

impl BlockwiseStream {
pub fn new() -> Self {
Self(XbdStream::new(&BLOCKWISE_QUEUE, &BLOCKWISE_WAKER))
pub fn get() -> Self {
XbdStream::get(&BLOCKWISE_QUEUE, &BLOCKWISE_WAKER)
.map_or_else(|| Self(XbdStream::new(&BLOCKWISE_QUEUE, &BLOCKWISE_WAKER)),
|xs| Self(xs))
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/xbd-net/src/xbd/gcoap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl ReqInner {
}

pub fn new_blockwise(method: CoapMethod, addr: &str, uri: &str, payload: Option<Vec<u8>>) -> BlockwiseStream {
let bs = BlockwiseStream::new();
let bs = BlockwiseStream::get();
Self::add_blockwise(method, addr, uri, payload);

bs
Expand Down
8 changes: 8 additions & 0 deletions examples/xbd-net/src/xbd/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ impl<T> XbdStream<T> {
XbdStream { queue, waker }
}

pub fn get(queue: &'static OnceCell<ArrayQueue<T>>, waker: &'static AtomicWaker) -> Option<Self> {
if queue.get().is_some() { // already init_once
Some(XbdStream { queue, waker })
} else {
None
}
}

// must not block/alloc/dealloc
pub fn add(queue: &'static OnceCell<ArrayQueue<T>>, waker: &'static AtomicWaker, item: T) {
if let Ok(queue) = queue.try_get() {
Expand Down

0 comments on commit e5438f2

Please sign in to comment.