1+ # Copyright 2021 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+ #
3+ # Licensed under the Apache License, Version 2.0 (the "License");
4+ # you may not use this file except in compliance with the License.
5+ # You may obtain a copy of the License at
6+ #
7+ # http://www.apache.org/licenses/LICENSE-2.0
8+ #
9+ # Unless required by applicable law or agreed to in writing, software
10+ # distributed under the License is distributed on an "AS IS" BASIS,
11+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ # See the License for the specific language governing permissions and
13+ # limitations under the License.
14+ """
15+ Script to test Fast DDS python bindings
16+ """
17+ import argparse
18+ import threading
19+
120import fastdds_wrapper
221import HelloWorld
322
4- class Reader :
23+ DESCRIPTION = """Script to test Fast DDS python bindings"""
24+ USAGE = ('python3 test.py -p publisher|subscriber [-d domainID -m machineID]' )
25+
26+ class Reader ():
527 def __init__ (self , domain ):
628 factory = fastdds_wrapper .DomainParticipantFactory .get_instance ()
729 self .participant_qos = fastdds_wrapper .DomainParticipantQos ()
830 factory .get_default_participant_qos (self .participant_qos )
9- self .participant = factory .create_participant (5 , self .participant_qos )
31+ self .participant = factory .create_participant (domain , self .participant_qos )
1032
1133 self .topic_data_type = HelloWorld .HelloWorldPubSubType ()
1234 self .topic_data_type .setName ("HelloWorldDataType" )
@@ -32,13 +54,27 @@ def read(self):
3254
3355 print ("Received {message} : {index}" .format (message = data .message (), index = data .index ()))
3456
57+ def delete (self ):
58+ factory = fastdds_wrapper .DomainParticipantFactory .get_instance ()
59+ self .participant .delete_contained_entities ()
60+ factory .delete_participant (self .participant )
61+
62+ def run (self ):
63+ keep_going = 'y'
64+ while keep_going != 'n' :
65+ if keep_going == 'y' :
66+ self .read ()
67+ keep_going = input ('Send another sample? (y-yes, n-no): ' )
68+ self .delete ()
3569
3670class Writer :
37- def __init__ (self , domain ):
71+ def __init__ (self , domain , machine ):
72+ self .machine = machine
73+
3874 factory = fastdds_wrapper .DomainParticipantFactory .get_instance ()
3975 self .participant_qos = fastdds_wrapper .DomainParticipantQos ()
4076 factory .get_default_participant_qos (self .participant_qos )
41- self .participant = factory .create_participant (5 , self .participant_qos )
77+ self .participant = factory .create_participant (domain , self .participant_qos )
4278
4379 self .topic_data_type = HelloWorld .HelloWorldPubSubType ()
4480 self .topic_data_type .setName ("HelloWorldDataType" )
@@ -61,9 +97,81 @@ def __init__(self, domain):
6197
6298 def write (self ):
6399 data = HelloWorld .HelloWorld ()
64- data .message ("Hello World" )
100+ if self .machine :
101+ data .message ("Hello World " + self .machine )
102+ else :
103+ data .message ("Hello World" )
65104 data .index (self .index )
66105 self .writer .write (data )
67106 print ("Sending {message} : {index}" .format (message = data .message (), index = data .index ()))
68107 self .index = self .index + 1
69108
109+ def delete (self ):
110+ factory = fastdds_wrapper .DomainParticipantFactory .get_instance ()
111+ self .participant .delete_contained_entities ()
112+ factory .delete_participant (self .participant )
113+
114+ def run (self ):
115+ keep_going = 'y'
116+ while keep_going != 'n' :
117+ if keep_going == 'y' :
118+ self .write ()
119+ keep_going = input ('Send another sample? (y-yes, n-no): ' )
120+ self .delete ()
121+
122+ def parse_options ():
123+ """"
124+ Parse arguments.
125+
126+ :return: Parsed arguments.
127+ """
128+ parser = argparse .ArgumentParser (
129+ formatter_class = argparse .ArgumentDefaultsHelpFormatter ,
130+ add_help = True ,
131+ description = (DESCRIPTION ),
132+ usage = (USAGE )
133+ )
134+ required_args = parser .add_argument_group ('required arguments' )
135+ required_args .add_argument (
136+ '-d' ,
137+ '--domain' ,
138+ type = int ,
139+ required = False ,
140+ help = 'DomainID.'
141+ )
142+ required_args .add_argument (
143+ '-p' ,
144+ '--parameter' ,
145+ type = str ,
146+ required = True ,
147+ help = 'Whether the application is run as publisher or subscriber.'
148+ )
149+ required_args .add_argument (
150+ '-m' ,
151+ '--machine' ,
152+ type = str ,
153+ required = False ,
154+ help = 'Distinguish the machine publishing. Only applies if the application is run as publisher.'
155+
156+ )
157+ return parser .parse_args ()
158+
159+ if __name__ == '__main__' :
160+ # Parse arguments
161+ args = parse_options ()
162+ if not args .domain :
163+ args .domain = 0
164+
165+ if args .parameter == 'publisher' :
166+ print ('Creating publisher.' )
167+ writer = Writer (args .domain , args .machine )
168+ writer .run ()
169+ elif args .parameter == 'subscriber' :
170+ print ('Creating subscriber.' )
171+ reader = Reader (args .domain )
172+ reader .run ()
173+ else :
174+ print ('Error: Incorrect arguments.' )
175+ print (USAGE )
176+
177+ exit ()
0 commit comments