I'm writing my first network application. It manipulates jabber client and communicates with some service with IQ-stanzas. But details are not important.
This interaction works through twisted xmlstream (python langu开发者_StackOverflow中文版age). Typical situations are described here: http://juick.com/help/api/xmpp/ To be short, I can send a "query" and server relies me with "result".
I implemented some basic actions like "send message", "do action on message received". The problem is in operating with stream. Is it better to use deferreds to request any data from server? And how to return the result to the place where it is needed?
You can look through my code here: http://bitbucket.org/boh/tmp/src/56c131cd1e62/juick_app/twisted_app.py I tried to make it minimalistic.
To pair a response with a reply, you use the iq.id attribute. You can store a few deferreds in a cache indexed by id and run them when they get a reply. Since XMPP is TCP-based, you will mostly get replies in the right order anyway. You can cap the cache and run the deferred's errback if the deferred has been in the cache for a while.
精彩评论