Repository: thrift
Updated Branches:
  refs/heads/master 9db23b7be -> 0e22c362b


http://git-wip-us.apache.org/repos/asf/thrift/blob/0e22c362/tutorial/rs/src/bin/tutorial_server.rs
----------------------------------------------------------------------
diff --git a/tutorial/rs/src/bin/tutorial_server.rs 
b/tutorial/rs/src/bin/tutorial_server.rs
index 9cc1866..8db8eed 100644
--- a/tutorial/rs/src/bin/tutorial_server.rs
+++ b/tutorial/rs/src/bin/tutorial_server.rs
@@ -24,12 +24,12 @@ extern crate thrift_tutorial;
 use std::collections::HashMap;
 use std::convert::{From, Into};
 use std::default::Default;
+use std::sync::Mutex;
 
-use thrift::protocol::{TInputProtocolFactory, TOutputProtocolFactory};
 use thrift::protocol::{TCompactInputProtocolFactory, 
TCompactOutputProtocolFactory};
-use thrift::server::TSimpleServer;
+use thrift::server::TServer;
 
-use thrift::transport::{TFramedTransportFactory, TTransportFactory};
+use thrift::transport::{TFramedReadTransportFactory, 
TFramedWriteTransportFactory};
 use thrift_tutorial::shared::{SharedServiceSyncHandler, SharedStruct};
 use thrift_tutorial::tutorial::{CalculatorSyncHandler, 
CalculatorSyncProcessor};
 use thrift_tutorial::tutorial::{InvalidOperation, Operation, Work};
@@ -58,33 +58,36 @@ fn run() -> thrift::Result<()> {
 
     println!("binding to {}", listen_address);
 
-    let i_tran_fact: Box<TTransportFactory> = 
Box::new(TFramedTransportFactory::new());
-    let i_prot_fact: Box<TInputProtocolFactory> = 
Box::new(TCompactInputProtocolFactory::new());
+    let i_tran_fact = TFramedReadTransportFactory::new();
+    let i_prot_fact = TCompactInputProtocolFactory::new();
 
-    let o_tran_fact: Box<TTransportFactory> = 
Box::new(TFramedTransportFactory::new());
-    let o_prot_fact: Box<TOutputProtocolFactory> = 
Box::new(TCompactOutputProtocolFactory::new());
+    let o_tran_fact = TFramedWriteTransportFactory::new();
+    let o_prot_fact = TCompactOutputProtocolFactory::new();
 
     // demux incoming messages
     let processor = CalculatorSyncProcessor::new(CalculatorServer { 
..Default::default() });
 
     // create the server and start listening
-    let mut server = TSimpleServer::new(i_tran_fact,
-                                        i_prot_fact,
-                                        o_tran_fact,
-                                        o_prot_fact,
-                                        processor);
+    let mut server = TServer::new(
+        i_tran_fact,
+        i_prot_fact,
+        o_tran_fact,
+        o_prot_fact,
+        processor,
+        10,
+    );
 
     server.listen(&listen_address)
 }
 
 /// Handles incoming Calculator service calls.
 struct CalculatorServer {
-    log: HashMap<i32, SharedStruct>,
+    log: Mutex<HashMap<i32, SharedStruct>>,
 }
 
 impl Default for CalculatorServer {
     fn default() -> CalculatorServer {
-        CalculatorServer { log: HashMap::new() }
+        CalculatorServer { log: Mutex::new(HashMap::new()) }
     }
 }
 
@@ -94,9 +97,9 @@ impl Default for CalculatorServer {
 
 // SharedService handler
 impl SharedServiceSyncHandler for CalculatorServer {
-    fn handle_get_struct(&mut self, key: i32) -> thrift::Result<SharedStruct> {
-        self.log
-            .get(&key)
+    fn handle_get_struct(&self, key: i32) -> thrift::Result<SharedStruct> {
+        let log = self.log.lock().unwrap();
+        log.get(&key)
             .cloned()
             .ok_or_else(|| format!("could not find log for key {}", 
key).into())
     }
@@ -104,25 +107,27 @@ impl SharedServiceSyncHandler for CalculatorServer {
 
 // Calculator handler
 impl CalculatorSyncHandler for CalculatorServer {
-    fn handle_ping(&mut self) -> thrift::Result<()> {
+    fn handle_ping(&self) -> thrift::Result<()> {
         println!("pong!");
         Ok(())
     }
 
-    fn handle_add(&mut self, num1: i32, num2: i32) -> thrift::Result<i32> {
+    fn handle_add(&self, num1: i32, num2: i32) -> thrift::Result<i32> {
         println!("handling add: n1:{} n2:{}", num1, num2);
         Ok(num1 + num2)
     }
 
-    fn handle_calculate(&mut self, logid: i32, w: Work) -> thrift::Result<i32> 
{
+    fn handle_calculate(&self, logid: i32, w: Work) -> thrift::Result<i32> {
         println!("handling calculate: l:{}, w:{:?}", logid, w);
 
         let res = if let Some(ref op) = w.op {
             if w.num1.is_none() || w.num2.is_none() {
-                Err(InvalidOperation {
-                    what_op: Some(*op as i32),
-                    why: Some("no operands specified".to_owned()),
-                })
+                Err(
+                    InvalidOperation {
+                        what_op: Some(*op as i32),
+                        why: Some("no operands specified".to_owned()),
+                    },
+                )
             } else {
                 // so that I don't have to call unwrap() multiple times below
                 let num1 = w.num1.as_ref().expect("operands checked");
@@ -134,10 +139,12 @@ impl CalculatorSyncHandler for CalculatorServer {
                     Operation::MULTIPLY => Ok(num1 * num2),
                     Operation::DIVIDE => {
                         if *num2 == 0 {
-                            Err(InvalidOperation {
-                                what_op: Some(*op as i32),
-                                why: Some("divide by 0".to_owned()),
-                            })
+                            Err(
+                                InvalidOperation {
+                                    what_op: Some(*op as i32),
+                                    why: Some("divide by 0".to_owned()),
+                                },
+                            )
                         } else {
                             Ok(num1 / num2)
                         }
@@ -145,12 +152,13 @@ impl CalculatorSyncHandler for CalculatorServer {
                 }
             }
         } else {
-            Err(InvalidOperation::new(None, "no operation 
specified".to_owned()))
+            Err(InvalidOperation::new(None, "no operation 
specified".to_owned()),)
         };
 
         // if the operation was successful log it
         if let Ok(ref v) = res {
-            self.log.insert(logid, SharedStruct::new(logid, format!("{}", v)));
+            let mut log = self.log.lock().unwrap();
+            log.insert(logid, SharedStruct::new(logid, format!("{}", v)));
         }
 
         // the try! macro automatically maps errors
@@ -161,7 +169,7 @@ impl CalculatorSyncHandler for CalculatorServer {
         res.map_err(From::from)
     }
 
-    fn handle_zip(&mut self) -> thrift::Result<()> {
+    fn handle_zip(&self) -> thrift::Result<()> {
         println!("handling zip");
         Ok(())
     }

Reply via email to