@@ -29,6 +29,7 @@ class ClientThread(threading.Thread):
29
29
Thread class to read all data from a connection and pass along the data to the
30
30
desired source.
31
31
"""
32
+
32
33
def __init__ (self , conn , tcp_server , incoming_ip , incoming_port ):
33
34
"""
34
35
Set class variables
@@ -66,7 +67,7 @@ def read_int32(conn):
66
67
67
68
"""
68
69
raw_bytes = ClientThread .recvall (conn , 4 )
69
- num = struct .unpack ('<I' , raw_bytes )[0 ]
70
+ num = struct .unpack ("<I" , raw_bytes )[0 ]
70
71
return num
71
72
72
73
def read_string (self ):
@@ -81,24 +82,24 @@ def read_string(self):
81
82
str_len = ClientThread .read_int32 (self .conn )
82
83
83
84
str_bytes = ClientThread .recvall (self .conn , str_len )
84
- decoded_str = str_bytes .decode (' utf-8' )
85
+ decoded_str = str_bytes .decode (" utf-8" )
85
86
86
87
return decoded_str
87
88
88
- def read_message (self ):
89
+ def read_message (self , conn ):
89
90
"""
90
91
Decode destination and full message size from socket connection.
91
92
Grab bytes in chunks until full message has been read.
92
93
"""
93
- data = b''
94
+ data = b""
94
95
95
96
destination = self .read_string ()
96
- full_message_size = ClientThread .read_int32 (self . conn )
97
+ full_message_size = ClientThread .read_int32 (conn )
97
98
98
99
while len (data ) < full_message_size :
99
100
# Only grabs max of 1024 bytes TODO: change to TCPServer's buffer_size
100
101
grab = 1024 if full_message_size - len (data ) > 1024 else full_message_size - len (data )
101
- packet = ClientThread .recvall (self . conn , grab )
102
+ packet = ClientThread .recvall (conn , grab )
102
103
103
104
if not packet :
104
105
self .logerr ("No packets..." )
@@ -110,7 +111,7 @@ def read_message(self):
110
111
self .logerr ("No data for a message size of {}, breaking!" .format (full_message_size ))
111
112
return
112
113
113
- destination = destination .rstrip (' \x00 ' )
114
+ destination = destination .rstrip (" \x00 " )
114
115
return destination , data
115
116
116
117
@staticmethod
@@ -125,13 +126,13 @@ def serialize_message(destination, message):
125
126
Returns:
126
127
serialized destination and message as a list of bytes
127
128
"""
128
- dest_bytes = destination .encode (' utf-8' )
129
+ dest_bytes = destination .encode (" utf-8" )
129
130
length = len (dest_bytes )
130
- dest_info = struct .pack (' <I%ss' % length , length , dest_bytes )
131
+ dest_info = struct .pack (" <I%ss" % length , length , dest_bytes )
131
132
132
133
serial_response = serialize_message (message )
133
134
134
- msg_length = struct .pack ('<I' , len (serial_response ))
135
+ msg_length = struct .pack ("<I" , len (serial_response ))
135
136
serialized_message = dest_info + msg_length + serial_response
136
137
137
138
return serialized_message
@@ -147,7 +148,7 @@ def serialize_command(command, params):
147
148
json_info = struct .pack ("<I%ss" % json_length , json_length , json_bytes )
148
149
149
150
return cmd_info + json_info
150
-
151
+
151
152
def send_ros_service_request (self , srv_id , destination , data ):
152
153
if destination not in self .tcp_server .source_destination_dict .keys ():
153
154
error_msg = "Service destination '{}' is not registered! Known topics are: {} " .format (
@@ -159,17 +160,17 @@ def send_ros_service_request(self, srv_id, destination, data):
159
160
return
160
161
else :
161
162
ros_communicator = self .tcp_server .source_destination_dict [destination ]
162
- service_thread = threading .Thread (target = self .service_call_thread , args = (srv_id , destination , data , ros_communicator ))
163
+ service_thread = threading .Thread (
164
+ target = self .service_call_thread , args = (srv_id , destination , data , ros_communicator )
165
+ )
163
166
service_thread .daemon = True
164
167
service_thread .start ()
165
168
166
169
def service_call_thread (self , srv_id , destination , data , ros_communicator ):
167
170
response = ros_communicator .send (data )
168
-
171
+
169
172
if not response :
170
- error_msg = "No response data from service '{}'!" .format (
171
- destination
172
- )
173
+ error_msg = "No response data from service '{}'!" .format (destination )
173
174
self .tcp_server .send_unity_error (error_msg )
174
175
self .logerr (error_msg )
175
176
# TODO: send a response to Unity anyway?
@@ -182,7 +183,7 @@ def loginfo(self, text):
182
183
183
184
def logerr (self , text ):
184
185
self .tcp_server .get_logger ().error (text )
185
-
186
+
186
187
def run (self ):
187
188
"""
188
189
Read a message and determine where to send it based on the source_destination_dict
@@ -203,32 +204,37 @@ def run(self):
203
204
self .tcp_server .unity_tcp_sender .start_sender (self .conn , halt_event )
204
205
try :
205
206
while not halt_event .is_set ():
206
- destination , data = self .read_message ()
207
+ destination , data = self .read_message (self . conn )
207
208
208
209
if self .tcp_server .pending_srv_id is not None :
209
210
# if we've been told that the next message will be a service request/response, process it as such
210
211
if self .tcp_server .pending_srv_is_request :
211
- self .send_ros_service_request (self .tcp_server .pending_srv_id , destination , data )
212
+ self .send_ros_service_request (
213
+ self .tcp_server .pending_srv_id , destination , data
214
+ )
212
215
else :
213
- self .tcp_server .send_unity_service_response (self .tcp_server .pending_srv_id , data )
216
+ self .tcp_server .send_unity_service_response (
217
+ self .tcp_server .pending_srv_id , data
218
+ )
214
219
self .tcp_server .pending_srv_id = None
215
220
elif destination == "" :
216
- #ignore this keepalive message, listen for more
221
+ # ignore this keepalive message, listen for more
217
222
pass
218
223
elif destination .startswith ("__" ):
219
- #handle a system command, such as registering new topics
224
+ # handle a system command, such as registering new topics
220
225
self .tcp_server .handle_syscommand (destination , data )
221
226
elif destination in self .tcp_server .source_destination_dict :
222
227
ros_communicator = self .tcp_server .source_destination_dict [destination ]
223
228
response = ros_communicator .send (data )
224
229
else :
225
- error_msg = "Topic '{}' is not registered! Known topics are: {} " \
226
- .format (destination , self .tcp_server .source_destination_dict .keys ())
230
+ error_msg = "Topic '{}' is not registered! Known topics are: {} " .format (
231
+ destination , self .tcp_server .source_destination_dict .keys ()
232
+ )
227
233
self .tcp_server .send_unity_error (error_msg )
228
234
self .logerr (error_msg )
229
235
except IOError as e :
230
236
self .logerr ("Exception: {}" .format (e ))
231
237
finally :
232
238
halt_event .set ()
233
239
self .conn .close ()
234
- self .loginfo ("Disconnected from {}" .format (self .incoming_ip ));
240
+ self .loginfo ("Disconnected from {}" .format (self .incoming_ip ))
0 commit comments