glyph,<br><br>Sorry for the delayed response.  I wanted to thank you for providing such a complete example.  This is excellent.  Now I just need to wrap by head around how it all goes together and works.<br><br>The logic of the queue append and pop of a deferred in ProxyClient forwardLine and lineRecieve are throwing me for a loop.  I&#39;ll keep tracing it and get it down.<br>
<br>Thanks Again!<br>-ab<br><br><div class="gmail_quote">On Thu, May 28, 2009 at 6:58 PM,  <span dir="ltr">&lt;<a href="mailto:glyph@divmod.com">glyph@divmod.com</a>&gt;</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;">
<div class="im"><br>
On 01:23 am, <a href="mailto:asb.bush@gmail.com" target="_blank">asb.bush@gmail.com</a> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I have just started to look at the Twisted framework and would like to put it<br>
to use for a new project I am working on.  Not being very familiar with the<br>
framework and fairly new to Python in general I would like to ask a<br>
design/architecture question.  (I have written similar applications in C but<br>
would prefer to start this in the right direction and not write Python like<br>
C.)<br>
</blockquote>
<br></div>
Thanks for asking!<br>
<br>
I apologize for the delay in my answer.  I started writing up a simple example (attached) but was discouraged to find that it was 100 lines long and required too much explaining.<br>
<br>
Then I started documenting it and explaining every line but that was a very long, tedious message.  So, it doesn&#39;t have much in the way of explanation; I hope you will find it useful regardless.<div class="im"><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
The application has the following model:<br>
</blockquote>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Many clients connect to the Application and prefer to leave the connection<br>
open.  They will send messages across this connection.  They will expect to<br>
get a message back at some point later, they do not wait for a response<br>
(async).  The clients are already coded (legacy) and just need to send their<br>
proprietary protocol to the new Application (written using Twisted).<br>
</blockquote>
<br></div>
This is *almost* a FAQ.  At least, you may find this to be a useful answer:<br>
<br>
&lt;<a href="http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#HowdoImakeinputononeconnectionresultinoutputonanother" target="_blank">http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#HowdoImakeinputononeconnectionresultinoutputonanother</a>&gt;<div class="im">
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
The Twisted application will take the data from the clients and do some<br>
transformation on it then send the message on to another server (3rd party).<br>
This connection to &quot;another&quot; server must be a single connection, not one<br>
connection per client.  This connection should also be persistent and not<br>
opened/closed for each client message sent.  Ideally if the 3rd party server<br>
is down then I would also not accept client connections as the messages are<br>
time sensitive and should not be stored and forwarded.  At some point the 3rd<br>
part will send a message back and the Application will route it back to the<br>
original source.  Basically request/reply pattern.<br>
</blockquote>
<br></div>
The example that I&#39;ve attached does basically this.  Run it and then run &#39;telnet localhost 4322&#39;, and type some lines; you will see that they are transformed and echoed back to you, both by the proxy and by the protocol being proxied.<br>

<br>
At a high level, the answer to your question is so simple that it&#39;s hard to express.  Basically, you just need to have all the relevant objects having references to each other, and calling methods to achieve the desired effect.  The less magic, the better.<br>

<br>
More precisely, you need an object responsible for managing your outgoing connections to your legacy server, so that it can handle disconnection and reconnection, queueing messages and so on.  Then you need your proxy server factory to hold a reference to that object, so that it can create references from each proxy server protocol connection object to the connection manager.<br>

<br>
This is related to another recent thread - you can see my message in that thread here:<br>
<br>
   <a href="http://thread.gmane.org/gmane.comp.python.twisted/18377/focus=18385" target="_blank">http://thread.gmane.org/gmane.comp.python.twisted/18377/focus=18385</a><div class="im"><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I have been reading through the archives and the twisted docs and have also<br>
looked over the Hex-dump port-forwarding recipe but not found anything that<br>
explains how to use twisted for this model.  Hex-dump is close but<br>
opens/closes the connection to the server on each client connection.<br>
</blockquote>
<br></div>
I&#39;m not sure why hex-dump port-forwarding is particularly relevant to this example.  Is it just because this is an application that connects from one host to another?<div class="im"><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I am thinking that there will be two Factories [and two protocols: 1) for<br>
clients and 2) for 3rd party].  I am not sure how to best establish both the<br>
listening factory and the client to 3rd party factory.  Once they are<br>
established what is the preferred way in Twisted to pass a message from one<br>
protocol to another?<br>
</blockquote>
<br></div>
This part of your question is almost exactly the FAQ I mentioned above :).  To reiterate that answer, you just need to have references between objects, and call methods on the objects you want to do stuff.<br>
<br>
If you have a client connection object, just get a reference to that from the relevant server connection object and call methods on the client object to emit messages on the client protocol, handling any responses appropriately.  Deferreds can help with that latter part.<br>

<br>
It is always better if you can establish that reference as simply as possible; for example, by passing parameters to the __init__ of various classes.  Again, for reasons that have nothing to do with Twisted specifically, it&#39;s a bad idea to try to establish these references by having global variables floating around.<br>

<br>
Here&#39;s a very very simple example of the &quot;good way&quot; to propagate some data to protocol instances that need it:<br>
<br>
   class MyProtocol(Protocol):<br>
       def __init__(self, data):<br>
           self.data = data<br>
<br>
   class MyFactory(Factory):<br>
       def __init__(self, data):<br>
           self.data = data<br>
<br>
       def buildProtocol(self, addr):<br>
           return MyProtocol(self.data)<br>
<br>
   reactor.listenTCP(8765, MyFactory(&quot;some data&quot;))<br>
<br>
and here&#39;s a simple example of a really bad way (don&#39;t do this!):<br>
<br>
   class MyProtocol(Protocol):<br>
       def connectionMade(self):<br>
           self.bleh = bleh<br>
<br>
   bleh = &quot;some data&quot;<br>
   f = Factory()<br>
   f.protocol = MyProtocol<br>
   reactor.listenTCP(9876, f)<br>
<br>
Even in C, I&#39;m pretty sure it&#39;s better style to pass structures to functions than to abuse piles of local variables :).  I only illustrate this bad style here because it seems to be a common antipattern.  The Protocol class itself doesn&#39;t take any parameters to __init__, and Twisted&#39;s users don&#39;t always realize that protocols and factories and so on are just regular objects, with no special rules; they just get methods called on them by the reactor.<div class="im">
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Any pointers or sample code that you can offer is greatly appreciated. I<br>
would really like to cook this in Twisted and not go back to the C way.<br>
</blockquote>
<br></div>
Based on what you&#39;ve said so far, I think you&#39;re basically on the right track.  Good luck!<br></blockquote></div><br>