mirror of
https://github.com/OMGeeky/gdriver2.git
synced 2026-02-23 15:38:32 +01:00
Initial prototype
This prototype has basic IPC working, with starting actions and waiting for actions or just getting information
This commit is contained in:
14
gdriver-client/Cargo.toml
Normal file
14
gdriver-client/Cargo.toml
Normal file
@@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "gdriver-client"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
tarpc.workspace = true
|
||||
tokio.workspace = true
|
||||
futures-sink = "0.3.30"
|
||||
|
||||
[dependencies.gdriver-common]
|
||||
path = "../gdriver-common/"
|
||||
15
gdriver-client/src/main.rs
Normal file
15
gdriver-client/src/main.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
use std::{error::Error, net::IpAddr, result::Result as StdResult};
|
||||
|
||||
use gdriver_common::{ipc::sample::*, prelude::*};
|
||||
use tarpc::{client, tokio_serde::formats::Json};
|
||||
|
||||
type Result<T> = StdResult<T, Box<dyn Error>>;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
service::start().await?;
|
||||
Ok(())
|
||||
}
|
||||
mod sample;
|
||||
|
||||
mod service;
|
||||
32
gdriver-client/src/sample.rs
Normal file
32
gdriver-client/src/sample.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
use gdriver_common::config::CONFIGURATION;
|
||||
|
||||
use super::*;
|
||||
pub async fn start() -> Result<()> {
|
||||
println!("Hello, world!");
|
||||
|
||||
let name = "test1".to_string();
|
||||
let config = &CONFIGURATION;
|
||||
let client: WorldClient = create_client(config.ip, config.port).await?;
|
||||
|
||||
let hello = client
|
||||
.hello(tarpc::context::current(), name.to_string())
|
||||
.await;
|
||||
|
||||
match hello {
|
||||
Ok(hello) => println!("{hello:?}"),
|
||||
Err(e) => println!("{:?}", (e)),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub async fn create_client(ip: IpAddr, port: u16) -> Result<WorldClient> {
|
||||
let server_addr = (ip, port);
|
||||
let transport = tarpc::serde_transport::tcp::connect(&server_addr, Json::default)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
println!("Could not connect");
|
||||
e
|
||||
})?;
|
||||
let var_name = WorldClient::new(client::Config::default(), transport);
|
||||
let client = var_name.spawn();
|
||||
Ok(client)
|
||||
}
|
||||
61
gdriver-client/src/service.rs
Normal file
61
gdriver-client/src/service.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
use std::time;
|
||||
|
||||
use gdriver_common::ipc::gdriver_service::{BackendActionRequest, GDriverServiceClient};
|
||||
|
||||
use super::*;
|
||||
|
||||
pub async fn start() -> Result<()> {
|
||||
println!("Hello, world!");
|
||||
let config = &CONFIGURATION;
|
||||
println!("Config: {:?}", **config);
|
||||
let client: GDriverServiceClient = create_client(config.ip, config.port).await?;
|
||||
|
||||
let hello = client
|
||||
.do_something2(tarpc::context::current(), BackendActionRequest::Ping)
|
||||
.await;
|
||||
match hello {
|
||||
Ok(hello) => println!("Yay: {:?}", hello),
|
||||
Err(e) => {
|
||||
println!(":( {:?}", (e));
|
||||
dbg!(e);
|
||||
}
|
||||
}
|
||||
let start = time::SystemTime::now();
|
||||
let hello = client
|
||||
.do_something2(tarpc::context::current(), BackendActionRequest::RunLong)
|
||||
.await;
|
||||
|
||||
let seconds = (time::SystemTime::now().duration_since(start))
|
||||
.unwrap()
|
||||
.as_secs();
|
||||
|
||||
match hello {
|
||||
Ok(hello) => println!("Run Long returned after {} seconds: {:?}", seconds, hello),
|
||||
Err(e) => println!(":( {:?}", (e)),
|
||||
}
|
||||
let start = time::SystemTime::now();
|
||||
let hello = client
|
||||
.do_something2(tarpc::context::current(), BackendActionRequest::StartLong)
|
||||
.await;
|
||||
let seconds = (time::SystemTime::now().duration_since(start))
|
||||
.unwrap()
|
||||
.as_secs();
|
||||
|
||||
match hello {
|
||||
Ok(hello) => println!("Start Long returned after {} seconds: {:?}", seconds, hello),
|
||||
Err(e) => println!(":( {:?}", (e)),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub async fn create_client(ip: IpAddr, port: u16) -> Result<GDriverServiceClient> {
|
||||
let server_addr = (ip, port);
|
||||
let transport = tarpc::serde_transport::tcp::connect(&server_addr, Json::default)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
println!("Could not connect");
|
||||
e
|
||||
})?;
|
||||
let service = GDriverServiceClient::new(client::Config::default(), transport);
|
||||
let client = service.spawn();
|
||||
Ok(client)
|
||||
}
|
||||
Reference in New Issue
Block a user