Created
May 25, 2010 05:42
-
-
Save hmax/412820 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from twisted.python import log | |
BOT_PROTOCOL_LIST = ['jabber'] | |
class queueReader: | |
def __init__(self, conn, exchange, basename): | |
self._conn = conn | |
self._basename = basename | |
self._exchange = exchange | |
def associateConnection(self, connection): | |
log.msg("associating") | |
self._conn = connection | |
self.openChannel() | |
def openChannel(self): | |
def _openChann(channel): | |
log.msg("Opening channel") | |
t = channel.channel_open().addCallback( lambda _: channel.queue_declare(queue=self._basename + "_out", durable=True, exclusive=False, | |
auto_delete=False)).addErrback(log.err) | |
t.addCallback(lambda _: channel.basic_consume(queue=self._basename + '_out', no_ack=True, consumer_tag=self._basename +"_out_consumer")) | |
self._chan = channel | |
t.addCallback(_createQueue).addErrback(log.err) | |
return channel | |
def _createQueue(ignored): | |
log.msg("Creating queue") | |
self._queue = self._conn.queue(self._basename + "_out_consumer") | |
self._queue.addCallback(readMessages).addErrback(log.err) | |
def handleMessage(msg, queue): | |
log.msg("Handling messages") | |
print 'Received: ' + msg.content.body + ' from channel #' + str(self._chan.id) | |
if msg.content.body == "STOP": | |
return failure.Failure(EndOfMessage()) | |
def readMessages(queue): | |
log.msg("Reading messages") | |
read = queue.get().addCallback(handleMessage, queue).addErrback(log.err) | |
read.addCallback(lambda _: readMessages(queue)).addErrback(log.err) | |
return read | |
self._chan = self._conn.channel(BOT_PROTOCOL_LIST.index(self._basename)+2 + len(BOT_PROTOCOL_LIST)) | |
return self._chan.addCallback(_openChann) | |
def getDeferred(self): | |
pass | |
def clientConnectionLost(self, connector, reason): | |
print reason | |
def clientConnectionFailed(self, connector, reason): | |
print reason | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment