-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathHelloMessagePublisher.java
More file actions
178 lines (152 loc) · 6.96 KB
/
HelloMessagePublisher.java
File metadata and controls
178 lines (152 loc) · 6.96 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
/*
* (c) Copyright, Real-Time Innovations, 2020. All rights reserved.
* RTI grants Licensee a license to use, modify, compile, and create derivative
* works of the software solely for use with RTI Connext DDS. Licensee may
* redistribute copies of the software provided that all such copies are subject
* to this license. The software is provided "as is", with no warranty of any
* type, including any warranty for fitness for any purpose. RTI is under no
* obligation to maintain or support the software. RTI shall not be liable for
* any incidental or consequential damages arising out of the use or inability
* to use the software.
*/
import com.rti.dds.domain.DomainParticipant;
import com.rti.dds.domain.DomainParticipantFactory;
import com.rti.dds.infrastructure.InstanceHandle_t;
import com.rti.dds.infrastructure.RETCODE_ERROR;
import com.rti.dds.infrastructure.StatusKind;
import com.rti.dds.publication.Publisher;
import com.rti.dds.topic.Topic;
import com.rti.ndds.config.LogVerbosity;
import com.rti.ndds.config.Logger;
// ===========================================================================
public class HelloMessagePublisher extends Application {
public static void main(String[] args) {
setUpLogging();
try {
// Parse arguments passed to application
parseArguments(args);
if (ApplicationArguments.runApplication.get() == true) {
// If the application is running, set a shutdown handler and
// set Connext logging verbosity
handleShutdown();
setVerbosity();
runExample();
}
} catch (Exception e) {
logger.severe("Exception: " + e.getLocalizedMessage());
}
}
private HelloMessagePublisher() {
super();
}
private static void runExample() {
DomainParticipant participant = null;
try {
// Connext DDS setup
// -----------------
// A DomainParticipant allows an application to begin communicating
// in a DDS domain. Typically there is one DomainParticipant per
// application. DomainParticipant QoS is configured in
// USER_QOS_PROFILES.xml
participant = DomainParticipantFactory.TheParticipantFactory
.create_participant(
ApplicationArguments.domainId.get(),
DomainParticipantFactory
.PARTICIPANT_QOS_DEFAULT,
null, // listener
StatusKind.STATUS_MASK_NONE);
if (participant == null) {
throw new RETCODE_ERROR("create_participant error");
}
// A Publisher allows an application to create one or more
// DataWriters. Publisher QoS is configured in USER_QOS_PROFILES.xml
Publisher publisher = participant.create_publisher(
DomainParticipant.PUBLISHER_QOS_DEFAULT,
null, // listener
StatusKind.STATUS_MASK_NONE);
if (publisher == null) {
throw new RETCODE_ERROR("create_publisher error");
}
// Register the datatype to use when creating the Topic
String typeName = HelloMessageTypeSupport.get_type_name();
HelloMessageTypeSupport.register_type(participant, typeName);
// A Topic has a name and a datatype. Create a Topic called
// "Example HelloMessage" with your registered data type
Topic topic = participant.create_topic(
"Example HelloMessage",
typeName,
DomainParticipant.TOPIC_QOS_DEFAULT,
null, // listener
StatusKind.STATUS_MASK_NONE);
if (topic == null) {
throw new RETCODE_ERROR("create_topic error");
}
// This DataWriter writes data on Topic "Example HelloMessage"
// DataWriter QoS is configured in USER_QOS_PROFILES.xml
HelloMessageDataWriter writer =
(HelloMessageDataWriter)publisher.create_datawriter(
topic,
Publisher.DATAWRITER_QOS_DEFAULT,
null, // listener
StatusKind.STATUS_MASK_NONE);
if (writer == null) {
throw new RETCODE_ERROR("create_datawriter error");
}
// Create data sample for writing
HelloMessage sample = new HelloMessage();
// Main loop, write data
// ---------------------
for (int count = 0;
shouldRun.get()
&& ((ApplicationArguments.sampleCount.get() == 0)
|| (count < ApplicationArguments.sampleCount.get()));
++count) {
logger.info("Writing HelloMessage, count " + count);
// Modify the data to be written here
// Write data
writer.write(sample, InstanceHandle_t.HANDLE_NIL);
try {
Thread.sleep(4000); // 4 seconds
} catch (InterruptedException ex) {
// Stop writing
break;
}
}
} finally {
// Shutdown
// --------
if (participant != null) {
participant.delete_contained_entities();
DomainParticipantFactory.TheParticipantFactory
.delete_participant(participant);
}
// Optional at shutdown
DomainParticipantFactory.finalize_instance();
}
}
// Sets Connext verbosity to help debugging
public static void setVerbosity() {
switch (ApplicationArguments.verbosity.get()) {
case 0:
Logger.get_instance().set_verbosity(
LogVerbosity.NDDS_CONFIG_LOG_VERBOSITY_SILENT);
break;
case 1:
Logger.get_instance().set_verbosity(
LogVerbosity.NDDS_CONFIG_LOG_VERBOSITY_ERROR);
break;
case 2:
Logger.get_instance().set_verbosity(
LogVerbosity.NDDS_CONFIG_LOG_VERBOSITY_WARNING);
break;
case 3:
Logger.get_instance().set_verbosity(
LogVerbosity.NDDS_CONFIG_LOG_VERBOSITY_STATUS_ALL);
break;
default:
Logger.get_instance().set_verbosity(
LogVerbosity.NDDS_CONFIG_LOG_VERBOSITY_ERROR);
break;
}
}
}