mirror of
https://github.com/n0-computer/iroh.git
synced 2026-12-05 08:01:20 +00:00
feat: add STDIN and STDOUT support (#39)
* feat: add STDIN and STDOUT support also avoid keeping files data in memory, after calculating the outboard data * Update src/server.rs Co-authored-by: Floris Bruynooghe <flub@devork.be> * Update src/server.rs Co-authored-by: Floris Bruynooghe <flub@devork.be> Co-authored-by: Floris Bruynooghe <flub@devork.be>
This commit is contained in:
1 parent
03e1f9377d
commit
c121094de2
6 files changed
+168
-72
No files matched your search
+85
-53
@@ -1,12 +1,13 @@
|
||||
use std::net::SocketAddr;
|
||||
use std::{collections::HashMap, path::Path, sync::Arc};
|
||||
use std::path::PathBuf;
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use anyhow::{anyhow, bail, ensure, Result};
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use s2n_quic::stream::BidirectionalStream;
|
||||
use s2n_quic::Server as QuicServer;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tracing::{debug, error};
|
||||
use tokio::io::AsyncWrite;
|
||||
use tracing::{debug, warn};
|
||||
|
||||
use crate::protocol::{read_lp, write_lp, Handshake, Request, Res, Response, VERSION};
|
||||
use crate::tls::{self, Keypair, PeerId};
|
||||
@@ -71,7 +72,7 @@ impl Server {
|
||||
let db = db.clone();
|
||||
tokio::spawn(async move {
|
||||
if let Err(err) = handle_stream(db, stream).await {
|
||||
error!("error: {:#?}", err);
|
||||
warn!("error: {:#?}", err);
|
||||
}
|
||||
debug!("disconnected");
|
||||
});
|
||||
@@ -114,40 +115,37 @@ async fn handle_stream(
|
||||
let name = bao::Hash::from(request.name);
|
||||
debug!("got request({}): {}", request.id, name.to_hex());
|
||||
|
||||
let (data, piece) = if let Some(data) = db.get(&name) {
|
||||
debug!("found {}", name.to_hex());
|
||||
(
|
||||
Res::Found {
|
||||
size: data.data.len(),
|
||||
outboard: &data.outboard,
|
||||
},
|
||||
Some(data.clone()),
|
||||
)
|
||||
} else {
|
||||
debug!("not found {}", name.to_hex());
|
||||
(Res::NotFound, None)
|
||||
};
|
||||
match db.get(&name) {
|
||||
Some(Data {
|
||||
outboard,
|
||||
path,
|
||||
size,
|
||||
}) => {
|
||||
debug!("found {}", name.to_hex());
|
||||
write_response(
|
||||
&mut writer,
|
||||
&mut out_buffer,
|
||||
request.id,
|
||||
Res::Found {
|
||||
size: *size,
|
||||
outboard,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
let response = Response {
|
||||
id: request.id,
|
||||
data,
|
||||
};
|
||||
|
||||
if out_buffer.len() < 20 + response.data.len() {
|
||||
out_buffer.resize(20 + response.data.len(), 0u8);
|
||||
debug!("writing data");
|
||||
let file = tokio::fs::File::open(&path).await?;
|
||||
let mut reader = tokio::io::BufReader::new(file);
|
||||
tokio::io::copy(&mut reader, &mut writer).await?;
|
||||
}
|
||||
None => {
|
||||
debug!("not found {}", name.to_hex());
|
||||
write_response(&mut writer, &mut out_buffer, request.id, Res::NotFound)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
let used = postcard::to_slice(&response, &mut out_buffer)?;
|
||||
|
||||
write_lp(&mut writer, used).await?;
|
||||
|
||||
debug!("written response of length {}", used.len());
|
||||
|
||||
if let Some(piece) = piece {
|
||||
debug!("writing data {}", piece.data.len());
|
||||
// if we found the data, write it out now
|
||||
writer.write_all(&piece.data).await?;
|
||||
debug!("done writing data");
|
||||
}
|
||||
debug!("finished response");
|
||||
}
|
||||
None => {
|
||||
break;
|
||||
@@ -159,33 +157,67 @@ async fn handle_stream(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Data {
|
||||
/// outboard data from bo
|
||||
/// Outboard data from bao.
|
||||
outboard: Bytes,
|
||||
/// actual data
|
||||
data: Bytes,
|
||||
/// Path to the original data, which must not change while in use.
|
||||
path: PathBuf,
|
||||
/// Size of the original data.
|
||||
size: usize,
|
||||
}
|
||||
|
||||
pub async fn create_db(paths: Vec<&Path>) -> Result<Arc<HashMap<bao::Hash, Data>>> {
|
||||
#[derive(Debug)]
|
||||
pub enum DataSource {
|
||||
File(PathBuf),
|
||||
}
|
||||
|
||||
pub async fn create_db(data_sources: Vec<DataSource>) -> Result<Arc<HashMap<bao::Hash, Data>>> {
|
||||
println!("Available Data:");
|
||||
|
||||
let mut db = HashMap::new();
|
||||
for path in paths {
|
||||
ensure!(path.is_file(), "can only transfer blob data");
|
||||
let data = tokio::fs::read(path).await?;
|
||||
let num = data.len();
|
||||
let (outboard, hash) = bao::encode::outboard(&data);
|
||||
for data in data_sources {
|
||||
match data {
|
||||
DataSource::File(path) => {
|
||||
ensure!(
|
||||
path.is_file(),
|
||||
"can only transfer blob data: {}",
|
||||
path.display()
|
||||
);
|
||||
let data = tokio::fs::read(&path).await?;
|
||||
let (outboard, hash) = bao::encode::outboard(&data);
|
||||
|
||||
println!("- {}: {}bytes", hash.to_hex(), num);
|
||||
db.insert(
|
||||
hash,
|
||||
Data {
|
||||
outboard: Bytes::from(outboard),
|
||||
data: Bytes::from(data),
|
||||
},
|
||||
);
|
||||
println!("- {}: {}bytes", hash.to_hex(), data.len());
|
||||
db.insert(
|
||||
hash,
|
||||
Data {
|
||||
outboard: Bytes::from(outboard),
|
||||
path,
|
||||
size: data.len(),
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Arc::new(db))
|
||||
}
|
||||
|
||||
async fn write_response<W: AsyncWrite + Unpin>(
|
||||
mut writer: W,
|
||||
buffer: &mut BytesMut,
|
||||
id: u64,
|
||||
res: Res<'_>,
|
||||
) -> Result<()> {
|
||||
let response = Response { id, data: res };
|
||||
|
||||
if buffer.len() < 20 + response.data.len() {
|
||||
buffer.resize(20 + response.data.len(), 0u8);
|
||||
}
|
||||
let used = postcard::to_slice(&response, buffer)?;
|
||||
|
||||
write_lp(&mut writer, used).await?;
|
||||
|
||||
debug!("written response of length {}", used.len());
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user