/******************************************************************************* (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. ******************************************************************************/ /* hello_worldSubscriber.java A publication of data of type hello_world This file is derived from code automatically generated by the rtiddsgen command: rtiddsgen -language java -example .idl Example publication of type hello_world 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 with the command java hello_worldSubscriber (3) Start the publication with the command java hello_worldPublisher (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 : Ensure that $(NDDSHOME)/lib/ is on the dynamic library path for Java. On UNIX systems: add $(NDDSHOME)/lib/ to the 'LD_LIBRARY_PATH' environment variable On Windows systems: add %NDDSHOME%\lib\ to the 'Path' environment variable Run the Java applications: java -Djava.ext.dirs=$NDDSHOME/class hello_worldPublisher java -Djava.ext.dirs=$NDDSHOME/class hello_worldSubscriber modification history ------------ ------- */ 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 hello_worldSubscriber { // ----------------------------------------------------------------------- // Public Methods // ----------------------------------------------------------------------- // This function will take the values from the command line parameters private static int READ_INTEGER_PARAM(int i, String parameter, String[] args, String syntax) { if (args[i].equals(parameter)) { if (i + 1 >= args.length) { System.out.println(syntax); return -1; } return Integer.valueOf(args[i + 1]).intValue(); } return 0; } public static void main(String[] args) { int domain_id = 0; int sample_count = 0; int drs = 0; String syntax; int i = 0; syntax = new String("[options] \n" + "-domain_id (default: 0)\n" + "-sample_count " + "(default: infinite)\n" + "-drs <1|0> Enable/Disable durable reader state " + "(default: 0)"); for (i = 0; i < args.length; ++i) { if (domain_id == 0) { domain_id = READ_INTEGER_PARAM(i, "-domain_id", args, syntax); } if (sample_count == 0) { sample_count = READ_INTEGER_PARAM(i, "-sample_count", args, syntax); } if (drs == 0) { drs = READ_INTEGER_PARAM(i, "-drs", args, syntax); } } if (domain_id < 0 || sample_count < 0 || drs < 0) { return; } /* 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(domain_id, sample_count, drs); } // ----------------------------------------------------------------------- // Private Methods // ----------------------------------------------------------------------- // --- Constructors: ----------------------------------------------------- private hello_worldSubscriber() { super(); } // ----------------------------------------------------------------------- private static void subscriberMain(int domainId, int sampleCount, int drs) { DomainParticipant participant = null; Subscriber subscriber = null; Topic topic = null; DataReaderListener listener = null; hello_worldDataReader 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 = hello_worldTypeSupport.get_type_name(); hello_worldTypeSupport.register_type(participant, typeName); /* To customize topic QoS, use the configuration file USER_QOS_PROFILES.xml */ topic = participant.create_topic( "Example hello_world", 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 hello_worldListener(); /* To customize data reader QoS, use the configuration file USER_QOS_PROFILES.xml */ /* If you use Durable Reader State, you need to set up several properties. * In this example, we have modified them using a QoS XML profile. See * further details in USER_QOS_PROFILES.xml. */ if (drs == 1) { reader = (hello_worldDataReader) subscriber.create_datareader_with_profile(topic, "persistence_example_Library", "durable_reader_state_Profile", listener, StatusKind.STATUS_MASK_ALL); } else { reader = (hello_worldDataReader) subscriber.create_datareader_with_profile(topic, "persistence_example_Library", "persistence_service_Profile", listener, StatusKind.STATUS_MASK_ALL); } if (reader == null) { System.err.println("create_datareader error\n"); return; } // --- Wait for data --- // final long receive_period_sec = 4; for (int count = 0; (sampleCount == 0) || (count < sampleCount); ++count) { System.out.println("hello_world subscriber sleeping for " + receive_period_sec + " sec..."); try { Thread.sleep(receive_period_sec * 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 Connext 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 hello_worldListener extends DataReaderAdapter { hello_worldSeq _dataSeq = new hello_worldSeq(); SampleInfoSeq _infoSeq = new SampleInfoSeq(); public void on_data_available(DataReader reader) { hello_worldDataReader hello_worldReader = (hello_worldDataReader)reader; try { hello_worldReader.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( ((hello_world)_dataSeq.get(i)). toString("Received",0)); } } } catch (RETCODE_NO_DATA noData) { // No data to process } finally { hello_worldReader.return_loan(_dataSeq, _infoSeq); } } } }