-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDDSCommunicator.java
More file actions
483 lines (412 loc) · 15.6 KB
/
DDSCommunicator.java
File metadata and controls
483 lines (412 loc) · 15.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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*******************************************************************************
(c) 2005-2014 Copyright, Real-Time Innovations, Inc. All rights reserved.
RTI grants Licensee a license to use, modify, compile, and create derivative
works of the Software. Licensee has the right to distribute object form only
for use with RTI products. 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.
******************************************************************************/
package TelebotDDSCore;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import com.rti.dds.domain.DomainParticipant;
import com.rti.dds.domain.DomainParticipantFactory;
import com.rti.dds.domain.DomainParticipantFactoryQos;
import com.rti.dds.infrastructure.StatusKind;
import com.rti.dds.publication.Publisher;
import com.rti.dds.subscription.Subscriber;
import com.rti.dds.topic.Topic;
import com.rti.dds.topic.TypeSupport;
/**
*
* This class is used by the application's DDS interface to create the core
* communication objects, such as the DomainParticipant, Publisher and/or
* Subscriber.
*
* The DomainParticipant is a DDS object responsible for:
* <ul>
* <li>Setting up network communications
* <li>Discovering other DomainParticipants within the same domain (created
* using the same domain ID number).
* <li>Discovering the DataWriters and DataReaders belonging to those
* DomainParticipants
* </ul>
*
* @author rose
*
*/
public class DDSCommunicator {
// --- Private members ---
// Used to create other DDS entities
private DomainParticipant _participant;
// Used to create DataWriters
private Publisher _pub;
// Used to create DataReaders
private Subscriber _sub;
// --- Constructor --- //
public DDSCommunicator() {
_participant = null;
_pub = null;
_sub = null;
}
// --- Finalization and Network Cleanup --- //
protected void finalize() {
if (_participant != null) {
_participant.delete_contained_entities();
}
DomainParticipantFactory.get_instance().
delete_participant(_participant);
}
// --- Public methods --- //
// --- Creating a DomainParticipant --- //
/**
* Creates a DomainParticipant with default QoS and domain ID 0.
*
* A DomainParticipant starts the DDS discovery process. It creates
* several threads, sends and receives discovery information over one or
* more transports, and maintains an in-memory discovery database of
* remote DomainParticipants, remote DataWriters, and remote DataReaders
* Quality of Service can be applied on the level of the DomainParticipant.
* This QoS controls the characteristics of:
*
* <ol>
* <li>Transport properties such as which type of network (UDPv4, UDPv6,
* shared memory) or which network interfaces it is allowed to use
* <li>Which applications this discovers. By default, apps will discover
* other DDS applications over multicast, loopback, and shared memory.
* <li>Resource limits for the DomainParticipant
* </ol>
* For more information on participant QoS, see the .xml files in the
* Config directory
* @return The newly-created DomainParticipant
* @throws Exception
*/
// --------------------------------------------------------------------- //
// Creating a DomainParticipant with a domain ID of zero
public DomainParticipant createParticipant() throws Exception {
_participant =
DomainParticipantFactory.get_instance().create_participant(
0,
DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT,
null, StatusKind.STATUS_MASK_NONE);
if (_participant == null) {
throw new Exception("Error creating DomainParticipant");
}
return _participant;
}
// --------------------------------------------------------------------- //
/**
* Creates a DomainParticipant with default QoS in the specified domain
*
* A DomainParticipant starts the DDS discovery process. It creates
* several threads, sends and receives discovery information over one or
* more transports, and maintains an in-memory discovery database of
* remote DomainParticipants, remote DataWriters, and remote DataReaders
* Quality of Service can be applied on the level of the DomainParticipant.
* This QoS controls the characteristics of:
*
* <ol>
* <li>Transport properties such as which type of network (UDPv4, UDPv6,
* shared memory) or which network interfaces it is allowed to use
* <li>Which applications this discovers. By default, apps will discover
* other DDS applications over multicast, loopback, and shared memory.
* <li>Resource limits for the DomainParticipant
* </ol>
* For more information on participant QoS, see the .xml files in the
* Config directory
* @return The newly-created DomainParticipant
* @throws Exception
*/
public DomainParticipant createParticipant(int domain) throws Exception {
_participant =
DomainParticipantFactory.get_instance().
create_participant(
domain,
DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT,
null, StatusKind.STATUS_MASK_NONE);
if (_participant == null) {
throw new Exception("Error creating DomainParticipant");
}
return _participant;
}
// ------------------------------------------------------------------------- //
/**
* Creates a DomainParticipant with specified QoS and domain ID.
*
* A DomainParticipant starts the DDS discovery process. It creates
* several threads, sends and receives discovery information over one or
* more transports, and maintains an in-memory discovery database of
* remote DomainParticipants, remote DataWriters, and remote DataReaders
* Quality of Service can be applied on the level of the DomainParticipant.
* This QoS controls the characteristics of:
*
* <ol>
* <li>Transport properties such as which type of network (UDPv4, UDPv6,
* shared memory) or which network interfaces it is allowed to use
* <li>Which applications this discovers. By default, apps will discover
* other DDS applications over multicast, loopback, and shared memory.
* <li>Resource limits for the DomainParticipant
* </ol>
* For more information on participant QoS, see the .xml files in the
* Config directory
* @return The newly-created DomainParticipant
* @throws Exception
*/
public DomainParticipant createParticipant(int domain,
String participantQosLibrary,
String participantQosProfile) throws Exception {
_participant =
DomainParticipantFactory.get_instance()
.create_participant_with_profile(
domain,
participantQosLibrary,
participantQosProfile,
null,
StatusKind.STATUS_MASK_NONE);
if (_participant == null) {
throw new Exception("Error creating DomainParticipant");
}
return _participant;
}
// --------------------------------------------------------------------- //
/**
* Creates a DomainParticipant with default QoS and domain ID 0.
*
* A DomainParticipant starts the DDS discovery process. It creates
* several threads, sends and receives discovery information over one or
* more transports, and maintains an in-memory discovery database of
* remote DomainParticipants, remote DataWriters, and remote DataReaders
* Quality of Service can be applied on the level of the DomainParticipant.
* This QoS controls the characteristics of:
*
* <ol>
* <li>Transport properties such as which type of network (UDPv4, UDPv6,
* shared memory) or which network interfaces it is allowed to use
* <li>Which applications this discovers. By default, apps will discover
* other DDS applications over multicast, loopback, and shared memory.
* <li>Resource limits for the DomainParticipant
* </ol>
* For more information on participant QoS, see the .xml files in the
* Config directory
*
* @return The newly-created DomainParticipant
* @throws Exception
*/
public DomainParticipant createParticipant(int domain,
List<String>fileNames,
String participantQosLibrary,
String participantQosProfile) throws Exception {
// Adding a list of explicit file names to the DomainParticipantFactory
// This gives the middleware a set of places to search for the files
DomainParticipantFactoryQos factoryQos =
new DomainParticipantFactoryQos();
DomainParticipantFactory.get_instance().get_qos(factoryQos);
factoryQos.profile.url_profile.setMaximum(fileNames.size());
for (int i = 0; i < fileNames.size(); i++) {
factoryQos.profile.url_profile.add(fileNames.get(i));
}
DomainParticipantFactory.get_instance().set_qos(factoryQos);
// Actually creating the DomainParticipant
_participant =
DomainParticipantFactory.get_instance()
.create_participant_with_profile(
domain,
participantQosLibrary,
participantQosProfile,
null,
StatusKind.STATUS_MASK_NONE);
if (_participant == null) {
throw new Exception("Failed to create DomainParticipant object");
}
return _participant;
}
// --------------------------------------------------------------------- //
/** Gets the DomainParticipant object
*
* @return the DomainParticipant object, or null if it has not been created
*/
public DomainParticipant getParticipant() {
return _participant;
}
// --- Creating a Publisher --- //
// --------------------------------------------------------------------- //
/** Creates a Publisher object. This is used to create type-specific
* DataWriter objects in the application
*
* @return The newly-created Publisher
* @throws Exception
*/
public Publisher createPublisher() throws Exception {
if (getParticipant() == null) {
throw new Exception(
"DomainParticipant null, communicator not properly initialized");
}
// Creating a Publisher.
// This object is used to create type-specific DataWriter objects that
// can actually send data.
//
_pub = getParticipant().create_publisher(
DomainParticipant.PUBLISHER_QOS_DEFAULT,
null, StatusKind.STATUS_MASK_NONE);
if (_pub == null) {
throw new Exception("Failed to create Publisher");
}
return _pub;
}
// ------------------------------------------------------------------------- //
/** Creates a Publisher object with specified QoS. This is used to create
* type-specific DataWriter objects in the application
*
* @param qosLibrary
* @param qosProfile
* @return The newly-created Publisher
* @throws Exception
*/
public Publisher createPublisher(String qosLibrary, String qosProfile)
throws Exception {
if (getParticipant() == null) {
throw new Exception(
"DomainParticipant NULL - communicator not properly " +
"initialized");
}
// Creating a Publisher.
// This object is used to create type-specific DataWriter objects that
// can actually send data.
//
_pub = getParticipant().create_publisher_with_profile(
qosLibrary,
qosProfile,
null, StatusKind.STATUS_MASK_NONE);
if (_pub == null) {
throw new Exception("Failed to create Publisher");
}
return _pub;
}
// --- Getting the Publisher --- //
/**
* Gets the publisher object
*
* @return The Publisher, or null if it was not created.
* @throws Exception
*/
public Publisher getPublisher() throws Exception {
if (_pub == null) {
createPublisher();
}
return _pub;
}
// --- Creating a Subscriber --- //
// --------------------------------------------------------------------- //
/** Creates a Subscriber object. This is used to create type-specific
* DataReader objects in the application
*
* @return The newly-created Subscriber
* @throws Exception
*/
public Subscriber createSubscriber() throws Exception {
if (getParticipant() == null) {
throw new Exception(
"DomainParticipant NULL - communicator not properly " +
"initialized");
}
// Creating a Subscriber.
// This object is used to create type-specific DataReader objects that
// can actually receive data. The Subscriber object is being created
// in the DDSCommunicator class because one Subscriber can be used to
// create multiple DDS DataReaders.
//
_sub = getParticipant().create_subscriber(
DomainParticipant.SUBSCRIBER_QOS_DEFAULT,
null, StatusKind.STATUS_MASK_NONE);
if (_sub == null) {
throw new Exception("Failed to create Subscriber");
}
return _sub;
}
// --------------------------------------------------------------------- //
/** Creates a Subscriber object with specified QoS. This is used to
* create type-specific DataReader objects in the application
*
* @param qosLibrary
* @param qosProfile
* @return The newly-created Subscriber
* @throws Exception
*/
public Subscriber createSubscriber(
String qosLibrary, String qosProfile) throws Exception {
if (getParticipant() == null) {
throw new Exception("DomainParticipant NULL - " +
"communicator not properly initialized");
}
// Creating a Subscriber.
// This object is used to create type-specific DataReader objects that
// can actually receive data. The Subscriber object is being created
// in the DDSCommunicator class because one Subscriber can be used to
// create multiple DDS DataReaders.
//
_sub = getParticipant().create_subscriber_with_profile(
qosLibrary,
qosProfile,
null, StatusKind.STATUS_MASK_NONE);
if (_sub == null) {
throw new Exception( "Failed to create Subscriber");
}
return _sub;
}
// --- Getting the Subscriber --- //
/**
* Gets the Subscriber
*
* @return The Subscriber, or null if it was not created
* @throws Exception
*/
public Subscriber getSubscriber() throws Exception {
if (_sub == null) {
return createSubscriber();
}
return _sub;
}
/**
* Creates Topics.
*
* Topics are objects in DDS that describe the meaning of data being sent.
* The data type may be simple and reusable, but the Topic gives that data
* a label that explains the meaning and context of that data. DataWriters
* and DataReaders communicate when they have the same Topic names.
*
* @param topicName
* @param type
* @return A Topic for type T
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws SecurityException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InvocationTargetException
* @throws InstantiationException
*/
@SuppressWarnings("unchecked")
public <T> Topic createTopic(String topicName, Class<T> type)
throws ClassNotFoundException,
NoSuchMethodException, SecurityException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException,
InstantiationException {
Class<T> typeClass = type;
String typeClassName = typeClass.getName();
Class<? extends TypeSupport> typeSupportClass =
(Class<? extends TypeSupport>) Class.forName(
typeClassName + "TypeSupport");
Method getInstanceMethod = typeSupportClass.getMethod("get_instance");
TypeSupport typeSupport = (TypeSupport) getInstanceMethod.invoke(null);
Method getTypeName = typeSupportClass.getMethod("get_type_name");
String typeName = (String) getTypeName.invoke(null);
// register the type
_participant.register_type(typeName, typeSupport, null);
return _participant.create_topic(topicName, typeName,
DomainParticipant.TOPIC_QOS_DEFAULT, null,
StatusKind.STATUS_MASK_NONE);
}
}