Skip to content

Commit 306dfc1

Browse files
authored
Merge pull request #133 from Tim-Zhang/exmaple-use-path-sock-addr
example: use path socket address to support macOS
2 parents 878b566 + 3119823 commit 306dfc1

File tree

5 files changed

+29
-4
lines changed

5 files changed

+29
-4
lines changed

example/async-client.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
//
55

66
mod protocols;
7+
mod utils;
78

89
use protocols::r#async::{agent, agent_ttrpc, health, health_ttrpc};
910
use ttrpc::context::{self, Context};
1011
use ttrpc::r#async::Client;
1112

1213
#[tokio::main(flavor = "current_thread")]
1314
async fn main() {
14-
let c = Client::connect("unix://@/tmp/1").unwrap();
15+
let c = Client::connect(utils::SOCK_ADDR).unwrap();
1516
let hc = health_ttrpc::HealthClient::new(c.clone());
1617
let ac = agent_ttrpc::AgentServiceClient::new(c);
1718

example/async-server.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//
55

66
mod protocols;
7+
mod utils;
78

89
#[macro_use]
910
extern crate log;
@@ -93,8 +94,10 @@ async fn main() {
9394
let a = Arc::new(a);
9495
let aservice = agent_ttrpc::create_agent_service(a);
9596

97+
utils::remove_if_sock_exist(utils::SOCK_ADDR).unwrap();
98+
9699
let mut server = Server::new()
97-
.bind("unix://@/tmp/1")
100+
.bind(utils::SOCK_ADDR)
98101
.unwrap()
99102
.register_service(hservice)
100103
.register_service(aservice);

example/client.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313
// limitations under the License.
1414

1515
mod protocols;
16+
mod utils;
1617

1718
use protocols::sync::{agent, agent_ttrpc, health, health_ttrpc};
1819
use std::thread;
1920
use ttrpc::context::{self, Context};
2021
use ttrpc::Client;
2122

2223
fn main() {
23-
let c = Client::connect("unix://@/tmp/1").unwrap();
24+
let c = Client::connect(utils::SOCK_ADDR).unwrap();
2425
let hc = health_ttrpc::HealthClient::new(c.clone());
2526
let ac = agent_ttrpc::AgentServiceClient::new(c);
2627

example/server.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
mod protocols;
16+
mod utils;
1617

1718
#[macro_use]
1819
extern crate log;
@@ -89,8 +90,9 @@ fn main() {
8990
let a = Arc::new(a);
9091
let aservice = agent_ttrpc::create_agent_service(a);
9192

93+
utils::remove_if_sock_exist(utils::SOCK_ADDR).unwrap();
9294
let mut server = Server::new()
93-
.bind("unix://@/tmp/1")
95+
.bind(utils::SOCK_ADDR)
9496
.unwrap()
9597
.register_service(hservice)
9698
.register_service(aservice);

example/utils.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![allow(dead_code)]
2+
use std::fs;
3+
use std::io::Result;
4+
use std::path::Path;
5+
6+
pub const SOCK_ADDR: &str = "unix:///tmp/ttrpc-test";
7+
8+
pub fn remove_if_sock_exist(sock_addr: &str) -> Result<()> {
9+
let path = sock_addr
10+
.strip_prefix("unix://")
11+
.expect("socket address is not expected");
12+
13+
if Path::new(path).exists() {
14+
fs::remove_file(&path)?;
15+
}
16+
17+
Ok(())
18+
}

0 commit comments

Comments
 (0)