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/SendBytes" 21 service_name =
"send_bytes" 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"]
39 count = int(args[
"count"] )
43 for i
in range(0,count):
45 message+= str(chr(randint(32,126)))
47 print(count - i,
"bytes left to generate")
51 use base64 encoding to avoid JSON-parsing problems! 52 --> use .decode("base64","strict") at client side 54 message = message.encode(
'base64',
'strict')
55 service_response_data = {
"data": message}
57 response_object = {
"op":
"service_response",
58 "id": request_object[
"id"],
59 "service": service_name,
60 "values": service_response_data
62 response_message = json.dumps(response_object)
63 return response_message
76 tcp_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
77 tcp_sock.settimeout(tcp_socket_timeout)
78 tcp_sock.connect((rosbridge_ip, rosbridge_port))
82 advertise_message_object = {
"op":
"advertise_service",
84 "service": service_name,
85 "fragment_size": receive_fragment_size,
86 "message_intervall": receive_message_intervall
88 advertise_message = json.dumps(advertise_message_object)
89 tcp_socket.send(str(advertise_message))
92 unadvertise_message_object = {
"op":
"unadvertise_service",
93 "service": service_name
95 unadvertise_message = json.dumps(unadvertise_message_object)
96 tcp_socket.send(str(unadvertise_message))
106 incoming = tcp_socket.recv(max_msg_length)
108 print(
"connection closed by peer")
110 buffer = buffer + incoming
112 data_object = json.loads(buffer)
113 if data_object[
"op"] ==
"call_service":
117 except Exception
as e:
124 result_string = buffer.split(
"}{")
126 for fragment
in result_string:
127 if fragment[0] !=
"{":
128 fragment =
"{"+fragment
129 if fragment[len(fragment)-1] !=
"}":
130 fragment = fragment +
"}" 131 result.append(json.loads(fragment))
134 fragment_count = len(result)
135 announced = int(result[0][
"total"])
137 if fragment_count == announced:
139 sorted_result = [
None] * fragment_count
141 for fragment
in result:
142 unsorted_result.append(fragment)
143 sorted_result[int(fragment[
"num"])] = fragment
145 for fragment
in sorted_result:
146 reconstructed = reconstructed + fragment[
"data"]
151 print(
"reconstructed message from", len(result),
"fragments")
154 except Exception
as e:
155 print(
"not possible to defragment:", buffer)
157 except Exception
as e:
158 print(
"defrag_error:", buffer)
161 except Exception
as e:
167 tcp_socket.send(response)
170 message_id = randint(0,64000)
173 while cursor < len(full_message):
174 fragment_begin = cursor
175 if len(full_message) < cursor + fragment_size:
176 fragment_end = len(full_message)
177 cursor = len(full_message)
179 fragment_end = cursor + fragment_size
180 cursor += fragment_size
181 fragment = full_message[fragment_begin:fragment_end]
182 fragments.append(fragment)
184 fragmented_messages_list = []
185 if len(fragments) > 1:
186 for count, fragment
in enumerate(fragments):
187 fragmented_message_object = {
"op":
"fragment",
188 "id": str(message_id),
189 "data": str(fragment),
191 "total": len(fragments)
193 fragmented_message = json.dumps(fragmented_message_object)
194 fragmented_messages_list.append(fragmented_message)
196 fragmented_messages_list.append(str(fragment))
197 return fragmented_messages_list
208 print(
"service provider started and waiting for requests")
217 elif data !=
None and len(data) > 0:
220 print(
"response calculated, now splitting into fragments..")
223 print(
"sending", len(fragment_list),
"messages as response")
224 for fragment
in fragment_list:
227 time.sleep(send_fragment_delay)
228 except Exception
as e:
231 except KeyboardInterrupt:
235 except Exception
as e:
237 print(
"non-ros_service_server stopped because user pressed \"Ctrl-C\"")
def wait_for_service_request()
def list_of_fragments(full_message, fragment_size)
def unadvertise_service()
def send_service_response(response)
def calculate_service_response(request)
variables end ##########################################