Thrift TTransportFactory model for servers
Summary: Servers need to create bufferedtransports etc. around the transports they get in a user-definable way. So use a factory pattern to allow the user to supply an object to the server that defines this behavior.
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664792 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/py/src/server/TServer.py b/lib/py/src/server/TServer.py
index a5d5621..53f6846 100644
--- a/lib/py/src/server/TServer.py
+++ b/lib/py/src/server/TServer.py
@@ -8,8 +8,13 @@
"""Base interface for a server, which must have a run method."""
- def __init__(self, proc):
- self.processor = proc
+ def __init__(self, processor, serverTransport, transportFactory=None):
+ self.processor = processor
+ self.serverTransport = serverTransport
+ if transportFactory == None:
+ self.transportFactory = TTransport.TTransportFactoryBase()
+ else:
+ self.transportFactory = transportFactory
def run(self):
pass
@@ -18,18 +23,20 @@
"""Simple single-threaded server that just pumps around one transport."""
- def __init__(self, proc, trans):
- TServer.__init__(self, proc)
- self.transport = trans
+ def __init__(self, processor, serverTransport, transportFactory=None):
+ TServer.__init__(self, processor, serverTransport, transportFactory)
def run(self):
- self.transport.listen()
+ self.serverTransport.listen()
while True:
- client = TTransport.TBufferedTransport(self.transport.accept())
+ client = self.serverTransport.accept()
+ (input, output) = self.transportFactory.getIOTransports(client)
try:
while True:
- self.processor.process(client, client)
+ self.processor.process(input, output)
except Exception, x:
print '%s, %s, %s' % (type(x), x, traceback.format_exc())
print 'Client died.'
- client.close()
+
+ input.close()
+ output.close()
diff --git a/lib/py/src/transport/TSocket.py b/lib/py/src/transport/TSocket.py
index 61f1cff..2c7dd3e 100644
--- a/lib/py/src/transport/TSocket.py
+++ b/lib/py/src/transport/TSocket.py
@@ -21,8 +21,9 @@
self.handle.connect((self.host, self.port))
def close(self):
- self.handle.close()
- self.handle = None
+ if self.handle != None:
+ self.handle.close()
+ self.handle = None
def readAll(self, sz):
buff = ''
diff --git a/lib/py/src/transport/TTransport.py b/lib/py/src/transport/TTransport.py
index 1e8b6c6..a7eb3b0 100644
--- a/lib/py/src/transport/TTransport.py
+++ b/lib/py/src/transport/TTransport.py
@@ -36,6 +36,22 @@
def close(self):
pass
+class TTransportFactoryBase:
+
+ """Base class for a Transport Factory"""
+
+ def getIOTransports(self, trans):
+ return (trans, trans)
+
+class TBufferedTransportFactory:
+
+ """Factory transport that builds buffered transports"""
+
+ def getIOTransports(self, trans):
+ buffered = TBufferedTransport(trans)
+ return (buffered, buffered)
+
+
class TBufferedTransport(TTransportBase):
"""Class that wraps another transport and buffers its I/O."""