|
| 1 | +/* |
| 2 | + * (c) Copyright, Real-Time Innovations, 2024. All rights reserved. |
| 3 | + * RTI grants Licensee a license to use, modify, compile, and create derivative |
| 4 | + * works of the software solely for use with RTI Connext DDS. Licensee may |
| 5 | + * redistribute copies of the software provided that all such copies are subject |
| 6 | + * to this license. The software is provided "as is", with no warranty of any |
| 7 | + * type, including any warranty for fitness for any purpose. RTI is under no |
| 8 | + * obligation to maintain or support the software. RTI shall not be liable for |
| 9 | + * any incidental or consequential damages arising out of the use or inability |
| 10 | + * to use the software. |
| 11 | + */ |
| 12 | + |
| 13 | +import com.rti.ndds.config.LogVerbosity; |
| 14 | + |
| 15 | +/** |
| 16 | +* |
| 17 | +* Code in the example application to handle clean shutdown, arguments, etc. |
| 18 | +* |
| 19 | +*/ |
| 20 | +public abstract class Application { |
| 21 | + |
| 22 | + public enum ParseReturn { |
| 23 | + FAILURE, SUCCESS, EXIT |
| 24 | + } |
| 25 | + |
| 26 | + private boolean shutdownRequested = false; |
| 27 | + private int domainId = 0; |
| 28 | + private int maxSampleCount = Integer.MAX_VALUE; |
| 29 | + |
| 30 | + public void parseArguments(String[] args) { |
| 31 | + final int defaultVerbosity = 1; // default to error |
| 32 | + int verbosity = defaultVerbosity; |
| 33 | + int argProcessing = 0; |
| 34 | + boolean showUsage = false; |
| 35 | + ParseReturn parseResult = ParseReturn.SUCCESS; |
| 36 | + |
| 37 | + while (argProcessing < args.length) { |
| 38 | + String argument = args[argProcessing]; |
| 39 | + if ((args.length > argProcessing + 1) |
| 40 | + && (argument.equals("-d") |
| 41 | + || argument.equals("--domain"))) { |
| 42 | + domainId = Integer.parseInt(args[argProcessing + 1]); |
| 43 | + argProcessing++; |
| 44 | + } else if ((args.length > argProcessing + 1) |
| 45 | + && (argument.equals("-s") |
| 46 | + || argument.equals("--sample-count"))) { |
| 47 | + maxSampleCount = Integer.parseInt(args[argProcessing + 1]); |
| 48 | + argProcessing++; |
| 49 | + } else if ((args.length > argProcessing + 1) |
| 50 | + && (argument.equals("-v") |
| 51 | + || argument.equals("--verbosity"))) { |
| 52 | + verbosity = Integer.parseInt(args[argProcessing + 1]); |
| 53 | + argProcessing++; |
| 54 | + } else if (argument.equals("-h") |
| 55 | + || argument.equals("--help")) { |
| 56 | + System.out.println(getClass().getSimpleName() + " application."); |
| 57 | + showUsage = true; |
| 58 | + parseResult = ParseReturn.EXIT; |
| 59 | + break; |
| 60 | + } else { |
| 61 | + System.err.println("Bad parameter: " + argument); |
| 62 | + showUsage = true; |
| 63 | + parseResult = ParseReturn.FAILURE; |
| 64 | + break; |
| 65 | + } |
| 66 | + argProcessing++; |
| 67 | + } |
| 68 | + |
| 69 | + final String NL = System.lineSeparator(); |
| 70 | + if (showUsage) { |
| 71 | + System.out.println("Usage:" + NL |
| 72 | + + " -d, --domain <int> DDS domain ID." + NL |
| 73 | + + " Default: 0" + NL |
| 74 | + + " -s, --sample_count <int> Number of samples to send/receive." + NL |
| 75 | + + " Default: Integer.MAX_VALUE" + NL |
| 76 | + + " -v, --verbosity <int> Debug output level (0-3)" + NL |
| 77 | + + " Default: " + defaultVerbosity); |
| 78 | + } |
| 79 | + |
| 80 | + if (parseResult == ParseReturn.EXIT) { |
| 81 | + // Help requested, so exit instead of running example |
| 82 | + System.exit(0); |
| 83 | + } |
| 84 | + if (parseResult == ParseReturn.FAILURE) { |
| 85 | + throw new IllegalArgumentException(); |
| 86 | + } |
| 87 | + // Always specify the verbosity, even if default |
| 88 | + setVerbosity(verbosity); |
| 89 | + } |
| 90 | + |
| 91 | + protected void addShutdownHook() { |
| 92 | + Runtime.getRuntime().addShutdownHook(new Thread() { |
| 93 | + public void run() { |
| 94 | + System.out.println("Shutting down..."); |
| 95 | + shutdownRequested = true; |
| 96 | + } |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + public boolean isShutdownRequested() { |
| 101 | + return shutdownRequested; |
| 102 | + } |
| 103 | + |
| 104 | + public int getDomainId() { |
| 105 | + return domainId; |
| 106 | + } |
| 107 | + |
| 108 | + public int getMaxSampleCount() { |
| 109 | + return maxSampleCount; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Sets Connext verbosity to help debugging |
| 114 | + * @param verbosity 0 = silent, 1 = error, 2 = warning, 3 = status all. |
| 115 | + */ |
| 116 | + public static void setVerbosity(int verbosity) { |
| 117 | + LogVerbosity logVerbosity; |
| 118 | + switch (verbosity) { |
| 119 | + case 0: |
| 120 | + logVerbosity = LogVerbosity.NDDS_CONFIG_LOG_VERBOSITY_SILENT; |
| 121 | + break; |
| 122 | + case 1: |
| 123 | + logVerbosity = LogVerbosity.NDDS_CONFIG_LOG_VERBOSITY_ERROR; |
| 124 | + break; |
| 125 | + case 2: |
| 126 | + logVerbosity = LogVerbosity.NDDS_CONFIG_LOG_VERBOSITY_WARNING; |
| 127 | + break; |
| 128 | + case 3: |
| 129 | + logVerbosity = LogVerbosity.NDDS_CONFIG_LOG_VERBOSITY_STATUS_ALL; |
| 130 | + break; |
| 131 | + default: |
| 132 | + logVerbosity = LogVerbosity.NDDS_CONFIG_LOG_VERBOSITY_ERROR; |
| 133 | + break; |
| 134 | + } |
| 135 | + com.rti.ndds.config.Logger.get_instance().set_verbosity(logVerbosity); |
| 136 | + } |
| 137 | +} |
0 commit comments