Fix python server bugs and go to new protocol wraps transport model

Reviewed By: ccheever


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664849 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/py/src/server/TServer.py b/lib/py/src/server/TServer.py
index 56ee9c0..7514264 100644
--- a/lib/py/src/server/TServer.py
+++ b/lib/py/src/server/TServer.py
@@ -5,18 +5,23 @@
 
 from thrift.Thrift import TProcessor
 from thrift.transport import TTransport
+from thrift.protocol import TBinaryProtocol
 
 class TServer:
 
   """Base interface for a server, which must have a serve method."""
 
-  def __init__(self, processor, serverTransport, transportFactory=None):
+  def __init__(self, processor, serverTransport, transportFactory=None, protocolFactory=None):
     self.processor = processor
     self.serverTransport = serverTransport
     if transportFactory == None:
       self.transportFactory = TTransport.TTransportFactoryBase()
     else:
       self.transportFactory = transportFactory
+    if protocolFactory == None:
+      self.protocolFactory = TBinaryProtocol.TBinaryProtocolFactory()
+    else:
+      self.protocolFactory = protocolFactory
 
   def serve(self):
     pass
@@ -25,31 +30,32 @@
 
   """Simple single-threaded server that just pumps around one transport."""
 
-  def __init__(self, processor, serverTransport, transportFactory=None):
-    TServer.__init__(self, processor, serverTransport, transportFactory)
+  def __init__(self, processor, serverTransport, transportFactory=None, protocolFactory=None):
+    TServer.__init__(self, processor, serverTransport, transportFactory, protocolFactory)
 
   def serve(self):
     self.serverTransport.listen()
     while True:
       client = self.serverTransport.accept()
-      (input, output) = self.transportFactory.getIOTransports(client)
+      (itrans, otrans) = self.transportFactory.getIOTransports(client)
+      (iprot, oprot) = self.protocolFactory.getIOProtocols(itrans, otrans)
       try:
         while True:
-          self.processor.process(input, output)
+          self.processor.process(iprot, oprot)
       except TTransport.TTransportException, tx:
         pass
       except Exception, x:
         print '%s, %s, %s' % (type(x), x, traceback.format_exc())
 
-      input.close()
-      output.close()
+      itrans.close()
+      otrans.close()
 
 class TThreadedServer(TServer):
 
   """Threaded server that spawns a new thread per each connection."""
 
-  def __init__(self, processor, serverTransport, transportFactory=None):
-    TServer.__init__(self, processor, serverTransport, transportFactory)
+  def __init__(self, processor, serverTransport, transportFactory=None, protocolFactory=None):
+    TServer.__init__(self, processor, serverTransport, transportFactory, protocolFactory)
 
   def serve(self):
     self.serverTransport.listen()
@@ -62,15 +68,19 @@
         print '%s, %s, %s,' % (type(x), x, traceback.format_exc())
 
   def handle(self, client):
-    (input, output) = self.transportFactory.getIOTransports(client)
+    (itrans, otrans) = self.transportFactory.getIOTransports(client)
+    (iprot, oprot) = self.protocolFactory.getIOProtocols(itrans, otrans)
     try:
       while True:
-        self.processor.process(input, output)
+        self.processor.process(iprot, oprot)
     except TTransport.TTransportException, tx:
       pass
     except Exception, x:
       print '%s, %s, %s' % (type(x), x, traceback.format_exc())
 
+    itrans.close()
+    otrans.close()
+
 class TThreadPoolServer(TServer):
 
   """Server with a fixed size pool of threads which service requests."""
@@ -95,15 +105,19 @@
       
   def serveClient(self, client):
     """Process input/output from a client for as long as possible"""
-    (input, output) = self.transportFactory.getIOTransports(client)
+    (itrans, otrans) = self.transportFactory.getIOTransports(client)
+    (iprot, oprot) = self.protocolFactory.getIOProtocols(itrans, otrans)
     try:
       while True:
-        self.processor.process(input, output)
+        self.processor.process(iprot, oprot)
     except TTransport.TTransportException, tx:
       pass
     except Exception, x:
       print '%s, %s, %s' % (type(x), x, traceback.format_exc())
 
+    itrans.close()
+    otrans.close()
+
   def serve(self):
     """Start a fixed number of worker threads and put client into a queue"""
     for i in range(self.threads):