all working fuck yey

4-update-check
plazmoid 4 years ago
parent e486ff03c2
commit 268b7783db
  1. 8
      bin/u_agent/src/main.rs
  2. 2
      bin/u_panel/src/main.rs
  3. 2
      bin/u_server/src/main.rs
  4. 137
      lib/u_lib/src/client/network.rs
  5. 2
      lib/u_lib/src/contracts/jobs.rs
  6. 20
      lib/u_lib/src/errors.rs

@ -11,7 +11,10 @@ use {
std::thread::sleep, std::thread::sleep,
std::time::Duration, std::time::Duration,
std::env, std::env,
u_lib::client::network::ClientHandler u_lib::client::{
ClientInfo,
network::ClientHandler
}
}; };
#[tokio::main] #[tokio::main]
@ -19,7 +22,8 @@ async fn main() {
//daemonize(); //daemonize();
let arg_ip = env::args().nth(1); let arg_ip = env::args().nth(1);
let instance = ClientHandler::new(arg_ip); let instance = ClientHandler::new(arg_ip);
instance.init().await; let cli_info = ClientInfo::gather();
instance.init(&cli_info).await;
loop { loop {
sleep(Duration::from_secs(2)); sleep(Duration::from_secs(2));
} }

@ -16,7 +16,7 @@ async fn main() -> Result<(), &'static str> {
.password("123qwe".to_string()); .password("123qwe".to_string());
match method.as_str() { match method.as_str() {
"ls" => { "ls" => {
let result = cli_handler.list().await; let result = cli_handler.ls().await;
for cli in result.iter() { for cli in result.iter() {
println!("{:#?}", cli) println!("{:#?}", cli)
} }

@ -26,7 +26,7 @@ async fn main() {
let db = warp::any().map(move || base_db.clone()); let db = warp::any().map(move || base_db.clone());
let new_client = warp::post() let new_client = warp::post()
.and(warp::path("new")) .and(warp::path("init"))
.and(get_content()) .and(get_content())
.and(db.clone()) .and(db.clone())
.and_then(handlers::add_client); .and_then(handlers::add_client);

@ -16,85 +16,6 @@ use std::{
str::FromStr str::FromStr
}; };
pub struct Paths;
impl Paths {
pub const LIST: &'static str = "list";
pub const NEW: &'static str = "new";
//pub const DEL: &'static str = "del";
pub const JOB_REQ: &'static str = "job_req";
pub const JOB_ANS: &'static str = "job_ans";
}
pub struct ClientHandler {
base_url: Url,
client: Client,
password: Option<String>
}
impl ClientHandler {
pub fn new(server: Option<String>) -> Self {
let master_server = server
.map(|s| Ipv4Addr::from_str(&s).unwrap())
.unwrap_or(MASTER_SERVER);
Self {
client: Client::new(),
base_url: Url::parse(
&format!("http://{}:{}", master_server, MASTER_PORT)
).unwrap(),
password: None
}
}
pub fn password(mut self, password: String) -> ClientHandler {
self.password = Some(password);
self
}
fn set_pwd(&self, rb: RequestBuilder) -> RequestBuilder {
match &self.password {
Some(p) => rb.bearer_auth(p),
None => rb
}
}
fn build_get(&self, url: &str) -> RequestBuilder {
let rb = self.client.get(self.base_url.join(url).unwrap());
self.set_pwd(rb)
}
fn build_post(&self, url: &str) -> RequestBuilder {
let rb = self.client.post(self.base_url.join(url).unwrap());
self.set_pwd(rb)
}
pub async fn init(&self, param: &ClientInfo) -> UResult<RawMsg> {
let response: Response = self.build_post(Paths::NEW)
.json::<Message<'_, ClientInfo>>(&param.as_message())
.send()
.await?;
let msg = response
.json::<Message<RawMsg>>()
.await?;
Ok(msg.into_item().into_owned())
}
pub async fn list(&self) -> UResult<Vec<ClientInfo>> {
let response: Response = self.build_get(Paths::LIST)
.send()
.await?;
let msg = response
.json::<Message<Vec<ClientInfo>>>()
.await?;
Ok(msg.into_item().into_owned())
}
/*
pub async fn del(&self) -> UResult<()> {
self.build_post(Paths::DEL).send().await?;
Ok(())
}*/
}
#[macro_export] #[macro_export]
macro_rules! epilogue { macro_rules! epilogue {
@ -111,6 +32,7 @@ macro_rules! epilogue {
} }
// build_handler![path = new, method = "post", param = ClientInfo, result = RawMsg] // build_handler![path = new, method = "post", param = ClientInfo, result = RawMsg]
// new syntax: build_handler!(POST new(ClientInfo) -> RawMsg)
// param and result must impl ToMsg // param and result must impl ToMsg
#[macro_export] #[macro_export]
macro_rules! build_handler { macro_rules! build_handler {
@ -164,4 +86,61 @@ macro_rules! build_handler {
); );
} }
pub struct Paths;
impl Paths {
pub const LIST: &'static str = "list";
pub const NEW: &'static str = "new";
//pub const DEL: &'static str = "del";
pub const JOB_REQ: &'static str = "job_req";
pub const JOB_ANS: &'static str = "job_ans";
}
pub struct ClientHandler {
base_url: Url,
client: Client,
password: Option<String>
}
impl ClientHandler {
pub fn new(server: Option<String>) -> Self {
let master_server = server
.map(|s| Ipv4Addr::from_str(&s).unwrap())
.unwrap_or(MASTER_SERVER);
Self {
client: Client::new(),
base_url: Url::parse(
&format!("http://{}:{}", master_server, MASTER_PORT)
).unwrap(),
password: None
}
}
pub fn password(mut self, password: String) -> ClientHandler {
self.password = Some(password);
self
}
fn set_pwd(&self, rb: RequestBuilder) -> RequestBuilder {
match &self.password {
Some(p) => rb.bearer_auth(p),
None => rb
}
}
fn build_get(&self, url: &str) -> RequestBuilder {
let rb = self.client.get(self.base_url.join(url).unwrap());
self.set_pwd(rb)
}
fn build_post(&self, url: &str) -> RequestBuilder {
let rb = self.client.post(self.base_url.join(url).unwrap());
self.set_pwd(rb)
}
}
build_handler!(path = init, method = "post", param = ClientInfo, result = RawMsg);
build_handler!(path = ls, method = "get", result = Vec<ClientInfo>);
build_handler!(path = del, method = "post"); build_handler!(path = del, method = "post");

@ -209,7 +209,7 @@ impl<'meta> Job<'meta> {
} }
Err(e) => { Err(e) => {
self.result.data = Err( self.result.data = Err(
UError::new(UErrType::JobError, Some(e)) UError::new(UErrType::JobError, e.to_string())
); );
self.result.retcode = None; self.result.retcode = None;
} }

@ -21,7 +21,7 @@ pub enum UErrType {
#[derive(Serialize, Deserialize, Clone, Debug)] #[derive(Serialize, Deserialize, Clone, Debug)]
struct Inner { struct Inner {
err_type: UErrType, err_type: UErrType,
source: Option<BoxError> source: String,
} }
#[derive(Serialize, Deserialize, Clone)] #[derive(Serialize, Deserialize, Clone)]
@ -30,10 +30,10 @@ pub struct UError {
} }
impl UError { impl UError {
pub fn new<E: Into<BoxError>>(err_type: UErrType, source: Option<E>) -> Self { pub fn new(err_type: UErrType, source: String) -> Self {
Self { Self {
inner: Box::new(Inner { inner: Box::new(Inner {
source: source.map(Into::into), source,
err_type err_type
}) })
} }
@ -45,11 +45,7 @@ impl fmt::Debug for UError {
let mut builder = f.debug_struct("errors::UError"); let mut builder = f.debug_struct("errors::UError");
builder.field("kind", &self.inner.err_type); builder.field("kind", &self.inner.err_type);
builder.field("source", &self.inner.source);
if let Some(ref source) = self.inner.source {
builder.field("source", source);
}
builder.finish() builder.finish()
} }
} }
@ -65,11 +61,7 @@ impl fmt::Display for UError {
}; };
f.write_str(e_type)?; f.write_str(e_type)?;
if let Some(ref e) = self.inner.source { write!(f, ": {}", self.inner.source)
write!(f, ": {}", e)
} else {
Ok(())
}
} }
} }
@ -82,6 +74,6 @@ impl From<ReqError> for UError {
} else { } else {
UErrType::Unknown UErrType::Unknown
}; };
UError::new(err_type, Some(e)) UError::new(err_type, e.to_string())
} }
} }
Loading…
Cancel
Save