Created
February 13, 2009 06:52
-
-
Save arunthampi/63082 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.internet import reactor, protocol, defer | |
from twisted.protocols import basic | |
class SimpleStompProtocol(protocol.Protocol): | |
def connectionMade(self): | |
self.connect("", "") | |
def dataReceived(self, data): | |
frame = self.unpackFrame(data) | |
if(frame['cmd'] == 'CONNECTED'): | |
self.subscribe("/topic/wego.com.hotel.search") | |
elif(frame['cmd'] == 'MESSAGE'): | |
print "Message Received:\n%s" % frame['body'] | |
else: | |
pass | |
def subscribe(self, topic): | |
self.sendFrame("SUBSCRIBE", {'destination' : topic, 'ack' : 'auto'}) | |
def connect(self, username, password): | |
self.sendFrame("CONNECT", {'login' : username, 'password' : password}) | |
def sendFrame(self, verb, headers = {}, body = ""): | |
ary = [verb, "\n"] | |
for k,v in headers.iteritems(): | |
ary.append("%s: %s\n" % (k,v)) | |
ary.append("content-length: %i\n" % len(str(body))) | |
ary.append("content-type: text-plain; charset=UTF-8\n\n") | |
ary.append(str(body)) | |
ary.append("\x00") | |
self.transport.write("".join(ary)) | |
def unpackFrame(self, message): | |
"""Called to unpack a STOMP message into a dictionary. | |
returned = { | |
# STOMP Command: | |
'cmd' : '...', | |
# Headers e.g. | |
'headers' : { | |
'destination' : 'xyz', 'message-id' : 'some event', | |
} | |
'body' : '...1234...\x00', | |
} | |
""" | |
body = [] | |
returned = dict(cmd='', headers={}, body='') | |
breakdown = message.split('\n') | |
# Get the message command: | |
returned['cmd'] = breakdown[0] | |
breakdown = breakdown[1:] | |
def headD(field): | |
# find the first ':' everything to the left of this is a | |
# header, everything to the right is data: | |
index = field.find(':') | |
if index: | |
header = field[:index].strip() | |
data = field[index+1:].strip() | |
returned['headers'][header.strip()] = data.strip() | |
def bodyD(field): | |
field = field.strip() | |
if field: | |
body.append(field) | |
# Recover the header fields and body data | |
handler = headD | |
for field in breakdown: | |
if field.strip() == '': | |
# End of headers, it body data next. | |
handler = bodyD | |
continue | |
handler(field) | |
# Stich the body data together: | |
body = "".join(body) | |
returned['body'] = body.replace('\x00', '') | |
return returned | |
class SimpleStompClientFactory(protocol.ClientFactory): | |
protocol = SimpleStompProtocol | |
def __init__(self): | |
self.deferred = defer.Deferred() | |
def clientConnectionLost(self, transport, reason): | |
reactor.stop() | |
def clientConnectionFailed(self, transport, reason): | |
print reason.getErrorMessage() | |
reactor.stop() | |
if __name__ == "__main__": | |
port = 61613 | |
reactor.connectTCP('localhost', port, SimpleStompClientFactory()) | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment