/******************************************************************************* (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_worldPublisher.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 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: add $(NDDSHOME)/lib/ to the 'LD_LIBRARY_PATH' environment variable On Windows: 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.publication.*; import com.rti.dds.topic.*; import com.rti.ndds.config.*; // =========================================================================== public class hello_worldPublisher { // ----------------------------------------------------------------------- // 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) { // --- Get domain ID --- // int domain_id = 0; int sample_count = 0; int initial_value = 0; int dwh = 0; int sleep = 0; String syntax; int i = 0; syntax = new String("[options] \n" + "-domain_id (default: 0)\n" + "-sample_count " + "(default: infinite)\n" + "-initial_value (default: 0)\n" + "-sleep " + "(default: 0)\n" + "-dwh <1|0> Enable/Disable durable writer history " + "(default: 0)\n"); for (i = 0; i < args.length; ++i) { if (sleep == 0) { sleep = READ_INTEGER_PARAM(i, "-sleep", args, syntax); } 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 (initial_value == 0) { initial_value = READ_INTEGER_PARAM(i, "-initial_value", args, syntax); } if (dwh == 0) { dwh = READ_INTEGER_PARAM(i, "-dwh", args, syntax); } } if (sleep < 0 || domain_id < 0 || sample_count < 0 || initial_value < 0 || dwh < 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 --- // publisherMain(domain_id, sample_count, initial_value, dwh, sleep); } // ----------------------------------------------------------------------- // Private Methods // ----------------------------------------------------------------------- // --- Constructors: ----------------------------------------------------- private hello_worldPublisher() { super(); } // ----------------------------------------------------------------------- private static void publisherMain(int domain_id, int sample_count, int initial_value, int dwh, int sleep) { DomainParticipant participant = null; Publisher publisher = null; Topic topic = null; hello_worldDataWriter writer = null; try { // --- Create participant --- // /* * To customize participant QoS, use the configuration file * USER_QOS_PROFILES.xml */ participant = DomainParticipantFactory.TheParticipantFactory .create_participant(domain_id, DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT, null /* listener */, StatusKind.STATUS_MASK_NONE); if (participant == null) { System.err.println("create_participant error\n"); return; } // --- Create publisher --- // /* * To customize publisher QoS, use the configuration file * USER_QOS_PROFILES.xml */ publisher = participant.create_publisher( DomainParticipant.PUBLISHER_QOS_DEFAULT, null /* listener */, StatusKind.STATUS_MASK_NONE); if (publisher == null) { System.err.println("create_publisher 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; } /* If you use Durable Writer History, you need to set several * properties. These properties are set in the USER_QOS_PROFILE.xml * file, "durable_writer_history_Profile" profile. See that file for * further details. */ // --- Create writer --- // if (dwh == 1) { writer = (hello_worldDataWriter) publisher .create_datawriter_with_profile(topic, "persistence_example_Library", "durable_writer_history_Profile", null, StatusKind.STATUS_MASK_NONE); } else { writer = (hello_worldDataWriter) publisher .create_datawriter_with_profile(topic, "persistence_example_Library", "persistence_service_Profile", null, StatusKind.STATUS_MASK_NONE); } if (writer == null) { System.err.println("create_writer error\n"); return; } // --- Write --- // /* Create data sample for writing */ hello_world instance = new hello_world(); InstanceHandle_t instance_handle = InstanceHandle_t.HANDLE_NIL; /* * For a data type that has a 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 send_period_millis = 1 * 1000; // 1 seconds final long one_sec = 1000; for (int count = 0; (sample_count == 0) || (count < sample_count); ++count) { System.out.println("Writing hello_world, count " + initial_value); /* Modify the instance to be written here */ instance.data = initial_value; initial_value++; /* Write data */ writer.write(instance, instance_handle); try { Thread.sleep(send_period_millis); } catch (InterruptedException ix) { System.err.println("INTERRUPTED"); break; } } while (sleep != 0) { try { Thread.sleep(one_sec); } catch (InterruptedException e) { e.printStackTrace(); } sleep--; } //writer.unregister_instance(instance, instance_handle); } finally { // --- Shutdown --- // if (participant != null) { participant.delete_contained_entities(); DomainParticipantFactory.TheParticipantFactory .delete_participant(participant); } /* RTI Connext 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(); } } }