2 from __future__
import print_function
12 client_socket_timeout = 6
13 max_msg_length = 2000000
15 rosbridge_ip =
"localhost"
18 service_name =
"nested_srv"
20 receiving_fragment_size = 1000
21 receive_message_intervall = 0.0
29 service_request_object = {
"op" :
"call_service",
30 "service":
"/"+service_name,
31 "fragment_size": receiving_fragment_size,
32 "message_intervall": receive_message_intervall,
33 "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}}}
36 service_request = json.dumps(service_request_object)
37 print(
"sending JSON-message to rosbridge:", service_request)
38 sock.send(service_request)
47 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
48 sock.settimeout(client_socket_timeout)
49 sock.connect((rosbridge_ip, rosbridge_port))
60 incoming = sock.recv(max_msg_length)
64 print(
"closing socket")
68 buffer = buffer + incoming
71 data_object = json.loads(buffer)
72 if data_object[
"op"] ==
"service_response":
73 reconstructed = buffer
75 except Exception
as e:
81 result_string = buffer.split(
"}{")
83 for fragment
in result_string:
84 if fragment[0] !=
"{":
85 fragment =
"{"+fragment
86 if fragment[len(fragment)-1] !=
"}":
87 fragment = fragment +
"}"
89 result.append(json.loads(fragment))
90 except Exception
as e:
95 fragment_count = len(result)
96 print(
"fragment_count:", fragment_count)
97 announced = int(result[0][
"total"])
98 if fragment_count == announced:
100 sorted_result = [
None] * fragment_count
102 for fragment
in result:
103 unsorted_result.append(fragment)
104 sorted_result[int(fragment[
"num"])] = fragment
106 for fragment
in sorted_result:
107 reconstructed = reconstructed + fragment[
"data"]
109 except Exception
as e:
112 except Exception
as e:
117 returned_data = json.loads(reconstructed)
118 if returned_data[
"values"] ==
None:
119 print(
"response was None -> service was not available")
124 except Exception
as e:
125 print(
"ERROR - could not receive service_response")