RTI Connext RTSJ Extension Kit  Version 5.1.0
NodeStatusPublisher.java

Node Status Publication Example

This example shows how to write data from a real-time thread. Notice that it is not necessary to use the RTI DDS RTSJ configuration APIs in order to write data from a real-time thread that is under the direct control of the application.

The corresponding subscription code example can be found in the Node Status Subscription Example.

Source Code

/* NodeStatusPublisher.java
A publication of data of type NodeStatus.
This file is derived from code automatically generated by the rtiddsgen
command:
rtiddsgen -language java -example <arch> NodeStatus.idl
Example publication of type NodeStatus 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 NodeStatusSubscriber <domain_id> <sample_count>
(3) Start the publication on the same domain used for RTI Data Distribution
Service with the command
java NodeStatusPublisher <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:
add $(NDDSHOME)/lib/<arch> to the 'LD_LIBRARY_PATH' environment
variable
On Windows:
add $(NDDSHOME)\lib<arch> to the 'Path' environment variable
Run the Java applications:
java -Djava.ext.dirs=$NDDSHOME/class NodeStatusPublisher <domain_id>
java -Djava.ext.dirs=$NDDSHOME/class NodeStatusSubscriber <domain_id>
*/
package com.rti.ndds.rtsj.example.nodestatus;
import javax.realtime.ImmortalMemory;
import javax.realtime.MemoryArea;
import javax.realtime.MemoryParameters;
import javax.realtime.RealtimeThread;
import com.rti.dds.domain.DomainParticipant;
import com.rti.dds.domain.DomainParticipantFactory;
import com.rti.dds.infrastructure.InstanceHandle_t;
import com.rti.dds.infrastructure.StatusKind;
import com.rti.dds.publication.Publisher;
import com.rti.dds.topic.Topic;
// ===========================================================================
public class NodeStatusPublisher {
// -----------------------------------------------------------------------
// 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 --- //
final int finalDomainId = domainId;
final int finalSampleCount = sampleCount;
MemoryArea memArea = ImmortalMemory.instance();
MemoryParameters memoryParameters = new MemoryParameters(
1048576, 4 * 1048576);
Thread thread = new RealtimeThread(
null, null, memoryParameters, memArea, null,
new Runnable() {
public void run() {
publisherMain(finalDomainId, finalSampleCount);
}
}
);
thread.start();
}
// -----------------------------------------------------------------------
// Private Methods
// -----------------------------------------------------------------------
// --- Constructors: -----------------------------------------------------
private NodeStatusPublisher() {
super();
}
// -----------------------------------------------------------------------
private static void publisherMain(int domainId, int sampleCount) {
DomainParticipant participant = null;
try {
// --- Create participant --- //
/* To create participant with default QoS,
use DomainParticipantFactory.DomainParticipantFactory.
participant.get_default_publisher_qos() instead */
participant = DomainParticipantFactory.TheParticipantFactory.
create_participant(
domainId, DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT,
null /* listener */, StatusKind.STATUS_MASK_NONE);
// --- Create publisher --- //
/* To customize publisher QoS, use
participant.get_default_publisher_qos() instead */
Publisher publisher = participant.create_publisher(
DomainParticipant.PUBLISHER_QOS_DEFAULT, null /* listener */,
StatusKind.STATUS_MASK_NONE);
// --- Create topic --- //
/* Register type before creating topic */
String typeName = NodeStatusTypeSupport.get_type_name();
NodeStatusTypeSupport.register_type(participant, typeName);
/* To customize topic QoS, use
participant.get_default_topic_qos() instead */
Topic topic = participant.create_topic(
"Example NodeStatus",
typeName, DomainParticipant.TOPIC_QOS_DEFAULT,
null /* listener */, StatusKind.STATUS_MASK_NONE);
// --- Create writer --- //
/* To customize data writer QoS, use
publisher.get_default_datawriter_qos() instead */
NodeStatusDataWriter writer = (NodeStatusDataWriter)
publisher.create_datawriter(
topic, Publisher.DATAWRITER_QOS_DEFAULT,
null /* listener */, StatusKind.STATUS_MASK_NONE);
// --- Write --- //
/* Create data sample for writing */
NodeStatus instance = new NodeStatus();
InstanceHandle_t instance_handle = InstanceHandle_t.HANDLE_NIL;
/* For data type that has key, if the same instance is going to be
written multiple times, initialize the key here
and register the keyed instance prior to writing */
//instance_handle = writer.register_instance(instance);
final long sendPeriodMillis = 16; // 4 seconds
for (int count = 0;
(sampleCount == 0) || (count < sampleCount);
++count) {
if (count%1024 == 0)
System.out.println("Writing NodeStatus, count " + count);
/* Modify the instance to be written here */
/* Write data */
writer.write(instance, InstanceHandle_t.HANDLE_NIL);
try {
Thread.sleep(sendPeriodMillis);
} catch (InterruptedException ix) {
System.err.println("INTERRUPTED");
break;
}
}
//writer.unregister_instance(instance, instance_handle);
} finally {
// --- Shutdown --- //
if(participant != null) {
participant.delete_contained_entities();
DomainParticipantFactory.TheParticipantFactory.
delete_participant(participant);
}
/* RTI Data Distribution Service provides finalize_instance()
method for people 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();
}
}
}

RTI Connext RTSJ Extension Kit Version 5.1.0 Copyright © Tue Feb 4 2014 Real-Time Innovations, Inc