Basic client, can send JSON

status
~erin 2023-04-02 18:07:23 -04:00
parent 529e635b44
commit 46bb2862d0
Signed by: erin
GPG Key ID: 9A8E308CEFA37A47
6 changed files with 96 additions and 8 deletions

3
Cargo.lock generated
View File

@ -30,7 +30,10 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
name = "forge-client"
version = "0.1.0"
dependencies = [
"serde",
"serde_json",
"tokio",
"url",
]
[[package]]

View File

@ -6,3 +6,9 @@ description = "Simple remote build system"
authors = ["Erin <contact@the-system.eu.org>"]
repository = "https://git.lavender.software/erin/forge"
license-file = "LICENSE.md"
[workspace.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"]}

View File

@ -4,4 +4,7 @@ version = "0.1.0"
edition = "2021"
[dependencies]
tokio = { version = "1.27.0", features = ["full"] }
tokio.workspace = true
serde_json.workspace = true
serde.workspace = true
url.workspace = true

View File

@ -1,14 +1,87 @@
use serde::{Deserialize, Serialize};
use std::error::Error;
use std::fmt;
use tokio::io::AsyncWriteExt;
use tokio::net::TcpStream;
use url::Url;
#[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<Vec<String>>,
tag: Tag,
repository: Url,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// Connect to a peer
let mut stream = TcpStream::connect("127.0.0.1:9134").await?;
let test_command = Command {
build_system: BuildSystem::Cargo,
subcommand: Subcommand::Build,
features: Some(vec!["one".to_string(), "two".to_string()]),
tag: Tag::Debug,
repository: Url::parse("https://lib.rs/crates/serde_json").unwrap(),
};
let j = serde_json::to_string(&test_command)?;
println!("{}", j);
// Write some data.
stream.write_all(b"hello world!").await?;
stream.write_all(&j.as_bytes()).await?;
Ok(())
}

View File

@ -4,7 +4,7 @@ version = "0.1.0"
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"]}
tokio.workspace = true
serde_json.workspace = true
serde.workspace = true
url.workspace = true

View File

@ -77,7 +77,10 @@ async fn process_socket(mut socket: TcpStream, address: SocketAddr) {
Err(e) => return,
};
let json: Command = serde_json::from_str(&line).unwrap();
let json: Command = match serde_json::from_str(&line) {
Ok(v) => v,
Err(e) => return,
};
let mut features = String::new();
match json.features {
Some(f) => {
@ -94,7 +97,7 @@ async fn process_socket(mut socket: TcpStream, address: SocketAddr) {
"Received message from {:?}: {} {} {} {}",
address, json.build_system, json.subcommand, json.tag, features
);
socket.write_all(&line.as_bytes()).await.unwrap();
// socket.write_all(&line.as_bytes()).await.unwrap();
}
// println!("client {:?} sent message: '{:?}'", address,);
}