Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 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 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 18 | //! Types used to implement a Thrift server. |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 19 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 20 | use protocol::{TInputProtocol, TOutputProtocol}; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 21 | |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 22 | mod multiplexed; |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 23 | mod threaded; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 24 | |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 25 | pub use self::multiplexed::TMultiplexedProcessor; |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 26 | pub use self::threaded::TServer; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 27 | |
| 28 | /// Handles incoming Thrift messages and dispatches them to the user-defined |
| 29 | /// handler functions. |
| 30 | /// |
| 31 | /// An implementation is auto-generated for each Thrift service. When used by a |
| 32 | /// server (for example, a `TSimpleServer`), it will demux incoming service |
| 33 | /// calls and invoke the corresponding user-defined handler function. |
| 34 | /// |
| 35 | /// # Examples |
| 36 | /// |
| 37 | /// Create and start a server using the auto-generated `TProcessor` for |
| 38 | /// a Thrift service `SimpleService`. |
| 39 | /// |
| 40 | /// ```no_run |
| 41 | /// use thrift; |
| 42 | /// use thrift::protocol::{TInputProtocol, TOutputProtocol}; |
| 43 | /// use thrift::server::TProcessor; |
| 44 | /// |
| 45 | /// // |
| 46 | /// // auto-generated |
| 47 | /// // |
| 48 | /// |
| 49 | /// // processor for `SimpleService` |
| 50 | /// struct SimpleServiceSyncProcessor; |
| 51 | /// impl SimpleServiceSyncProcessor { |
| 52 | /// fn new<H: SimpleServiceSyncHandler>(processor: H) -> SimpleServiceSyncProcessor { |
| 53 | /// unimplemented!(); |
| 54 | /// } |
| 55 | /// } |
| 56 | /// |
| 57 | /// // `TProcessor` implementation for `SimpleService` |
| 58 | /// impl TProcessor for SimpleServiceSyncProcessor { |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 59 | /// fn process(&self, i: &mut TInputProtocol, o: &mut TOutputProtocol) -> thrift::Result<()> { |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 60 | /// unimplemented!(); |
| 61 | /// } |
| 62 | /// } |
| 63 | /// |
| 64 | /// // service functions for SimpleService |
| 65 | /// trait SimpleServiceSyncHandler { |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 66 | /// fn service_call(&self) -> thrift::Result<()>; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 67 | /// } |
| 68 | /// |
| 69 | /// // |
| 70 | /// // user-code follows |
| 71 | /// // |
| 72 | /// |
| 73 | /// // define a handler that will be invoked when `service_call` is received |
| 74 | /// struct SimpleServiceHandlerImpl; |
| 75 | /// impl SimpleServiceSyncHandler for SimpleServiceHandlerImpl { |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 76 | /// fn service_call(&self) -> thrift::Result<()> { |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 77 | /// unimplemented!(); |
| 78 | /// } |
| 79 | /// } |
| 80 | /// |
| 81 | /// // instantiate the processor |
| 82 | /// let processor = SimpleServiceSyncProcessor::new(SimpleServiceHandlerImpl {}); |
| 83 | /// |
| 84 | /// // at this point you can pass the processor to the server |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 85 | /// // let server = TServer::new(..., processor); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 86 | /// ``` |
| 87 | pub trait TProcessor { |
| 88 | /// Process a Thrift service call. |
| 89 | /// |
| 90 | /// Reads arguments from `i`, executes the user's handler code, and writes |
| 91 | /// the response to `o`. |
| 92 | /// |
| 93 | /// Returns `()` if the handler was executed; `Err` otherwise. |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame^] | 94 | fn process(&self, i: &mut TInputProtocol, o: &mut TOutputProtocol) -> ::Result<()>; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 95 | } |