[Twisted-Python] simple tcpgate in Twisted

Jean-Paul Calderone exarkun at divmod.com
Wed Feb 6 10:15:14 EST 2008


On Wed, 06 Feb 2008 09:03:11 -0600, Andy Leszczynski <leszczynscy at yahoo.com> wrote:
>Hi,
>
>I need to write a simple TCP/IP gate which would accept connections, 
>reconnect to arbitrary (address,port), retransmit all data from both ends, 
>and finally closed the connection upon either end closing it too.

twisted.protocols.portforward probably does something quite similar to what
you want.

>
>I was not able to find anything ready so I wrote a simple piece of the code 
>(attached at the end along with the tcpecho used for testing purposes, for 
>initiator side I use simply telnet). I also want to be as clean as possible 
>from Twisted perspective. There are two problems I have encountered related 
>to the cleanness of the solution:
>
>1- I store self.gate_server_protocol in both GateClientProtocol(Protocol) 
>and GateClientProtocolFactory(ClientFactory)
>      This is an obvious redundancy, but it is not clear for me what is a 
>relationship between a Protocol and a ClientFactory. Is it composition or 
>aggregation, is it 1:1 or n:n. Is Factory created for each Protocol?

The relationship is up to the factory.  In your code:

>    def connectionMade(self):
>        print 'GateServerProtocol.connectionMade'
>        reactor.connectTCP('127.0.0.1',8002,GateClientProtocolFactory(self))

It seems the relationship is 1:1.  Each time you want to establish a new
connection, you create a new GateClientProtocolFactory instance and there
is no reconnection logic implemented by GateClientProtocolFactory.

You may see simplification by using twisted.internet.protocols.ClientCreator
instead of having GateClientProtocolFactory.

>
>2 - Not sure id the relationship between GateServerProtocol and 
>GateClientProtocol is correctly coded too.
>

It seems basically correct, aside from the timing/connecting issue you
mentioned.

>
>The prototype does not accumulate the data in case a connection for is 
>GateClientProtocol not established. I will finish it of once I am confident 
>the main structure is solid.
>

Jean-Paul




More information about the Twisted-Python mailing list