]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/rs/test/src/bin/kitchen_sink_client.rs
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / jaegertracing / thrift / lib / rs / test / src / bin / kitchen_sink_client.rs
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17
18 #[macro_use]
19 extern crate clap;
20
21 extern crate kitchen_sink;
22 extern crate thrift;
23
24 use std::convert::Into;
25
26 use kitchen_sink::base_two::{TNapkinServiceSyncClient, TRamenServiceSyncClient};
27 use kitchen_sink::midlayer::{MealServiceSyncClient, TMealServiceSyncClient};
28 use kitchen_sink::recursive;
29 use kitchen_sink::recursive::{CoRec, CoRec2, RecList, RecTree, TTestServiceSyncClient};
30 use kitchen_sink::ultimate::{FullMealServiceSyncClient, TFullMealServiceSyncClient};
31 use thrift::protocol::{
32 TBinaryInputProtocol, TBinaryOutputProtocol, TCompactInputProtocol, TCompactOutputProtocol,
33 TInputProtocol, TOutputProtocol,
34 };
35 use thrift::transport::{
36 ReadHalf, TFramedReadTransport, TFramedWriteTransport, TIoChannel, TTcpChannel, WriteHalf,
37 };
38
39 fn main() {
40 match run() {
41 Ok(()) => println!("kitchen sink client completed successfully"),
42 Err(e) => {
43 println!("kitchen sink client failed with error {:?}", e);
44 std::process::exit(1);
45 }
46 }
47 }
48
49 fn run() -> thrift::Result<()> {
50 let matches = clap_app!(rust_kitchen_sink_client =>
51 (version: "0.1.0")
52 (author: "Apache Thrift Developers <dev@thrift.apache.org>")
53 (about: "Thrift Rust kitchen sink client")
54 (@arg host: --host +takes_value "Host on which the Thrift test server is located")
55 (@arg port: --port +takes_value "Port on which the Thrift test server is listening")
56 (@arg protocol: --protocol +takes_value "Thrift protocol implementation to use (\"binary\", \"compact\")")
57 (@arg service: --service +takes_value "Service type to contact (\"part\", \"full\", \"recursive\")")
58 )
59 .get_matches();
60
61 let host = matches.value_of("host").unwrap_or("127.0.0.1");
62 let port = value_t!(matches, "port", u16).unwrap_or(9090);
63 let protocol = matches.value_of("protocol").unwrap_or("compact");
64 let service = matches.value_of("service").unwrap_or("part");
65
66 let (i_chan, o_chan) = tcp_channel(host, port)?;
67 let (i_tran, o_tran) = (
68 TFramedReadTransport::new(i_chan),
69 TFramedWriteTransport::new(o_chan),
70 );
71
72 let (i_prot, o_prot): (Box<TInputProtocol>, Box<TOutputProtocol>) = match protocol {
73 "binary" => (
74 Box::new(TBinaryInputProtocol::new(i_tran, true)),
75 Box::new(TBinaryOutputProtocol::new(o_tran, true)),
76 ),
77 "compact" => (
78 Box::new(TCompactInputProtocol::new(i_tran)),
79 Box::new(TCompactOutputProtocol::new(o_tran)),
80 ),
81 unmatched => return Err(format!("unsupported protocol {}", unmatched).into()),
82 };
83
84 run_client(service, i_prot, o_prot)
85 }
86
87 fn run_client(
88 service: &str,
89 i_prot: Box<TInputProtocol>,
90 o_prot: Box<TOutputProtocol>,
91 ) -> thrift::Result<()> {
92 match service {
93 "full" => exec_full_meal_client(i_prot, o_prot),
94 "part" => exec_meal_client(i_prot, o_prot),
95 "recursive" => exec_recursive_client(i_prot, o_prot),
96 _ => Err(thrift::Error::from(format!(
97 "unknown service type {}",
98 service
99 ))),
100 }
101 }
102
103 fn tcp_channel(
104 host: &str,
105 port: u16,
106 ) -> thrift::Result<(ReadHalf<TTcpChannel>, WriteHalf<TTcpChannel>)> {
107 let mut c = TTcpChannel::new();
108 c.open(&format!("{}:{}", host, port))?;
109 c.split()
110 }
111
112 fn exec_meal_client(
113 i_prot: Box<TInputProtocol>,
114 o_prot: Box<TOutputProtocol>,
115 ) -> thrift::Result<()> {
116 let mut client = MealServiceSyncClient::new(i_prot, o_prot);
117
118 // client.full_meal(); // <-- IMPORTANT: if you uncomment this, compilation *should* fail
119 // this is because the MealService struct does not contain the appropriate service marker
120
121 // only the following three calls work
122 execute_call("part", "ramen", || client.ramen(50)).map(|_| ())?;
123 execute_call("part", "meal", || client.meal()).map(|_| ())?;
124 execute_call("part", "napkin", || client.napkin()).map(|_| ())?;
125
126 Ok(())
127 }
128
129 fn exec_full_meal_client(
130 i_prot: Box<TInputProtocol>,
131 o_prot: Box<TOutputProtocol>,
132 ) -> thrift::Result<()> {
133 let mut client = FullMealServiceSyncClient::new(i_prot, o_prot);
134
135 execute_call("full", "ramen", || client.ramen(100)).map(|_| ())?;
136 execute_call("full", "meal", || client.meal()).map(|_| ())?;
137 execute_call("full", "napkin", || client.napkin()).map(|_| ())?;
138 execute_call("full", "full meal", || client.full_meal()).map(|_| ())?;
139
140 Ok(())
141 }
142
143 fn exec_recursive_client(
144 i_prot: Box<TInputProtocol>,
145 o_prot: Box<TOutputProtocol>,
146 ) -> thrift::Result<()> {
147 let mut client = recursive::TestServiceSyncClient::new(i_prot, o_prot);
148
149 let tree = RecTree {
150 children: Some(vec![Box::new(RecTree {
151 children: Some(vec![
152 Box::new(RecTree {
153 children: None,
154 item: Some(3),
155 }),
156 Box::new(RecTree {
157 children: None,
158 item: Some(4),
159 }),
160 ]),
161 item: Some(2),
162 })]),
163 item: Some(1),
164 };
165
166 let expected_tree = RecTree {
167 children: Some(vec![Box::new(RecTree {
168 children: Some(vec![
169 Box::new(RecTree {
170 children: Some(Vec::new()), // remote returns an empty list
171 item: Some(3),
172 }),
173 Box::new(RecTree {
174 children: Some(Vec::new()), // remote returns an empty list
175 item: Some(4),
176 }),
177 ]),
178 item: Some(2),
179 })]),
180 item: Some(1),
181 };
182
183 let returned_tree = execute_call("recursive", "echo_tree", || client.echo_tree(tree.clone()))?;
184 if returned_tree != expected_tree {
185 return Err(format!(
186 "mismatched recursive tree {:?} {:?}",
187 expected_tree, returned_tree
188 )
189 .into());
190 }
191
192 let list = RecList {
193 nextitem: Some(Box::new(RecList {
194 nextitem: Some(Box::new(RecList {
195 nextitem: None,
196 item: Some(3),
197 })),
198 item: Some(2),
199 })),
200 item: Some(1),
201 };
202 let returned_list = execute_call("recursive", "echo_list", || client.echo_list(list.clone()))?;
203 if returned_list != list {
204 return Err(format!("mismatched recursive list {:?} {:?}", list, returned_list).into());
205 }
206
207 let co_rec = CoRec {
208 other: Some(Box::new(CoRec2 {
209 other: Some(CoRec {
210 other: Some(Box::new(CoRec2 { other: None })),
211 }),
212 })),
213 };
214 let returned_co_rec = execute_call("recursive", "echo_co_rec", || {
215 client.echo_co_rec(co_rec.clone())
216 })?;
217 if returned_co_rec != co_rec {
218 return Err(format!("mismatched co_rec {:?} {:?}", co_rec, returned_co_rec).into());
219 }
220
221 Ok(())
222 }
223
224 fn execute_call<F, R>(service_type: &str, call_name: &str, mut f: F) -> thrift::Result<R>
225 where
226 F: FnMut() -> thrift::Result<R>,
227 {
228 let res = f();
229
230 match res {
231 Ok(_) => println!("{}: completed {} call", service_type, call_name),
232 Err(ref e) => println!(
233 "{}: failed {} call with error {:?}",
234 service_type, call_name, e
235 ),
236 }
237
238 res
239 }