|
| 1 | +/* Copyright 2023-present University of Tuebingen, Chair of Communication Networks |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +//! Thrift client helpers |
| 17 | +//! |
| 18 | +//! This module provides convenience functions for connecting to Thrift services. |
| 19 | +//! |
| 20 | +//! # Example - Using Timestamp Service |
| 21 | +//! ```no_run |
| 22 | +//! use rbfrt::thrift_client; |
| 23 | +//! use rbfrt::thrift_generated::ts::{TsSyncClient, TTsSyncClient}; |
| 24 | +//! |
| 25 | +//! # fn example() -> Result<(), Box<dyn std::error::Error>> { |
| 26 | +//! // Create Thrift client with all protocol setup done |
| 27 | +//! let mut ts_client: TsSyncClient<_, _> = thrift_client::connect("localhost:9090")?; |
| 28 | +//! |
| 29 | +//! // Use ALL Thrift APIs directly |
| 30 | +//! ts_client.ts_global_ts_value_set(0, 1_000_000_000)?; |
| 31 | +//! # Ok(()) |
| 32 | +//! # } |
| 33 | +//! ``` |
| 34 | +
|
| 35 | +use thrift::protocol::{TBinaryInputProtocol, TBinaryOutputProtocol}; |
| 36 | +use thrift::transport::{ |
| 37 | + ReadHalf, TFramedReadTransport, TFramedWriteTransport, TIoChannel, TTcpChannel, WriteHalf, |
| 38 | +}; |
| 39 | + |
| 40 | +use crate::error::RBFRTError; |
| 41 | + |
| 42 | +/// Type alias for Thrift input protocol |
| 43 | +pub type ThriftInputProtocol = TBinaryInputProtocol<TFramedReadTransport<ReadHalf<TTcpChannel>>>; |
| 44 | + |
| 45 | +/// Type alias for Thrift output protocol |
| 46 | +pub type ThriftOutputProtocol = |
| 47 | + TBinaryOutputProtocol<TFramedWriteTransport<WriteHalf<TTcpChannel>>>; |
| 48 | + |
| 49 | +/// Connect to a Thrift service and return protocol layers |
| 50 | +/// |
| 51 | +/// This function handles all the boilerplate of creating a TCP connection |
| 52 | +/// and setting up the Thrift protocol layers. |
| 53 | +/// |
| 54 | +/// # Arguments |
| 55 | +/// * `address` - Server address in format "host:port" (e.g., "localhost:9090") |
| 56 | +/// |
| 57 | +/// # Returns |
| 58 | +/// Returns a tuple of (InputProtocol, OutputProtocol) that can be passed to any Thrift client's new() method |
| 59 | +/// |
| 60 | +/// # Example |
| 61 | +/// ```no_run |
| 62 | +/// use rbfrt::thrift_client; |
| 63 | +/// use rbfrt::thrift_generated::ts::{TsSyncClient, TTsSyncClient}; |
| 64 | +/// |
| 65 | +/// # fn example() -> Result<(), Box<dyn std::error::Error>> { |
| 66 | +/// // Create timestamp client |
| 67 | +/// let (i_prot, o_prot) = thrift_client::connect("localhost:9090")?; |
| 68 | +/// let mut ts_client = TsSyncClient::new(i_prot, o_prot); |
| 69 | +/// ts_client.ts_global_ts_value_set(0, 1_000_000_000)?; |
| 70 | +/// |
| 71 | +/// // Create port manager client |
| 72 | +/// use rbfrt::thrift_generated::port_mgr::PortMgrSyncClient; |
| 73 | +/// let (i_prot, o_prot) = thrift_client::connect("localhost:9090")?; |
| 74 | +/// let mut port_client = PortMgrSyncClient::new(i_prot, o_prot); |
| 75 | +/// port_client.port_mgr_mtu_set(0, 128, 9000, 9000)?; |
| 76 | +/// # Ok(()) |
| 77 | +/// # } |
| 78 | +/// ``` |
| 79 | +pub fn connect(address: &str) -> Result<(ThriftInputProtocol, ThriftOutputProtocol), RBFRTError> { |
| 80 | + // Create TCP connection |
| 81 | + let mut channel = TTcpChannel::new(); |
| 82 | + channel |
| 83 | + .open(address) |
| 84 | + .map_err(|e| RBFRTError::GenericError { |
| 85 | + message: format!("Thrift connection to {} failed: {}", address, e), |
| 86 | + })?; |
| 87 | + |
| 88 | + // Split channel for bidirectional communication |
| 89 | + let (i_chan, o_chan) = channel.split().map_err(|e| RBFRTError::GenericError { |
| 90 | + message: format!("Failed to split channel: {}", e), |
| 91 | + })?; |
| 92 | + |
| 93 | + // Create protocol layers |
| 94 | + let i_prot = TBinaryInputProtocol::new(TFramedReadTransport::new(i_chan), true); |
| 95 | + let o_prot = TBinaryOutputProtocol::new(TFramedWriteTransport::new(o_chan), true); |
| 96 | + |
| 97 | + Ok((i_prot, o_prot)) |
| 98 | +} |
| 99 | + |
| 100 | +#[cfg(test)] |
| 101 | +mod tests { |
| 102 | + use super::*; |
| 103 | + use crate::thrift_generated::ts::TsSyncClient; |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn test_connect_signature() { |
| 107 | + // This test ensures the types compile correctly |
| 108 | + // Actual connection would require a running Thrift server |
| 109 | + fn _example() -> Result<(), RBFRTError> { |
| 110 | + let (i_prot, o_prot) = connect("localhost:9090")?; |
| 111 | + let _client = TsSyncClient::new(i_prot, o_prot); |
| 112 | + Ok(()) |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments