Skip to content
Snippets Groups Projects
Commit de45a4ae authored by Constantin Jucovschi's avatar Constantin Jucovschi
Browse files

adding sally client

parents
No related branches found
No related tags found
No related merge requests found
generateDS.py -o commcore.py --namespacedef="xmlns=\"http://kwarc.info/sally/comm/core\"" core.xsd
generateDS.py -o commlmh.py --namespacedef="xmlns=\"http://kwarc.info/sally/comm/lmh\"" lmh.xsd
import stomp
import random
import commcore
import StringIO
class SallyClient:
def __init__(self, host, port, user, password, envid, userid, interfaces, messageHandler):
conn = stomp.Connection(host_and_ports = [(host, port)])
conn.set_listener('', self)
conn.start()
conn.connect(login=user,passcode=password)
self.queue = "lmh_"+str(random.randint(1,1000000));
self.fullqueue = "/queue/"+self.queue
self.temp_queues = {};
self.conn = conn;
self.messageHandler = messageHandler
conn.subscribe(destination=self.queue, id=0, ack='auto')
regdoc = commcore.registerdocument(self.queue, interfaces, envid, userid);
self.send("sally_register", regdoc, self.registered)
def messageOnDefaultQueue(self, headers, message):
if message.find("xmlns=\"http://kwarc.info/sally/comm/core\"") > 0:
print "core message: trying to parse"
obj = commcore.parseString(message)
if type(obj) is commcore.heartbeatrequest:
self.respond(headers, commcore.heartbeatresponse());
else:
self.messageHandler(headers, message)
pass
def registered(self, headers, message):
obj = commcore.parseString(message);
if type(obj) is commcore.registerdocumentresponse:
self.sallyqueue = obj.sallyqueue
print "registered " + obj.sallyqueue
pass
def respond(self, headers, commObj):
self.send(headers["reply-to"], commObj, headers={"correlation-id" : headers["correlation-id"]});
def send(self, destination, commObj, callback = None, headers={}):
output = StringIO.StringIO()
commObj.export(output, 0);
msgToSend = output.getvalue()
print(msgToSend);
if callback != None:
tmp_id = str(random.randint(1,1000000));
temp_queue = "/temp-queue/lmh_temp_"+tmp_id;
headers["reply-to"] = temp_queue;
headers["correlation-id"] = tmp_id;
self.temp_queues[temp_queue] = callback
self.conn.subscribe(destination=temp_queue, id=0, ack='auto')
self.conn.send(destination, msgToSend, headers=headers)
def on_error(self, headers, message):
print('received an error %s' % message)
def on_message(self, headers, message):
print "on message = ", message
if headers["destination"] == self.fullqueue:
self.messageOnDefaultQueue(headers, message);
return
if self.temp_queues[headers["destination"]]:
self.temp_queues[headers["destination"]](headers, message);
return
print "cannot sort message "+message
pass
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment