-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.rs
228 lines (195 loc) · 6 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use std::env;
use std::process::exit;
use std::sync::{Arc, RwLock};
use std::{thread, time};
use varlink::{Connection, VarlinkService};
use crate::org_example_more::*;
// Dynamically build the varlink rust code.
mod org_example_more;
#[cfg(test)]
mod test;
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
// Main
fn print_usage(program: &str, opts: &getopts::Options) {
let brief = format!("Usage: {} [--varlink=<address>] [--client]", program);
print!("{}", opts.usage(&brief));
}
fn main() {
let args: Vec<_> = env::args().collect();
let program = args[0].clone();
let mut opts = getopts::Options::new();
opts.optopt("", "varlink", "varlink address URL", "<address>");
opts.optflag("", "client", "run in client mode");
opts.optflag("h", "help", "print this help menu");
opts.optopt("", "bridge", "bridge", "<bridge>");
opts.optopt("", "timeout", "server timeout", "<seconds>");
opts.optopt("", "sleep", "sleep duration", "<milliseconds>");
let matches = match opts.parse(&args[1..]) {
Ok(m) => m,
Err(f) => {
eprintln!("{}", f);
print_usage(&program, &opts);
return;
}
};
if matches.opt_present("h") {
print_usage(&program, &opts);
return;
}
let client_mode = matches.opt_present("client");
let timeout = matches
.opt_str("timeout")
.unwrap_or_default()
.parse::<u64>()
.unwrap_or(0);
let sleep = matches
.opt_str("sleep")
.unwrap_or_default()
.parse::<u64>()
.unwrap_or(1000);
let bridge = matches.opt_str("bridge").unwrap_or_default();
let ret: Result<()> = if client_mode {
let connection = if bridge.is_empty() {
match matches.opt_str("varlink") {
None => {
Connection::with_activate(&format!("{} --varlink=$VARLINK_ADDRESS", program))
.unwrap()
}
Some(address) => Connection::with_address(&address).unwrap(),
}
} else {
Connection::with_bridge(&bridge).unwrap()
};
run_client(connection)
} else if let Some(address) = matches.opt_str("varlink") {
run_server(&address, timeout, sleep).map_err(|e| e.into())
} else {
print_usage(&program, &opts);
eprintln!("Need varlink address in server mode.");
exit(1);
};
exit(match ret {
Ok(_) => 0,
Err(err) => {
eprintln!("error: {:?}", err);
1
}
});
}
// Client
fn run_client(connection: Arc<RwLock<varlink::Connection>>) -> Result<()> {
/*
let new_addr = {
let conn = connection.read().unwrap();
conn.address()
};
*/
let mut iface = org_example_more::VarlinkClient::new(connection);
//let con2 = varlink::Connection::with_address(&new_addr)?;
//let mut pingiface = org_example_more::VarlinkClient::new(con2);
for reply in iface.test_more(10).more()? {
let reply = reply?;
//assert!(reply.state.is_some());
let state = reply.state;
match state {
State {
start: Some(true),
end: None,
progress: None,
..
} => {
eprintln!("--- Start ---");
}
State {
start: None,
end: Some(true),
progress: None,
..
} => {
eprintln!("--- End ---");
}
State {
start: None,
end: None,
progress: Some(progress),
..
} => {
eprintln!("Progress: {}", progress);
/*
if progress > 50 {
let reply = pingiface.ping("Test".into()).call()?;
eprintln!("Pong: '{}'", reply.pong);
}
*/
}
_ => eprintln!("Got unknown state: {:?}", state),
}
}
Ok(())
}
// Server
struct MyOrgExampleMore {
sleep_duration: u64,
}
impl VarlinkInterface for MyOrgExampleMore {
fn ping(&self, call: &mut dyn Call_Ping, ping: String) -> varlink::Result<()> {
call.reply(ping)
}
fn stop_serving(&self, call: &mut dyn Call_StopServing) -> varlink::Result<()> {
call.reply()?;
Err(varlink::ErrorKind::ConnectionClosed.into())
}
fn test_more(&self, call: &mut dyn Call_TestMore, n: i64) -> varlink::Result<()> {
if !call.wants_more() {
return call.reply_test_more_error("called without more".into());
}
if n == 0 {
return call.reply_test_more_error("n == 0".into());
}
call.set_continues(true);
call.reply(State {
start: Some(true),
end: None,
progress: None,
})?;
for i in 0..n {
thread::sleep(time::Duration::from_millis(self.sleep_duration));
call.reply(State {
progress: Some(i * 100 / n),
start: None,
end: None,
})?;
}
call.reply(State {
progress: Some(100),
start: None,
end: None,
})?;
call.set_continues(false);
call.reply(State {
end: Some(true),
progress: None,
start: None,
})
}
}
fn run_server(address: &str, timeout: u64, sleep_duration: u64) -> varlink::Result<()> {
let myexamplemore = MyOrgExampleMore { sleep_duration };
let myinterface = org_example_more::new(Box::new(myexamplemore));
let service = VarlinkService::new(
"org.varlink",
"test service",
"0.1",
"http://varlink.org",
vec![Box::new(myinterface)],
);
varlink::listen(
service,
&address,
&varlink::ListenConfig {
idle_timeout: timeout,
..Default::default()
},
)?;
Ok(())
}