@@ -71,8 +71,7 @@ def read_int32(conn):
71
71
num = struct .unpack ("<I" , raw_bytes )[0 ]
72
72
return num
73
73
74
- @staticmethod
75
- def read_string (conn ):
74
+ def read_string (self ):
76
75
"""
77
76
Reads int32 from socket connection to determine how many bytes to
78
77
read to get the string that follows. Read that number of bytes and
@@ -81,30 +80,32 @@ def read_string(conn):
81
80
Returns: string
82
81
83
82
"""
84
- str_len = ClientThread .read_int32 (conn )
83
+ str_len = ClientThread .read_int32 (self . conn )
85
84
86
- str_bytes = ClientThread .recvall (conn , str_len )
85
+ str_bytes = ClientThread .recvall (self . conn , str_len )
87
86
decoded_str = str_bytes .decode ("utf-8" )
88
87
89
88
return decoded_str
90
89
91
- @staticmethod
92
- def read_message (conn ):
90
+ def read_message (self , conn ):
93
91
"""
94
92
Decode destination and full message size from socket connection.
95
93
Grab bytes in chunks until full message has been read.
96
94
"""
97
95
data = b""
98
96
99
- destination = ClientThread .read_string (conn )
97
+ destination = self .read_string ()
100
98
full_message_size = ClientThread .read_int32 (conn )
101
99
102
100
data = ClientThread .recvall (conn , full_message_size )
103
101
104
102
if full_message_size > 0 and not data :
105
- rospy .logerr ("No data for a message size of {}, breaking!" .format (full_message_size ))
103
+ self .tcp_server .logerr (
104
+ "No data for a message size of {}, breaking!" .format (full_message_size )
105
+ )
106
106
return
107
107
108
+ destination = destination .rstrip ("\x00 " )
108
109
return destination , data
109
110
110
111
@staticmethod
@@ -133,8 +134,8 @@ def serialize_message(destination, message):
133
134
# SEEK_END or 2 - end of the stream; offset is usually negative
134
135
response_len = serial_response .seek (0 , 2 )
135
136
136
- msg_length = struct .pack ("<I" , response_len )
137
- serialized_message = dest_info + msg_length + serial_response . getvalue ()
137
+ msg_length = struct .pack ("<I" , len ( serial_response ) )
138
+ serialized_message = dest_info + msg_length + serial_response
138
139
139
140
return serialized_message
140
141
@@ -151,16 +152,16 @@ def serialize_command(command, params):
151
152
return cmd_info + json_info
152
153
153
154
def send_ros_service_request (self , srv_id , destination , data ):
154
- if destination not in self .tcp_server .ros_services .keys ():
155
+ if destination not in self .tcp_server .ros_services_table .keys ():
155
156
error_msg = "Service destination '{}' is not registered! Known services are: {} " .format (
156
- destination , self .tcp_server .ros_services .keys ()
157
+ destination , self .tcp_server .ros_services_table .keys ()
157
158
)
158
159
self .tcp_server .send_unity_error (error_msg )
159
- rospy .logerr (error_msg )
160
+ self . tcp_server .logerr (error_msg )
160
161
# TODO: send a response to Unity anyway?
161
162
return
162
163
else :
163
- ros_communicator = self .tcp_server .ros_services [destination ]
164
+ ros_communicator = self .tcp_server .ros_services_table [destination ]
164
165
service_thread = threading .Thread (
165
166
target = self .service_call_thread , args = (srv_id , destination , data , ros_communicator )
166
167
)
@@ -173,7 +174,7 @@ def service_call_thread(self, srv_id, destination, data, ros_communicator):
173
174
if not response :
174
175
error_msg = "No response data from service '{}'!" .format (destination )
175
176
self .tcp_server .send_unity_error (error_msg )
176
- rospy .logerr (error_msg )
177
+ self . tcp_server .logerr (error_msg )
177
178
# TODO: send a response to Unity anyway?
178
179
return
179
180
@@ -194,7 +195,7 @@ def run(self):
194
195
msg: the ROS msg type as bytes
195
196
196
197
"""
197
- rospy .loginfo ("Connection from {}" .format (self .incoming_ip ))
198
+ self . tcp_server .loginfo ("Connection from {}" .format (self .incoming_ip ))
198
199
halt_event = threading .Event ()
199
200
self .tcp_server .unity_tcp_sender .start_sender (self .conn , halt_event )
200
201
try :
@@ -219,18 +220,18 @@ def run(self):
219
220
elif destination .startswith ("__" ):
220
221
# handle a system command, such as registering new topics
221
222
self .tcp_server .handle_syscommand (destination , data )
222
- elif destination in self .tcp_server .publishers :
223
- ros_communicator = self .tcp_server .publishers [destination ]
223
+ elif destination in self .tcp_server .publishers_table :
224
+ ros_communicator = self .tcp_server .publishers_table [destination ]
224
225
ros_communicator .send (data )
225
226
else :
226
227
error_msg = "Not registered to publish topic '{}'! Valid publish topics are: {} " .format (
227
- destination , self .tcp_server .publishers .keys ()
228
+ destination , self .tcp_server .publishers_table .keys ()
228
229
)
229
230
self .tcp_server .send_unity_error (error_msg )
230
- rospy .logerr (error_msg )
231
+ self . tcp_server .logerr (error_msg )
231
232
except IOError as e :
232
- rospy .logerr ("Exception: {}" .format (e ))
233
+ self . tcp_server .logerr ("Exception: {}" .format (e ))
233
234
finally :
234
235
halt_event .set ()
235
236
self .conn .close ()
236
- rospy .loginfo ("Disconnected from {}" .format (self .incoming_ip ))
237
+ self . tcp_server .loginfo ("Disconnected from {}" .format (self .incoming_ip ))
0 commit comments