Go to the documentation of this file.00001
00002 import socket
00003 try:
00004 import ujson as json
00005 except ImportError:
00006 try:
00007 import simplejson as json
00008 except ImportError:
00009 import json
00010
00011
00012
00013
00014
00015
00016
00017 client_socket_timeout = 6
00018 max_msg_length = 2000000
00019
00020 rosbridge_ip = "localhost"
00021 rosbridge_port = 9090
00022
00023 service_name = "nested_srv"
00024
00025 receiving_fragment_size = 1000
00026 receive_message_intervall = 0.0
00027
00028
00029
00030
00031
00032
00033 def request_service():
00034 service_request_object = { "op" : "call_service",
00035 "service": "/"+service_name,
00036 "fragment_size": receiving_fragment_size,
00037 "message_intervall": receive_message_intervall,
00038 "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}}}
00039
00040 }
00041 service_request = json.dumps(service_request_object)
00042 print "sending JSON-message to rosbridge:", service_request
00043 sock.send(service_request)
00044
00045
00046
00047
00048
00049
00050
00051 try:
00052 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
00053 sock.settimeout(client_socket_timeout)
00054 sock.connect((rosbridge_ip, rosbridge_port))
00055
00056 request_service()
00057
00058 incoming = None
00059 buffer = ""
00060 done = False
00061 result = None
00062 reconstructed = None
00063 while not done:
00064 try:
00065 incoming = sock.recv(max_msg_length)
00066 if buffer == "":
00067 buffer = incoming
00068 if incoming == "":
00069 print "closing socket"
00070 sock.close()
00071 break
00072 else:
00073 buffer = buffer + incoming
00074
00075 try:
00076 data_object = json.loads(buffer)
00077 if data_object["op"] == "service_response":
00078 reconstructed = buffer
00079 done = True
00080 except Exception, e:
00081
00082
00083 pass
00084 try:
00085
00086 result_string = buffer.split("}{")
00087 result = []
00088 for fragment in result_string:
00089 if fragment[0] != "{":
00090 fragment = "{"+fragment
00091 if fragment[len(fragment)-1] != "}":
00092 fragment = fragment + "}"
00093 try:
00094 result.append(json.loads(fragment))
00095 except Exception, e:
00096
00097
00098 raise
00099
00100 fragment_count = len(result)
00101 print "fragment_count:", fragment_count
00102 announced = int(result[0]["total"])
00103 if fragment_count == announced:
00104
00105 sorted_result = [None] * fragment_count
00106 unsorted_result = []
00107 for fragment in result:
00108 unsorted_result.append(fragment)
00109 sorted_result[int(fragment["num"])] = fragment
00110 reconstructed = ''
00111 for fragment in sorted_result:
00112 reconstructed = reconstructed + fragment["data"]
00113 done = True
00114 except Exception, e:
00115
00116 pass
00117 except Exception, e:
00118
00119 pass
00120
00121
00122 returned_data = json.loads(reconstructed)
00123 if returned_data["values"] == None:
00124 print "response was None -> service was not available"
00125 else:
00126 print "received:"
00127 print returned_data
00128
00129 except Exception, e:
00130 print "ERROR - could not receive service_response"
00131 print e
00132
00133 sock.close()