1717import json
1818import sys
1919import threading
20+ import importlib
2021
2122from .tcp_sender import UnityTcpSender
2223from .client import ClientThread
@@ -29,7 +30,7 @@ class TcpServer:
2930 Initializes ROS node and TCP server.
3031 """
3132
32- def __init__ (self , node_name , buffer_size = 1024 , connections = 10 ):
33+ def __init__ (self , node_name , buffer_size = 1024 , connections = 10 , tcp_ip = "" , tcp_port = - 1 ):
3334 """
3435 Initializes ROS node and class variables.
3536
@@ -38,8 +39,15 @@ def __init__(self, node_name, buffer_size=1024, connections=10):
3839 buffer_size: The read buffer size used when reading from a socket
3940 connections: Max number of queued connections. See Python Socket documentation
4041 """
41- self .tcp_ip = rospy .get_param ("/ROS_IP" )
42- self .tcp_port = rospy .get_param ("/ROS_TCP_PORT" , 10000 )
42+ if tcp_ip != "" :
43+ self .tcp_ip = tcp_ip
44+ else :
45+ self .tcp_ip = rospy .get_param ("/ROS_IP" )
46+
47+ if tcp_port != - 1 :
48+ self .tcp_port = tcp_port
49+ else :
50+ self .tcp_port = rospy .get_param ("/ROS_TCP_PORT" , 10000 )
4351
4452 unity_machine_ip = rospy .get_param ("/UNITY_IP" , '' )
4553 unity_machine_port = rospy .get_param ("/UNITY_SERVER_PORT" , 5005 )
@@ -51,7 +59,9 @@ def __init__(self, node_name, buffer_size=1024, connections=10):
5159 self .connections = connections
5260 self .syscommands = SysCommands (self )
5361
54- def start (self ):
62+ def start (self , source_destination_dict = None ):
63+ if source_destination_dict is not None :
64+ self .source_destination_dict = source_destination_dict
5565 server_thread = threading .Thread (target = self .listen_loop )
5666 # Exit the server thread when the main thread terminates
5767 server_thread .daemon = True
@@ -82,6 +92,9 @@ def send_unity_error(self, error):
8292 def send_unity_message (self , topic , message ):
8393 self .unity_tcp_sender .send_unity_message (topic , message )
8494
95+ def send_unity_service (self , topic , service_class , request ):
96+ return self .unity_tcp_sender .send_unity_service (topic , service_class , request )
97+
8598 def handle_syscommand (self , data ):
8699 message = RosUnitySysCommand ().deserialize (data )
87100 function = getattr (self .syscommands , message .command )
@@ -139,6 +152,7 @@ def resolve_message_name(name):
139152 names = name .split ('/' )
140153 module_name = names [0 ]
141154 class_name = names [1 ]
155+ importlib .import_module (module_name + ".msg" )
142156 module = sys .modules [module_name ]
143157 if module is None :
144158 rospy .loginfo ("Failed to resolve module {}" .format (module_name ))
@@ -150,4 +164,5 @@ def resolve_message_name(name):
150164 rospy .loginfo ("Failed to resolve module {}.msg.{}" .format (module_name , class_name ))
151165 return module
152166 except (IndexError , KeyError , AttributeError ) as e :
167+ rospy .loginfo ("Exception Raised: {}" .format (e ))
153168 return None
0 commit comments