Struct thrift::server::TServer

source ·
pub struct TServer<PRC, RTF, IPF, WTF, OPF>where
    PRC: TProcessor + Send + Sync + 'static,
    RTF: TReadTransportFactory + 'static,
    IPF: TInputProtocolFactory + 'static,
    WTF: TWriteTransportFactory + 'static,
    OPF: TOutputProtocolFactory + 'static,{ /* private fields */ }
Expand description

Fixed-size thread-pool blocking Thrift server.

A TServer listens on a given address and submits accepted connections to an unbounded queue. Connections from this queue are serviced by the first available worker thread from a fixed-size thread pool. Each accepted connection is handled by that worker thread, and communication over this thread occurs sequentially and synchronously (i.e. calls block). Accepted connections have an input half and an output half, each of which uses a TTransport and TInputProtocol/TOutputProtocol to translate messages to and from byes. Any combination of TInputProtocol, TOutputProtocol and TTransport may be used.

Examples

Creating and running a TServer using Thrift-compiler-generated service code.

use thrift::protocol::{TInputProtocolFactory, TOutputProtocolFactory};
use thrift::protocol::{TBinaryInputProtocolFactory, TBinaryOutputProtocolFactory};
use thrift::protocol::{TInputProtocol, TOutputProtocol};
use thrift::transport::{TBufferedReadTransportFactory, TBufferedWriteTransportFactory,
                        TReadTransportFactory, TWriteTransportFactory};
use thrift::server::{TProcessor, TServer};

//
// auto-generated
//

// processor for `SimpleService`
struct SimpleServiceSyncProcessor;
impl SimpleServiceSyncProcessor {
    fn new<H: SimpleServiceSyncHandler>(processor: H) -> SimpleServiceSyncProcessor {
        unimplemented!();
    }
}

// `TProcessor` implementation for `SimpleService`
impl TProcessor for SimpleServiceSyncProcessor {
    fn process(&self, i: &mut dyn TInputProtocol, o: &mut dyn TOutputProtocol) -> thrift::Result<()> {
        unimplemented!();
    }
}

// service functions for SimpleService
trait SimpleServiceSyncHandler {
    fn service_call(&self) -> thrift::Result<()>;
}

//
// user-code follows
//

// define a handler that will be invoked when `service_call` is received
struct SimpleServiceHandlerImpl;
impl SimpleServiceSyncHandler for SimpleServiceHandlerImpl {
    fn service_call(&self) -> thrift::Result<()> {
        unimplemented!();
    }
}

// instantiate the processor
let processor = SimpleServiceSyncProcessor::new(SimpleServiceHandlerImpl {});

// instantiate the server
let i_tr_fact: Box<dyn TReadTransportFactory> = Box::new(TBufferedReadTransportFactory::new());
let i_pr_fact: Box<dyn TInputProtocolFactory> = Box::new(TBinaryInputProtocolFactory::new());
let o_tr_fact: Box<dyn TWriteTransportFactory> = Box::new(TBufferedWriteTransportFactory::new());
let o_pr_fact: Box<dyn TOutputProtocolFactory> = Box::new(TBinaryOutputProtocolFactory::new());

let mut server = TServer::new(
    i_tr_fact,
    i_pr_fact,
    o_tr_fact,
    o_pr_fact,
    processor,
    10
);

// start listening for incoming connections
match server.listen("127.0.0.1:8080") {
  Ok(_)  => println!("listen completed"),
  Err(e) => println!("listen failed with error {:?}", e),
}

Implementations§

source§

impl<PRC, RTF, IPF, WTF, OPF> TServer<PRC, RTF, IPF, WTF, OPF>where PRC: TProcessor + Send + Sync + 'static, RTF: TReadTransportFactory + 'static, IPF: TInputProtocolFactory + 'static, WTF: TWriteTransportFactory + 'static, OPF: TOutputProtocolFactory + 'static,

source

pub fn new( read_transport_factory: RTF, input_protocol_factory: IPF, write_transport_factory: WTF, output_protocol_factory: OPF, processor: PRC, num_workers: usize ) -> TServer<PRC, RTF, IPF, WTF, OPF>

Create a TServer.

Each accepted connection has an input and output half, each of which requires a TTransport and TProtocol. TServer uses read_transport_factory and input_protocol_factory to create implementations for the input, and write_transport_factory and output_protocol_factory to create implementations for the output.

source

pub fn listen<A: ToSocketAddrs>(&mut self, listen_address: A) -> Result<()>

Listen for incoming connections on listen_address.

listen_address should implement ToSocketAddrs trait.

Return () if successful.

Return Err when the server cannot bind to listen_address or there is an unrecoverable error.

Trait Implementations§

source§

impl<PRC, RTF, IPF, WTF, OPF> Debug for TServer<PRC, RTF, IPF, WTF, OPF>where PRC: TProcessor + Send + Sync + 'static + Debug, RTF: TReadTransportFactory + 'static + Debug, IPF: TInputProtocolFactory + 'static + Debug, WTF: TWriteTransportFactory + 'static + Debug, OPF: TOutputProtocolFactory + 'static + Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<PRC, RTF, IPF, WTF, OPF> !RefUnwindSafe for TServer<PRC, RTF, IPF, WTF, OPF>

§

impl<PRC, RTF, IPF, WTF, OPF> Send for TServer<PRC, RTF, IPF, WTF, OPF>where IPF: Send, OPF: Send, RTF: Send, WTF: Send,

§

impl<PRC, RTF, IPF, WTF, OPF> !Sync for TServer<PRC, RTF, IPF, WTF, OPF>

§

impl<PRC, RTF, IPF, WTF, OPF> Unpin for TServer<PRC, RTF, IPF, WTF, OPF>where IPF: Unpin, OPF: Unpin, RTF: Unpin, WTF: Unpin,

§

impl<PRC, RTF, IPF, WTF, OPF> !UnwindSafe for TServer<PRC, RTF, IPF, WTF, OPF>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.