service_response.py
Go to the documentation of this file.
00001 from rosbridge_library.capability import Capability
00002 from advertise_service import ReceivedResponses
00003 from rosbridge_library.internal import ros_loader, message_conversion
00004 
00005 # try to import json-lib: 1st try usjon, 2nd try simplejson, else import standard python json
00006 try:
00007     import ujson as json
00008 except ImportError:
00009     try:
00010         import simplejson as json
00011     except ImportError:
00012         import json
00013 
00014 
00015 
00016 class ServiceResponse(Capability):
00017 
00018     # this defines an opcode for incoming responses (seen from rosbridge, response coming from non-ros service provider connected to rosbridge)
00019     #  call_service uses the same opcode for sending responses to non-ros clients that requested a service..
00020     #  this is works fine, but feel free to change opcode-names to whatever you think suits better..
00021     opcode_service_response = "service_response"        # rosbridge-client -> rosbridge # register in protocol.py!
00022     response_list = ReceivedResponses().list
00023 
00024     def __init__(self, protocol):
00025         self.protocol = protocol
00026         Capability.__init__(self, protocol)
00027 
00028         protocol.register_operation(self.opcode_service_response, self.service_response)
00029 
00030 
00031     def service_response(self, message):
00032         # convert the message into according response instance
00033         # then just put response into singleton list for responses.. the requesting process will find it there and clean up after delivery to client
00034 
00035         # get information about the request with the same id as the incoming response
00036         # ..this information gets written into "request_list" by advertise_service.py within "handle_service_request()"
00037         request = self.protocol.request_list[message["request_id"]]
00038 
00039         # get module and type
00040         service_module = request["service_module"]
00041         service_type = request["service_type"]
00042 
00043         ## Create a message instance
00044         inst = ros_loader.get_service_response_instance(service_module+"/"+service_type)
00045         
00046         # Populate the instance, propagating any exceptions that may be thrown
00047         message_conversion.populate_instance(message["data"], inst)
00048 
00049         # add response instance to response_list
00050         self.response_list[message["request_id"]] = inst
00051 
00052 
00053     def finish(self):
00054         self.protocol.unregister_operation("service_response")
00055 
00056 
00057 
00058 
00059 
00060 


rosbridge_library
Author(s): Jonathan Mace
autogenerated on Mon Oct 6 2014 06:58:09