test_non-ros_service_client_complex-srv.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 from __future__ import print_function
3 import socket
4 from rosbridge_library.util import json
5 
6 
7 
8 
11 
12 client_socket_timeout = 6 # seconds
13 max_msg_length = 2000000 # bytes
14 
15 rosbridge_ip = "localhost" # hostname or ip
16 rosbridge_port = 9090 # port as integer
17 
18 service_name = "nested_srv" # service name
19 #request_byte_count = 5000
20 receiving_fragment_size = 1000
21 receive_message_intervall = 0.0
22 
23 
24 
25 
26 
27 
29  service_request_object = { "op" : "call_service", # op-code for rosbridge
30  "service": "/"+service_name, # select service
31  "fragment_size": receiving_fragment_size, # optional: tells rosbridge to send fragments if message size is bigger than requested
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}}}
34  #"count" : request_byte_count # count is the parameter for send_bytes as defined in srv-file (always put into args field!)
35  }
36  service_request = json.dumps(service_request_object)
37  print("sending JSON-message to rosbridge:", service_request)
38  sock.send(service_request)
39 
40 
41 
42 
43 
46 try:
47  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #connect to rosbridge
48  sock.settimeout(client_socket_timeout)
49  sock.connect((rosbridge_ip, rosbridge_port))
50 
51  request_service() # send service_request
52 
53  incoming = None
54  buffer = ""
55  done = False
56  result = None
57  reconstructed = None
58  while not done: # should not need a loop (maximum wait can be set by client_socket_timeout), but since its for test/demonstration only .. leave it as it is for now
59  try:
60  incoming = sock.recv(max_msg_length) # receive service_response from rosbridge
61  if buffer == "":
62  buffer = incoming
63  if incoming == "":
64  print("closing socket")
65  sock.close()
66  break
67  else:
68  buffer = buffer + incoming
69  #print "buffer-length:", len(buffer)
70  try: # try to access service_request directly (not fragmented)
71  data_object = json.loads(buffer)
72  if data_object["op"] == "service_response":
73  reconstructed = buffer
74  done = True
75  except Exception as e:
76  #print "direct access to JSON failed.."
77  #print e
78  pass
79  try:
80  #print "defragmenting incoming messages"
81  result_string = buffer.split("}{") # split buffer into fragments and re-fill curly brackets
82  result = []
83  for fragment in result_string:
84  if fragment[0] != "{":
85  fragment = "{"+fragment
86  if fragment[len(fragment)-1] != "}":
87  fragment = fragment + "}"
88  try:
89  result.append(json.loads(fragment)) # try to parse json from string, and append if successful
90  except Exception as e:
91  #print e
92  #print result_string
93  raise # re-raise the last exception, allows to see and continue with processing of exception
94 
95  fragment_count = len(result)
96  print("fragment_count:", fragment_count)
97  announced = int(result[0]["total"])
98  if fragment_count == announced: # if all fragments received --> sort and defragment
99  # sort fragments
100  sorted_result = [None] * fragment_count
101  unsorted_result = []
102  for fragment in result:
103  unsorted_result.append(fragment)
104  sorted_result[int(fragment["num"])] = fragment
105  reconstructed = ''
106  for fragment in sorted_result:
107  reconstructed = reconstructed + fragment["data"]
108  done = True
109  except Exception as e:
110  #print e
111  pass
112  except Exception as e:
113 # print e
114  pass
115 
116 
117  returned_data = json.loads(reconstructed) # when service response is received --> access it (as defined in srv-file)
118  if returned_data["values"] == None:
119  print("response was None -> service was not available")
120  else:
121  print("received:")
122  print(returned_data)#["values"]#["data"].decode('base64','strict') # decode values-field
123 
124 except Exception as e:
125  print("ERROR - could not receive service_response")
126  print(e)
127 
128 sock.close() # close socket


rosbridge_library
Author(s): Jonathan Mace
autogenerated on Fri Oct 21 2022 02:45:18