http://git-wip-us.apache.org/repos/asf/thrift/blob/8b96bfbf/lib/rs/src/transport/mem.rs ---------------------------------------------------------------------- diff --git a/lib/rs/src/transport/mem.rs b/lib/rs/src/transport/mem.rs new file mode 100644 index 0000000..8ec2a98 --- /dev/null +++ b/lib/rs/src/transport/mem.rs @@ -0,0 +1,342 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::cmp; +use std::io; + +/// Simple transport that contains both a fixed-length internal read buffer and +/// a fixed-length internal write buffer. +/// +/// On a `write` bytes are written to the internal write buffer. Writes are no +/// longer accepted once this buffer is full. Callers must `empty_write_buffer()` +/// before subsequent writes are accepted. +/// +/// You can set readable bytes in the internal read buffer by filling it with +/// `set_readable_bytes(...)`. Callers can then read until the buffer is +/// depleted. No further reads are accepted until the internal read buffer is +/// replenished again. +pub struct TBufferTransport { + rbuf: Box<[u8]>, + rpos: usize, + ridx: usize, + rcap: usize, + wbuf: Box<[u8]>, + wpos: usize, + wcap: usize, +} + +impl TBufferTransport { + /// Constructs a new, empty `TBufferTransport` with the given + /// read buffer capacity and write buffer capacity. + pub fn with_capacity(read_buffer_capacity: usize, + write_buffer_capacity: usize) + -> TBufferTransport { + TBufferTransport { + rbuf: vec![0; read_buffer_capacity].into_boxed_slice(), + ridx: 0, + rpos: 0, + rcap: read_buffer_capacity, + wbuf: vec![0; write_buffer_capacity].into_boxed_slice(), + wpos: 0, + wcap: write_buffer_capacity, + } + } + + /// Return a slice containing the bytes held by the internal read buffer. + /// Returns an empty slice if no readable bytes are present. + pub fn read_buffer(&self) -> &[u8] { + &self.rbuf[..self.ridx] + } + + // FIXME: do I really need this API call? + // FIXME: should this simply reset to the last set of readable bytes? + /// Reset the number of readable bytes to zero. + /// + /// Subsequent calls to `read` will return nothing. + pub fn empty_read_buffer(&mut self) { + self.rpos = 0; + self.ridx = 0; + } + + /// Copy bytes from the source buffer `buf` into the internal read buffer, + /// overwriting any existing bytes. Returns the number of bytes copied, + /// which is `min(buf.len(), internal_read_buf.len())`. + pub fn set_readable_bytes(&mut self, buf: &[u8]) -> usize { + self.empty_read_buffer(); + let max_bytes = cmp::min(self.rcap, buf.len()); + self.rbuf[..max_bytes].clone_from_slice(&buf[..max_bytes]); + self.ridx = max_bytes; + max_bytes + } + + /// Return a slice containing the bytes held by the internal write buffer. + /// Returns an empty slice if no bytes were written. + pub fn write_buffer_as_ref(&self) -> &[u8] { + &self.wbuf[..self.wpos] + } + + /// Return a vector with a copy of the bytes held by the internal write buffer. + /// Returns an empty vector if no bytes were written. + pub fn write_buffer_to_vec(&self) -> Vec<u8> { + let mut buf = vec![0u8; self.wpos]; + buf.copy_from_slice(&self.wbuf[..self.wpos]); + buf + } + + /// Resets the internal write buffer, making it seem like no bytes were + /// written. Calling `write_buffer` after this returns an empty slice. + pub fn empty_write_buffer(&mut self) { + self.wpos = 0; + } + + /// Overwrites the contents of the read buffer with the contents of the + /// write buffer. The write buffer is emptied after this operation. + pub fn copy_write_buffer_to_read_buffer(&mut self) { + let buf = { + let b = self.write_buffer_as_ref(); + let mut b_ret = vec![0; b.len()]; + b_ret.copy_from_slice(&b); + b_ret + }; + + let bytes_copied = self.set_readable_bytes(&buf); + assert_eq!(bytes_copied, buf.len()); + + self.empty_write_buffer(); + } +} + +impl io::Read for TBufferTransport { + fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { + let nread = cmp::min(buf.len(), self.ridx - self.rpos); + buf[..nread].clone_from_slice(&self.rbuf[self.rpos..self.rpos + nread]); + self.rpos += nread; + Ok(nread) + } +} + +impl io::Write for TBufferTransport { + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { + let nwrite = cmp::min(buf.len(), self.wcap - self.wpos); + self.wbuf[self.wpos..self.wpos + nwrite].clone_from_slice(&buf[..nwrite]); + self.wpos += nwrite; + Ok(nwrite) + } + + fn flush(&mut self) -> io::Result<()> { + Ok(()) // nothing to do on flush + } +} + +#[cfg(test)] +mod tests { + use std::io::{Read, Write}; + + use super::TBufferTransport; + + #[test] + fn must_empty_write_buffer() { + let mut t = TBufferTransport::with_capacity(0, 1); + + let bytes_to_write: [u8; 1] = [0x01]; + let result = t.write(&bytes_to_write); + assert_eq!(result.unwrap(), 1); + assert_eq!(&t.write_buffer_as_ref(), &bytes_to_write); + + t.empty_write_buffer(); + assert_eq!(t.write_buffer_as_ref().len(), 0); + } + + #[test] + fn must_accept_writes_after_buffer_emptied() { + let mut t = TBufferTransport::with_capacity(0, 2); + + let bytes_to_write: [u8; 2] = [0x01, 0x02]; + + // first write (all bytes written) + let result = t.write(&bytes_to_write); + assert_eq!(result.unwrap(), 2); + assert_eq!(&t.write_buffer_as_ref(), &bytes_to_write); + + // try write again (nothing should be written) + let result = t.write(&bytes_to_write); + assert_eq!(result.unwrap(), 0); + assert_eq!(&t.write_buffer_as_ref(), &bytes_to_write); // still the same as before + + // now reset the buffer + t.empty_write_buffer(); + assert_eq!(t.write_buffer_as_ref().len(), 0); + + // now try write again - the write should succeed + let result = t.write(&bytes_to_write); + assert_eq!(result.unwrap(), 2); + assert_eq!(&t.write_buffer_as_ref(), &bytes_to_write); + } + + #[test] + fn must_accept_multiple_writes_until_buffer_is_full() { + let mut t = TBufferTransport::with_capacity(0, 10); + + // first write (all bytes written) + let bytes_to_write_0: [u8; 2] = [0x01, 0x41]; + let write_0_result = t.write(&bytes_to_write_0); + assert_eq!(write_0_result.unwrap(), 2); + assert_eq!(t.write_buffer_as_ref(), &bytes_to_write_0); + + // second write (all bytes written, starting at index 2) + let bytes_to_write_1: [u8; 7] = [0x24, 0x41, 0x32, 0x33, 0x11, 0x98, 0xAF]; + let write_1_result = t.write(&bytes_to_write_1); + assert_eq!(write_1_result.unwrap(), 7); + assert_eq!(&t.write_buffer_as_ref()[2..], &bytes_to_write_1); + + // third write (only 1 byte written - that's all we have space for) + let bytes_to_write_2: [u8; 3] = [0xBF, 0xDA, 0x98]; + let write_2_result = t.write(&bytes_to_write_2); + assert_eq!(write_2_result.unwrap(), 1); + assert_eq!(&t.write_buffer_as_ref()[9..], &bytes_to_write_2[0..1]); // how does this syntax work?! + + // fourth write (no writes are accepted) + let bytes_to_write_3: [u8; 3] = [0xBF, 0xAA, 0xFD]; + let write_3_result = t.write(&bytes_to_write_3); + assert_eq!(write_3_result.unwrap(), 0); + + // check the full write buffer + let mut expected: Vec<u8> = Vec::with_capacity(10); + expected.extend_from_slice(&bytes_to_write_0); + expected.extend_from_slice(&bytes_to_write_1); + expected.extend_from_slice(&bytes_to_write_2[0..1]); + assert_eq!(t.write_buffer_as_ref(), &expected[..]); + } + + #[test] + fn must_empty_read_buffer() { + let mut t = TBufferTransport::with_capacity(1, 0); + + let bytes_to_read: [u8; 1] = [0x01]; + let result = t.set_readable_bytes(&bytes_to_read); + assert_eq!(result, 1); + assert_eq!(&t.read_buffer(), &bytes_to_read); + + t.empty_read_buffer(); + assert_eq!(t.read_buffer().len(), 0); + } + + #[test] + fn must_allow_readable_bytes_to_be_set_after_read_buffer_emptied() { + let mut t = TBufferTransport::with_capacity(1, 0); + + let bytes_to_read_0: [u8; 1] = [0x01]; + let result = t.set_readable_bytes(&bytes_to_read_0); + assert_eq!(result, 1); + assert_eq!(&t.read_buffer(), &bytes_to_read_0); + + t.empty_read_buffer(); + assert_eq!(t.read_buffer().len(), 0); + + let bytes_to_read_1: [u8; 1] = [0x02]; + let result = t.set_readable_bytes(&bytes_to_read_1); + assert_eq!(result, 1); + assert_eq!(&t.read_buffer(), &bytes_to_read_1); + } + + #[test] + fn must_accept_multiple_reads_until_all_bytes_read() { + let mut t = TBufferTransport::with_capacity(10, 0); + + let readable_bytes: [u8; 10] = [0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0x00, 0x1A, 0x2B, 0x3C, 0x4D]; + + // check that we're able to set the bytes to be read + let result = t.set_readable_bytes(&readable_bytes); + assert_eq!(result, 10); + assert_eq!(&t.read_buffer(), &readable_bytes); + + // first read + let mut read_buf_0 = vec![0; 5]; + let read_result = t.read(&mut read_buf_0); + assert_eq!(read_result.unwrap(), 5); + assert_eq!(read_buf_0.as_slice(), &(readable_bytes[0..5])); + + // second read + let mut read_buf_1 = vec![0; 4]; + let read_result = t.read(&mut read_buf_1); + assert_eq!(read_result.unwrap(), 4); + assert_eq!(read_buf_1.as_slice(), &(readable_bytes[5..9])); + + // third read (only 1 byte remains to be read) + let mut read_buf_2 = vec![0; 3]; + let read_result = t.read(&mut read_buf_2); + assert_eq!(read_result.unwrap(), 1); + read_buf_2.truncate(1); // FIXME: does the caller have to do this? + assert_eq!(read_buf_2.as_slice(), &(readable_bytes[9..])); + + // fourth read (nothing should be readable) + let mut read_buf_3 = vec![0; 10]; + let read_result = t.read(&mut read_buf_3); + assert_eq!(read_result.unwrap(), 0); + read_buf_3.truncate(0); + + // check that all the bytes we received match the original (again!) + let mut bytes_read = Vec::with_capacity(10); + bytes_read.extend_from_slice(&read_buf_0); + bytes_read.extend_from_slice(&read_buf_1); + bytes_read.extend_from_slice(&read_buf_2); + bytes_read.extend_from_slice(&read_buf_3); + assert_eq!(&bytes_read, &readable_bytes); + } + + #[test] + fn must_allow_reads_to_succeed_after_read_buffer_replenished() { + let mut t = TBufferTransport::with_capacity(3, 0); + + let readable_bytes_0: [u8; 3] = [0x02, 0xAB, 0x33]; + + // check that we're able to set the bytes to be read + let result = t.set_readable_bytes(&readable_bytes_0); + assert_eq!(result, 3); + assert_eq!(&t.read_buffer(), &readable_bytes_0); + + let mut read_buf = vec![0; 4]; + + // drain the read buffer + let read_result = t.read(&mut read_buf); + assert_eq!(read_result.unwrap(), 3); + assert_eq!(t.read_buffer(), &read_buf[0..3]); + + // check that a subsequent read fails + let read_result = t.read(&mut read_buf); + assert_eq!(read_result.unwrap(), 0); + + // we don't modify the read buffer on failure + let mut expected_bytes = Vec::with_capacity(4); + expected_bytes.extend_from_slice(&readable_bytes_0); + expected_bytes.push(0x00); + assert_eq!(&read_buf, &expected_bytes); + + // replenish the read buffer again + let readable_bytes_1: [u8; 2] = [0x91, 0xAA]; + + // check that we're able to set the bytes to be read + let result = t.set_readable_bytes(&readable_bytes_1); + assert_eq!(result, 2); + assert_eq!(&t.read_buffer(), &readable_bytes_1); + + // read again + let read_result = t.read(&mut read_buf); + assert_eq!(read_result.unwrap(), 2); + assert_eq!(t.read_buffer(), &read_buf[0..2]); + } +}
http://git-wip-us.apache.org/repos/asf/thrift/blob/8b96bfbf/lib/rs/src/transport/mod.rs ---------------------------------------------------------------------- diff --git a/lib/rs/src/transport/mod.rs b/lib/rs/src/transport/mod.rs new file mode 100644 index 0000000..bbabd66 --- /dev/null +++ b/lib/rs/src/transport/mod.rs @@ -0,0 +1,51 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! Types required to send and receive bytes over an I/O channel. +//! +//! The core type is the `TTransport` trait, through which a `TProtocol` can +//! send and receive primitives over the wire. While `TProtocol` instances deal +//! with primitive types, `TTransport` instances understand only bytes. + +use std::cell::RefCell; +use std::io; +use std::rc::Rc; + +mod buffered; +mod framed; +mod passthru; +mod socket; + +#[cfg(test)] +pub mod mem; + +pub use self::buffered::{TBufferedTransport, TBufferedTransportFactory}; +pub use self::framed::{TFramedTransport, TFramedTransportFactory}; +pub use self::passthru::TPassThruTransport; +pub use self::socket::TTcpTransport; + +/// Identifies an I/O channel that can be used to send and receive bytes. +pub trait TTransport: io::Read + io::Write {} +impl<I: io::Read + io::Write> TTransport for I {} + +/// Helper type used by servers to create `TTransport` instances for accepted +/// client connections. +pub trait TTransportFactory { + /// Create a `TTransport` that wraps an `inner` transport, thus creating + /// a transport stack. + fn create(&self, inner: Rc<RefCell<Box<TTransport>>>) -> Box<TTransport>; +} http://git-wip-us.apache.org/repos/asf/thrift/blob/8b96bfbf/lib/rs/src/transport/passthru.rs ---------------------------------------------------------------------- diff --git a/lib/rs/src/transport/passthru.rs b/lib/rs/src/transport/passthru.rs new file mode 100644 index 0000000..60dc3a6 --- /dev/null +++ b/lib/rs/src/transport/passthru.rs @@ -0,0 +1,73 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::cell::RefCell; +use std::rc::Rc; +use std::io; +use std::io::{Read, Write}; + +use super::TTransport; + +/// Proxy that wraps an inner `TTransport` and delegates all calls to it. +/// +/// Unlike other `TTransport` wrappers, `TPassThruTransport` is generic with +/// regards to the wrapped transport. This allows callers to use methods +/// specific to the type being wrapped instead of being constrained to methods +/// on the `TTransport` trait. +/// +/// # Examples +/// +/// Create and use a `TPassThruTransport`. +/// +/// ```no_run +/// use std::cell::RefCell; +/// use std::rc::Rc; +/// use thrift::transport::{TPassThruTransport, TTcpTransport}; +/// +/// let t = TTcpTransport::new(); +/// let t = TPassThruTransport::new(Rc::new(RefCell::new(Box::new(t)))); +/// +/// // since the type parameter is maintained, we are able +/// // to use functions specific to `TTcpTransport` +/// t.inner.borrow_mut().open("localhost:9090").unwrap(); +/// ``` +pub struct TPassThruTransport<I: TTransport> { + pub inner: Rc<RefCell<Box<I>>>, +} + +impl<I: TTransport> TPassThruTransport<I> { + /// Create a `TPassThruTransport` that wraps an `inner` TTransport. + pub fn new(inner: Rc<RefCell<Box<I>>>) -> TPassThruTransport<I> { + TPassThruTransport { inner: inner } + } +} + +impl<I: TTransport> Read for TPassThruTransport<I> { + fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { + self.inner.borrow_mut().read(buf) + } +} + +impl<I: TTransport> Write for TPassThruTransport<I> { + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { + self.inner.borrow_mut().write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.inner.borrow_mut().flush() + } +} http://git-wip-us.apache.org/repos/asf/thrift/blob/8b96bfbf/lib/rs/src/transport/socket.rs ---------------------------------------------------------------------- diff --git a/lib/rs/src/transport/socket.rs b/lib/rs/src/transport/socket.rs new file mode 100644 index 0000000..9f2b8ba --- /dev/null +++ b/lib/rs/src/transport/socket.rs @@ -0,0 +1,141 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::convert::From; +use std::io; +use std::io::{ErrorKind, Read, Write}; +use std::net::{Shutdown, TcpStream}; +use std::ops::Drop; + +use ::{TransportError, TransportErrorKind}; + +/// Communicate with a Thrift service over a TCP socket. +/// +/// # Examples +/// +/// Create a `TTcpTransport`. +/// +/// ```no_run +/// use std::io::{Read, Write}; +/// use thrift::transport::TTcpTransport; +/// +/// let mut t = TTcpTransport::new(); +/// t.open("localhost:9090").unwrap(); +/// +/// let mut buf = vec![0u8; 4]; +/// t.read(&mut buf).unwrap(); +/// t.write(&vec![0, 1, 2]).unwrap(); +/// ``` +/// +/// Create a `TTcpTransport` by wrapping an existing `TcpStream`. +/// +/// ```no_run +/// use std::io::{Read, Write}; +/// use std::net::TcpStream; +/// use thrift::transport::TTcpTransport; +/// +/// let stream = TcpStream::connect("127.0.0.1:9189").unwrap(); +/// let mut t = TTcpTransport::with_stream(stream); +/// +/// // no need to call t.open() since we've already connected above +/// +/// let mut buf = vec![0u8; 4]; +/// t.read(&mut buf).unwrap(); +/// t.write(&vec![0, 1, 2]).unwrap(); +/// ``` +#[derive(Default)] +pub struct TTcpTransport { + stream: Option<TcpStream>, +} + +impl TTcpTransport { + /// Create an uninitialized `TTcpTransport`. + /// + /// The returned instance must be opened using `TTcpTransport::open(...)` + /// before it can be used. + pub fn new() -> TTcpTransport { + TTcpTransport { stream: None } + } + + /// Create a `TTcpTransport` that wraps an existing `TcpStream`. + /// + /// The passed-in stream is assumed to have been opened before being wrapped + /// by the created `TTcpTransport` instance. + pub fn with_stream(stream: TcpStream) -> TTcpTransport { + TTcpTransport { stream: Some(stream) } + } + + /// Connect to `remote_address`, which should have the form `host:port`. + pub fn open(&mut self, remote_address: &str) -> ::Result<()> { + if self.stream.is_some() { + Err(::Error::Transport(TransportError::new(TransportErrorKind::AlreadyOpen, + "transport previously opened"))) + } else { + match TcpStream::connect(&remote_address) { + Ok(s) => { + self.stream = Some(s); + Ok(()) + } + Err(e) => Err(From::from(e)), + } + } + } + + /// Shutdown this transport. + /// + /// Both send and receive halves are closed, and this instance can no + /// longer be used to communicate with another endpoint. + pub fn close(&mut self) -> ::Result<()> { + self.if_set(|s| s.shutdown(Shutdown::Both)).map_err(From::from) + } + + fn if_set<F, T>(&mut self, mut stream_operation: F) -> io::Result<T> + where F: FnMut(&mut TcpStream) -> io::Result<T> + { + + if let Some(ref mut s) = self.stream { + stream_operation(s) + } else { + Err(io::Error::new(ErrorKind::NotConnected, "tcp endpoint not connected")) + } + } +} + +impl Read for TTcpTransport { + fn read(&mut self, b: &mut [u8]) -> io::Result<usize> { + self.if_set(|s| s.read(b)) + } +} + +impl Write for TTcpTransport { + fn write(&mut self, b: &[u8]) -> io::Result<usize> { + self.if_set(|s| s.write(b)) + } + + fn flush(&mut self) -> io::Result<()> { + self.if_set(|s| s.flush()) + } +} + +// Do I have to implement the Drop trait? TcpStream closes the socket on drop. +impl Drop for TTcpTransport { + fn drop(&mut self) { + if let Err(e) = self.close() { + warn!("error while closing socket transport: {:?}", e) + } + } +} http://git-wip-us.apache.org/repos/asf/thrift/blob/8b96bfbf/lib/rs/test/Cargo.toml ---------------------------------------------------------------------- diff --git a/lib/rs/test/Cargo.toml b/lib/rs/test/Cargo.toml new file mode 100644 index 0000000..8655a76 --- /dev/null +++ b/lib/rs/test/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "kitchen-sink" +version = "0.1.0" +license = "Apache-2.0" +authors = ["Apache Thrift Developers <[email protected]>"] +publish = false + +[dependencies] +clap = "2.18.0" +ordered-float = "0.3.0" +try_from = "0.2.0" + +[dependencies.thrift] +path = "../" + http://git-wip-us.apache.org/repos/asf/thrift/blob/8b96bfbf/lib/rs/test/Makefile.am ---------------------------------------------------------------------- diff --git a/lib/rs/test/Makefile.am b/lib/rs/test/Makefile.am new file mode 100644 index 0000000..8896940 --- /dev/null +++ b/lib/rs/test/Makefile.am @@ -0,0 +1,49 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +THRIFT = $(top_builddir)/compiler/cpp/thrift + +stubs: thrifts/Base_One.thrift thrifts/Base_Two.thrift thrifts/Midlayer.thrift thrifts/Ultimate.thrift $(THRIFT) + $(THRIFT) -I ./thrifts -out src --gen rs thrifts/Base_One.thrift + $(THRIFT) -I ./thrifts -out src --gen rs thrifts/Base_Two.thrift + $(THRIFT) -I ./thrifts -out src --gen rs thrifts/Midlayer.thrift + $(THRIFT) -I ./thrifts -out src --gen rs thrifts/Ultimate.thrift + +check: stubs + $(CARGO) build + $(CARGO) test + [ -d bin ] || mkdir bin + cp target/debug/kitchen_sink_server bin/kitchen_sink_server + cp target/debug/kitchen_sink_client bin/kitchen_sink_client + +clean-local: + $(CARGO) clean + -$(RM) Cargo.lock + -$(RM) src/base_one.rs + -$(RM) src/base_two.rs + -$(RM) src/midlayer.rs + -$(RM) src/ultimate.rs + -$(RM) -r bin + +EXTRA_DIST = \ + Cargo.toml \ + src/lib.rs \ + src/bin/kitchen_sink_server.rs \ + src/bin/kitchen_sink_client.rs + http://git-wip-us.apache.org/repos/asf/thrift/blob/8b96bfbf/lib/rs/test/src/bin/kitchen_sink_client.rs ---------------------------------------------------------------------- diff --git a/lib/rs/test/src/bin/kitchen_sink_client.rs b/lib/rs/test/src/bin/kitchen_sink_client.rs new file mode 100644 index 0000000..27171be --- /dev/null +++ b/lib/rs/test/src/bin/kitchen_sink_client.rs @@ -0,0 +1,142 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#[macro_use] +extern crate clap; + +extern crate kitchen_sink; +extern crate thrift; + +use std::cell::RefCell; +use std::rc::Rc; + +use kitchen_sink::base_two::{TNapkinServiceSyncClient, TRamenServiceSyncClient}; +use kitchen_sink::midlayer::{MealServiceSyncClient, TMealServiceSyncClient}; +use kitchen_sink::ultimate::{FullMealServiceSyncClient, TFullMealServiceSyncClient}; +use thrift::transport::{TFramedTransport, TTcpTransport, TTransport}; +use thrift::protocol::{TBinaryInputProtocol, TBinaryOutputProtocol, TCompactInputProtocol, + TCompactOutputProtocol, TInputProtocol, TOutputProtocol}; + +fn main() { + match run() { + Ok(()) => println!("kitchen sink client completed successfully"), + Err(e) => { + println!("kitchen sink client failed with error {:?}", e); + std::process::exit(1); + } + } +} + +fn run() -> thrift::Result<()> { + let matches = clap_app!(rust_kitchen_sink_client => + (version: "0.1.0") + (author: "Apache Thrift Developers <[email protected]>") + (about: "Thrift Rust kitchen sink client") + (@arg host: --host +takes_value "Host on which the Thrift test server is located") + (@arg port: --port +takes_value "Port on which the Thrift test server is listening") + (@arg protocol: --protocol +takes_value "Thrift protocol implementation to use (\"binary\", \"compact\")") + (@arg service: --service +takes_value "Service type to contact (\"part\", \"full\")") + ).get_matches(); + + let host = matches.value_of("host").unwrap_or("127.0.0.1"); + let port = value_t!(matches, "port", u16).unwrap_or(9090); + let protocol = matches.value_of("protocol").unwrap_or("compact"); + let service = matches.value_of("service").unwrap_or("part"); + + let t = open_tcp_transport(host, port)?; + let t = Rc::new(RefCell::new(Box::new(TFramedTransport::new(t)) as Box<TTransport>)); + + let (i_prot, o_prot): (Box<TInputProtocol>, Box<TOutputProtocol>) = match protocol { + "binary" => { + (Box::new(TBinaryInputProtocol::new(t.clone(), true)), + Box::new(TBinaryOutputProtocol::new(t.clone(), true))) + } + "compact" => { + (Box::new(TCompactInputProtocol::new(t.clone())), + Box::new(TCompactOutputProtocol::new(t.clone()))) + } + unmatched => return Err(format!("unsupported protocol {}", unmatched).into()), + }; + + run_client(service, i_prot, o_prot) +} + +fn run_client(service: &str, + i_prot: Box<TInputProtocol>, + o_prot: Box<TOutputProtocol>) + -> thrift::Result<()> { + match service { + "full" => run_full_meal_service(i_prot, o_prot), + "part" => run_meal_service(i_prot, o_prot), + _ => Err(thrift::Error::from(format!("unknown service type {}", service))), + } +} + +fn open_tcp_transport(host: &str, port: u16) -> thrift::Result<Rc<RefCell<Box<TTransport>>>> { + let mut t = TTcpTransport::new(); + match t.open(&format!("{}:{}", host, port)) { + Ok(()) => Ok(Rc::new(RefCell::new(Box::new(t) as Box<TTransport>))), + Err(e) => Err(e), + } +} + +fn run_meal_service(i_prot: Box<TInputProtocol>, + o_prot: Box<TOutputProtocol>) + -> thrift::Result<()> { + let mut client = MealServiceSyncClient::new(i_prot, o_prot); + + // client.full_meal(); // <-- IMPORTANT: if you uncomment this, compilation *should* fail + // this is because the MealService struct does not contain the appropriate service marker + + // only the following three calls work + execute_call("part", "ramen", || client.ramen(50))?; + execute_call("part", "meal", || client.meal())?; + execute_call("part", "napkin", || client.napkin())?; + + Ok(()) +} + +fn run_full_meal_service(i_prot: Box<TInputProtocol>, + o_prot: Box<TOutputProtocol>) + -> thrift::Result<()> { + let mut client = FullMealServiceSyncClient::new(i_prot, o_prot); + + execute_call("full", "ramen", || client.ramen(100))?; + execute_call("full", "meal", || client.meal())?; + execute_call("full", "napkin", || client.napkin())?; + execute_call("full", "full meal", || client.full_meal())?; + + Ok(()) +} + +fn execute_call<F, R>(service_type: &str, call_name: &str, mut f: F) -> thrift::Result<()> + where F: FnMut() -> thrift::Result<R> +{ + let res = f(); + + match res { + Ok(_) => println!("{}: completed {} call", service_type, call_name), + Err(ref e) => { + println!("{}: failed {} call with error {:?}", + service_type, + call_name, + e) + } + } + + res.map(|_| ()) +} http://git-wip-us.apache.org/repos/asf/thrift/blob/8b96bfbf/lib/rs/test/src/bin/kitchen_sink_server.rs ---------------------------------------------------------------------- diff --git a/lib/rs/test/src/bin/kitchen_sink_server.rs b/lib/rs/test/src/bin/kitchen_sink_server.rs new file mode 100644 index 0000000..4ce4fa3 --- /dev/null +++ b/lib/rs/test/src/bin/kitchen_sink_server.rs @@ -0,0 +1,225 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#[macro_use] +extern crate clap; + +extern crate kitchen_sink; +extern crate thrift; + +use kitchen_sink::base_one::Noodle; +use kitchen_sink::base_two::{Napkin, Ramen, NapkinServiceSyncHandler, RamenServiceSyncHandler}; +use kitchen_sink::midlayer::{Dessert, Meal, MealServiceSyncHandler, MealServiceSyncProcessor}; +use kitchen_sink::ultimate::{Drink, FullMeal, FullMealAndDrinks, + FullMealAndDrinksServiceSyncProcessor, FullMealServiceSyncHandler}; +use kitchen_sink::ultimate::FullMealAndDrinksServiceSyncHandler; +use thrift::protocol::{TBinaryInputProtocolFactory, TBinaryOutputProtocolFactory, + TCompactInputProtocolFactory, TCompactOutputProtocolFactory, + TInputProtocolFactory, TOutputProtocolFactory}; +use thrift::transport::{TFramedTransportFactory, TTransportFactory}; +use thrift::server::TSimpleServer; + +fn main() { + match run() { + Ok(()) => println!("kitchen sink server completed successfully"), + Err(e) => { + println!("kitchen sink server failed with error {:?}", e); + std::process::exit(1); + } + } +} + +fn run() -> thrift::Result<()> { + + let matches = clap_app!(rust_kitchen_sink_server => + (version: "0.1.0") + (author: "Apache Thrift Developers <[email protected]>") + (about: "Thrift Rust kitchen sink test server") + (@arg port: --port +takes_value "port on which the test server listens") + (@arg protocol: --protocol +takes_value "Thrift protocol implementation to use (\"binary\", \"compact\")") + (@arg service: --service +takes_value "Service type to contact (\"part\", \"full\")") + ).get_matches(); + + let port = value_t!(matches, "port", u16).unwrap_or(9090); + let protocol = matches.value_of("protocol").unwrap_or("compact"); + let service = matches.value_of("service").unwrap_or("part"); + let listen_address = format!("127.0.0.1:{}", port); + + println!("binding to {}", listen_address); + + let (i_transport_factory, o_transport_factory): (Box<TTransportFactory>, + Box<TTransportFactory>) = + (Box::new(TFramedTransportFactory {}), Box::new(TFramedTransportFactory {})); + + let (i_protocol_factory, o_protocol_factory): (Box<TInputProtocolFactory>, + Box<TOutputProtocolFactory>) = + match &*protocol { + "binary" => { + (Box::new(TBinaryInputProtocolFactory::new()), + Box::new(TBinaryOutputProtocolFactory::new())) + } + "compact" => { + (Box::new(TCompactInputProtocolFactory::new()), + Box::new(TCompactOutputProtocolFactory::new())) + } + unknown => { + return Err(format!("unsupported transport type {}", unknown).into()); + } + }; + + // FIXME: should processor be boxed as well? + // + // [sigh] I hate Rust generics implementation + // + // I would have preferred to build a server here, return it, and then do + // the common listen-and-handle stuff, but since the server doesn't have a + // common type (because each match arm instantiates a server with a + // different processor) this isn't possible. + // + // Since what I'm doing is uncommon I'm just going to duplicate the code + match &*service { + "part" => { + run_meal_server(&listen_address, + i_transport_factory, + i_protocol_factory, + o_transport_factory, + o_protocol_factory) + } + "full" => { + run_full_meal_server(&listen_address, + i_transport_factory, + i_protocol_factory, + o_transport_factory, + o_protocol_factory) + } + unknown => Err(format!("unsupported service type {}", unknown).into()), + } +} + +fn run_meal_server(listen_address: &str, + i_transport_factory: Box<TTransportFactory>, + i_protocol_factory: Box<TInputProtocolFactory>, + o_transport_factory: Box<TTransportFactory>, + o_protocol_factory: Box<TOutputProtocolFactory>) + -> thrift::Result<()> { + let processor = MealServiceSyncProcessor::new(PartHandler {}); + let mut server = TSimpleServer::new(i_transport_factory, + i_protocol_factory, + o_transport_factory, + o_protocol_factory, + processor); + + server.listen(listen_address) +} + +fn run_full_meal_server(listen_address: &str, + i_transport_factory: Box<TTransportFactory>, + i_protocol_factory: Box<TInputProtocolFactory>, + o_transport_factory: Box<TTransportFactory>, + o_protocol_factory: Box<TOutputProtocolFactory>) + -> thrift::Result<()> { + let processor = FullMealAndDrinksServiceSyncProcessor::new(FullHandler {}); + let mut server = TSimpleServer::new(i_transport_factory, + i_protocol_factory, + o_transport_factory, + o_protocol_factory, + processor); + + server.listen(listen_address) +} + +struct PartHandler; + +impl MealServiceSyncHandler for PartHandler { + fn handle_meal(&mut self) -> thrift::Result<Meal> { + println!("part: handling meal call"); + Ok(meal()) + } +} + +impl RamenServiceSyncHandler for PartHandler { + fn handle_ramen(&mut self, _: i32) -> thrift::Result<Ramen> { + println!("part: handling ramen call"); + Ok(ramen()) + } +} + +impl NapkinServiceSyncHandler for PartHandler { + fn handle_napkin(&mut self) -> thrift::Result<Napkin> { + println!("part: handling napkin call"); + Ok(napkin()) + } +} + +// full service +// + +struct FullHandler; + +impl FullMealAndDrinksServiceSyncHandler for FullHandler { + fn handle_full_meal_and_drinks(&mut self) -> thrift::Result<FullMealAndDrinks> { + Ok(FullMealAndDrinks::new(full_meal(), Drink::WHISKEY)) + } +} + +impl FullMealServiceSyncHandler for FullHandler { + fn handle_full_meal(&mut self) -> thrift::Result<FullMeal> { + println!("full: handling full meal call"); + Ok(full_meal()) + } +} + +impl MealServiceSyncHandler for FullHandler { + fn handle_meal(&mut self) -> thrift::Result<Meal> { + println!("full: handling meal call"); + Ok(meal()) + } +} + +impl RamenServiceSyncHandler for FullHandler { + fn handle_ramen(&mut self, _: i32) -> thrift::Result<Ramen> { + println!("full: handling ramen call"); + Ok(ramen()) + } +} + +impl NapkinServiceSyncHandler for FullHandler { + fn handle_napkin(&mut self) -> thrift::Result<Napkin> { + println!("full: handling napkin call"); + Ok(napkin()) + } +} + +fn full_meal() -> FullMeal { + FullMeal::new(meal(), Dessert::Port("Graham's Tawny".to_owned())) +} + +fn meal() -> Meal { + Meal::new(noodle(), ramen()) +} + +fn noodle() -> Noodle { + Noodle::new("spelt".to_owned(), 100) +} + +fn ramen() -> Ramen { + Ramen::new("Mr Ramen".to_owned(), 72) +} + +fn napkin() -> Napkin { + Napkin {} +} http://git-wip-us.apache.org/repos/asf/thrift/blob/8b96bfbf/lib/rs/test/src/lib.rs ---------------------------------------------------------------------- diff --git a/lib/rs/test/src/lib.rs b/lib/rs/test/src/lib.rs new file mode 100644 index 0000000..8a7ccd0 --- /dev/null +++ b/lib/rs/test/src/lib.rs @@ -0,0 +1,53 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +extern crate ordered_float; +extern crate thrift; +extern crate try_from; + +pub mod base_one; +pub mod base_two; +pub mod midlayer; +pub mod ultimate; + +#[cfg(test)] +mod tests { + + use std::default::Default; + + use super::*; + + #[test] + fn must_be_able_to_use_constructor() { + let _ = midlayer::Meal::new(Some(base_one::Noodle::default()), None); + } + + #[test] + fn must_be_able_to_use_constructor_with_no_fields() { + let _ = midlayer::Meal::new(None, None); + } + + #[test] + fn must_be_able_to_use_constructor_without_option_wrap() { + let _ = midlayer::Meal::new(base_one::Noodle::default(), None); + } + + #[test] + fn must_be_able_to_use_defaults() { + let _ = midlayer::Meal { noodle: Some(base_one::Noodle::default()), ..Default::default() }; + } +} http://git-wip-us.apache.org/repos/asf/thrift/blob/8b96bfbf/lib/rs/test/thrifts/Base_One.thrift ---------------------------------------------------------------------- diff --git a/lib/rs/test/thrifts/Base_One.thrift b/lib/rs/test/thrifts/Base_One.thrift new file mode 100644 index 0000000..ceb1207 --- /dev/null +++ b/lib/rs/test/thrifts/Base_One.thrift @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * Contains some contributions under the Thrift Software License. + * Please see doc/old-thrift-license.txt in the Thrift distribution for + * details. + */ + +typedef i64 Temperature + +typedef i8 Size + +typedef string Location + +const i32 BoilingPoint = 100 + +const list<Temperature> Temperatures = [10, 11, 22, 33] + +const double MealsPerDay = 2.5; + +struct Noodle { + 1: string flourType + 2: Temperature cookTemp +} + +struct Spaghetti { + 1: optional list<Noodle> noodles +} + +const Noodle SpeltNoodle = { "flourType": "spelt", "cookTemp": 110 } + +struct MeasuringSpoon { + 1: Size size +} + +struct Recipe { + 1: string recipeName + 2: string cuisine + 3: i8 page +} + +union CookingTools { + 1: set<MeasuringSpoon> measuringSpoons + 2: map<Size, Location> measuringCups, + 3: list<Recipe> recipes +} + http://git-wip-us.apache.org/repos/asf/thrift/blob/8b96bfbf/lib/rs/test/thrifts/Base_Two.thrift ---------------------------------------------------------------------- diff --git a/lib/rs/test/thrifts/Base_Two.thrift b/lib/rs/test/thrifts/Base_Two.thrift new file mode 100644 index 0000000..b4b4ea1 --- /dev/null +++ b/lib/rs/test/thrifts/Base_Two.thrift @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * Contains some contributions under the Thrift Software License. + * Please see doc/old-thrift-license.txt in the Thrift distribution for + * details. + */ + +const i32 WaterWeight = 200 + +struct Ramen { + 1: optional string ramenType + 2: required i32 noodleCount +} + +struct Napkin { + // empty +} + +service NapkinService { + Napkin napkin() +} + +service RamenService extends NapkinService { + Ramen ramen(1: i32 requestedNoodleCount) +} + +/* const struct CookedRamen = { "bar": 10 } */ + http://git-wip-us.apache.org/repos/asf/thrift/blob/8b96bfbf/lib/rs/test/thrifts/Midlayer.thrift ---------------------------------------------------------------------- diff --git a/lib/rs/test/thrifts/Midlayer.thrift b/lib/rs/test/thrifts/Midlayer.thrift new file mode 100644 index 0000000..cf1157c --- /dev/null +++ b/lib/rs/test/thrifts/Midlayer.thrift @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * Contains some contributions under the Thrift Software License. + * Please see doc/old-thrift-license.txt in the Thrift distribution for + * details. + */ + +include "Base_One.thrift" +include "Base_Two.thrift" + +const i32 WaterBoilingPoint = Base_One.BoilingPoint + +const map<string, Base_One.Temperature> TemperatureNames = { "freezing": 0, "boiling": 100 } + +const map<set<i32>, map<list<string>, string>> MyConstNestedMap = { + [0, 1, 2, 3]: { ["foo"]: "bar" }, + [20]: { ["nut", "ton"] : "bar" }, + [30, 40]: { ["bouncy", "tinkly"]: "castle" } +} + +const list<list<i32>> MyConstNestedList = [ + [0, 1, 2], + [3, 4, 5], + [6, 7, 8] +] + +const set<set<i32>> MyConstNestedSet = [ + [0, 1, 2], + [3, 4, 5], + [6, 7, 8] +] + +struct Meal { + 1: Base_One.Noodle noodle + 2: Base_Two.Ramen ramen +} + +union Dessert { + 1: string port + 2: string iceWine +} + +service MealService extends Base_Two.RamenService { + Meal meal() +} + http://git-wip-us.apache.org/repos/asf/thrift/blob/8b96bfbf/lib/rs/test/thrifts/Ultimate.thrift ---------------------------------------------------------------------- diff --git a/lib/rs/test/thrifts/Ultimate.thrift b/lib/rs/test/thrifts/Ultimate.thrift new file mode 100644 index 0000000..8154d91 --- /dev/null +++ b/lib/rs/test/thrifts/Ultimate.thrift @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * Contains some contributions under the Thrift Software License. + * Please see doc/old-thrift-license.txt in the Thrift distribution for + * details. + */ + +include "Midlayer.thrift" + +enum Drink { + WATER, + WHISKEY, + WINE, +} + +struct FullMeal { + 1: required Midlayer.Meal meal + 2: required Midlayer.Dessert dessert +} + +struct FullMealAndDrinks { + 1: required FullMeal fullMeal + 2: optional Drink drink +} + +service FullMealService extends Midlayer.MealService { + FullMeal fullMeal() +} + +service FullMealAndDrinksService extends FullMealService { + FullMealAndDrinks fullMealAndDrinks() +} + http://git-wip-us.apache.org/repos/asf/thrift/blob/8b96bfbf/test/Makefile.am ---------------------------------------------------------------------- diff --git a/test/Makefile.am b/test/Makefile.am index 51da3ba..01fab4f 100755 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -91,6 +91,11 @@ SUBDIRS += lua PRECROSS_TARGET += precross-lua endif +if WITH_RS +SUBDIRS += rs +PRECROSS_TARGET += precross-rs +endif + # # generate html for ThriftTest.thrift # @@ -117,6 +122,7 @@ EXTRA_DIST = \ py.twisted \ py.tornado \ rb \ + rs \ threads \ AnnotationTest.thrift \ BrokenConstants.thrift \ http://git-wip-us.apache.org/repos/asf/thrift/blob/8b96bfbf/test/rs/Cargo.toml ---------------------------------------------------------------------- diff --git a/test/rs/Cargo.toml b/test/rs/Cargo.toml new file mode 100644 index 0000000..8167390 --- /dev/null +++ b/test/rs/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "thrift-test" +version = "0.1.0" +license = "Apache-2.0" +authors = ["Apache Thrift Developers <[email protected]>"] +publish = false + +[dependencies] +clap = "2.18.0" +ordered-float = "0.3.0" +try_from = "0.2.0" + +[dependencies.thrift] +path = "../../lib/rs" + http://git-wip-us.apache.org/repos/asf/thrift/blob/8b96bfbf/test/rs/Makefile.am ---------------------------------------------------------------------- diff --git a/test/rs/Makefile.am b/test/rs/Makefile.am new file mode 100644 index 0000000..1a409b8 --- /dev/null +++ b/test/rs/Makefile.am @@ -0,0 +1,42 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +THRIFT = $(top_builddir)/compiler/cpp/thrift + +stubs: ../ThriftTest.thrift + $(THRIFT) -I ./thrifts -out src --gen rs ../ThriftTest.thrift + +precross: stubs + $(CARGO) build + [ -d bin ] || mkdir bin + cp target/debug/test_server bin/test_server + cp target/debug/test_client bin/test_client + +clean-local: + $(CARGO) clean + -$(RM) Cargo.lock + -$(RM) src/thrift_test.rs + -$(RM) -r bin + +EXTRA_DIST = \ + Cargo.toml \ + src/lib.rs \ + src/bin/test_server.rs \ + src/bin/test_client.rs + http://git-wip-us.apache.org/repos/asf/thrift/blob/8b96bfbf/test/rs/src/bin/test_client.rs ---------------------------------------------------------------------- diff --git a/test/rs/src/bin/test_client.rs b/test/rs/src/bin/test_client.rs new file mode 100644 index 0000000..a2ea832 --- /dev/null +++ b/test/rs/src/bin/test_client.rs @@ -0,0 +1,500 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#[macro_use] +extern crate clap; +extern crate ordered_float; +extern crate thrift; +extern crate thrift_test; // huh. I have to do this to use my lib + +use ordered_float::OrderedFloat; +use std::cell::RefCell; +use std::collections::{BTreeMap, BTreeSet}; +use std::fmt::Debug; +use std::rc::Rc; + +use thrift::protocol::{TBinaryInputProtocol, TBinaryOutputProtocol, TCompactInputProtocol, + TCompactOutputProtocol, TInputProtocol, TOutputProtocol}; +use thrift::transport::{TBufferedTransport, TFramedTransport, TTcpTransport, TTransport}; +use thrift_test::*; + +fn main() { + match run() { + Ok(()) => println!("cross-test client succeeded"), + Err(e) => { + println!("cross-test client failed with error {:?}", e); + std::process::exit(1); + } + } +} + +fn run() -> thrift::Result<()> { + // unsupported options: + // --domain-socket + // --named-pipe + // --anon-pipes + // --ssl + // --threads + let matches = clap_app!(rust_test_client => + (version: "1.0") + (author: "Apache Thrift Developers <[email protected]>") + (about: "Rust Thrift test client") + (@arg host: --host +takes_value "Host on which the Thrift test server is located") + (@arg port: --port +takes_value "Port on which the Thrift test server is listening") + (@arg transport: --transport +takes_value "Thrift transport implementation to use (\"buffered\", \"framed\")") + (@arg protocol: --protocol +takes_value "Thrift protocol implementation to use (\"binary\", \"compact\")") + (@arg testloops: -n --testloops +takes_value "Number of times to run tests") + ).get_matches(); + + let host = matches.value_of("host").unwrap_or("127.0.0.1"); + let port = value_t!(matches, "port", u16).unwrap_or(9090); + let testloops = value_t!(matches, "testloops", u8).unwrap_or(1); + let transport = matches.value_of("transport").unwrap_or("buffered"); + let protocol = matches.value_of("protocol").unwrap_or("binary"); + + let t = open_tcp_transport(host, port)?; + + let t: Box<TTransport> = match transport { + "buffered" => Box::new(TBufferedTransport::new(t)), + "framed" => Box::new(TFramedTransport::new(t)), + unmatched => return Err(format!("unsupported transport {}", unmatched).into()), + }; + let t = Rc::new(RefCell::new(t)); + + let (i_prot, o_prot): (Box<TInputProtocol>, Box<TOutputProtocol>) = match protocol { + "binary" => { + (Box::new(TBinaryInputProtocol::new(t.clone(), true)), + Box::new(TBinaryOutputProtocol::new(t.clone(), true))) + } + "compact" => { + (Box::new(TCompactInputProtocol::new(t.clone())), + Box::new(TCompactOutputProtocol::new(t.clone()))) + } + unmatched => return Err(format!("unsupported protocol {}", unmatched).into()), + }; + + println!("connecting to {}:{} with {}+{} stack", + host, + port, + protocol, + transport); + + let mut client = ThriftTestSyncClient::new(i_prot, o_prot); + + for _ in 0..testloops { + make_thrift_calls(&mut client)? + } + + Ok(()) +} + +// FIXME: expose "open" through the client interface so I don't have to early open the transport +fn open_tcp_transport(host: &str, port: u16) -> thrift::Result<Rc<RefCell<Box<TTransport>>>> { + let mut t = TTcpTransport::new(); + match t.open(&format!("{}:{}", host, port)) { + Ok(()) => Ok(Rc::new(RefCell::new(Box::new(t) as Box<TTransport>))), + Err(e) => Err(e), + } +} + +fn make_thrift_calls(client: &mut ThriftTestSyncClient) -> Result<(), thrift::Error> { + println!("testVoid"); + client.test_void()?; + + println!("testString"); + verify_expected_result(client.test_string("thing".to_owned()), "thing".to_owned())?; + + println!("testBool"); + verify_expected_result(client.test_bool(true), true)?; + + println!("testBool"); + verify_expected_result(client.test_bool(false), false)?; + + println!("testByte"); + verify_expected_result(client.test_byte(42), 42)?; + + println!("testi32"); + verify_expected_result(client.test_i32(1159348374), 1159348374)?; + + println!("testi64"); + // try!(verify_expected_result(client.test_i64(-8651829879438294565), -8651829879438294565)); + verify_expected_result(client.test_i64(i64::min_value()), i64::min_value())?; + + println!("testDouble"); + verify_expected_result(client.test_double(OrderedFloat::from(42.42)), + OrderedFloat::from(42.42))?; + + println!("testTypedef"); + { + let u_snd: UserId = 2348; + let u_cmp: UserId = 2348; + verify_expected_result(client.test_typedef(u_snd), u_cmp)?; + } + + println!("testEnum"); + { + verify_expected_result(client.test_enum(Numberz::TWO), Numberz::TWO)?; + } + + println!("testBinary"); + { + let b_snd = vec![0x77, 0x30, 0x30, 0x74, 0x21, 0x20, 0x52, 0x75, 0x73, 0x74]; + let b_cmp = vec![0x77, 0x30, 0x30, 0x74, 0x21, 0x20, 0x52, 0x75, 0x73, 0x74]; + verify_expected_result(client.test_binary(b_snd), b_cmp)?; + } + + println!("testStruct"); + { + let x_snd = Xtruct { + string_thing: Some("foo".to_owned()), + byte_thing: Some(12), + i32_thing: Some(219129), + i64_thing: Some(12938492818), + }; + let x_cmp = Xtruct { + string_thing: Some("foo".to_owned()), + byte_thing: Some(12), + i32_thing: Some(219129), + i64_thing: Some(12938492818), + }; + verify_expected_result(client.test_struct(x_snd), x_cmp)?; + } + + // Xtruct again, with optional values + // FIXME: apparently the erlang thrift server does not like opt-in-req-out parameters that are undefined. Joy. + // { + // let x_snd = Xtruct { string_thing: Some("foo".to_owned()), byte_thing: None, i32_thing: None, i64_thing: Some(12938492818) }; + // let x_cmp = Xtruct { string_thing: Some("foo".to_owned()), byte_thing: Some(0), i32_thing: Some(0), i64_thing: Some(12938492818) }; // the C++ server is responding correctly + // try!(verify_expected_result(client.test_struct(x_snd), x_cmp)); + // } + // + + + println!("testNest"); // (FIXME: try Xtruct2 with optional values) + { + let x_snd = Xtruct2 { + byte_thing: Some(32), + struct_thing: Some(Xtruct { + string_thing: Some("foo".to_owned()), + byte_thing: Some(1), + i32_thing: Some(324382098), + i64_thing: Some(12938492818), + }), + i32_thing: Some(293481098), + }; + let x_cmp = Xtruct2 { + byte_thing: Some(32), + struct_thing: Some(Xtruct { + string_thing: Some("foo".to_owned()), + byte_thing: Some(1), + i32_thing: Some(324382098), + i64_thing: Some(12938492818), + }), + i32_thing: Some(293481098), + }; + verify_expected_result(client.test_nest(x_snd), x_cmp)?; + } + + println!("testList"); + { + let mut v_snd: Vec<i32> = Vec::new(); + v_snd.push(29384); + v_snd.push(238); + v_snd.push(32498); + + let mut v_cmp: Vec<i32> = Vec::new(); + v_cmp.push(29384); + v_cmp.push(238); + v_cmp.push(32498); + + verify_expected_result(client.test_list(v_snd), v_cmp)?; + } + + println!("testSet"); + { + let mut s_snd: BTreeSet<i32> = BTreeSet::new(); + s_snd.insert(293481); + s_snd.insert(23); + s_snd.insert(3234); + + let mut s_cmp: BTreeSet<i32> = BTreeSet::new(); + s_cmp.insert(293481); + s_cmp.insert(23); + s_cmp.insert(3234); + + verify_expected_result(client.test_set(s_snd), s_cmp)?; + } + + println!("testMap"); + { + let mut m_snd: BTreeMap<i32, i32> = BTreeMap::new(); + m_snd.insert(2, 4); + m_snd.insert(4, 6); + m_snd.insert(8, 7); + + let mut m_cmp: BTreeMap<i32, i32> = BTreeMap::new(); + m_cmp.insert(2, 4); + m_cmp.insert(4, 6); + m_cmp.insert(8, 7); + + verify_expected_result(client.test_map(m_snd), m_cmp)?; + } + + println!("testStringMap"); + { + let mut m_snd: BTreeMap<String, String> = BTreeMap::new(); + m_snd.insert("2".to_owned(), "4_string".to_owned()); + m_snd.insert("4".to_owned(), "6_string".to_owned()); + m_snd.insert("8".to_owned(), "7_string".to_owned()); + + let mut m_rcv: BTreeMap<String, String> = BTreeMap::new(); + m_rcv.insert("2".to_owned(), "4_string".to_owned()); + m_rcv.insert("4".to_owned(), "6_string".to_owned()); + m_rcv.insert("8".to_owned(), "7_string".to_owned()); + + verify_expected_result(client.test_string_map(m_snd), m_rcv)?; + } + + // nested map + // expect : {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2 => 2, 3 => 3, 4 => 4, }, } + println!("testMapMap"); + { + let mut m_cmp_nested_0: BTreeMap<i32, i32> = BTreeMap::new(); + for i in (-4 as i32)..0 { + m_cmp_nested_0.insert(i, i); + } + let mut m_cmp_nested_1: BTreeMap<i32, i32> = BTreeMap::new(); + for i in 1..5 { + m_cmp_nested_1.insert(i, i); + } + + let mut m_cmp: BTreeMap<i32, BTreeMap<i32, i32>> = BTreeMap::new(); + m_cmp.insert(-4, m_cmp_nested_0); + m_cmp.insert(4, m_cmp_nested_1); + + verify_expected_result(client.test_map_map(42), m_cmp)?; + } + + println!("testMulti"); + { + let mut m_snd: BTreeMap<i16, String> = BTreeMap::new(); + m_snd.insert(1298, "fizz".to_owned()); + m_snd.insert(-148, "buzz".to_owned()); + + let s_cmp = Xtruct { + string_thing: Some("Hello2".to_owned()), + byte_thing: Some(1), + i32_thing: Some(-123948), + i64_thing: Some(-19234123981), + }; + + verify_expected_result(client.test_multi(1, + -123948, + -19234123981, + m_snd, + Numberz::EIGHT, + 81), + s_cmp)?; + } + + // Insanity + // returns: + // { 1 => { 2 => argument, + // 3 => argument, + // }, + // 2 => { 6 => <empty Insanity struct>, }, + // } + { + let mut arg_map_usermap: BTreeMap<Numberz, i64> = BTreeMap::new(); + arg_map_usermap.insert(Numberz::ONE, 4289); + arg_map_usermap.insert(Numberz::EIGHT, 19); + + let mut arg_vec_xtructs: Vec<Xtruct> = Vec::new(); + arg_vec_xtructs.push(Xtruct { + string_thing: Some("foo".to_owned()), + byte_thing: Some(8), + i32_thing: Some(29), + i64_thing: Some(92384), + }); + arg_vec_xtructs.push(Xtruct { + string_thing: Some("bar".to_owned()), + byte_thing: Some(28), + i32_thing: Some(2), + i64_thing: Some(-1281), + }); + arg_vec_xtructs.push(Xtruct { + string_thing: Some("baz".to_owned()), + byte_thing: Some(0), + i32_thing: Some(3948539), + i64_thing: Some(-12938492), + }); + + let mut s_cmp_nested_1: BTreeMap<Numberz, Insanity> = BTreeMap::new(); + let insanity = Insanity { + user_map: Some(arg_map_usermap), + xtructs: Some(arg_vec_xtructs), + }; + s_cmp_nested_1.insert(Numberz::TWO, insanity.clone()); + s_cmp_nested_1.insert(Numberz::THREE, insanity.clone()); + + let mut s_cmp_nested_2: BTreeMap<Numberz, Insanity> = BTreeMap::new(); + let empty_insanity = Insanity { + user_map: Some(BTreeMap::new()), + xtructs: Some(Vec::new()), + }; + s_cmp_nested_2.insert(Numberz::SIX, empty_insanity); + + let mut s_cmp: BTreeMap<UserId, BTreeMap<Numberz, Insanity>> = BTreeMap::new(); + s_cmp.insert(1 as UserId, s_cmp_nested_1); + s_cmp.insert(2 as UserId, s_cmp_nested_2); + + verify_expected_result(client.test_insanity(insanity.clone()), s_cmp)?; + } + + println!("testException - remote throws Xception"); + { + let r = client.test_exception("Xception".to_owned()); + let x = match r { + Err(thrift::Error::User(ref e)) => { + match e.downcast_ref::<Xception>() { + Some(x) => Ok(x), + None => Err(thrift::Error::User("did not get expected Xception struct".into())), + } + } + _ => Err(thrift::Error::User("did not get exception".into())), + }?; + + let x_cmp = Xception { + error_code: Some(1001), + message: Some("Xception".to_owned()), + }; + + verify_expected_result(Ok(x), &x_cmp)?; + } + + println!("testException - remote throws TApplicationException"); + { + let r = client.test_exception("TException".to_owned()); + match r { + Err(thrift::Error::Application(ref e)) => { + println!("received an {:?}", e); + Ok(()) + } + _ => Err(thrift::Error::User("did not get exception".into())), + }?; + } + + println!("testException - remote succeeds"); + { + let r = client.test_exception("foo".to_owned()); + match r { + Ok(_) => Ok(()), + _ => Err(thrift::Error::User("received an exception".into())), + }?; + } + + println!("testMultiException - remote throws Xception"); + { + let r = client.test_multi_exception("Xception".to_owned(), "ignored".to_owned()); + let x = match r { + Err(thrift::Error::User(ref e)) => { + match e.downcast_ref::<Xception>() { + Some(x) => Ok(x), + None => Err(thrift::Error::User("did not get expected Xception struct".into())), + } + } + _ => Err(thrift::Error::User("did not get exception".into())), + }?; + + let x_cmp = Xception { + error_code: Some(1001), + message: Some("This is an Xception".to_owned()), + }; + + verify_expected_result(Ok(x), &x_cmp)?; + } + + println!("testMultiException - remote throws Xception2"); + { + let r = client.test_multi_exception("Xception2".to_owned(), "ignored".to_owned()); + let x = match r { + Err(thrift::Error::User(ref e)) => { + match e.downcast_ref::<Xception2>() { + Some(x) => Ok(x), + None => Err(thrift::Error::User("did not get expected Xception struct".into())), + } + } + _ => Err(thrift::Error::User("did not get exception".into())), + }?; + + let x_cmp = Xception2 { + error_code: Some(2002), + struct_thing: Some(Xtruct { + string_thing: Some("This is an Xception2".to_owned()), + byte_thing: Some(0), /* since this is an OPT_IN_REQ_OUT field the sender sets a default */ + i32_thing: Some(0), /* since this is an OPT_IN_REQ_OUT field the sender sets a default */ + i64_thing: Some(0), /* since this is an OPT_IN_REQ_OUT field the sender sets a default */ + }), + }; + + verify_expected_result(Ok(x), &x_cmp)?; + } + + println!("testMultiException - remote succeeds"); + { + let r = client.test_multi_exception("haha".to_owned(), "RETURNED".to_owned()); + let x = match r { + Err(e) => { + Err(thrift::Error::User(format!("received an unexpected exception {:?}", e).into())) + } + _ => r, + }?; + + let x_cmp = Xtruct { + string_thing: Some("RETURNED".to_owned()), + byte_thing: Some(0), // since this is an OPT_IN_REQ_OUT field the sender sets a default + i32_thing: Some(0), // since this is an OPT_IN_REQ_OUT field the sender sets a default + i64_thing: Some(0), // since this is an OPT_IN_REQ_OUT field the sender sets a default + }; + + verify_expected_result(Ok(x), x_cmp)?; + } + + println!("testOneWay - remote sleeps for 1 second"); + { + client.test_oneway(1)?; + } + + // final test to verify that the connection is still writable after the one-way call + client.test_void() +} + +fn verify_expected_result<T: Debug + PartialEq + Sized>(actual: Result<T, thrift::Error>, + expected: T) + -> Result<(), thrift::Error> { + match actual { + Ok(v) => { + if v == expected { + Ok(()) + } else { + Err(thrift::Error::User(format!("expected {:?} but got {:?}", &expected, &v) + .into())) + } + } + Err(e) => Err(e), + } +} http://git-wip-us.apache.org/repos/asf/thrift/blob/8b96bfbf/test/rs/src/bin/test_server.rs ---------------------------------------------------------------------- diff --git a/test/rs/src/bin/test_server.rs b/test/rs/src/bin/test_server.rs new file mode 100644 index 0000000..613cd55 --- /dev/null +++ b/test/rs/src/bin/test_server.rs @@ -0,0 +1,337 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#[macro_use] +extern crate clap; +extern crate ordered_float; +extern crate thrift; +extern crate thrift_test; + +use ordered_float::OrderedFloat; +use std::collections::{BTreeMap, BTreeSet}; +use std::thread; +use std::time::Duration; + +use thrift::protocol::{TBinaryInputProtocolFactory, TBinaryOutputProtocolFactory, + TCompactInputProtocolFactory, TCompactOutputProtocolFactory, + TInputProtocolFactory, TOutputProtocolFactory}; +use thrift::server::TSimpleServer; +use thrift::transport::{TBufferedTransportFactory, TFramedTransportFactory, TTransportFactory}; +use thrift_test::*; + +fn main() { + match run() { + Ok(()) => println!("cross-test server succeeded"), + Err(e) => { + println!("cross-test server failed with error {:?}", e); + std::process::exit(1); + } + } +} + +fn run() -> thrift::Result<()> { + + // unsupported options: + // --domain-socket + // --named-pipe + // --ssl + // --workers + let matches = clap_app!(rust_test_client => + (version: "1.0") + (author: "Apache Thrift Developers <[email protected]>") + (about: "Rust Thrift test server") + (@arg port: --port +takes_value "port on which the test server listens") + (@arg transport: --transport +takes_value "transport implementation to use (\"buffered\", \"framed\")") + (@arg protocol: --protocol +takes_value "protocol implementation to use (\"binary\", \"compact\")") + (@arg server_type: --server_type +takes_value "type of server instantiated (\"simple\", \"thread-pool\", \"threaded\", \"non-blocking\")") + ).get_matches(); + + let port = value_t!(matches, "port", u16).unwrap_or(9090); + let transport = matches.value_of("transport").unwrap_or("buffered"); + let protocol = matches.value_of("protocol").unwrap_or("binary"); + let server_type = matches.value_of("server_type").unwrap_or("simple"); + let listen_address = format!("127.0.0.1:{}", port); + + println!("binding to {}", listen_address); + + let (i_transport_factory, o_transport_factory): (Box<TTransportFactory>, + Box<TTransportFactory>) = match &*transport { + "buffered" => { + (Box::new(TBufferedTransportFactory::new()), Box::new(TBufferedTransportFactory::new())) + } + "framed" => { + (Box::new(TFramedTransportFactory::new()), Box::new(TFramedTransportFactory::new())) + } + unknown => { + return Err(format!("unsupported transport type {}", unknown).into()); + } + }; + + let (i_protocol_factory, o_protocol_factory): (Box<TInputProtocolFactory>, + Box<TOutputProtocolFactory>) = + match &*protocol { + "binary" => { + (Box::new(TBinaryInputProtocolFactory::new()), + Box::new(TBinaryOutputProtocolFactory::new())) + } + "compact" => { + (Box::new(TCompactInputProtocolFactory::new()), + Box::new(TCompactOutputProtocolFactory::new())) + } + unknown => { + return Err(format!("unsupported transport type {}", unknown).into()); + } + }; + + let processor = ThriftTestSyncProcessor::new(ThriftTestSyncHandlerImpl {}); + + let mut server = match &*server_type { + "simple" => { + TSimpleServer::new(i_transport_factory, + i_protocol_factory, + o_transport_factory, + o_protocol_factory, + processor) + } + unknown => { + return Err(format!("unsupported server type {}", unknown).into()); + } + }; + + server.listen(&listen_address) +} + +struct ThriftTestSyncHandlerImpl; +impl ThriftTestSyncHandler for ThriftTestSyncHandlerImpl { + fn handle_test_void(&mut self) -> thrift::Result<()> { + println!("testVoid()"); + Ok(()) + } + + fn handle_test_string(&mut self, thing: String) -> thrift::Result<String> { + println!("testString({})", &thing); + Ok(thing) + } + + fn handle_test_bool(&mut self, thing: bool) -> thrift::Result<bool> { + println!("testBool({})", thing); + Ok(thing) + } + + fn handle_test_byte(&mut self, thing: i8) -> thrift::Result<i8> { + println!("testByte({})", thing); + Ok(thing) + } + + fn handle_test_i32(&mut self, thing: i32) -> thrift::Result<i32> { + println!("testi32({})", thing); + Ok(thing) + } + + fn handle_test_i64(&mut self, thing: i64) -> thrift::Result<i64> { + println!("testi64({})", thing); + Ok(thing) + } + + fn handle_test_double(&mut self, + thing: OrderedFloat<f64>) + -> thrift::Result<OrderedFloat<f64>> { + println!("testDouble({})", thing); + Ok(thing) + } + + fn handle_test_binary(&mut self, thing: Vec<u8>) -> thrift::Result<Vec<u8>> { + println!("testBinary({:?})", thing); + Ok(thing) + } + + fn handle_test_struct(&mut self, thing: Xtruct) -> thrift::Result<Xtruct> { + println!("testStruct({:?})", thing); + Ok(thing) + } + + fn handle_test_nest(&mut self, thing: Xtruct2) -> thrift::Result<Xtruct2> { + println!("testNest({:?})", thing); + Ok(thing) + } + + fn handle_test_map(&mut self, thing: BTreeMap<i32, i32>) -> thrift::Result<BTreeMap<i32, i32>> { + println!("testMap({:?})", thing); + Ok(thing) + } + + fn handle_test_string_map(&mut self, + thing: BTreeMap<String, String>) + -> thrift::Result<BTreeMap<String, String>> { + println!("testStringMap({:?})", thing); + Ok(thing) + } + + fn handle_test_set(&mut self, thing: BTreeSet<i32>) -> thrift::Result<BTreeSet<i32>> { + println!("testSet({:?})", thing); + Ok(thing) + } + + fn handle_test_list(&mut self, thing: Vec<i32>) -> thrift::Result<Vec<i32>> { + println!("testList({:?})", thing); + Ok(thing) + } + + fn handle_test_enum(&mut self, thing: Numberz) -> thrift::Result<Numberz> { + println!("testEnum({:?})", thing); + Ok(thing) + } + + fn handle_test_typedef(&mut self, thing: UserId) -> thrift::Result<UserId> { + println!("testTypedef({})", thing); + Ok(thing) + } + + /// @return map<i32,map<i32,i32>> - returns a dictionary with these values: + /// {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2 => 2, 3 => 3, 4 => 4, }, } + fn handle_test_map_map(&mut self, + hello: i32) + -> thrift::Result<BTreeMap<i32, BTreeMap<i32, i32>>> { + println!("testMapMap({})", hello); + + let mut inner_map_0: BTreeMap<i32, i32> = BTreeMap::new(); + for i in -4..(0 as i32) { + inner_map_0.insert(i, i); + } + + let mut inner_map_1: BTreeMap<i32, i32> = BTreeMap::new(); + for i in 1..5 { + inner_map_1.insert(i, i); + } + + let mut ret_map: BTreeMap<i32, BTreeMap<i32, i32>> = BTreeMap::new(); + ret_map.insert(-4, inner_map_0); + ret_map.insert(4, inner_map_1); + + Ok(ret_map) + } + + /// Creates a the returned map with these values and prints it out: + /// { 1 => { 2 => argument, + /// 3 => argument, + /// }, + /// 2 => { 6 => <empty Insanity struct>, }, + /// } + /// return map<UserId, map<Numberz,Insanity>> - a map with the above values + fn handle_test_insanity(&mut self, + argument: Insanity) + -> thrift::Result<BTreeMap<UserId, BTreeMap<Numberz, Insanity>>> { + println!("testInsanity({:?})", argument); + let mut map_0: BTreeMap<Numberz, Insanity> = BTreeMap::new(); + map_0.insert(Numberz::TWO, argument.clone()); + map_0.insert(Numberz::THREE, argument.clone()); + + let mut map_1: BTreeMap<Numberz, Insanity> = BTreeMap::new(); + let insanity = Insanity { + user_map: None, + xtructs: None, + }; + map_1.insert(Numberz::SIX, insanity); + + let mut ret: BTreeMap<UserId, BTreeMap<Numberz, Insanity>> = BTreeMap::new(); + ret.insert(1, map_0); + ret.insert(2, map_1); + + Ok(ret) + } + + /// returns an Xtruct with string_thing = "Hello2", byte_thing = arg0, i32_thing = arg1 and i64_thing = arg2 + fn handle_test_multi(&mut self, + arg0: i8, + arg1: i32, + arg2: i64, + _: BTreeMap<i16, String>, + _: Numberz, + _: UserId) + -> thrift::Result<Xtruct> { + let x_ret = Xtruct { + string_thing: Some("Hello2".to_owned()), + byte_thing: Some(arg0), + i32_thing: Some(arg1), + i64_thing: Some(arg2), + }; + + Ok(x_ret) + } + + /// if arg == "Xception" throw Xception with errorCode = 1001 and message = arg + /// else if arg == "TException" throw TException + /// else do not throw anything + fn handle_test_exception(&mut self, arg: String) -> thrift::Result<()> { + println!("testException({})", arg); + + match &*arg { + "Xception" => { + Err((Xception { + error_code: Some(1001), + message: Some(arg), + }) + .into()) + } + "TException" => Err("this is a random error".into()), + _ => Ok(()), + } + } + + /// if arg0 == "Xception" throw Xception with errorCode = 1001 and message = "This is an Xception" + /// else if arg0 == "Xception2" throw Xception2 with errorCode = 2002 and struct_thing.string_thing = "This is an Xception2" + // else do not throw anything and return Xtruct with string_thing = arg1 + fn handle_test_multi_exception(&mut self, + arg0: String, + arg1: String) + -> thrift::Result<Xtruct> { + match &*arg0 { + "Xception" => { + Err((Xception { + error_code: Some(1001), + message: Some("This is an Xception".to_owned()), + }) + .into()) + } + "Xception2" => { + Err((Xception2 { + error_code: Some(2002), + struct_thing: Some(Xtruct { + string_thing: Some("This is an Xception2".to_owned()), + byte_thing: None, + i32_thing: None, + i64_thing: None, + }), + }) + .into()) + } + _ => { + Ok(Xtruct { + string_thing: Some(arg1), + byte_thing: None, + i32_thing: None, + i64_thing: None, + }) + } + } + } + + fn handle_test_oneway(&mut self, seconds_to_sleep: i32) -> thrift::Result<()> { + thread::sleep(Duration::from_secs(seconds_to_sleep as u64)); + Ok(()) + } +} http://git-wip-us.apache.org/repos/asf/thrift/blob/8b96bfbf/test/rs/src/lib.rs ---------------------------------------------------------------------- diff --git a/test/rs/src/lib.rs b/test/rs/src/lib.rs new file mode 100644 index 0000000..479bf90 --- /dev/null +++ b/test/rs/src/lib.rs @@ -0,0 +1,23 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +extern crate ordered_float; +extern crate thrift; +extern crate try_from; + +mod thrift_test; +pub use thrift_test::*; http://git-wip-us.apache.org/repos/asf/thrift/blob/8b96bfbf/test/tests.json ---------------------------------------------------------------------- diff --git a/test/tests.json b/test/tests.json index b101bfd..09d4c89 100644 --- a/test/tests.json +++ b/test/tests.json @@ -574,5 +574,31 @@ ] }, "workdir": "lua" + }, + { + "name": "rs", + "server": { + "command": [ + "test_server" + ] + }, + "client": { + "timeout": 6, + "command": [ + "test_client" + ] + }, + "transports": [ + "buffered", + "framed" + ], + "sockets": [ + "ip" + ], + "protocols": [ + "binary", + "compact" + ], + "workdir": "rs/bin" } ] http://git-wip-us.apache.org/repos/asf/thrift/blob/8b96bfbf/tutorial/Makefile.am ---------------------------------------------------------------------- diff --git a/tutorial/Makefile.am b/tutorial/Makefile.am index efa314a..d8ad09c 100755 --- a/tutorial/Makefile.am +++ b/tutorial/Makefile.am @@ -74,6 +74,10 @@ if WITH_DART SUBDIRS += dart endif +if WITH_RS +SUBDIRS += rs +endif + # # generate html for ThriftTest.thrift # http://git-wip-us.apache.org/repos/asf/thrift/blob/8b96bfbf/tutorial/rs/Cargo.toml ---------------------------------------------------------------------- diff --git a/tutorial/rs/Cargo.toml b/tutorial/rs/Cargo.toml new file mode 100644 index 0000000..9075db7 --- /dev/null +++ b/tutorial/rs/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "thrift-tutorial" +version = "0.1.0" +license = "Apache-2.0" +authors = ["Apache Thrift Developers <[email protected]>"] +exclude = ["Makefile*", "shared.rs", "tutorial.rs"] +publish = false + +[dependencies] +clap = "2.18.0" +ordered-float = "0.3.0" +try_from = "0.2.0" + +[dependencies.thrift] +path = "../../lib/rs" + http://git-wip-us.apache.org/repos/asf/thrift/blob/8b96bfbf/tutorial/rs/Makefile.am ---------------------------------------------------------------------- diff --git a/tutorial/rs/Makefile.am b/tutorial/rs/Makefile.am new file mode 100644 index 0000000..666331e --- /dev/null +++ b/tutorial/rs/Makefile.am @@ -0,0 +1,52 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +THRIFT = $(top_builddir)/compiler/cpp/thrift + +gen-rs/tutorial.rs gen-rs/shared.rs: $(top_srcdir)/tutorial/tutorial.thrift + $(THRIFT) -out src --gen rs -r $< + +all-local: gen-rs/tutorial.rs + $(CARGO) build + [ -d bin ] || mkdir bin + cp target/debug/tutorial_server bin/tutorial_server + cp target/debug/tutorial_client bin/tutorial_client + +check: all + +tutorialserver: all + bin/tutorial_server + +tutorialclient: all + bin/tutorial_client + +clean-local: + $(CARGO) clean + -$(RM) Cargo.lock + -$(RM) src/shared.rs + -$(RM) src/tutorial.rs + -$(RM) -r bin + +EXTRA_DIST = \ + Cargo.toml \ + src/lib.rs \ + src/bin/tutorial_server.rs \ + src/bin/tutorial_client.rs \ + README.md +
