[$(HOME)/rti_workspace//examples/connext_dds/java/hello_world_wan/HelloWorldSubscriber.java]1
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 HelloWorldSubscriber {
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();
}
subscriberMain(domainId, sampleCount);
}
private HelloWorldSubscriber() {
super();
}
private static void subscriberMain(int domainId, int sampleCount) {
DomainParticipant participant = null;
Subscriber subscriber = null;
Topic topic = null;
DataReaderListener listener = null;
HelloWorldDataReader reader = null;
DomainParticipantQos participant_qos = new DomainParticipantQos();
String WAN_SERVER = "127.0.0.1";
String WAN_ID = "2";
boolean USE_SECURITY = false;
String WAN_LIB = "nddstransportwan";
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.wan_plugin.wan", false);
PropertyQosPolicyHelper.add_property(participant_qos.property,
"dds.transport.wan_plugin.wan.library",
WAN_LIB, false);
PropertyQosPolicyHelper.add_property(participant_qos.property,
"dds.transport.wan_plugin.wan.create_function", "NDDS_Transport_WAN_create", false);
if (USE_SECURITY) {
System.out.println("Enabling secure WAN transport");
PropertyQosPolicyHelper.add_property(participant_qos.property,
"dds.transport.wan_plugin.wan.enable_security", "1", false);
PropertyQosPolicyHelper.add_property(participant_qos.property,
"dds.transport.wan_plugin.wan.tls.verify.ca_file", "cacert.pem", false);
PropertyQosPolicyHelper.add_property(participant_qos.property,
"dds.transport.wan_plugin.wan.tls.identity.certificate_chain_file", "peer2.pem", false);
}
PropertyQosPolicyHelper.add_property(participant_qos.property,
"dds.transport.wan_plugin.wan.server", WAN_SERVER, false);
PropertyQosPolicyHelper.add_property(participant_qos.property,
"dds.transport.wan_plugin.wan.transport_instance_id", WAN_ID, 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;
}
subscriber = participant.create_subscriber(
DomainParticipant.SUBSCRIBER_QOS_DEFAULT, null ,
StatusKind.STATUS_MASK_NONE);
if (subscriber == null) {
System.err.println("create_subscriber 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;
}
listener = new HelloWorldListener();
reader = (HelloWorldDataReader)
subscriber.create_datareader(
topic, Subscriber.DATAREADER_QOS_DEFAULT, listener,
StatusKind.STATUS_MASK_ALL);
if (reader == null) {
System.err.println("create_datareader error\n");
return;
}
final long receivePeriodSec = 4;
for (int count = 0;
(sampleCount == 0) || (count < sampleCount);
++count) {
System.out.println("HelloWorld subscriber sleeping for "
+ receivePeriodSec + " sec...");
try {
Thread.sleep(receivePeriodSec * 1000);
} catch (InterruptedException ix) {
System.err.println("INTERRUPTED");
break;
}
}
} finally {
if(participant != null) {
participant.delete_contained_entities();
DomainParticipantFactory.TheParticipantFactory.
delete_participant(participant);
}
}
}
private static class HelloWorldListener extends DataReaderAdapter {
HelloWorldSeq _dataSeq = new HelloWorldSeq();
SampleInfoSeq _infoSeq = new SampleInfoSeq();
public void on_data_available(DataReader reader) {
HelloWorldDataReader HelloWorldReader =
(HelloWorldDataReader)reader;
try {
HelloWorldReader.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);
if (info.valid_data) {
System.out.println(
((HelloWorld)_dataSeq.get(i)).toString("Received",0));
}
}
} catch (RETCODE_NO_DATA noData) {
} finally {
HelloWorldReader.return_loan(_dataSeq, _infoSeq);
}
}
}
}