[$(HOME)/rti_workspace//examples/connext_dds/java/hello_world_dtls/HelloWorldPublisher.java]
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.publication.*;
import com.rti.dds.topic.*;
import com.rti.ndds.config.*;
public class HelloWorldPublisher {
public static void main(String[] args) {
int domainId = 0;
if (args.length >= 1) {
domainId = Integer.valueOf(args[0]).intValue();
}
int sampleCount = 0;
if (args.length >= 2) {
sampleCount = Integer.valueOf(args[1]).intValue();
}
publisherMain(domainId, sampleCount);
}
private HelloWorldPublisher() {
super();
}
private static void publisherMain(int domainId, int sampleCount) {
DomainParticipant participant = null;
Publisher publisher = null;
Topic topic = null;
HelloWorldDataWriter writer = null;
DomainParticipantQos participant_qos = new DomainParticipantQos();
try {
DomainParticipantFactory.TheParticipantFactory.get_default_participant_qos(
participant_qos);
participant_qos.transport_builtin.mask =
TransportBuiltinKind.MASK_NONE;
PropertyQosPolicyHelper.add_property(participant_qos.property,
"dds.transport.load_plugins",
"dds.transport.DTLS.dtls1", false);
PropertyQosPolicyHelper.add_property(participant_qos.property,
"dds.transport.DTLS.dtls1.library",
"nddstransporttls", false);
PropertyQosPolicyHelper.add_property(participant_qos.property,
"dds.transport.DTLS.dtls1.create_function",
"NDDS_Transport_DTLS_create", false);
PropertyQosPolicyHelper.add_property(participant_qos.property,
"dds.transport.DTLS.dtls1.tls.verify.ca_file",
"cacert.pem", false);
PropertyQosPolicyHelper.add_property(participant_qos.property,
"dds.transport.DTLS.dtls1.tls.identity.certificate_chain_file",
"peer1.pem", false);
participant = DomainParticipantFactory.TheParticipantFactory.
create_participant(
domainId, participant_qos,
null , StatusKind.STATUS_MASK_NONE);
if (participant == null) {
System.err.println("create_participant error\n");
return;
}
publisher = participant.create_publisher(
DomainParticipant.PUBLISHER_QOS_DEFAULT, null ,
StatusKind.STATUS_MASK_NONE);
if (publisher == null) {
System.err.println("create_publisher error\n");
return;
}
String typeName = HelloWorldTypeSupport.get_type_name();
HelloWorldTypeSupport.register_type(participant, typeName);
topic = participant.create_topic(
"Example HelloWorld",
typeName, DomainParticipant.TOPIC_QOS_DEFAULT,
null , StatusKind.STATUS_MASK_NONE);
if (topic == null) {
System.err.println("create_topic error\n");
return;
}
writer = (HelloWorldDataWriter)
publisher.create_datawriter(
topic, Publisher.DATAWRITER_QOS_DEFAULT,
null , StatusKind.STATUS_MASK_NONE);
if (writer == null) {
System.err.println("create_datawriter error\n");
return;
}
HelloWorld instance = new HelloWorld();
InstanceHandle_t instance_handle = InstanceHandle_t.HANDLE_NIL;
final long sendPeriodMillis = 4 * 1000;
for (int count = 0;
(sampleCount == 0) || (count < sampleCount);
++count) {
System.out.println("Writing HelloWorld to DTLS, count " + count);
instance.msg = "Hello Secure World! (" + count + ")";
writer.write(instance, instance_handle);
try {
Thread.sleep(sendPeriodMillis);
} catch (InterruptedException ix) {
System.err.println("INTERRUPTED");
break;
}
}
} finally {
if(participant != null) {
participant.delete_contained_entities();
DomainParticipantFactory.TheParticipantFactory.
delete_participant(participant);
}
}
}
}