Skip to content

Commit 1b2bd13

Browse files
committed
EVALG-77: Add Java example to Data Modeling
1 parent 0940bfd commit 1b2bd13

11 files changed

+986
-0
lines changed
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Forbid generated code
2+
Coord*.java
3+
VIN*.java
4+
VehicleMetrics*.java
5+
VehicleTransit*.java
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* (c) Copyright, Real-Time Innovations, 2024. All rights reserved.
3+
* RTI grants Licensee a license to use, modify, compile, and create derivative
4+
* works of the software solely for use with RTI Connext DDS. Licensee may
5+
* redistribute copies of the software provided that all such copies are subject
6+
* to this license. The software is provided "as is", with no warranty of any
7+
* type, including any warranty for fitness for any purpose. RTI is under no
8+
* obligation to maintain or support the software. RTI shall not be liable for
9+
* any incidental or consequential damages arising out of the use or inability
10+
* to use the software.
11+
*/
12+
13+
import com.rti.ndds.config.LogVerbosity;
14+
15+
/**
16+
*
17+
* Code in the example application to handle clean shutdown, arguments, etc.
18+
*
19+
*/
20+
public abstract class Application {
21+
22+
public enum ParseReturn {
23+
FAILURE, SUCCESS, EXIT
24+
}
25+
26+
private boolean shutdownRequested = false;
27+
private int domainId = 0;
28+
private int maxSampleCount = Integer.MAX_VALUE;
29+
30+
public void parseArguments(String[] args) {
31+
final int defaultVerbosity = 1; // default to error
32+
int verbosity = defaultVerbosity;
33+
int argProcessing = 0;
34+
boolean showUsage = false;
35+
ParseReturn parseResult = ParseReturn.SUCCESS;
36+
37+
while (argProcessing < args.length) {
38+
String argument = args[argProcessing];
39+
if ((args.length > argProcessing + 1)
40+
&& (argument.equals("-d")
41+
|| argument.equals("--domain"))) {
42+
domainId = Integer.parseInt(args[argProcessing + 1]);
43+
argProcessing++;
44+
} else if ((args.length > argProcessing + 1)
45+
&& (argument.equals("-s")
46+
|| argument.equals("--sample-count"))) {
47+
maxSampleCount = Integer.parseInt(args[argProcessing + 1]);
48+
argProcessing++;
49+
} else if ((args.length > argProcessing + 1)
50+
&& (argument.equals("-v")
51+
|| argument.equals("--verbosity"))) {
52+
verbosity = Integer.parseInt(args[argProcessing + 1]);
53+
argProcessing++;
54+
} else if (argument.equals("-h")
55+
|| argument.equals("--help")) {
56+
System.out.println(getClass().getSimpleName() + " application.");
57+
showUsage = true;
58+
parseResult = ParseReturn.EXIT;
59+
break;
60+
} else {
61+
System.err.println("Bad parameter: " + argument);
62+
showUsage = true;
63+
parseResult = ParseReturn.FAILURE;
64+
break;
65+
}
66+
argProcessing++;
67+
}
68+
69+
final String NL = System.lineSeparator();
70+
if (showUsage) {
71+
System.out.println("Usage:" + NL
72+
+ " -d, --domain <int> DDS domain ID." + NL
73+
+ " Default: 0" + NL
74+
+ " -s, --sample_count <int> Number of samples to send/receive." + NL
75+
+ " Default: Integer.MAX_VALUE" + NL
76+
+ " -v, --verbosity <int> Debug output level (0-3)" + NL
77+
+ " Default: " + defaultVerbosity);
78+
}
79+
80+
if (parseResult == ParseReturn.EXIT) {
81+
// Help requested, so exit instead of running example
82+
System.exit(0);
83+
}
84+
if (parseResult == ParseReturn.FAILURE) {
85+
throw new IllegalArgumentException();
86+
}
87+
// Always specify the verbosity, even if default
88+
setVerbosity(verbosity);
89+
}
90+
91+
protected void addShutdownHook() {
92+
Runtime.getRuntime().addShutdownHook(new Thread() {
93+
public void run() {
94+
System.out.println("Shutting down...");
95+
shutdownRequested = true;
96+
}
97+
});
98+
}
99+
100+
public boolean isShutdownRequested() {
101+
return shutdownRequested;
102+
}
103+
104+
public int getDomainId() {
105+
return domainId;
106+
}
107+
108+
public int getMaxSampleCount() {
109+
return maxSampleCount;
110+
}
111+
112+
/**
113+
* Sets Connext verbosity to help debugging
114+
* @param verbosity 0 = silent, 1 = error, 2 = warning, 3 = status all.
115+
*/
116+
public static void setVerbosity(int verbosity) {
117+
LogVerbosity logVerbosity;
118+
switch (verbosity) {
119+
case 0:
120+
logVerbosity = LogVerbosity.NDDS_CONFIG_LOG_VERBOSITY_SILENT;
121+
break;
122+
case 1:
123+
logVerbosity = LogVerbosity.NDDS_CONFIG_LOG_VERBOSITY_ERROR;
124+
break;
125+
case 2:
126+
logVerbosity = LogVerbosity.NDDS_CONFIG_LOG_VERBOSITY_WARNING;
127+
break;
128+
case 3:
129+
logVerbosity = LogVerbosity.NDDS_CONFIG_LOG_VERBOSITY_STATUS_ALL;
130+
break;
131+
default:
132+
logVerbosity = LogVerbosity.NDDS_CONFIG_LOG_VERBOSITY_ERROR;
133+
break;
134+
}
135+
com.rti.ndds.config.Logger.get_instance().set_verbosity(logVerbosity);
136+
}
137+
}
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Tutorial: Data Modeling
2+
3+
This code is part of the Connext Data Modeling tutorial and is included
4+
here in full for convenience.
5+
6+
This code targets Java 8 or and uses the Connext Java API.
7+
8+
## How to build
9+
10+
The example already provides generated example files
11+
(`VehicleMetricsPublisher.java`, `VehicleMetricsSubscriber.java` and
12+
`Application.java`), but requires generating some additional files.
13+
14+
This functionality is built-in into the Gradle build script, which
15+
itself requires the `NDDSHOME` environment variable to be set:
16+
17+
```console
18+
$ export NDDSHOME=/path/to/connext
19+
```
20+
21+
```console
22+
> set NDDSHOME=\path\to\connext
23+
```
24+
25+
26+
## How to run
27+
28+
The publisher can be run with the `runPublisher` Gradle task. The publisher is a
29+
slighly modified generated example file that publishes "default" samples in a loop.
30+
31+
```console
32+
Buildfile: build.xml
33+
34+
compile:
35+
36+
VehicleMetricsPublisher:
37+
[java] Writing VehicleMetrics, count 0
38+
[java] Writing VehicleMetrics, count 1
39+
[java] Writing VehicleMetrics, count 2
40+
[java] Writing VehicleMetrics, count 3
41+
```
42+
43+
The subscriber can be run with the `runSubscriber` Gradle task. The subscriber is
44+
a slighly modified generated example file that subscribes to data and displays all its
45+
values as they're received.
46+
47+
```console
48+
$ ant VehicleMetricsSubscriber
49+
Buildfile: build.xml
50+
51+
compile:
52+
53+
VehicleMetricsSubscriber:
54+
[java] Received:
55+
[java] vehicle_vin:
56+
[java] fuel_level: 0.0
57+
[java]
58+
[java] No data after 1 seconds.
59+
[java] Received:
60+
[java] vehicle_vin:
61+
[java] fuel_level: 0.0
62+
[java]
63+
[java] Received:
64+
[java] vehicle_vin:
65+
[java] fuel_level: 0.0
66+
[java]
67+
[java] No data after 1 seconds.
68+
[java] Received:
69+
[java] vehicle_vin:
70+
[java] fuel_level: 0.0
71+
[java]
72+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
(c) Copyright, Real-Time Innovations, 2024. All rights reserved.
4+
RTI grants Licensee a license to use, modify, compile, and create derivative
5+
works of the software solely for use with RTI Connext DDS. Licensee may
6+
redistribute copies of the software provided that all such copies are subject
7+
to this license. The software is provided "as is", with no warranty of any
8+
type, including any warranty for fitness for any purpose. RTI is under no
9+
obligation to maintain or support the software. RTI shall not be liable for
10+
any incidental or consequential damages arising out of the use or inability
11+
to use the software.
12+
-->
13+
14+
<dds xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
15+
xsi:noNamespaceSchemaLocation="https://community.rti.com/schema/7.3.0/rti_dds_profiles.xsd">
16+
<!-- QoS Library containing the QoS profile used in the generated example.
17+
18+
A QoS library is a named set of QoS profiles.
19+
-->
20+
<qos_library name="VehicleModeling_Library">
21+
22+
<!-- QoS profile used to configure reliable communication between the DataWriter
23+
and DataReader created in the example code.
24+
25+
A QoS profile groups a set of related QoS.
26+
-->
27+
<qos_profile name="VehicleModeling_Profile"
28+
base_name="BuiltinQosLib::Generic.StrictReliable" is_default_qos="true">
29+
<!-- QoS used to configure the data writer created in the example code -->
30+
<datawriter_qos>
31+
<publication_name>
32+
<name>VehicleModelingDataWriter</name>
33+
</publication_name>
34+
</datawriter_qos>
35+
36+
<!-- QoS used to configure the data reader created in the example code -->
37+
<datareader_qos>
38+
<subscription_name>
39+
<name>VehicleModelingDataReader</name>
40+
</subscription_name>
41+
</datareader_qos>
42+
<domain_participant_qos>
43+
<!--
44+
The participant name, if it is set, will be displayed in the
45+
RTI tools, making it easier for you to tell one
46+
application from another when you're debugging.
47+
-->
48+
<participant_name>
49+
<name>VehicleModelingParticipant</name>
50+
<role_name>VehicleModelingParticipantRole</role_name>
51+
</participant_name>
52+
53+
</domain_participant_qos>
54+
</qos_profile>
55+
56+
<qos_profile name="VehicleMetrics_Profile">
57+
<datareader_qos base_name="BuiltinQosLib::Generic.BestEffort">
58+
<deadline>
59+
<period>
60+
<sec>15</sec>
61+
<nanosec>0</nanosec>
62+
</period>
63+
</deadline>
64+
</datareader_qos>
65+
<datawriter_qos base_name="BuiltinQosLib::Generic.BestEffort">
66+
<deadline>
67+
<period>
68+
<sec>10</sec>
69+
<nanosec>0</nanosec>
70+
</period>
71+
</deadline>
72+
</datawriter_qos>
73+
</qos_profile>
74+
<qos_profile name="VehicleTransit_Profile">
75+
<datareader_qos base_name="BuiltinQosLib::Generic.KeepLastReliable">
76+
<durability>
77+
<kind>TRANSIENT_LOCAL_DURABILITY_QOS</kind>
78+
</durability>
79+
</datareader_qos>
80+
<datawriter_qos base_name="BuiltinQosLib::Generic.StrictReliable">
81+
<durability>
82+
<kind>TRANSIENT_LOCAL_DURABILITY_QOS</kind>
83+
</durability>
84+
</datawriter_qos>
85+
</qos_profile>
86+
</qos_library>
87+
</dds>

0 commit comments

Comments
 (0)