2 from __future__
import print_function
6 from random
import randint
14 tcp_socket_timeout = 10
15 max_msg_length = 20000
17 rosbridge_ip =
"localhost"
20 service_type =
"rosbridge_library/TestNestedService"
21 service_name =
"nested_srv"
23 send_fragment_size = 1000
25 send_fragment_delay = 0.000
26 receive_fragment_size = 10
27 receive_message_intervall = 0.0
37 request_object = json.loads(request)
38 args = request_object[
"args"]
47 message = {
"data": {
"data": 42.0}}
51 use base64 encoding to avoid JSON-parsing problems!
52 --> use .decode("base64","strict") at client side
55 service_response_data = message
57 response_object = {
"op":
"service_response",
58 "id": request_object[
"id"],
59 "data": service_response_data
61 response_message = json.dumps(response_object)
62 return response_message
75 tcp_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
76 tcp_sock.settimeout(tcp_socket_timeout)
77 tcp_sock.connect((rosbridge_ip, rosbridge_port))
81 advertise_message_object = {
"op":
"advertise_service",
83 "service": service_name,
84 "fragment_size": receive_fragment_size,
85 "message_intervall": receive_message_intervall
87 advertise_message = json.dumps(advertise_message_object)
88 tcp_socket.send(str(advertise_message))
91 unadvertise_message_object = {
"op":
"unadvertise_service",
92 "service": service_name
94 unadvertise_message = json.dumps(unadvertise_message_object)
95 tcp_socket.send(str(unadvertise_message))
105 incoming = tcp_socket.recv(max_msg_length)
107 print(
"connection closed by peer")
109 buffer = buffer + incoming
111 data_object = json.loads(buffer)
112 if data_object[
"op"] ==
"call_service":
116 except Exception
as e:
123 result_string = buffer.split(
"}{")
125 for fragment
in result_string:
126 if fragment[0] !=
"{":
127 fragment =
"{"+fragment
128 if fragment[len(fragment)-1] !=
"}":
129 fragment = fragment +
"}"
130 result.append(json.loads(fragment))
133 fragment_count = len(result)
134 announced = int(result[0][
"total"])
136 if fragment_count == announced:
138 sorted_result = [
None] * fragment_count
140 for fragment
in result:
141 unsorted_result.append(fragment)
142 sorted_result[int(fragment[
"num"])] = fragment
144 for fragment
in sorted_result:
145 reconstructed = reconstructed + fragment[
"data"]
150 print(
"reconstructed message from", len(result),
"fragments")
153 except Exception
as e:
154 print(
"not possible to defragment:", buffer)
156 except Exception
as e:
157 print(
"defrag_error:", buffer)
160 except Exception
as e:
166 tcp_socket.send(response)
169 message_id = randint(0,64000)
172 while cursor < len(full_message):
173 fragment_begin = cursor
174 if len(full_message) < cursor + fragment_size:
175 fragment_end = len(full_message)
176 cursor = len(full_message)
178 fragment_end = cursor + fragment_size
179 cursor += fragment_size
180 fragment = full_message[fragment_begin:fragment_end]
181 fragments.append(fragment)
183 fragmented_messages_list = []
184 if len(fragments) > 1:
185 for count, fragment
in enumerate(fragments):
186 fragmented_message_object = {
"op":
"fragment",
187 "id": str(message_id),
188 "data": str(fragment),
190 "total": len(fragments)
192 fragmented_message = json.dumps(fragmented_message_object)
193 fragmented_messages_list.append(fragmented_message)
195 fragmented_messages_list.append(str(fragment))
196 return fragmented_messages_list
207 print(
"service provider started and waiting for requests")
216 elif data !=
None and len(data) > 0:
219 print(
"response calculated, now splitting into fragments..")
222 print(
"sending", len(fragment_list),
"messages as response")
223 for fragment
in fragment_list:
226 time.sleep(send_fragment_delay)
227 except Exception
as e:
230 except KeyboardInterrupt:
234 except Exception
as e:
236 print(
"non-ros_service_server stopped because user pressed \"Ctrl-C\"")