def unadvertise_service(): # unadvertise service unadvertise_message_object = { "op": "unadvertise_service", "service": service_name } unadvertise_message = json.dumps(unadvertise_message_object) tcp_socket.send(str(unadvertise_message))
def list_of_fragments(full_message, fragment_size): # create fragment messages for a huge message message_id = randint(0,64000) # generate random message id fragments = [] # generate list of data fragments cursor = 0 while cursor < len(full_message): fragment_begin = cursor if len(full_message) < cursor + fragment_size: fragment_end = len(full_message) cursor = len(full_message) else: fragment_end = cursor + fragment_size cursor += fragment_size fragment = full_message[fragment_begin:fragment_end] fragments.append(fragment) fragmented_messages_list = [] # generate list of fragmented messages (including headers) if len(fragments) > 1: for count, fragment in enumerate(fragments): # iterate through list and have index counter fragmented_message_object = {"op":"fragment", # create python-object for each fragment message "id": str(message_id), "data": str(fragment), "num": count, "total": len(fragments) } fragmented_message = json.dumps(fragmented_message_object) # create JSON-object from python-object for each fragment message fragmented_messages_list.append(fragmented_message) # append JSON-object to list of fragmented messages else: # if only 1 fragment --> do not send as fragment, but as service_response fragmented_messages_list.append(str(fragment)) return fragmented_messages_list # return list of 'ready-to-send' fragmented messages
def calculate_service_response(request): request_object = json.loads(request) # parse string for service request args = request_object["args"] # get parameter field (args) # count = int(args["count"] ) # get parameter(s) as described in corresponding ROS srv-file # # message = "" # calculate service response # for i in range(0,count): # message += str(chr(randint(32,126))) # if i% 100000 == 0: # print count - i, "bytes left to generate" message = {"data": {"data": 42.0}} """ IMPORTANT! use base64 encoding to avoid JSON-parsing problems! --> use .decode("base64","strict") at client side """ #message = message.encode('base64','strict') service_response_data = message # service response (as defined in srv-file) response_object = { "op": "service_response", "id": request_object["id"], "data": service_response_data # put service response in "data"-field of response object (in this case it's twice "data", because response value is also named data (in srv-file) } response_message = json.dumps(response_object) return response_message
def request_service(): service_request_object = { "op": "call_service", "service": "/" + service_name, # select service "fragment_size": receiving_fragment_size, # optional: tells rosbridge to send fragments if # message size is bigger than requested "message_intervall": receive_message_intervall, "args": { "pose": { "position": { "y": 0.0, "x": 0.0, "z": 0.0 }, "orientation": { "y": 0.0, "x": 0.0, "z": 0.0, "w": 0.0 } } } } service_request = json.dumps(service_request_object) print "sending JSON-message to rosbridge:", service_request sock.send(service_request)
def calculate_service_response(request): request_object = json.loads(request) # parse string for service request args = request_object["args"] # get parameter field (args) count = int(args["count"]) # get parameter(s) as described in corresponding ROS srv-file message = "" # calculate service response for i in range(0, count): # message += str(chr(randint(32,126))) message += str(chr(randint(32, 126))) if i % 100000 == 0: print count - i, "bytes left to generate" """ IMPORTANT! use base64 encoding to avoid JSON-parsing problems! --> use .decode("base64","strict") at client side """ message = message.encode('base64', 'strict') service_response_data = {"data": message} # service response (as defined in srv-file) response_object = { "op": "service_response", "id": request_object["id"], "data": service_response_data } response_message = json.dumps(response_object) return response_message
def request_service(): service_request_object = { "op": "call_service", # op-code for rosbridge "service": "/" + service_name, # select service "fragment_size": receiving_fragment_size, # optional: tells rosbridge to send fragments if message size is bigger than requested "message_intervall": receive_message_intervall, "args": { "pose": { "position": { "y": 0.0, "x": 0.0, "z": 0.0 }, "orientation": { "y": 0.0, "x": 0.0, "z": 0.0, "w": 0.0 } } } #"count" : request_byte_count # count is the parameter for send_bytes as defined in srv-file (always put into args field!) } service_request = json.dumps(service_request_object) print "sending JSON-message to rosbridge:", service_request sock.send(service_request)
def send(self, message, cid=None): """ Called internally in preparation for sending messages to the client This method pre-processes the message then passes it to the overridden outgoing method. Keyword arguments: message -- a dict of message values to be marshalled and sent cid -- (optional) an associated id """ serialized = self.serialize(message, cid) if serialized is not None: if self.png == "png": # TODO: png compression on outgoing messages # encode message pass fragment_list = None if self.fragment_size is not None and len( serialized) > self.fragment_size: mid = message.get("id", None) fragment_list = Fragmentation(self).fragment( message, self.fragment_size, mid) # fragment list not empty -> send fragments if fragment_list is not None: for fragment in fragment_list: self.outgoing(json.dumps(fragment)) time.sleep(self.delay_between_messages) # else send message as it is else: self.outgoing(serialized) time.sleep(self.delay_between_messages)
def calculate_service_response(request): request_object = json.loads(request) # parse string for service request args = request_object["args"] # get parameter field (args) count = int(args["count"] ) # get parameter(s) as described in corresponding ROS srv-file message = "" # calculate service response for i in range(0,count): #message += str(chr(randint(32,126))) message+= str(chr(randint(32,126))) if i% 100000 == 0: print count - i, "bytes left to generate" """ IMPORTANT! use base64 encoding to avoid JSON-parsing problems! --> use .decode("base64","strict") at client side """ message = message.encode('base64','strict') service_response_data = { "data": message} # service response (as defined in srv-file) response_object = { "op": "service_response", "id": request_object["id"], "data": service_response_data # put service response in "data"-field of response object (in this case it's twice "data", because response value is also named data (in srv-file) } response_message = json.dumps(response_object) return response_message
def serialize(self, msg, cid=None): """ Turns a dictionary of values into the appropriate wire-level representation. Default behaviour uses JSON. Override to use a different container. Keyword arguments: msg -- the dictionary of values to serialize cid -- (optional) an ID associated with this. Will be logged on err. Returns a JSON string representing the dictionary """ try: if has_binary(msg) or self.bson_only_mode: return bson.BSON.encode(msg) else: return json.dumps(msg) except: if cid is not None: # Only bother sending the log message if there's an id self.log( "error", "Unable to serialize %s message to client" % msg["op"], cid) return None
def advertise_service(): # advertise service advertise_message_object = {"op":"advertise_service", "type": service_type, "service": service_name, "fragment_size": receive_fragment_size, "message_intervall": receive_message_intervall } advertise_message = json.dumps(advertise_message_object) tcp_socket.send(str(advertise_message))
def advertise_service(): # advertise service advertise_message_object = { "op": "advertise_service", "type": service_type, "service": service_name, "fragment_size": receive_fragment_size, "message_intervall": receive_message_intervall } advertise_message = json.dumps(advertise_message_object) tcp_socket.send(str(advertise_message))
def request_service(): service_request_object = { "op" : "call_service", # op-code for rosbridge "service": "/"+service_name, # select service "fragment_size": receiving_fragment_size, # optional: tells rosbridge to send fragments if message size is bigger than requested "message_intervall": receive_message_intervall, "args": { "count" : request_byte_count # count is the parameter for send_bytes as defined in srv-file (always put into args field!) } } service_request = json.dumps(service_request_object) print "sending JSON-message to rosbridge:", service_request sock.send(service_request)
def send(self, message, cid=None): """ Called internally in preparation for sending messages to the client This method pre-processes the message then passes it to the overridden outgoing method. Keyword arguments: message -- a dict of message values to be marshalled and sent cid -- (optional) an associated id """ serialized = self.serialize(message, cid) if serialized is not None: if self.png == "png": # TODO: png compression on outgoing messages # encode message pass fragment_list = None if self.fragment_size != None and len( serialized) > self.fragment_size: mid = message.get("id", None) # TODO: think about splitting into fragments that have specified size including header-fields! # --> estimate header size --> split content into fragments that have the requested overall size, rather than requested content size fragment_list = Fragmentation(self).fragment( message, self.fragment_size, mid) # fragment list not empty -> send fragments if fragment_list != None: for fragment in fragment_list: if self.bson_only_mode: self.outgoing(bson.BSON.encode(fragment)) else: self.outgoing(json.dumps(fragment)) # okay to use delay here (sender's send()-function) because rosbridge is sending next request only to service provider when last one had finished) # --> if this was not the case this delay needed to be implemented in service-provider's (meaning message receiver's) send_message()-function in rosbridge_tcp.py) time.sleep(self.delay_between_messages) # else send message as it is else: self.outgoing(serialized) time.sleep(self.delay_between_messages)
def serialize(self, msg, cid=None): """ Turns a dictionary of values into the appropriate wire-level representation. Default behaviour uses JSON. Override to use a different container. Keyword arguments: msg -- the dictionary of values to serialize cid -- (optional) an ID associated with this. Will be logged on err. Returns a JSON string representing the dictionary """ try: return json.dumps(msg) except: if cid is not None: # Only bother sending the log message if there's an id self.log("error", "Unable to serialize %s message to client" % msg["op"], cid) return None
def calculate_service_response(request): request_object = json.loads(request) # parse string for service request args = request_object["args"] message = {"data": {"data": 42.0}} """ IMPORTANT! use base64 encoding to avoid JSON-parsing problems! --> use .decode("base64","strict") at client side """ # message = message.encode('base64','strict') service_response_data = message response_object = { "op": "service_response", "id": request_object["id"], "data": service_response_data } response_message = json.dumps(response_object) return response_message
def send(self, message, cid=None): """ Called internally in preparation for sending messages to the client This method pre-processes the message then passes it to the overridden outgoing method. Keyword arguments: message -- a dict of message values to be marshalled and sent cid -- (optional) an associated id """ serialized = self.serialize(message, cid) if serialized is not None: if self.png == "png": # TODO: png compression on outgoing messages # encode message pass fragment_list = None if self.fragment_size != None and len(serialized) > self.fragment_size: mid = message.get("id", None) # TODO: think about splitting into fragments that have specified size including header-fields! # --> estimate header size --> split content into fragments that have the requested overall size, rather than requested content size fragment_list = Fragmentation(self).fragment(message, self.fragment_size, mid ) # fragment list not empty -> send fragments if fragment_list != None: for fragment in fragment_list: if self.bson_only_mode: self.outgoing(bson.BSON.encode(fragment)) else: self.outgoing(json.dumps(fragment)) # okay to use delay here (sender's send()-function) because rosbridge is sending next request only to service provider when last one had finished) # --> if this was not the case this delay needed to be implemented in service-provider's (meaning message receiver's) send_message()-function in rosbridge_tcp.py) time.sleep(self.delay_between_messages) # else send message as it is else: self.outgoing(serialized) time.sleep(self.delay_between_messages)
def list_of_fragments( full_message, fragment_size): # create fragment messages for a huge message message_id = randint(0, 64000) # generate random message id fragments = [] # generate list of data fragments cursor = 0 while cursor < len(full_message): fragment_begin = cursor if len(full_message) < cursor + fragment_size: fragment_end = len(full_message) cursor = len(full_message) else: fragment_end = cursor + fragment_size cursor += fragment_size fragment = full_message[fragment_begin:fragment_end] fragments.append(fragment) fragmented_messages_list = [ ] # generate list of fragmented messages (including headers) if len(fragments) > 1: for count, fragment in enumerate( fragments): # iterate through list and have index counter fragmented_message_object = { "op": "fragment", # create python-object for each fragment message "id": str(message_id), "data": str(fragment), "num": count, "total": len(fragments) } fragmented_message = json.dumps( fragmented_message_object ) # create JSON-object from python-object for each fragment message fragmented_messages_list.append( fragmented_message ) # append JSON-object to list of fragmented messages else: # if only 1 fragment --> do not send as fragment, but as service_response fragmented_messages_list.append(str(fragment)) return fragmented_messages_list # return list of 'ready-to-send' fragmented messages
def list_of_fragments(full_message, fragment_size): message_id = randint(0, 64000) # generate random message id fragments = [] # generate list of data fragments cursor = 0 while cursor < len(full_message): fragment_begin = cursor if len(full_message) < cursor + fragment_size: fragment_end = len(full_message) cursor = len(full_message) else: fragment_end = cursor + fragment_size cursor += fragment_size fragment = full_message[fragment_begin:fragment_end] fragments.append(fragment) fragmented_messages_list = [] # generate list of fragmented messages (including headers) if len(fragments) > 1: for count, fragment in enumerate(fragments): fragmented_message_object = { "op": "fragment", "id": str(message_id), "data": str(fragment), "num": count, "total": len(fragments) } fragmented_message = json.dumps(fragmented_message_object) # create JSON-object from # python-object for each fragment # message fragmented_messages_list.append(fragmented_message) # append JSON-object to list of # fragmented messages else: fragmented_messages_list.append(str(fragment)) return fragmented_messages_list
def unadvertise_service(): # unadvertise service unadvertise_message_object = {"op":"unadvertise_service", "service": service_name } unadvertise_message = json.dumps(unadvertise_message_object) tcp_socket.send(str(unadvertise_message))
def get_pose(self, target, source, time): msg = {"op": "subscribe", "id": "gym_ros", "topic": "/tf", "queue_length": 20} self.send(json.dumps(msg))