Skip to content

Commit d42b2e7

Browse files
Merge branch 'laurie/SyncRos1ToRos2' into laurie/RedoRos2
2 parents 624eb02 + aac8042 commit d42b2e7

File tree

10 files changed

+165
-106
lines changed

10 files changed

+165
-106
lines changed

ros_tcp_endpoint/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,4 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from .publisher import RosPublisher
16-
from .subscriber import RosSubscriber
17-
from .service import RosService
1815
from .server import TcpServer
19-
from .unity_service import UnityService

ros_tcp_endpoint/client.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ def read_int32(conn):
7171
num = struct.unpack("<I", raw_bytes)[0]
7272
return num
7373

74-
@staticmethod
75-
def read_string(conn):
74+
def read_string(self):
7675
"""
7776
Reads int32 from socket connection to determine how many bytes to
7877
read to get the string that follows. Read that number of bytes and
@@ -81,30 +80,32 @@ def read_string(conn):
8180
Returns: string
8281
8382
"""
84-
str_len = ClientThread.read_int32(conn)
83+
str_len = ClientThread.read_int32(self.conn)
8584

86-
str_bytes = ClientThread.recvall(conn, str_len)
85+
str_bytes = ClientThread.recvall(self.conn, str_len)
8786
decoded_str = str_bytes.decode("utf-8")
8887

8988
return decoded_str
9089

91-
@staticmethod
92-
def read_message(conn):
90+
def read_message(self, conn):
9391
"""
9492
Decode destination and full message size from socket connection.
9593
Grab bytes in chunks until full message has been read.
9694
"""
9795
data = b""
9896

99-
destination = ClientThread.read_string(conn)
97+
destination = self.read_string()
10098
full_message_size = ClientThread.read_int32(conn)
10199

102100
data = ClientThread.recvall(conn, full_message_size)
103101

104102
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+
)
106106
return
107107

108+
destination = destination.rstrip("\x00")
108109
return destination, data
109110

110111
@staticmethod
@@ -133,8 +134,8 @@ def serialize_message(destination, message):
133134
# SEEK_END or 2 - end of the stream; offset is usually negative
134135
response_len = serial_response.seek(0, 2)
135136

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
138139

139140
return serialized_message
140141

@@ -151,16 +152,16 @@ def serialize_command(command, params):
151152
return cmd_info + json_info
152153

153154
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():
155156
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()
157158
)
158159
self.tcp_server.send_unity_error(error_msg)
159-
rospy.logerr(error_msg)
160+
self.tcp_server.logerr(error_msg)
160161
# TODO: send a response to Unity anyway?
161162
return
162163
else:
163-
ros_communicator = self.tcp_server.ros_services[destination]
164+
ros_communicator = self.tcp_server.ros_services_table[destination]
164165
service_thread = threading.Thread(
165166
target=self.service_call_thread, args=(srv_id, destination, data, ros_communicator)
166167
)
@@ -173,7 +174,7 @@ def service_call_thread(self, srv_id, destination, data, ros_communicator):
173174
if not response:
174175
error_msg = "No response data from service '{}'!".format(destination)
175176
self.tcp_server.send_unity_error(error_msg)
176-
rospy.logerr(error_msg)
177+
self.tcp_server.logerr(error_msg)
177178
# TODO: send a response to Unity anyway?
178179
return
179180

@@ -194,7 +195,7 @@ def run(self):
194195
msg: the ROS msg type as bytes
195196
196197
"""
197-
rospy.loginfo("Connection from {}".format(self.incoming_ip))
198+
self.tcp_server.loginfo("Connection from {}".format(self.incoming_ip))
198199
halt_event = threading.Event()
199200
self.tcp_server.unity_tcp_sender.start_sender(self.conn, halt_event)
200201
try:
@@ -219,18 +220,18 @@ def run(self):
219220
elif destination.startswith("__"):
220221
# handle a system command, such as registering new topics
221222
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]
224225
ros_communicator.send(data)
225226
else:
226227
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()
228229
)
229230
self.tcp_server.send_unity_error(error_msg)
230-
rospy.logerr(error_msg)
231+
self.tcp_server.logerr(error_msg)
231232
except IOError as e:
232-
rospy.logerr("Exception: {}".format(e))
233+
self.tcp_server.logerr("Exception: {}".format(e))
233234
finally:
234235
halt_event.set()
235236
self.conn.close()
236-
rospy.loginfo("Disconnected from {}".format(self.incoming_ip))
237+
self.tcp_server.loginfo("Disconnected from {}".format(self.incoming_ip))

ros_tcp_endpoint/communication.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515

1616
class RosSender:
1717
"""
18-
Base class for ROS communication where data is sent to the ROS network.
18+
Base class for ROS communication where data is sent to the ROS network.
1919
"""
2020

21-
def __init__(self):
21+
def __init__(self, node_name):
22+
self.node_name = node_name
2223
pass
2324

2425
def send(self, *args):
@@ -27,10 +28,11 @@ def send(self, *args):
2728

2829
class RosReceiver:
2930
"""
30-
Base class for ROS communication where data is being sent outside of the ROS network.
31+
Base class for ROS communication where data is being sent outside of the ROS network.
3132
"""
3233

33-
def __init__(self):
34+
def __init__(self, node_name):
35+
self.node_name = node_name
3436
pass
3537

3638
def send(self, *args):

ros_tcp_endpoint/default_server_endpoint.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
from ros_tcp_endpoint import TcpServer
66

77

8-
def main():
9-
ros_node_name = rospy.get_param("/TCP_NODE_NAME", "TCPServer")
10-
tcp_server = TcpServer(ros_node_name)
8+
def main(args=None):
9+
tcp_server = TcpServer("UnityEndpoint")
1110

1211
# Start the Server Endpoint
1312
rospy.init_node(ros_node_name, anonymous=True)

ros_tcp_endpoint/publisher.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class RosPublisher(RosSender):
2222
Class to publish messages to a ROS topic
2323
"""
2424

25+
# TODO: surface latch functionality
2526
def __init__(self, topic, message_class, queue_size=10, latch=False):
2627
"""
2728
@@ -30,7 +31,9 @@ def __init__(self, topic, message_class, queue_size=10, latch=False):
3031
message_class: The message class in catkin workspace
3132
queue_size: Max number of entries to maintain in an outgoing queue
3233
"""
33-
RosSender.__init__(self)
34+
strippedTopic = re.sub("[^A-Za-z0-9_]+", "", topic)
35+
node_name = f"{strippedTopic}_RosPublisher"
36+
RosSender.__init__(self, node_name)
3437
self.msg = message_class()
3538
self.pub = rospy.Publisher(topic, message_class, queue_size=queue_size, latch=latch)
3639

0 commit comments

Comments
 (0)