-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperatorSubscriber.java
More file actions
257 lines (192 loc) · 8.6 KB
/
OperatorSubscriber.java
File metadata and controls
257 lines (192 loc) · 8.6 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
/* VehicleSubscriber.java
A publication of data of type Vehicle
This file is derived from code automatically generated by the rtiddsgen
command:
rtiddsgen -language java -example <arch> .idl
Example publication of type Vehicle automatically generated by
'rtiddsgen' To test them follow these steps:
(1) Compile this file and the example subscription.
(2) Start the subscription on the same domain used for RTI Data Distribution
Service with the command
java VehicleSubscriber <domain_id> <sample_count>
(3) Start the publication on the same domain used for RTI Data Distribution
Service with the command
java VehiclePublisher <domain_id> <sample_count>
(4) [Optional] Specify the list of discovery initial peers and
multicast receive addresses via an environment variable or a file
(in the current working directory) called NDDS_DISCOVERY_PEERS.
You can run any number of publishers and subscribers programs, and can
add and remove them dynamically from the domain.
Example:
To run the example application on domain <domain_id>:
Ensure that $(NDDSHOME)/lib/<arch> is on the dynamic library path for
Java.
On UNIX systems:
add $(NDDSHOME)/lib/<arch> to the 'LD_LIBRARY_PATH' environment
variable
On Windows systems:
add %NDDSHOME%\lib\<arch> to the 'Path' environment variable
Run the Java applications:
java -Djava.ext.dirs=$NDDSHOME/class VehiclePublisher <domain_id>
java -Djava.ext.dirs=$NDDSHOME/class VehicleSubscriber <domain_id>
*/
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import com.rti.dds.domain.*;
import com.rti.dds.infrastructure.*;
import com.rti.dds.subscription.*;
import com.rti.dds.topic.*;
import com.rti.ndds.config.*;
// ===========================================================================
public class OperatorSubscriber {
// -----------------------------------------------------------------------
// Public Methods
// -----------------------------------------------------------------------
public static void main(String[] args) {
// --- Get domain ID --- //
int domainId = 0;
if (args.length >= 1) {
domainId = Integer.valueOf(args[0]).intValue();
}
// -- Get max loop count; 0 means infinite loop --- //
int sampleCount = 0;
if (args.length >= 2) {
sampleCount = Integer.valueOf(args[1]).intValue();
}
/* Uncomment this to turn on additional logging
Logger.get_instance().set_verbosity_by_category(
LogCategory.NDDS_CONFIG_LOG_CATEGORY_API,
LogVerbosity.NDDS_CONFIG_LOG_VERBOSITY_STATUS_ALL);
*/
// --- Run --- //
subscriberMain(domainId, sampleCount);
}
// -----------------------------------------------------------------------
// Private Methods
// -----------------------------------------------------------------------
// --- Constructors: -----------------------------------------------------
private OperatorSubscriber() {
super();
}
// -----------------------------------------------------------------------
private static void subscriberMain(int domainId, int sampleCount) {
DomainParticipant participant = null;
Subscriber subscriber = null;
Topic topic = null;
DataReaderListener listener = null;
VehicleDataReader reader = null;
try {
// --- Create participant --- //
/* To customize participant QoS, use
the configuration file
USER_QOS_PROFILES.xml */
participant = DomainParticipantFactory.TheParticipantFactory.create_participant(domainId,
DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT, null /* listener */, StatusKind.STATUS_MASK_NONE);
if (participant == null) {
System.err.println("create_participant error\n");
return;
}
// --- Create subscriber --- //
/* To customize subscriber QoS, use
the configuration file USER_QOS_PROFILES.xml */
subscriber = participant.create_subscriber(DomainParticipant.SUBSCRIBER_QOS_DEFAULT, null /* listener */,
StatusKind.STATUS_MASK_NONE);
if (subscriber == null) {
System.err.println("create_subscriber error\n");
return;
}
// --- Create topic --- //
/* Register type before creating topic */
String typeName = VehicleTypeSupport.get_type_name();
VehicleTypeSupport.register_type(participant, typeName);
/* To customize topic QoS, use
the configuration file USER_QOS_PROFILES.xml */
topic = participant.create_topic("Example Vehicle", typeName, DomainParticipant.TOPIC_QOS_DEFAULT,
null /* listener */, StatusKind.STATUS_MASK_NONE);
if (topic == null) {
System.err.println("create_topic error\n");
return;
}
// --- Create reader --- //
listener = new VehicleListener();
/* To customize data reader QoS, use
the configuration file USER_QOS_PROFILES.xml */
reader = (VehicleDataReader) subscriber.create_datareader(topic, Subscriber.DATAREADER_QOS_DEFAULT, listener,
StatusKind.STATUS_MASK_ALL);
if (reader == null) {
System.err.println("create_datareader error\n");
return;
}
// --- Wait for data --- //
System.out.format("%10s%20s%20s%20s%20s%20s%20s%20s%20s", "MessageType", "Route", "vehicle", "Traffic", "Stop#",
"#Stops", "TimeBetweenStops", "Fill%", "TimeStamp");
for (int count = 0; (sampleCount == 0) || (count < sampleCount); ++count) {
try {
Thread.sleep(2 * 1000); // in millisec
} catch (InterruptedException ix) {
System.err.println("INTERRUPTED");
break;
}
}
} finally {
// --- Shutdown --- //
if (participant != null) {
participant.delete_contained_entities();
DomainParticipantFactory.TheParticipantFactory.delete_participant(participant);
}
/* RTI Data Distribution Service provides the finalize_instance()
method for users who want to release memory used by the
participant factory singleton. Uncomment the following block of
code for clean destruction of the participant factory
singleton. */
//DomainParticipantFactory.finalize_instance();
}
}
// -----------------------------------------------------------------------
// Private Types
// -----------------------------------------------------------------------
// =======================================================================
private static class VehicleListener extends DataReaderAdapter {
VehicleSeq _dataSeq = new VehicleSeq();
SampleInfoSeq _infoSeq = new SampleInfoSeq();
public void on_data_available(DataReader reader) {
VehicleDataReader VehicleReader = (VehicleDataReader) reader;
try {
VehicleReader.take(_dataSeq, _infoSeq, ResourceLimitsQosPolicy.LENGTH_UNLIMITED,
SampleStateKind.ANY_SAMPLE_STATE, ViewStateKind.ANY_VIEW_STATE, InstanceStateKind.ANY_INSTANCE_STATE);
for (int i = 0; i < _dataSeq.size(); ++i) {
SampleInfo info = (SampleInfo) _infoSeq.get(i);
// Since I wrap all the message together (breakdown, accident, and position)
//what we need to do is parse the instances from data readers and out put in format
// The way I distinguish the message type is to check if there is time stamp in the message
if (info.valid_data) {
Vehicle currV = (Vehicle) _dataSeq.get(i);
if (!currV.breakdown.timestamp.equals("")) {
System.out.println();
System.out.format("%10s%20s%20s%20s%20s%20s%20s%20s%20s", "Breakdown", currV.breakdown.route,
currV.breakdown.vehicle, " ", currV.breakdown.stopNumber, " ", " ", " ",
currV.breakdown.timestamp);
} else {
if (!currV.accident.timestamp.equals("")) {
System.out.println();
System.out.format("%10s%20s%20s%20s%20s%20s%20s%20s%20s", "Accident", currV.accident.route,
currV.accident.vehicle, " ", currV.accident.stopNumber, " ", " ", " ",
currV.accident.timestamp);
}
System.out.println();
System.out.format("%10s%20s%20s%20s%20s%20s%20s%20s%20s", "Position", currV.position.route,
currV.position.vehicle, currV.position.trafficConditions, currV.position.stopNumber,
currV.position.numStops, currV.position.timeBetweenStops, currV.position.fillInRatio,
currV.position.timestamp);
}
}
}
} catch (RETCODE_NO_DATA noData) {
// No data to process
} finally {
VehicleReader.return_loan(_dataSeq, _infoSeq);
}
}
}
}