1515from rclpy .client import Client
1616from rclpy .constants import S_TO_NS
1717from rclpy .exceptions import NoTypeSupportImportedException
18+ from rclpy .expand_topic_name import expand_topic_name
1819from rclpy .impl .implementation_singleton import rclpy_implementation as _rclpy
1920from rclpy .publisher import Publisher
2021from rclpy .qos import qos_profile_default , qos_profile_services_default
2122from rclpy .service import Service
2223from rclpy .subscription import Subscription
2324from rclpy .timer import WallTimer
25+ from rclpy .validate_full_topic_name import validate_full_topic_name
26+ from rclpy .validate_namespace import validate_namespace
27+ from rclpy .validate_node_name import validate_node_name
28+ from rclpy .validate_topic_name import validate_topic_name
2429
2530
2631class Node :
@@ -44,14 +49,32 @@ def handle(self, value):
4449 def get_name (self ):
4550 return _rclpy .rclpy_get_node_name (self .handle )
4651
52+ def get_namespace (self ):
53+ return _rclpy .rclpy_get_node_namespace (self .handle )
54+
55+ def _validate_topic_or_service_name (self , topic_or_service_name , * , is_service = False ):
56+ name = self .get_name ()
57+ namespace = self .get_namespace ()
58+ validate_node_name (name )
59+ validate_namespace (namespace )
60+ validate_topic_name (topic_or_service_name , is_service = is_service )
61+ expanded_topic_or_service_name = expand_topic_name (topic_or_service_name , name , namespace )
62+ validate_full_topic_name (expanded_topic_or_service_name , is_service = is_service )
63+
4764 def create_publisher (self , msg_type , topic , * , qos_profile = qos_profile_default ):
4865 # this line imports the typesupport for the message module if not already done
4966 if msg_type .__class__ ._TYPE_SUPPORT is None :
5067 msg_type .__class__ .__import_type_support__ ()
5168 if msg_type .__class__ ._TYPE_SUPPORT is None :
5269 raise NoTypeSupportImportedException
53- publisher_handle = _rclpy .rclpy_create_publisher (
54- self .handle , msg_type , topic , qos_profile .get_c_qos_profile ())
70+ failed = False
71+ try :
72+ publisher_handle = _rclpy .rclpy_create_publisher (
73+ self .handle , msg_type , topic , qos_profile .get_c_qos_profile ())
74+ except ValueError :
75+ failed = True
76+ if failed :
77+ self ._validate_topic_or_service_name (topic )
5578 publisher = Publisher (publisher_handle , msg_type , topic , qos_profile , self .handle )
5679 self .publishers .append (publisher )
5780 return publisher
@@ -62,8 +85,14 @@ def create_subscription(self, msg_type, topic, callback, *, qos_profile=qos_prof
6285 msg_type .__class__ .__import_type_support__ ()
6386 if msg_type .__class__ ._TYPE_SUPPORT is None :
6487 raise NoTypeSupportImportedException
65- [subscription_handle , subscription_pointer ] = _rclpy .rclpy_create_subscription (
66- self .handle , msg_type , topic , qos_profile .get_c_qos_profile ())
88+ failed = False
89+ try :
90+ [subscription_handle , subscription_pointer ] = _rclpy .rclpy_create_subscription (
91+ self .handle , msg_type , topic , qos_profile .get_c_qos_profile ())
92+ except ValueError :
93+ failed = True
94+ if failed :
95+ self ._validate_topic_or_service_name (topic )
6796
6897 subscription = Subscription (
6998 subscription_handle , subscription_pointer , msg_type ,
@@ -76,11 +105,17 @@ def create_client(self, srv_type, srv_name, *, qos_profile=qos_profile_services_
76105 srv_type .__class__ .__import_type_support__ ()
77106 if srv_type .__class__ ._TYPE_SUPPORT is None :
78107 raise NoTypeSupportImportedException
79- [client_handle , client_pointer ] = _rclpy .rclpy_create_client (
80- self .handle ,
81- srv_type ,
82- srv_name ,
83- qos_profile .get_c_qos_profile ())
108+ failed = False
109+ try :
110+ [client_handle , client_pointer ] = _rclpy .rclpy_create_client (
111+ self .handle ,
112+ srv_type ,
113+ srv_name ,
114+ qos_profile .get_c_qos_profile ())
115+ except ValueError :
116+ failed = True
117+ if failed :
118+ self ._validate_topic_or_service_name (srv_name , is_service = True )
84119 client = Client (
85120 self .handle , client_handle , client_pointer , srv_type , srv_name , qos_profile )
86121 self .clients .append (client )
@@ -92,11 +127,17 @@ def create_service(
92127 srv_type .__class__ .__import_type_support__ ()
93128 if srv_type .__class__ ._TYPE_SUPPORT is None :
94129 raise NoTypeSupportImportedException
95- [service_handle , service_pointer ] = _rclpy .rclpy_create_service (
96- self .handle ,
97- srv_type ,
98- srv_name ,
99- qos_profile .get_c_qos_profile ())
130+ failed = False
131+ try :
132+ [service_handle , service_pointer ] = _rclpy .rclpy_create_service (
133+ self .handle ,
134+ srv_type ,
135+ srv_name ,
136+ qos_profile .get_c_qos_profile ())
137+ except ValueError :
138+ failed = True
139+ if failed :
140+ self ._validate_topic_or_service_name (srv_name , is_service = True )
100141 service = Service (
101142 self .handle , service_handle , service_pointer ,
102143 srv_type , srv_name , callback , qos_profile )
0 commit comments