This commit is contained in:
Donald Knuth 2022-02-06 19:54:36 -05:00
parent 1c27531b5f
commit c6b45dea57
7 changed files with 215 additions and 39 deletions

141
Cargo.lock generated
View file

@ -2,6 +2,17 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "autocfg"
version = "1.0.1"
@ -38,6 +49,36 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "3.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b63edc3f163b3c71ec8aa23f9bd6070f77edbf3d1d198b164afa90ff00e4ec62"
dependencies = [
"atty",
"bitflags",
"clap_derive",
"indexmap",
"lazy_static",
"os_str_bytes",
"strsim",
"termcolor",
"textwrap",
]
[[package]]
name = "clap_derive"
version = "3.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a1132dc3944b31c20dd8b906b3a9f0a5d0243e092d59171414969657ac6aa85"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "educe"
version = "0.4.18"
@ -153,6 +194,18 @@ dependencies = [
"slab",
]
[[package]]
name = "hashbrown"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
[[package]]
name = "heck"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
[[package]]
name = "hermit-abi"
version = "0.1.19"
@ -162,6 +215,16 @@ dependencies = [
"libc",
]
[[package]]
name = "indexmap"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282a6247722caba404c065016bbfa522806e51714c34f5dfc3e4a3a46fcb4223"
dependencies = [
"autocfg",
"hashbrown",
]
[[package]]
name = "instant"
version = "0.1.12"
@ -171,6 +234,12 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.117"
@ -278,6 +347,15 @@ version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5"
[[package]]
name = "os_str_bytes"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64"
dependencies = [
"memchr",
]
[[package]]
name = "parking_lot"
version = "0.11.2"
@ -335,6 +413,30 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.36"
@ -363,11 +465,12 @@ dependencies = [
]
[[package]]
name = "ruck-01"
name = "ruck"
version = "0.1.0"
dependencies = [
"bincode",
"bytes",
"clap",
"futures",
"serde",
"tokio",
@ -423,6 +526,12 @@ version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83"
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "1.0.86"
@ -434,6 +543,21 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "termcolor"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
dependencies = [
"winapi-util",
]
[[package]]
name = "textwrap"
version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80"
[[package]]
name = "tokio"
version = "1.16.1"
@ -512,6 +636,12 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "winapi"
version = "0.3.9"
@ -528,6 +658,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"

View file

@ -1,22 +1,15 @@
[package]
name = "ruck-01"
name = "ruck"
version = "0.1.0"
edition = "2021"
[dependencies]
bytes = { version = "1", features = ["serde"] }
bincode = {version = "1.3.3" }
clap = { version = "3.0.14", features = ["derive"] }
futures = { version = "0.3.0", features = ["thread-pool"]}
serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1.16.1", features = ["full"] }
tokio-serde = { version = "~0.8", features = ["bincode"] }
tokio-stream = { version = "~0.1.6", features = ["net"]}
tokio-util = { version = "0.6.3", features = ["full"]}
[[bin]]
name = "server"
path = "src/server.rs"
[[bin]]
name = "client"
path = "src/client.rs"
tokio-util = { version = "0.6.3", features = ["full"]}

26
src/cli.rs Normal file
View file

@ -0,0 +1,26 @@
use std::path::PathBuf;
use clap::{AppSettings, Parser, Subcommand};
/// A fictional versioning CLI
#[derive(Parser)]
#[clap(name = "ruck")]
#[clap(about = "Croc in rust", long_about = None)]
pub struct Cli {
#[clap(subcommand)]
pub command: Commands,
}
#[derive(Subcommand)]
pub enum Commands {
#[clap(setting(AppSettings::ArgRequiredElseHelp))]
Send {
#[clap(required = true, parse(from_os_str))]
paths: Vec<PathBuf>,
},
#[clap(setting(AppSettings::ArgRequiredElseHelp))]
Receive {
password: String,
},
Relay {},
}

View file

@ -1,27 +1,28 @@
mod message;
use message::Message;
use crate::message::Message;
use bytes::Bytes;
use bytes::{BufMut, BytesMut};
use futures::prelude::*;
use std::path::PathBuf;
use tokio::net::TcpStream;
use tokio_util::codec::{FramedWrite, LengthDelimitedCodec};
#[tokio::main]
pub async fn main() {
// Bind a server socket
let socket = TcpStream::connect("127.0.0.1:8080").await.unwrap();
pub async fn send(paths: &Vec<PathBuf>) -> Result<(), Box<dyn std::error::Error>> {
// Delimit frames using a length header
let socket = TcpStream::connect("127.0.0.1:8080").await.unwrap();
let length_delimited = FramedWrite::new(socket, LengthDelimitedCodec::new());
let m = Message {
body: Bytes::from("hello world"),
};
let mut stream = tokio_serde::SymmetricallyFramed::new(
length_delimited,
tokio_serde::formats::SymmetricalBincode::<Message>::default(),
);
// Send the value
stream.send(m).await.unwrap()
for path in paths.iter() {
let b = path.to_str().unwrap().as_bytes();
let mut buf = BytesMut::with_capacity(1024);
buf.put(&b[..]);
let body = buf.freeze();
let m = Message { body: body };
stream.send(m).await.unwrap();
}
Ok(())
}

28
src/main.rs Normal file
View file

@ -0,0 +1,28 @@
mod cli;
mod client;
mod message;
mod server;
use clap::Parser;
use cli::{Cli, Commands};
use client::send;
use server::serve;
use std::error::Error;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let args = Cli::parse();
match &args.command {
Commands::Send { paths } => {
println!("Sending {:?}", paths);
send(&paths).await?;
}
Commands::Receive { password } => {
println!("Receiving password {}", password);
}
Commands::Relay {} => {
serve().await?;
}
}
Ok(())
}

View file

@ -1,3 +0,0 @@
mod message;
mod server;
pub use message::Message;

View file

@ -1,21 +1,13 @@
mod message;
use message::Message;
use crate::message::Message;
use futures::prelude::*;
use tokio::net::{TcpListener, TcpStream};
use tokio_util::codec::{FramedRead, LengthDelimitedCodec};
use std::env;
use std::error::Error;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let addr = env::args()
.nth(1)
.unwrap_or_else(|| "127.0.0.1:8080".to_string());
pub async fn serve() -> Result<(), Box<dyn std::error::Error>> {
let addr = "127.0.0.1:8080".to_string();
let server = TcpListener::bind(&addr).await?;
println!("Listening on: {}", addr);
loop {
let (stream, _) = server.accept().await?;
tokio::spawn(async move {
@ -27,7 +19,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
}
async fn process(socket: TcpStream) -> Result<(), Box<dyn std::error::Error>> {
pub async fn process(socket: TcpStream) -> Result<(), Box<dyn std::error::Error>> {
let length_delimited = FramedRead::new(socket, LengthDelimitedCodec::new());
let mut stream = tokio_serde::SymmetricallyFramed::new(
length_delimited,