Thanks, Arjan. Of course, factories are breeding like rabbits, but that was the obvious solution! :-) I wish I had thought of it.<br><br>You saved me several MORE hours...I've spend all day on this. <br><br>(It's snowing and work was CLOSED! Yeehah!)<br>
<br>Mark<br><br><div class="gmail_quote">On Wed, Feb 10, 2010 at 2:38 PM, Arjan Scherpenisse <span dir="ltr"><<a href="mailto:arjan@scherpenisse.net">arjan@scherpenisse.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
-----BEGIN PGP SIGNED MESSAGE-----<br>
Hash: SHA1<br>
<br>
If you create two instances of your ClusterClientFactory, each with a<br>
unique string, you should be fine. Requires only a small modification:<br>
<div class="im"><br>
factory = ClusterClientFactory()<br>
factory.maxDelay = 120 # two minutes<br>
factory.connectString = "FirstString..."<br>
reactor.connectTCP("localhost", 7300, factory)<br>
<br>
</div> factory2 = ClusterClientFactory()<br>
factory2.maxDelay = 120 # two minutes<br>
factory2.connectString = "SecondString"<br>
reactor2.connectTCP("localhost", 7300, factory)<br>
<br>
You can clean this up by putting the maxDelay and connectString in the<br>
constructor of the factory:<br>
<br>
reactor2.connectTCP("localhost", 7300, ClusterClientFactory(120, "First"))<br>
reactor2.connectTCP("localhost", 7300, ClusterClientFactory(120, "Second"))<br>
<br>
Arjan<br>
<div><div></div><div class="h5"><br>
Mark Bailey wrote:<br>
> Good day:<br>
><br>
> I've been having fun with Twisted. I have my application running fine,<br>
> with multiple server and client connections using Telnet. :-)<br>
><br>
> However, users always want something. I need to send some unique<br>
> configuration information to each connection. The connections are made<br>
> using connectTCP. My first attempt assumed that the connection was made<br>
> when the call to connectTCP was executed. It didn't work! :-(<br>
><br>
> -------------------------------<br>
><br>
> from twisted.internet.protocol import ClientFactory<br>
> from twisted.internet import reactor<br>
> from twisted.conch.telnet import StatefulTelnetProtocol<br>
><br>
> class testClient(StatefulTelnetProtocol):<br>
><br>
> def connectionMade(self):<br>
> self.title = self.factory.connectString<br>
> print "Client Connected: " + self.title<br>
> self.setRawMode()<br>
> self.factory.connections.append(self)<br>
><br>
> def connectionLost(self, reason):<br>
> if self in self.factory.connections:<br>
> self.factory.connections.remove(self)<br>
><br>
> def rawDataReceived(self,data):<br>
> print data + "\n"<br>
><br>
> class ClusterClientFactory(ClientFactory):<br>
><br>
> protocol = testClient<br>
><br>
> def __init__(self):<br>
> self.connections = []<br>
> self.connectString = ''<br>
><br>
> def startFactory(self):<br>
> print "startFactory: " + self.connectString<br>
><br>
> def startedConnecting(self, connector):<br>
> print "Started connecting: " + str(connector)<br>
> print self.connectString<br>
><br>
> def buildProtocol(self, addr):<br>
> print "bulldProtocol: " + str(addr)<br>
> print "buildProtocol: " + self.connectString<br>
><br>
> p = self.protocol()<br>
> p.factory = self<br>
> return p<br>
><br>
> if __name__ == '__main__':<br>
><br>
><br>
> def startUp():<br>
><br>
> factory = ClusterClientFactory()<br>
> factory.maxDelay = 120 # two minutes<br>
><br>
> factory.connectString = "FirstString..."<br>
> reactor.connectTCP("localhost", 7300, factory)<br>
><br>
> factory.connectString = "SecondString"<br>
> reactor.connectTCP("localhost", 7300, factory)<br>
><br>
> reactor.callWhenRunning(startUp)<br>
> reactor.run()<br>
><br>
> ----------------------------------<br>
><br>
> The results are that buildProtocol gets the second string both times.<br>
> startedConnecting gets the correct string, but all I have there is a<br>
> connection object. I need the correct data in buildProtocol to do it<br>
> this way.<br>
><br>
> ------------------------------------<br>
><br>
> C:\Users\Mark\src\play>python testclient.py<br>
> startFactory: FirstString...<br>
> Started connecting: <twisted.internet.tcp.Connector instance at 0x01DAB620><br>
> FirstString...<br>
> Started connecting: <twisted.internet.tcp.Connector instance at 0x01E0EFD0><br>
> SecondString<br>
> bulldProtocol: IPv4Address(TCP, '127.0.0.1', 7300)<br>
> buildProtocol: SecondString<br>
> Client Connected: SecondString<br>
> Welcome to SimpleServer5<br>
><br>
><br>
><br>
> bulldProtocol: IPv4Address(TCP, '127.0.0.1', 7300)<br>
> buildProtocol: SecondString<br>
> Client Connected: SecondString<br>
> Welcome to SimpleServer5<br>
><br>
> --------------------------<br>
><br>
><br>
> My "best" idea is to pass the strings to the factory in a dictionary<br>
> indexed by the IP address and the port. Then, buildProtocol() can use<br>
> that to recover the string and I can use reactor.resolve() to translate<br>
> the host name to the IP address. The real application won't have<br>
> multiple connections to the same host and port like this example, so<br>
> this would work.<br>
><br>
> There MUST be a better way. :-)<br>
><br>
> My "simpleserver" is below if you want to run this.<br>
><br>
> THANKS for all of your help. I have the basic application running now<br>
> (including a Tkinter GUI :-) ) and Twisted has saved me hundreds or<br>
> even thousands of lines of code...<br>
><br>
> Mark Bailey<br>
><br>
> ------------------------------<br>
><br>
> from twisted.conch.telnet import StatefulTelnetProtocol<br>
> from twisted.internet import reactor, protocol<br>
> from twisted.protocols.basic import LineReceiver<br>
><br>
> class TelnetEcho(StatefulTelnetProtocol):<br>
> def connectionMade(self):<br>
> self.factory.connection.append(self)<br>
> self.sendLine("Welcome to SimpleServer5\r\n")<br>
><br>
><br>
> def lineReceived(self, data):<br>
> data = data.rstrip('\n\r')<br>
><br>
> if data.upper() == 'BYE':<br>
> self.sendLine("Goodbye...\r")<br>
> self.transport.loseConnection()<br>
> else:<br>
> self.sendLine("Unrecognized command: %r\r" % (data,))<br>
><br>
> def connectionLost(self, reason):<br>
> self.factory.connection.remove(self)<br>
><br>
><br>
> class TelnetEchoFactory(protocol.Factory):<br>
> protocol = TelnetEcho<br>
> def __init__(self):<br>
> self.connection = []<br>
><br>
><br>
> def createTelnetServer(port=7300):<br>
> telnetinstance = TelnetEchoFactory() # needs to be a list<br>
> reactor.listenTCP(port,telnetinstance)<br>
><br>
> if __name__ == "__main__":<br>
> reactor.callWhenRunning(createTelnetServer)<br>
> reactor.run()<br>
><br>
><br>
</div></div>> ------------------------------------------------------------------------<br>
><br>
> _______________________________________________<br>
> Twisted-Python mailing list<br>
> <a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>
> <a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
-----BEGIN PGP SIGNATURE-----<br>
Version: GnuPG v1.4.9 (GNU/Linux)<br>
Comment: Using GnuPG with Mozilla - <a href="http://enigmail.mozdev.org/" target="_blank">http://enigmail.mozdev.org/</a><br>
<br>
iEYEARECAAYFAktzC0MACgkQigE4AbflYer9nwCgixra0FaTD6ubvGJufjApRG/m<br>
A34An3C2ng+4x3/halWSgGQUvQvsCiTM<br>
=Cg6W<br>
-----END PGP SIGNATURE-----<br>
<br>
_______________________________________________<br>
Twisted-Python mailing list<br>
<a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
</blockquote></div><br>