12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- use anyhow::Result;
- use std::collections::HashMap;
- fn print_usage(program: &str, opts: getopts::Options) {
- let brief = format!(
- "test-server: run a test CBORPC server on standard input-output that provides a counter (testproto/add1)\nUsage: {}",
- program
- );
- eprint!("{}", opts.usage(&brief));
- }
- fn main() {
- let args: Vec<String> = std::env::args().collect();
- let program = args[0].clone();
- let mut opts = getopts::Options::new();
- opts.optflag("h", "help", "print this help menu");
- let matches = match opts.parse(&args[1..]) {
- Ok(m) => m,
- Err(f) => panic!(f.to_string()),
- };
- if matches.opt_present("h") {
- print_usage(&program, opts);
- return;
- }
- let state = 0;
- fn adder(v: &mut i64, _: &cborpc::Data) -> Result<cborpc::CallResponse> {
- *v += 1;
- Ok(cborpc::CallResponse {
- success: true,
- message: vec![*v as u8],
- })
- };
- let mut responder = cborpc::Responder::new(state);
- {
- let mut protocol = HashMap::new();
- protocol.insert("add1".to_string(), adder as cborpc::CallHandler<i64>);
- let protocols = responder.protocols();
- protocols.insert("testproto".to_string(), protocol);
- }
- loop {
- let r: Result<()> = match responder.answer_call(&mut std::io::stdin(), &mut std::io::stdout()) {
- Ok(success) => {
- eprint!("Call answered, success={}", success);
- Ok(())
- }
- Err(error) => Err(error),
- };
- r.unwrap();
- }
- }
|