-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHello.java
More file actions
413 lines (370 loc) · 18.1 KB
/
Copy pathHello.java
File metadata and controls
413 lines (370 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// ****************************************************************************
// (c) Copyright, Real-Time Innovations, All rights reserved.
//
// Permission to modify and use for internal purposes granted.
// This software is provided "as is", without warranty, express or implied.
//
// ****************************************************************************
package dynamicExample;
import java.io.File;
import com.rti.dds.domain.DomainParticipant;
import com.rti.dds.domain.DomainParticipantFactory;
import com.rti.dds.dynamicdata.DynamicDataTypeSupport;
import com.rti.dds.infrastructure.ProductVersion_t;
import com.rti.dds.infrastructure.StatusKind;
import com.rti.dds.topic.Topic;
import com.rti.dds.typecode.TypeCode;
import com.rti.ndds.config.LogCategory;
import com.rti.ndds.config.LogVerbosity;
import com.rti.ndds.config.Logger;
import com.rti.ndds.config.Version;
// ****************************************************************************
/**
* The main class contains only static methods. It contains the main entry
* method and delegates the pub/sub to the other classes.
*/
public class Hello {
/**************************************************************************
* Creates the DDS Domain Participant, then delegates the execution to the
* correct pub/sub function.
* If entity creation is successful, this function does not return.
*/
private static void startApplication(final CommandLineArguments arg) {
System.out.println("# The output below depends on the QoS profile");
System.out.println("# provided to this application.");
System.out.println("# -> For more information on the provided example");
System.out.println("# profiles, please see the Getting Started Guide.");
System.out.println("# -> For detailed product performance metrics, visit");
System.out.println("# http://www.rti.com/products/data_distribution/index.html");
System.out.println("# and click on Benchmarks.\n");
// This example creates DDS entities using the default QoS.
// The default QoS can be modified in two ways:
// 1. By placing a file called 'USER_QOS_PROFILES.xml' in the directory
// where the application is launched.
// 2. By setting the environment variable 'NDDS_QOS_PROFILES' to point
// to a valid file containing QoS policy definitions.
//
// This section check if a QoS profile file is accessible, and prints
// a warning if that's not true.
//
// First look in the current directory to see if the USER_QOS_PROFILES.xml
// file exist.
//
if (!new File("USER_QOS_PROFILES.xml").exists()) {
/* Then look for the environment variable 'NDDS_QOS_PROFILES'...
*/
String version = System.getProperty("java.version");
if (version.startsWith("1.4")) { /* deprecated System.getenv() */
System.out.println("! Warning:");
System.out.println("! Java "+version+" cannot determine if the");
System.out.println("! environment variable NDDS_QOS_PROFILES is set.");
System.out.println("! If not set, the application will use the ");
System.out.println("! DDS default QoS.");
System.out.println("! If you want to use different QoS, make sure you have the " +
"QoS definition file");
System.out.println("! (USER_QOS_PROFILES.xml) in the current working directory");
System.out.println("! or set the environment variable NDDS_QOS_PROFILES to");
System.out.println("! point to a file containing the default QoS profile");
} else {
String envVal = System.getenv("NDDS_QOS_PROFILES");
if (envVal == null || !new File(envVal).exists()) {
System.out.println("! Warning:");
System.out.println("! Default QoS profile definition file not found.");
System.out.println("! The application will use the DDS default QoS.");
System.out.println("! If you want to use different QoS, make sure you have the " +
"QoS definition file");
System.out.println("! (USER_QOS_PROFILES.xml) in the current working directory");
System.out.println("! or set the environment variable NDDS_QOS_PROFILES to");
System.out.println("! point to a file containing the default QoS profile");
}
}
}
/* If you need to customize any DDS factory QoS, uncomment the following
* code:
*
try {
DomainParticipantFactoryQos factoryQos = new DomainParticipantFactoryQos();
DomainParticipantFactory.get_instance().get_qos(factoryQos);
// Modify the factory QoS here
DomainParticipantFactory.get_instance().set_qos(factoryQos);
}
catch(RETCODE_ERROR e) {
System.err.println("An error occurred while changing factory QoS: "
+ e);
}
*/
/* Creates the DDS Domain Participant.
* The following command will create a DDS Domain participant without
* installing any status callback listener.
* If you want to create a domain participant with different QoS,
* use DomainParticipantFactory.get_default_participant_qos
* to obtain a copy of the default participant QoS, change them,
* then use them instead of PARTICIPANT_QOS_DEFAULT:
*
DomainParticipantQos myQos = new DomainParticipantQos();
DomainParticipantFactory.get_instance().get_default_participant_qos(myQos);
// Modify the participant QoS here
// Then create the domain participant using 'myQos' instead of
// DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT
*
* Note: for more info on the domain participant API see:
* $NDDSHOME/doc/html/api_java/group__DDSDomainParticipantModule.html
*/
if (arg.verbose > 1) {
System.out.println("Creating domain participant...");
}
DomainParticipant participant =
DomainParticipantFactory.get_instance().create_participant(
arg.domainId,
DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT,
null, // listener
StatusKind.STATUS_MASK_NONE);
if (participant == null) {
System.err.println("! Unable to create DDS domain participant");
return;
}
// Create TypeCode, DynamicDataTypeSupport for dynamic data type
TypeCode type = HelloWorldType.create();
if (type == null) {
System.err.println("! Unable to create dynamic type code");
return;
}
// Create the Dynamic data type support object
DynamicDataTypeSupport typeSupport = new DynamicDataTypeSupport(
type,
DynamicDataTypeSupport.TYPE_PROPERTY_DEFAULT);
// Register type before creating topic
typeSupport.register_type(participant, HelloWorldType.getTypeName());
/* Creates the topic.
* The following command will create the topic without
* installing any status callback listener.
* If you want to create a topic with different QoS,
* use DomainParticipant.get_default_topic_qos
* to obtain a copy of the default topic QoS, change them,
* then use them instead of TOPIC_QOS_DEFAULT:
*
TopicQos myQos = new TopicQos();
participant.get_default_topic_qos(myQos);
// Modify the topic QoS here
// Then create the topic using 'myQos'...
*
* Note: for more info on the topic API see:
* $NDDSHOME/doc/html/api_c/group__DDSTopicEntityModule.html
*/
if (arg.verbose > 0) {
System.out.println("Creating the topic...");
}
Topic topic = participant.create_topic(
arg.topicName,
HelloWorldType.getTypeName(),
DomainParticipant.TOPIC_QOS_DEFAULT,
null, // listener
StatusKind.STATUS_MASK_NONE);
if (topic == null) {
System.err.println("! Unable to create topic " + arg.topicName);
return;
}
/* Creates the publisher or subscriber */
if (arg.mode == CommandLineArguments.APP_MODE_PUBLISHER) {
HelloPublisher publisher = new HelloPublisher(
participant,
topic,
type,
arg.verbose,
arg.dataSize,
arg.sampleCount);
publisher.writeSamples();
} else {
HelloSubscriber subscriber = new HelloSubscriber(
participant,
topic,
arg.verbose,
arg.sampleCount);
subscriber.waitForTermination();
}
/* Shutdown, when sampleCount is finite */
if(participant != null) {
participant.delete_contained_entities();
DomainParticipantFactory.TheParticipantFactory.
delete_participant(participant);
}
typeSupport.delete();
HelloWorldType.delete(type);
DomainParticipantFactory.finalize_instance();
}
/**************************************************************************
* Prints on stdout brief instructions on command-line arguments accepted.
*
*/
public static void usage() {
System.out.println("Usage:");
System.out.println(" Hello pub [arguments] Run as publisher");
System.out.println(" Hello sub [arguments] Run as subscriber");
System.out.println("Where arguments are:");
System.out.println(" -h | --help " +
"Shows this page");
System.out.println(" -v | --verbose " +
"Increase output verbosity (can be repeated)");
System.out.println(" -d | --domain <domainID> " +
"Sets the DDS domain ID [default=" +
Integer.toString(CommandLineArguments.DEFAULT_DOMAIN_ID) + "]");
System.out.println(" -t | --topic <name> " +
"Sets topic name [default=" +
CommandLineArguments.DEFAULT_TOPIC_NAME + "]");
System.out.println(" -s | --size <num> " +
"Sets payload size in bytes [default=" +
Integer.toString(CommandLineArguments.DEFAULT_PAYLOAD) + "]");
System.out.println(" -c | --sampleCount <num> " +
"Sets number of samples to send/receive [default=0(UNLIMITED)]");
System.out.println("");
}
/**************************************************************************
* A simple function used to check if there are enough command line args
*/
static boolean ensureOneMoreArgument(int i, String [] args, String argName) {
if (i+1 > args.length) {
usage();
System.out.println("! Error: missing value for " + argName +
" argument");
return false;
}
return true;
}
/**************************************************************************
* Application main entry point
* @param argv array of strings containing the command-line arguments
*/
public static void main(String[] argv) {
CommandLineArguments args = new CommandLineArguments();
System.out.println("Hello Example Application");
System.out.println("Copyright 2008 Real-Time Innovations, Inc.\n");
if (argv.length == 0) {
usage();
System.err.println("! Invalid number of arguments.\n" +
"! You must specify at least running mode (pub/sub)");
return;
}
// Parse the command: pub or sub
if (argv[0].equalsIgnoreCase("pub")) {
args.mode = CommandLineArguments.APP_MODE_PUBLISHER;
} else if (argv[0].equalsIgnoreCase("sub")) {
args.mode = CommandLineArguments.APP_MODE_SUBSCRIBER;
} else if (argv[0].equals("-h") || argv[0].equals("--help")) {
usage();
return;
} else {
usage();
System.err.println("! Invalid mode: " + argv[0]);
System.err.println("! Valid modes are only 'pub' or 'sub'.");
return;
}
// Parse the rest of the arguments
for (int i = 1; i < argv.length; ++i) {
if (argv[i].equals("-h") || argv[i].equals("--help")) {
usage();
return;
}
if (argv[i].equals("-v") || argv[i].equals("--verbose")) {
++args.verbose;
continue;
}
if (argv[i].equals("-d") || argv[i].equals("--domain")) {
if (!ensureOneMoreArgument(i, argv, "--domain")) {
return;
}
args.domainId = Integer.parseInt(argv[++i]);
if (args.domainId < 0 ||
args.domainId > CommandLineArguments.DOMAIN_ID_MAX) {
usage();
System.err.println("! Invalid DDS Domain ID:" +
Integer.toString(args.domainId));
System.err.println("! The domain ID must be between 0 and " +
Integer.toString(CommandLineArguments.DOMAIN_ID_MAX) +
" (inclusive)");
return;
}
continue;
}
if (argv[i].equals("-s") || argv[i].equals("--size")) {
if (!ensureOneMoreArgument(i, argv, "--size")) {
return;
}
args.dataSize = Integer.parseInt(argv[++i]);
if (args.dataSize <= 0) {
usage();
System.err.println("! Invalid value for --size argument: " +
Integer.toString(args.dataSize));
return;
}
if (args.dataSize > CommandLineArguments.MAX_PAYLOAD) {
usage();
System.err.println("! Value too big for --size argument: " +
Integer.toString(args.dataSize));
System.err.println("! For built-in types, the default max "+
"string size is set to " +
Integer.toString(CommandLineArguments.MAX_PAYLOAD));
System.err.println("! See manual on built-in types for "+
"more information.");
return;
}
continue;
}
if (argv[i].equals("-t") || argv[i].equals("--topic")) {
if (!ensureOneMoreArgument(i, argv, "--topic")) {
return;
}
args.topicName = argv[++i];
continue;
}
if (argv[i].equals("-c") || argv[i].equals("--sampleCount")) {
if (!ensureOneMoreArgument(i, argv, "--sampleCount")) {
return;
}
args.sampleCount = Integer.parseInt(argv[++i]);
if (args.sampleCount < 0) {
usage();
System.err.println("! Invalid value for --sampleCount argument: " +
Integer.toString(args.dataSize));
return;
}
continue;
}
// Else, is an invalid argument
usage();
System.err.println("! Unknown argument: " + argv[i]);
return;
}
if (args.verbose > 0) {
Version rtiddsVersion = Version.get_instance();
ProductVersion_t prodVersion = rtiddsVersion.get_product_version();
System.out.println("Running with the following arguments:");
System.out.println(" Verbosity level.. : " +
Integer.toString(args.verbose));
System.out.println(" Payload size..... : " +
Integer.toString(args.dataSize));
System.out.println(" Sample count..... : " +
Integer.toString(args.sampleCount));
System.out.println(" Domain ID........ : " +
Integer.toString(args.domainId));
System.out.println(" Topic name....... : " + args.topicName);
System.out.println("RTI Product Version.. : " +
Integer.toString(prodVersion.major) + "." +
Integer.toString(prodVersion.minor) +
Character.toString(prodVersion.release) + "(rev " +
Integer.toString(prodVersion.revision) + ")");
}
/* If the verbosity is bigger than 1, also turn on RTI DDS status logging
* For more info, see:
* $NDDSHOME/doc/html/api_java/group__NDDSConfigModule.html
*/
if (args.verbose > 1) {
Logger logger = Logger.get_instance();
logger.set_verbosity_by_category(
LogCategory.NDDS_CONFIG_LOG_CATEGORY_API,
LogVerbosity.NDDS_CONFIG_LOG_VERBOSITY_STATUS_ALL);
}
// Finally start the application
startApplication(args);
System.out.println("Done.");
}
}