From 97b47dff3f365e1c804f002c96c52db2eca36585 Mon Sep 17 00:00:00 2001 From: Mark Slee Date: Tue, 10 Oct 2006 01:38:05 +0000 Subject: [PATCH] Python framed thrift transports Reviewed By: aditya git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664819 13f79535-47bb-0310-9956-ffa450edef68 --- lib/py/src/transport/TTransport.py | 43 ++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/lib/py/src/transport/TTransport.py b/lib/py/src/transport/TTransport.py index 7117aa0b..4d988376 100644 --- a/lib/py/src/transport/TTransport.py +++ b/lib/py/src/transport/TTransport.py @@ -1,4 +1,5 @@ from cStringIO import StringIO +from struct import pack,unpack class TTransportException(Exception): @@ -89,3 +90,45 @@ class TBufferedTransport(TTransportBase): def flush(self): self.__trans.write(self.__buf.getvalue()) self.__buf = StringIO() + +class TFramedTransportFactory: + + """Factory transport that builds framed transports""" + + def getIOTransports(self, trans): + framed = TFramedTransport(trans) + return (framed, framed) + + +class TFramedTransport(TTransportBase): + + """Class that wraps another transport and frames its I/O when writing.""" + + def __init__(self, trans): + self.__trans = trans + self.__wbuf = StringIO() + + def isOpen(self): + return self.__trans.isOpen() + + def open(self): + return self.__trans.open() + + def close(self): + return self.__trans.close() + + def read(self, sz): + return self.__trans.read(sz) + + def readAll(self, sz): + return self.__trans.readAll(sz) + + def write(self, buf): + self.__wbuf.write(buf) + + def flush(self): + wout = self.__wbuf.getvalue() + wsz = len(wout) + self.__trans.write(pack("!i", wsz)) + self.__trans.write(wout) + self.__wbuf = StringIO() -- 2.17.1