From 48c763d8f3737ecedddf830688787b44d4507414 Mon Sep 17 00:00:00 2001 From: Rose Wahlin Date: Tue, 24 Mar 2020 14:41:18 -0700 Subject: [PATCH] Early commit for review --- 1_hello_world/java/Application.java | 103 ++++++++++ 1_hello_world/java/HelloMessagePublisher.java | 178 ++++++++++++++++++ 2 files changed, 281 insertions(+) create mode 100644 1_hello_world/java/Application.java create mode 100644 1_hello_world/java/HelloMessagePublisher.java diff --git a/1_hello_world/java/Application.java b/1_hello_world/java/Application.java new file mode 100644 index 0000000..321cf0b --- /dev/null +++ b/1_hello_world/java/Application.java @@ -0,0 +1,103 @@ +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.logging.ConsoleHandler; +import java.util.logging.Logger; + +public class Application { + public Application() { + } + + protected static AtomicBoolean shouldRun; + protected static Logger logger; + + static class ApplicationArguments { + public static AtomicInteger domainId = new AtomicInteger(0); + public static AtomicInteger sampleCount = new AtomicInteger(0); + public static AtomicInteger verbosity = new AtomicInteger(0); + public static AtomicBoolean runApplication = new AtomicBoolean(true); + } + + public static void parseArguments( + String[] args) throws Exception { + + int argProcessing = 0; + boolean showUsage = false; + ParseReturn parseResult = ParseReturn.PARSE_RETURN_SUCCESS; + + while (argProcessing < args.length) { + if (args[argProcessing] == "-d" + || args[argProcessing] == "--domain") { + ApplicationArguments.domainId.set( + Integer.parseInt(args[argProcessing + 1])); + argProcessing += 2; + } else if (args[argProcessing] == "-s" + || args[argProcessing] == "--sample-count") { + ApplicationArguments.sampleCount.set( + Integer.parseInt(args[argProcessing + 1])); + argProcessing += 2; + } else if (args[argProcessing] == "-v" + || args[argProcessing] == "--verbosity") { + ApplicationArguments.verbosity.set( + Integer.parseInt(args[argProcessing + 1])); + argProcessing += 2; + } else if (args[argProcessing] == "-h" + || args[argProcessing] == "--help") { + logger.info("Example application."); + showUsage = true; + parseResult = ParseReturn.PARSE_RETURN_EXIT; + break; + } else { + logger.severe("Bad parameter."); + showUsage = true; + parseResult = ParseReturn.PARSE_RETURN_FAILURE; + break; + } + } + if (showUsage) { + logger.info("Usage:\n" + + " -d, --domain Domain ID this application will\n" + + " subscribe in. \n" + + " Default: 0\n" + + " -s, --sample_count Number of samples to receive before\n" + + " cleanly shutting down. \n" + + " Default: infinite\n" + + " -v, --verbosity How much debugging output to show.\n" + + " Range: 0-5 \n" + + " Default: 0"); + } + + if (parseResult == ParseReturn.PARSE_RETURN_FAILURE) { + throw new Exception("Bad parameter passed to applicaiton"); + } else { + ApplicationArguments.runApplication.set(true); + } + } + + public static void handleShutdown() { + shouldRun = new AtomicBoolean(true); + + Runtime.getRuntime().addShutdownHook(new Thread() { + public void run() { + logger.info("Shutting down..."); + shouldRun.set(false); + } + }); + + } + + public static void setUpLogging() { + logger = Logger.getLogger(HelloMessagePublisher.class.getName()); + logger.setUseParentHandlers(false); + logger.addHandler(new ConsoleHandler() { + {setOutputStream(System.out);} + }); + + } + + enum ParseReturn { + PARSE_RETURN_FAILURE, + PARSE_RETURN_SUCCESS, + PARSE_RETURN_EXIT + } + +} diff --git a/1_hello_world/java/HelloMessagePublisher.java b/1_hello_world/java/HelloMessagePublisher.java new file mode 100644 index 0000000..2e17b74 --- /dev/null +++ b/1_hello_world/java/HelloMessagePublisher.java @@ -0,0 +1,178 @@ +/* + * (c) Copyright, Real-Time Innovations, 2020. All rights reserved. + * RTI grants Licensee a license to use, modify, compile, and create derivative + * works of the software solely for use with RTI Connext DDS. Licensee may + * redistribute copies of the software provided that all such copies are subject + * to this license. 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. + */ + +import com.rti.dds.domain.DomainParticipant; +import com.rti.dds.domain.DomainParticipantFactory; +import com.rti.dds.infrastructure.InstanceHandle_t; +import com.rti.dds.infrastructure.RETCODE_ERROR; +import com.rti.dds.infrastructure.StatusKind; +import com.rti.dds.publication.Publisher; +import com.rti.dds.topic.Topic; +import com.rti.ndds.config.LogVerbosity; +import com.rti.ndds.config.Logger; + +// =========================================================================== + +public class HelloMessagePublisher extends Application { + + + public static void main(String[] args) { + setUpLogging(); + + try { + + // Parse arguments passed to application + parseArguments(args); + if (ApplicationArguments.runApplication.get() == true) { + // If the application is running, set a shutdown handler and + // set Connext logging verbosity + handleShutdown(); + setVerbosity(); + + runExample(); + } + } catch (Exception e) { + logger.severe("Exception: " + e.getLocalizedMessage()); + } + } + + + private HelloMessagePublisher() { + super(); + } + + private static void runExample() { + + DomainParticipant participant = null; + try { + // Connext DDS setup + // ----------------- + // A DomainParticipant allows an application to begin communicating + // in a DDS domain. Typically there is one DomainParticipant per + // application. DomainParticipant QoS is configured in + // USER_QOS_PROFILES.xml + participant = DomainParticipantFactory.TheParticipantFactory + .create_participant( + ApplicationArguments.domainId.get(), + DomainParticipantFactory + .PARTICIPANT_QOS_DEFAULT, + null, // listener + StatusKind.STATUS_MASK_NONE); + if (participant == null) { + throw new RETCODE_ERROR("create_participant error"); + } + + // A Publisher allows an application to create one or more + // DataWriters. Publisher QoS is configured in USER_QOS_PROFILES.xml + Publisher publisher = participant.create_publisher( + DomainParticipant.PUBLISHER_QOS_DEFAULT, + null, // listener + StatusKind.STATUS_MASK_NONE); + if (publisher == null) { + throw new RETCODE_ERROR("create_publisher error"); + } + + // Register the datatype to use when creating the Topic + String typeName = HelloMessageTypeSupport.get_type_name(); + HelloMessageTypeSupport.register_type(participant, typeName); + + // A Topic has a name and a datatype. Create a Topic called + // "Example HelloMessage" with your registered data type + Topic topic = participant.create_topic( + "Example HelloMessage", + typeName, + DomainParticipant.TOPIC_QOS_DEFAULT, + null, // listener + StatusKind.STATUS_MASK_NONE); + if (topic == null) { + throw new RETCODE_ERROR("create_topic error"); + } + + // This DataWriter writes data on Topic "Example HelloMessage" + // DataWriter QoS is configured in USER_QOS_PROFILES.xml + HelloMessageDataWriter writer = + (HelloMessageDataWriter)publisher.create_datawriter( + topic, + Publisher.DATAWRITER_QOS_DEFAULT, + null, // listener + StatusKind.STATUS_MASK_NONE); + if (writer == null) { + throw new RETCODE_ERROR("create_datawriter error"); + } + + // Create data sample for writing + HelloMessage sample = new HelloMessage(); + + // Main loop, write data + // --------------------- + for (int count = 0; + shouldRun.get() + && ((ApplicationArguments.sampleCount.get() == 0) + || (count < ApplicationArguments.sampleCount.get())); + ++count) { + logger.info("Writing HelloMessage, count " + count); + + // Modify the data to be written here + + // Write data + writer.write(sample, InstanceHandle_t.HANDLE_NIL); + try { + Thread.sleep(4000); // 4 seconds + } catch (InterruptedException ex) { + // Stop writing + break; + } + } + + } finally { + + // Shutdown + // -------- + if (participant != null) { + participant.delete_contained_entities(); + + DomainParticipantFactory.TheParticipantFactory + .delete_participant(participant); + } + // Optional at shutdown + DomainParticipantFactory.finalize_instance(); + } + } + + // Sets Connext verbosity to help debugging + public static void setVerbosity() { + + switch (ApplicationArguments.verbosity.get()) { + case 0: + Logger.get_instance().set_verbosity( + LogVerbosity.NDDS_CONFIG_LOG_VERBOSITY_SILENT); + break; + case 1: + Logger.get_instance().set_verbosity( + LogVerbosity.NDDS_CONFIG_LOG_VERBOSITY_ERROR); + break; + case 2: + Logger.get_instance().set_verbosity( + LogVerbosity.NDDS_CONFIG_LOG_VERBOSITY_WARNING); + break; + case 3: + Logger.get_instance().set_verbosity( + LogVerbosity.NDDS_CONFIG_LOG_VERBOSITY_STATUS_ALL); + break; + default: + Logger.get_instance().set_verbosity( + LogVerbosity.NDDS_CONFIG_LOG_VERBOSITY_ERROR); + break; + } + + } +}