From 529e635b44ea7ccb7d085b7e8006645f62a4d3c8 Mon Sep 17 00:00:00 2001 From: Erin Abicht Date: Sun, 2 Apr 2023 17:54:55 -0400 Subject: [PATCH] Basic JSON --- Cargo.lock | 113 ++++++++++++++++++++++++++++++++++++++ forge-server/Cargo.toml | 3 + forge-server/src/main.rs | 116 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 224 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f896cb0..7c16d14 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -37,7 +37,19 @@ dependencies = [ name = "forge-server" version = "0.1.0" dependencies = [ + "serde", + "serde_json", "tokio", + "url", +] + +[[package]] +name = "form_urlencoded" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9c384f161156f5260c24a097c56119f9be8c798586aecc13afbcbe7b7e26bf8" +dependencies = [ + "percent-encoding", ] [[package]] @@ -49,6 +61,22 @@ dependencies = [ "libc", ] +[[package]] +name = "idna" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e14ddfc70884202db2244c223200c204c2bda1bc6e0998d11b5e024d657209e6" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + +[[package]] +name = "itoa" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" + [[package]] name = "libc" version = "0.2.140" @@ -119,6 +147,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "percent-encoding" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" + [[package]] name = "pin-project-lite" version = "0.2.9" @@ -152,12 +186,49 @@ dependencies = [ "bitflags", ] +[[package]] +name = "ryu" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" + [[package]] name = "scopeguard" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "serde" +version = "1.0.159" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c04e8343c3daeec41f58990b9d77068df31209f2af111e059e9fe9646693065" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.159" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c614d17805b093df4b147b51339e7e44bf05ef59fba1e45d83500bcfb4d8585" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d721eca97ac802aa7777b701877c8004d950fc142651367300d21c1cc0194744" +dependencies = [ + "itoa", + "ryu", + "serde", +] + [[package]] name = "signal-hook-registry" version = "1.4.1" @@ -194,6 +265,21 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "tinyvec" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + [[package]] name = "tokio" version = "1.27.0" @@ -224,12 +310,39 @@ dependencies = [ "syn", ] +[[package]] +name = "unicode-bidi" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92888ba5573ff080736b3648696b70cafad7d250551175acbaa4e0385b3e1460" + [[package]] name = "unicode-ident" version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" +[[package]] +name = "unicode-normalization" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "url" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d68c799ae75762b8c3fe375feb6600ef5602c883c5d21eb51c09f22b83c4643" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", + "serde", +] + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/forge-server/Cargo.toml b/forge-server/Cargo.toml index fb89720..899a977 100644 --- a/forge-server/Cargo.toml +++ b/forge-server/Cargo.toml @@ -5,3 +5,6 @@ edition = "2021" [dependencies] tokio = { version = "1.27.0", features = ["full"] } +serde_json = "1.0" +serde = { version = "1.0", features = ["derive"]} +url = { version = "2.3.1", features = ["serde"]} diff --git a/forge-server/src/main.rs b/forge-server/src/main.rs index e26747b..e502df1 100644 --- a/forge-server/src/main.rs +++ b/forge-server/src/main.rs @@ -1,18 +1,118 @@ -use tokio::net::TcpListener; - +use serde::{Deserialize, Serialize}; +use serde_json::Result; +use std::fmt; use std::io; +use std::net::SocketAddr; +use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader}; +use tokio::net::{TcpListener, TcpStream}; +use url::Url; -async fn process_socket(socket: T) { - println!("socketed!"); - // do work with socket here +#[derive(Serialize, Deserialize, Debug)] +enum BuildSystem { + Cargo, + Make, + Custom(String), +} + +impl fmt::Display for BuildSystem { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + BuildSystem::Cargo => write!(f, "cargo"), + BuildSystem::Make => write!(f, "make"), + BuildSystem::Custom(s) => write!(f, "{}", s), + } + } +} + +#[derive(Serialize, Deserialize, Debug)] +enum Subcommand { + Run, + Build, + Install, + Custom(String), +} + +impl fmt::Display for Subcommand { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Subcommand::Run => write!(f, "run"), + Subcommand::Build => write!(f, "build"), + Subcommand::Install => write!(f, "install"), + Subcommand::Custom(s) => write!(f, "{}", s), + } + } +} + +#[derive(Serialize, Deserialize)] +enum Tag { + Release, + Debug, +} + +impl fmt::Display for Tag { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Tag::Release => write!(f, "--release"), + Tag::Debug => write!(f, "--debug"), + } + } +} + +#[derive(Serialize, Deserialize)] +struct Command { + build_system: BuildSystem, + subcommand: Subcommand, + features: Option>, + tag: Tag, + repository: Url, +} + +async fn process_socket(mut socket: TcpStream, address: SocketAddr) { + loop { + let mut reader = BufReader::new(&mut socket); + let mut line = String::new(); + + let bytes_read = match reader.read_line(&mut line).await { + Ok(b) => b, + Err(e) => return, + }; + + let json: Command = serde_json::from_str(&line).unwrap(); + let mut features = String::new(); + match json.features { + Some(f) => { + features.push_str("--features '"); + for i in f { + features.push_str(&i); + features.push_str(" "); + } + features.push_str("'"); + } + None => features = "".to_string(), + } + println!( + "Received message from {:?}: {} {} {} {}", + address, json.build_system, json.subcommand, json.tag, features + ); + socket.write_all(&line.as_bytes()).await.unwrap(); + } + // println!("client {:?} sent message: '{:?}'", address,); } #[tokio::main] async fn main() -> io::Result<()> { - let listener = TcpListener::bind("127.0.0.1:9134").await?; + let addr = "127.0.0.1:9134".to_string(); + let listener = TcpListener::bind(&addr).await?; + println!("Listening on: {}", addr); loop { - let (socket, _) = listener.accept().await?; - process_socket(socket).await; + match listener.accept().await { + Ok((socket, addr)) => { + tokio::spawn(async move { + process_socket(socket, addr).await; + }); + } + Err(e) => eprintln!("couldn't get client: {:?}", e), + } } }