WIP on simple disk serve

This commit is contained in:
Dominik Werder
2021-03-31 22:17:54 +02:00
commit 6dbc7cb605
14 changed files with 359 additions and 0 deletions

15
disk/Cargo.toml Normal file
View File

@@ -0,0 +1,15 @@
[package]
name = "disk"
version = "0.0.1-a.0"
authors = ["Dominik Werder <dominik.werder@gmail.com>"]
edition = "2018"
[dependencies]
tokio = { version = "1.4.0", features = ["rt-multi-thread", "io-util", "net", "time", "sync", "fs"] }
tracing = "0.1.25"
serde_json = "1.0"
async-channel = "1.6"
bytes = "1.0.1"
futures-core = "0.3.12"
err = { path = "../err" }
netpod = { path = "../netpod" }

45
disk/src/lib.rs Normal file
View File

@@ -0,0 +1,45 @@
use err::Error;
use std::task::{Context, Poll};
use std::pin::Pin;
use tokio::io::AsyncRead;
//use std::future::Future;
//use futures_core::Stream;
pub async fn read_test_1() -> Result<netpod::BodyStream, Error> {
let path = "/data/sf-databuffer/daq_swissfel/daq_swissfel_3/byTime/S10CB01-RIQM-DCP10:FOR-AMPLT/0000000000000018714/0000000012/0000000000086400000_00000_Data";
let fin = tokio::fs::OpenOptions::new()
.read(true)
.open(path)
.await?;
let stream = netpod::BodyStream {
inner: Box::new(FileReader { file: fin }),
};
Ok(stream)
}
struct FileReader {
file: tokio::fs::File,
}
impl futures_core::Stream for FileReader {
type Item = Result<bytes::Bytes, Error>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let mut buf2 = bytes::BytesMut::with_capacity(13);
if buf2.as_mut().len() != 13 {
panic!("todo prepare slice");
}
let mut buf = tokio::io::ReadBuf::new(buf2.as_mut());
let g = Pin::new(&mut self.file).poll_read(cx, &mut buf);
match g {
Poll::Ready(Ok(_)) => {
Poll::Ready(Some(Ok(buf2.freeze())))
}
Poll::Ready(Err(e)) => {
Poll::Ready(Some(Err(Error::from(e))))
}
Poll::Pending => Poll::Pending
}
}
}