|
| 1 | +package de.dhbw.ravensburg.verteiltesysteme.client; |
| 2 | + |
| 3 | + |
| 4 | +import de.dhbw.ravensburg.verteiltesysteme.de.dhbw.ravensburg.verteiltesysteme.rpc.SamplingMessageGrpc; |
| 5 | +import de.dhbw.ravensburg.verteiltesysteme.de.dhbw.ravensburg.verteiltesysteme.rpc.SamplingMessageGrpcService; |
| 6 | +import io.grpc.ManagedChannel; |
| 7 | +import io.grpc.ManagedChannelBuilder; |
| 8 | +import lombok.extern.slf4j.Slf4j; |
| 9 | +import org.apache.commons.cli.*; |
| 10 | + |
| 11 | +@Slf4j |
| 12 | +public class CommandLineClient { |
| 13 | + |
| 14 | + private Options options; |
| 15 | + private HelpFormatter helpFormatter; |
| 16 | + |
| 17 | + |
| 18 | + CommandLineClient() { |
| 19 | + options = new Options(); |
| 20 | + |
| 21 | + options.addOption(Option.builder("n") |
| 22 | + .longOpt("name") |
| 23 | + .hasArg(true) |
| 24 | + .required(true) |
| 25 | + .desc("message name") |
| 26 | + .build()); |
| 27 | + |
| 28 | + options.addOption(Option.builder("c") |
| 29 | + .longOpt("content") |
| 30 | + .hasArg(true) |
| 31 | + .desc("message content") |
| 32 | + .build()); |
| 33 | + |
| 34 | + options.addOption(Option.builder("d") |
| 35 | + .longOpt("duration") |
| 36 | + .hasArg(true) |
| 37 | + .desc("duration of the message") |
| 38 | + .build()); |
| 39 | + |
| 40 | + options.addOption(Option.builder("m") |
| 41 | + .longOpt("method") |
| 42 | + .hasArg(true) |
| 43 | + .required(true) |
| 44 | + .desc("client method to be used") |
| 45 | + .build()); |
| 46 | + |
| 47 | + options.addOption(Option.builder("a") |
| 48 | + .longOpt("address") |
| 49 | + .hasArg(true) |
| 50 | + .required(true) |
| 51 | + .desc("sampling message server address") |
| 52 | + .build()); |
| 53 | + |
| 54 | + options.addOption(Option.builder("p") |
| 55 | + .longOpt("port") |
| 56 | + .hasArg(true) |
| 57 | + .required(true) |
| 58 | + .desc("sampling message server port") |
| 59 | + .build()); |
| 60 | + } |
| 61 | + |
| 62 | + public void run(String[] args) throws Exception { |
| 63 | + CommandLineParser commandLineParser = new DefaultParser(); |
| 64 | + helpFormatter = new HelpFormatter(); |
| 65 | + CommandLine commandLine; |
| 66 | + |
| 67 | + try { |
| 68 | + commandLine = commandLineParser.parse(options, args); |
| 69 | + } catch (ParseException e) { |
| 70 | + exitWithError(e.getMessage()); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + final String address = commandLine.getOptionValue("address"); |
| 75 | + final int port = Integer.parseInt(commandLine.getOptionValue("port")); |
| 76 | + |
| 77 | + final int method = Integer.parseInt(commandLine.getOptionValue("method")); |
| 78 | + |
| 79 | + ManagedChannelBuilder<?> managedChannelBuilder = ManagedChannelBuilder.forAddress(address, port).usePlaintext(); |
| 80 | + ManagedChannel managedChannel = managedChannelBuilder.build(); |
| 81 | + SamplingMessageGrpc.SamplingMessageStub samplingMessageStub = SamplingMessageGrpc.newStub(managedChannel); |
| 82 | + SamplingMessageGrpc.SamplingMessageBlockingStub samplingMessageBlockingStub = SamplingMessageGrpc.newBlockingStub(managedChannel); |
| 83 | + |
| 84 | + if (method == 0) { |
| 85 | + createSamplingMessage(commandLine, samplingMessageBlockingStub); |
| 86 | + } else if (method == 1) { |
| 87 | + writeSamplingMessage(commandLine, samplingMessageBlockingStub); |
| 88 | + } else if (method == 2) { |
| 89 | + clearSamplingMessage(commandLine, samplingMessageBlockingStub); |
| 90 | + } else if (method == 3) { |
| 91 | + readSamplingMessage(commandLine, samplingMessageBlockingStub); |
| 92 | + } else if (method == 4) { |
| 93 | + getSamplingMessageStatus(commandLine, samplingMessageBlockingStub); |
| 94 | + } else if (method == 5) { |
| 95 | + deleteSamplingMessage(commandLine, samplingMessageBlockingStub); |
| 96 | + } else { |
| 97 | + exitWithError("Unknown method: " + method); |
| 98 | + } |
| 99 | + |
| 100 | + managedChannel.shutdown(); |
| 101 | + } |
| 102 | + |
| 103 | + private void createSamplingMessage(CommandLine commandLine, SamplingMessageGrpc.SamplingMessageBlockingStub samplingMessageBlockingStub) { |
| 104 | + String name = commandLine.getOptionValue("name"); |
| 105 | + long duration; |
| 106 | + |
| 107 | + if (commandLine.getOptionValue("duration") == null) { |
| 108 | + exitWithError("Missing argument -d: no sampling message duration given"); |
| 109 | + // only required to have the IDE not complain about a possibly not initialized duration variable |
| 110 | + return; |
| 111 | + } else { |
| 112 | + duration = Long.parseLong(commandLine.getOptionValue("duration")); |
| 113 | + } |
| 114 | + |
| 115 | + SamplingMessageGrpcService.CreateSamplingMessageRequest request = |
| 116 | + SamplingMessageGrpcService.CreateSamplingMessageRequest.newBuilder() |
| 117 | + .setMessageName(name) |
| 118 | + .setLifetimeInSec(duration) |
| 119 | + .build(); |
| 120 | + log.info("creating sampling message " + request.getMessageName()); |
| 121 | + |
| 122 | + SamplingMessageGrpcService.CreateSamplingMessageResponse response = samplingMessageBlockingStub.createSamplingMessage(request); |
| 123 | + log.info("createSamplingMessageResponse Status Code: " + response.getStatusCode().name()); |
| 124 | + } |
| 125 | + |
| 126 | + private void writeSamplingMessage(CommandLine commandLine, SamplingMessageGrpc.SamplingMessageBlockingStub samplingMessageBlockingStub) { |
| 127 | + String name = commandLine.getOptionValue("name"); |
| 128 | + String content = commandLine.getOptionValue("content"); |
| 129 | + |
| 130 | + if (content == null) { |
| 131 | + exitWithError("Missing argument -c: no sampling message content given"); |
| 132 | + } |
| 133 | + |
| 134 | + SamplingMessageGrpcService.WriteSamplingMessageRequest request = |
| 135 | + SamplingMessageGrpcService.WriteSamplingMessageRequest.newBuilder() |
| 136 | + .setMessageName(name) |
| 137 | + .setMessageContent(content) |
| 138 | + .build(); |
| 139 | + log.info("writing sampling message " + request.getMessageName()); |
| 140 | + |
| 141 | + SamplingMessageGrpcService.WriteSamplingMessageResponse response = samplingMessageBlockingStub.writeSamplingMessage(request); |
| 142 | + log.info("writeSamplingMessageResponse Status Code: " + response.getStatusCode().name()); |
| 143 | + } |
| 144 | + |
| 145 | + private void clearSamplingMessage(CommandLine commandLine, SamplingMessageGrpc.SamplingMessageBlockingStub samplingMessageBlockingStub) { |
| 146 | + String name = commandLine.getOptionValue("name"); |
| 147 | + SamplingMessageGrpcService.ClearSamplingMessageRequest request = |
| 148 | + SamplingMessageGrpcService.ClearSamplingMessageRequest.newBuilder() |
| 149 | + .setMessageName(name) |
| 150 | + .build(); |
| 151 | + log.info("clearing sampling message " + request.getMessageName()); |
| 152 | + |
| 153 | + SamplingMessageGrpcService.ClearSamplingMessageResponse response = samplingMessageBlockingStub.clearSamplingMessage(request); |
| 154 | + log.info("writeSamplingMessageResponse Status Code: " + response.getStatusCode().name()); |
| 155 | + } |
| 156 | + |
| 157 | + private void readSamplingMessage(CommandLine commandLine, SamplingMessageGrpc.SamplingMessageBlockingStub samplingMessageBlockingStub) { |
| 158 | + String name = commandLine.getOptionValue("name"); |
| 159 | + |
| 160 | + SamplingMessageGrpcService.ReadSamplingMessageRequest request = |
| 161 | + SamplingMessageGrpcService.ReadSamplingMessageRequest.newBuilder() |
| 162 | + .setMessageName(name) |
| 163 | + .build(); |
| 164 | + log.info("reading sampling message " + request.getMessageName()); |
| 165 | + |
| 166 | + SamplingMessageGrpcService.ReadSamplingMessageResponse response = samplingMessageBlockingStub.readSamplingMessage(request); |
| 167 | + log.info("readSamplingMessageResponse Status Code: " + response.getStatusCode().name()); |
| 168 | + log.info("readSamplingMessageResponse Content: " + response.getMessageContent()); |
| 169 | + log.info("readSamplingMessageResponse Valid: " + response.getMessageIsValid()); |
| 170 | + } |
| 171 | + |
| 172 | + private void getSamplingMessageStatus(CommandLine commandLine, SamplingMessageGrpc.SamplingMessageBlockingStub samplingMessageBlockingStub) { |
| 173 | + String name = commandLine.getOptionValue("name"); |
| 174 | + |
| 175 | + SamplingMessageGrpcService.GetSamplingMessageStatusRequest request = |
| 176 | + SamplingMessageGrpcService.GetSamplingMessageStatusRequest.newBuilder() |
| 177 | + .setMessageName(name) |
| 178 | + .build(); |
| 179 | + log.info("getting status code for message " + request.getMessageName()); |
| 180 | + |
| 181 | + SamplingMessageGrpcService.GetSamplingMessageStatusResponse response = samplingMessageBlockingStub.getSamplingMessageStatus(request); |
| 182 | + log.info("getSamplingMessageStatusResponse Status Code: " + response.getStatusCode().name()); |
| 183 | + } |
| 184 | + |
| 185 | + private void deleteSamplingMessage(CommandLine commandLine, SamplingMessageGrpc.SamplingMessageBlockingStub samplingMessageBlockingStub) { |
| 186 | + String name = commandLine.getOptionValue("name"); |
| 187 | + |
| 188 | + SamplingMessageGrpcService.DeleteSamplingMessageRequest request = |
| 189 | + SamplingMessageGrpcService.DeleteSamplingMessageRequest.newBuilder() |
| 190 | + .setMessageName(name) |
| 191 | + .build(); |
| 192 | + log.info("deleting status code for message " + request.getMessageName()); |
| 193 | + |
| 194 | + SamplingMessageGrpcService.DeleteSamplingMessageResponse response = samplingMessageBlockingStub.deleteSamplingMessage(request); |
| 195 | + log.info("getSamplingMessageStatusResponse Status Code: " + response.getStatusCode().name()); |
| 196 | + } |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | + private void exitWithError(String errorMessage) { |
| 201 | + System.out.println(errorMessage); |
| 202 | + helpFormatter.printHelp("sampling-message-client", options); |
| 203 | + System.exit(1); |
| 204 | + } |
| 205 | +} |
0 commit comments