Skip to content
Snippets Groups Projects
Commit 6ba569af authored by Jorrit Schaap's avatar Jorrit Schaap
Browse files

Task #8582: merged changes to PyMessaging back to trunk

parent be9d1176
No related branches found
No related tags found
No related merge requests found
...@@ -50,7 +50,11 @@ class MessageHandlerInterface(object): ...@@ -50,7 +50,11 @@ class MessageHandlerInterface(object):
handler.finalize_loop() handler.finalize_loop()
""" """
def __init__(self, **kwargs): def __init__(self, **kwargs):
pass # if you want your subclass to handle multiple services
# then you can specify for each service which method has to be called
# In case this map is empty, or the called service is not in this map,
# then the default handle_message is called
self.service2MethodMap = {}
def prepare_loop(self): def prepare_loop(self):
"Called before main processing loop is entered." "Called before main processing loop is entered."
...@@ -73,7 +77,6 @@ class MessageHandlerInterface(object): ...@@ -73,7 +77,6 @@ class MessageHandlerInterface(object):
"Called after main processing loop is finished." "Called after main processing loop is finished."
pass pass
# create service: # create service:
class Service(object): class Service(object):
...@@ -296,15 +299,15 @@ class Service(object): ...@@ -296,15 +299,15 @@ class Service(object):
try: try:
# get the next message # get the next message
msg = self.listener.receive(1) lofar_msg = self.listener.receive(1)
# retry if timed-out # retry if timed-out
if msg is None: if lofar_msg is None:
continue continue
# report if messages are not Service Messages # report if messages are not Service Messages
if isinstance(msg, RequestMessage) is not True: if not isinstance(lofar_msg, RequestMessage):
logger.error( "Received wrong messagetype %s, RequestMessage expected." %(str(type(msg)))) logger.error( "Received wrong messagetype %s, RequestMessage expected." %(str(type(lofar_msg))))
self.listener.ack(msg) self.listener.ack(lofar_msg)
continue continue
# Keep track of number of received messages # Keep track of number of received messages
...@@ -313,33 +316,51 @@ class Service(object): ...@@ -313,33 +316,51 @@ class Service(object):
# Execute the service handler function and send reply back to client # Execute the service handler function and send reply back to client
try: try:
self._debug("Running handler") self._debug("Running handler")
# determine which handler method has to be called
if hasattr(service_handler, 'service2MethodMap') and lofar_msg.subject in service_handler.service2MethodMap:
# pass the handling of this message on to the specific method for this service
serviceHandlerMethod = service_handler.service2MethodMap[lofar_msg.subject]
else:
serviceHandlerMethod = service_handler.handle_message
if self.parsefullmessage is True: if self.parsefullmessage is True:
replymessage = service_handler.handle_message(msg) replymessage = serviceHandlerMethod(lofar_msg)
else: else:
# check for positional arguments and named arguments # check for positional arguments and named arguments
if msg.has_args=="True": # depending on presence of args and kwargs,
rpcargs=msg.content # the signature of the handler method should vary as well
if msg.has_kwargs=="True": if lofar_msg.has_args and lofar_msg.has_kwargs:
# both positional and named arguments # both positional and named arguments
rpckwargs=rpcargs[-1] # rpcargs and rpckwargs are packed in the content
del rpcargs[-1] rpcargs = lofar_msg.content
rpcargs=tuple(rpcargs)
replymessage = service_handler.handle_message(*rpcargs,**rpckwargs) # rpckwargs is the last argument in the content
else: # rpcargs is the rest in front
# only positional arguments rpckwargs = rpcargs[-1]
rpcargs=tuple(rpcargs) del rpcargs[-1]
replymessage = service_handler.handle_message(*rpcargs) rpcargs = tuple(rpcargs)
replymessage = serviceHandlerMethod(*rpcargs, **rpckwargs)
elif lofar_msg.has_args:
# only positional arguments
# msg.content should be a list
rpcargs = tuple(lofar_msg.content)
replymessage = serviceHandlerMethod(*rpcargs)
elif lofar_msg.has_kwargs:
# only named arguments
# msg.content should be a dict
rpckwargs = lofar_msg.content
replymessage = serviceHandlerMethod(**rpckwargs)
elif lofar_msg.content:
rpccontent = lofar_msg.content
replymessage = serviceHandlerMethod(rpccontent)
else: else:
if msg.has_kwargs=="True": replymessage = serviceHandlerMethod()
# only named arguments
replymessage = service_handler.handle_message(**(msg.content))
else:
replymessage = service_handler.handle_message(msg.content)
self._debug("finished handler") self._debug("finished handler")
self._send_reply(replymessage,"OK",msg.reply_to) self._send_reply(replymessage,"OK",lofar_msg.reply_to)
self.okcounter[thread_idx] += 1 self.okcounter[thread_idx] += 1
self.listener.ack(msg) self.listener.ack(lofar_msg)
try: try:
service_handler.finalize_handling(True) service_handler.finalize_handling(True)
except Exception as e: except Exception as e:
...@@ -365,7 +386,7 @@ class Service(object): ...@@ -365,7 +386,7 @@ class Service(object):
logger.info("[Service:] Status: %s", str(status)) logger.info("[Service:] Status: %s", str(status))
logger.info("[Service:] ERRTXT: %s", str(errtxt)) logger.info("[Service:] ERRTXT: %s", str(errtxt))
logger.info("[Service:] BackTrace: %s", str( backtrace )) logger.info("[Service:] BackTrace: %s", str( backtrace ))
self._send_reply(None, status, msg.reply_to, errtxt=errtxt, backtrace=backtrace) self._send_reply(None, status, lofar_msg.reply_to, errtxt=errtxt, backtrace=backtrace)
try: try:
service_handler.finalize_handling(False) service_handler.finalize_handling(False)
except Exception as e: except Exception as e:
......
...@@ -295,8 +295,8 @@ class RequestMessage(LofarMessage): ...@@ -295,8 +295,8 @@ class RequestMessage(LofarMessage):
#reply_to = kwargs.pop("reply_to",None) #reply_to = kwargs.pop("reply_to",None)
#if (reply_to!=None): #if (reply_to!=None):
self.reply_to = reply_to self.reply_to = reply_to
self.has_args = str(kwargs.pop("has_args",False)) self.has_args = kwargs.pop("has_args",False)
self.has_kwargs = str(kwargs.pop("has_kwargs",False)) self.has_kwargs = kwargs.pop("has_kwargs",False)
class ReplyMessage(LofarMessage): class ReplyMessage(LofarMessage):
""" """
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment