diff --git a/.gitignore b/.gitignore index 8090af8a..aa4b5d1b 100644 --- a/.gitignore +++ b/.gitignore @@ -6,5 +6,5 @@ Cargo.lock .vscode .idea *.o -example/protocols/*.rs -!example/protocols/mod.rs +example/protocols/**/*.rs +!example/protocols/**/mod.rs diff --git a/example/async-client.rs b/example/async-client.rs index fc096bb1..9f8f4c66 100644 --- a/example/async-client.rs +++ b/example/async-client.rs @@ -6,6 +6,7 @@ mod protocols; use nix::sys::socket::*; +use protocols::r#async::{agent, agent_ttrpc, health, health_ttrpc}; use tokio; use ttrpc::r#async::Client; @@ -26,8 +27,8 @@ async fn main() { connect(fd, &sockaddr).unwrap(); let c = Client::new(fd); - let mut hc = protocols::health_ttrpc::HealthClient::new(c.clone()); - let mut ac = protocols::agent_ttrpc::AgentServiceClient::new(c); + let mut hc = health_ttrpc::HealthClient::new(c.clone()); + let mut ac = agent_ttrpc::AgentServiceClient::new(c); let mut thc = hc.clone(); let mut tac = ac.clone(); @@ -35,7 +36,7 @@ async fn main() { let now = std::time::Instant::now(); let t1 = tokio::spawn(async move { - let req = protocols::health::CheckRequest::new(); + let req = health::CheckRequest::new(); println!( "Green Thread 1 - {} started: {:?}", "health.check()", @@ -57,7 +58,7 @@ async fn main() { ); let show = match tac - .list_interfaces(&protocols::agent::ListInterfacesRequest::new(), 0) + .list_interfaces(&agent::ListInterfacesRequest::new(), 0) .await { Err(e) => format!("{:?}", e), @@ -80,7 +81,7 @@ async fn main() { ); let show = match ac - .online_cpu_mem(&protocols::agent::OnlineCPUMemRequest::new(), 0) + .online_cpu_mem(&agent::OnlineCPUMemRequest::new(), 0) .await { Err(e) => format!("{:?}", e), @@ -101,7 +102,7 @@ async fn main() { println!( "Green Thread 3 - {} -> {:?} ended: {:?}", "health.version()", - hc.version(&protocols::health::CheckRequest::new(), 0).await, + hc.version(&health::CheckRequest::new(), 0).await, now.elapsed() ); }); diff --git a/example/async-server.rs b/example/async-server.rs index 108d942a..25159dc0 100644 --- a/example/async-server.rs +++ b/example/async-server.rs @@ -12,6 +12,7 @@ use std::sync::Arc; use log::LevelFilter; +use protocols::r#async::{agent, agent_ttrpc, health, health_ttrpc, types}; use ttrpc::asynchronous::server::*; use ttrpc::error::{Error, Result}; use ttrpc::ttrpc::{Code, Status}; @@ -23,12 +24,12 @@ use tokio::signal::unix::{signal, SignalKind}; struct HealthService; #[async_trait] -impl protocols::health_ttrpc::Health for HealthService { +impl health_ttrpc::Health for HealthService { async fn check( &self, _ctx: &::ttrpc::r#async::TtrpcContext, - _req: protocols::health::CheckRequest, - ) -> Result { + _req: health::CheckRequest, + ) -> Result { let mut status = Status::new(); status.set_code(Code::NOT_FOUND); @@ -42,10 +43,10 @@ impl protocols::health_ttrpc::Health for HealthService { async fn version( &self, _ctx: &::ttrpc::r#async::TtrpcContext, - req: protocols::health::CheckRequest, - ) -> Result { + req: health::CheckRequest, + ) -> Result { info!("version {:?}", req); - let mut rep = protocols::health::VersionCheckResponse::new(); + let mut rep = health::VersionCheckResponse::new(); rep.agent_version = "mock.0.1".to_string(); rep.grpc_version = "0.0.1".to_string(); let mut status = Status::new(); @@ -57,22 +58,22 @@ impl protocols::health_ttrpc::Health for HealthService { struct AgentService; #[async_trait] -impl protocols::agent_ttrpc::AgentService for AgentService { +impl agent_ttrpc::AgentService for AgentService { async fn list_interfaces( &self, _ctx: &::ttrpc::r#async::TtrpcContext, - _req: protocols::agent::ListInterfacesRequest, - ) -> ::ttrpc::Result { + _req: agent::ListInterfacesRequest, + ) -> ::ttrpc::Result { let mut rp = protobuf::RepeatedField::new(); - let mut i = protocols::types::Interface::new(); + let mut i = types::Interface::new(); i.set_name("first".to_string()); rp.push(i); - let mut i = protocols::types::Interface::new(); + let mut i = types::Interface::new(); i.set_name("second".to_string()); rp.push(i); - let mut i = protocols::agent::Interfaces::new(); + let mut i = agent::Interfaces::new(); i.set_Interfaces(rp); Ok(i) @@ -83,14 +84,13 @@ impl protocols::agent_ttrpc::AgentService for AgentService { async fn main() { simple_logging::log_to_stderr(LevelFilter::Trace); - let h = Box::new(HealthService {}) as Box; + let h = Box::new(HealthService {}) as Box; let h = Arc::new(h); - let hservice = protocols::health_ttrpc::create_health(h); + let hservice = health_ttrpc::create_health(h); - let a = - Box::new(AgentService {}) as Box; + let a = Box::new(AgentService {}) as Box; let a = Arc::new(a); - let aservice = protocols::agent_ttrpc::create_agent_service(a); + let aservice = agent_ttrpc::create_agent_service(a); let server = Server::new() .bind("unix:///tmp/1") diff --git a/example/build.rs b/example/build.rs index a33c1ebe..6dc605aa 100644 --- a/example/build.rs +++ b/example/build.rs @@ -22,7 +22,18 @@ fn main() { .for_each(|p| println!("cargo:rerun-if-changed={}", &p)); protoc_rust_ttrpc::Codegen::new() - .out_dir("protocols") + .out_dir("protocols/sync") + .inputs(&protos) + .include("protocols/protos") + .rust_protobuf() + .customize(Customize { + ..Default::default() + }) + .run() + .expect("Gen sync codes failed."); + + protoc_rust_ttrpc::Codegen::new() + .out_dir("protocols/asynchronous") .inputs(&protos) .include("protocols/protos") .rust_protobuf() @@ -31,13 +42,20 @@ fn main() { ..Default::default() }) .run() - .expect("Codegen failed."); + .expect("Gen async codes failed."); // There is a message named 'Box' in oci.proto // so there is a struct named 'Box', we should replace Box to ::std::boxed::Box // to avoid the conflict. replace_text_in_file( - "protocols/oci.rs", + "protocols/sync/oci.rs", + "self: Box", + "self: ::std::boxed::Box", + ) + .unwrap(); + + replace_text_in_file( + "protocols/asynchronous/oci.rs", "self: Box", "self: ::std::boxed::Box", ) diff --git a/example/client.rs b/example/client.rs index fa252d48..dd6ee23a 100644 --- a/example/client.rs +++ b/example/client.rs @@ -15,6 +15,7 @@ mod protocols; use nix::sys::socket::*; +use protocols::sync::{agent, agent_ttrpc, health, health_ttrpc}; use std::thread; use ttrpc::client::Client; @@ -34,8 +35,8 @@ fn main() { connect(fd, &sockaddr).unwrap(); let c = Client::new(fd); - let hc = protocols::health_ttrpc::HealthClient::new(c.clone()); - let ac = protocols::agent_ttrpc::AgentServiceClient::new(c); + let hc = health_ttrpc::HealthClient::new(c.clone()); + let ac = agent_ttrpc::AgentServiceClient::new(c); let thc = hc.clone(); let tac = ac.clone(); @@ -43,7 +44,7 @@ fn main() { let now = std::time::Instant::now(); let t = thread::spawn(move || { - let req = protocols::health::CheckRequest::new(); + let req = health::CheckRequest::new(); println!( "OS Thread {:?} - {} started: {:?}", std::thread::current().id(), @@ -67,7 +68,7 @@ fn main() { now.elapsed(), ); - let show = match tac.list_interfaces(&protocols::agent::ListInterfacesRequest::new(), 0) { + let show = match tac.list_interfaces(&agent::ListInterfacesRequest::new(), 0) { Err(e) => format!("{:?}", e), Ok(s) => format!("{:?}", s), }; @@ -86,7 +87,7 @@ fn main() { "agent.online_cpu_mem()", now.elapsed() ); - let show = match ac.online_cpu_mem(&protocols::agent::OnlineCPUMemRequest::new(), 0) { + let show = match ac.online_cpu_mem(&agent::OnlineCPUMemRequest::new(), 0) { Err(e) => format!("{:?}", e), Ok(s) => format!("{:?}", s), }; @@ -107,7 +108,7 @@ fn main() { println!( "Main OS Thread - {} -> {:?} ended: {:?}", "health.version()", - hc.version(&protocols::health::CheckRequest::new(), 0), + hc.version(&health::CheckRequest::new(), 0), now.elapsed() ); diff --git a/example/protocols/asynchronous/mod.rs b/example/protocols/asynchronous/mod.rs new file mode 100644 index 00000000..fd7082cc --- /dev/null +++ b/example/protocols/asynchronous/mod.rs @@ -0,0 +1,12 @@ +// Copyright (c) 2020 Ant Financial +// +// SPDX-License-Identifier: Apache-2.0 +// + +pub mod agent; +pub mod agent_ttrpc; +pub mod empty; +pub mod health; +pub mod health_ttrpc; +mod oci; +pub mod types; diff --git a/example/protocols/mod.rs b/example/protocols/mod.rs index ba84915e..b81f3d7d 100644 --- a/example/protocols/mod.rs +++ b/example/protocols/mod.rs @@ -1,15 +1,8 @@ -pub mod agent; -pub mod agent_ttrpc; -pub mod health; -pub mod health_ttrpc; -mod oci; -pub mod types; -pub mod empty; +// Copyright (c) 2020 Ant Financial +// +// SPDX-License-Identifier: Apache-2.0 +// -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - assert_eq!(2 + 2, 4); - } -} +pub mod asynchronous; +pub mod sync; +pub use asynchronous as r#async; diff --git a/example/protocols/sync/mod.rs b/example/protocols/sync/mod.rs new file mode 100644 index 00000000..fd7082cc --- /dev/null +++ b/example/protocols/sync/mod.rs @@ -0,0 +1,12 @@ +// Copyright (c) 2020 Ant Financial +// +// SPDX-License-Identifier: Apache-2.0 +// + +pub mod agent; +pub mod agent_ttrpc; +pub mod empty; +pub mod health; +pub mod health_ttrpc; +mod oci; +pub mod types; diff --git a/example/server.rs b/example/server.rs index 487374b9..e77415d5 100644 --- a/example/server.rs +++ b/example/server.rs @@ -21,17 +21,18 @@ use log::LevelFilter; use std::sync::Arc; use std::thread; +use protocols::sync::{agent, agent_ttrpc, health, health_ttrpc, types}; use ttrpc::error::{Error, Result}; use ttrpc::server::*; use ttrpc::ttrpc::{Code, Status}; struct HealthService; -impl protocols::health_ttrpc::Health for HealthService { +impl health_ttrpc::Health for HealthService { fn check( &self, _ctx: &::ttrpc::TtrpcContext, - _req: protocols::health::CheckRequest, - ) -> Result { + _req: health::CheckRequest, + ) -> Result { let mut status = Status::new(); status.set_code(Code::NOT_FOUND); status.set_message("Just for fun".to_string()); @@ -40,10 +41,10 @@ impl protocols::health_ttrpc::Health for HealthService { fn version( &self, _ctx: &::ttrpc::TtrpcContext, - req: protocols::health::CheckRequest, - ) -> Result { + req: health::CheckRequest, + ) -> Result { info!("version {:?}", req); - let mut rep = protocols::health::VersionCheckResponse::new(); + let mut rep = health::VersionCheckResponse::new(); rep.agent_version = "mock.0.1".to_string(); rep.grpc_version = "0.0.1".to_string(); let mut status = Status::new(); @@ -53,22 +54,22 @@ impl protocols::health_ttrpc::Health for HealthService { } struct AgentService; -impl protocols::agent_ttrpc::AgentService for AgentService { +impl agent_ttrpc::AgentService for AgentService { fn list_interfaces( &self, _ctx: &::ttrpc::TtrpcContext, - _req: protocols::agent::ListInterfacesRequest, - ) -> ::ttrpc::Result { + _req: agent::ListInterfacesRequest, + ) -> ::ttrpc::Result { let mut rp = protobuf::RepeatedField::new(); - let mut i = protocols::types::Interface::new(); + let mut i = types::Interface::new(); i.set_name("first".to_string()); rp.push(i); - let mut i = protocols::types::Interface::new(); + let mut i = types::Interface::new(); i.set_name("second".to_string()); rp.push(i); - let mut i = protocols::agent::Interfaces::new(); + let mut i = agent::Interfaces::new(); i.set_Interfaces(rp); Ok(i) @@ -79,14 +80,13 @@ impl protocols::agent_ttrpc::AgentService for AgentService { fn main() { simple_logging::log_to_stderr(LevelFilter::Trace); - let h = Box::new(HealthService {}) as Box; + let h = Box::new(HealthService {}) as Box; let h = Arc::new(h); - let hservice = protocols::health_ttrpc::create_health(h); + let hservice = health_ttrpc::create_health(h); - let a = - Box::new(AgentService {}) as Box; + let a = Box::new(AgentService {}) as Box; let a = Arc::new(a); - let aservice = protocols::agent_ttrpc::create_agent_service(a); + let aservice = agent_ttrpc::create_agent_service(a); let mut server = Server::new() .bind("unix:///tmp/1")