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