88import lombok .extern .slf4j .Slf4j ;
99import org .apache .commons .cli .*;
1010
11+ /**
12+ * This class is the CLI Client implementation for communications with the server.
13+ * It extends the abstract {@see Client} class and parses command line parameters.
14+ */
1115@ Slf4j
12- public class CommandLineClient {
16+ public class CommandLineClient extends Client {
1317
1418 private Options options ;
1519 private HelpFormatter helpFormatter ;
@@ -59,6 +63,12 @@ public class CommandLineClient {
5963 .build ());
6064 }
6165
66+ /**
67+ * Parse the passed command line arguments to create a RPC request with the SamplingMessageGrpcService.
68+ * In case a mandatory argument is missing, the program shuts down with an error message.
69+ *
70+ * @param args Command line arguments passed to the JAR call
71+ */
6272 public void run (String [] args ) {
6373 CommandLineParser commandLineParser = new DefaultParser ();
6474 helpFormatter = new HelpFormatter ();
@@ -78,7 +88,6 @@ public void run(String[] args) {
7888
7989 ManagedChannelBuilder <?> managedChannelBuilder = ManagedChannelBuilder .forAddress (address , port ).usePlaintext ();
8090 ManagedChannel managedChannel = managedChannelBuilder .build ();
81- SamplingMessageGrpc .SamplingMessageStub samplingMessageStub = SamplingMessageGrpc .newStub (managedChannel );
8291 SamplingMessageGrpc .SamplingMessageBlockingStub samplingMessageBlockingStub = SamplingMessageGrpc .newBlockingStub (managedChannel );
8392
8493 if (method == 0 ) {
@@ -100,6 +109,19 @@ public void run(String[] args) {
100109 managedChannel .shutdown ();
101110 }
102111
112+ /**
113+ * Create an empty sampling message.
114+ *
115+ * Expected arguments are:
116+ * -d for the duration the message should be valid
117+ * -n for the name the message should bare
118+ *
119+ * If the message name is already in use, an error is returned by the server with the status code CONFLICT.
120+ * A successful request returns a status code SUCCESS.
121+ *
122+ * @param commandLine CommandLine Object for argument extraction
123+ * @param samplingMessageBlockingStub The message stub that executes the sampling message request
124+ */
103125 private void createSamplingMessage (CommandLine commandLine , SamplingMessageGrpc .SamplingMessageBlockingStub samplingMessageBlockingStub ) {
104126 String name = commandLine .getOptionValue ("name" );
105127 long duration ;
@@ -123,6 +145,19 @@ private void createSamplingMessage(CommandLine commandLine, SamplingMessageGrpc.
123145 log .info ("createSamplingMessageResponse Status Code: " + response .getStatusCode ().name ());
124146 }
125147
148+ /**
149+ * Write an existing sampling message.
150+ *
151+ * Expected arguments are:
152+ * -d for the duration the message should be valid
153+ * -c for the content of the message
154+ * -n for the name the message should bare
155+ *
156+ * If the message name is unknown the server returns a status code NOT_FOUND and SUCCESS otherwise.
157+ *
158+ * @param commandLine CommandLine Object for argument extraction
159+ * @param samplingMessageBlockingStub The message stub that executes the sampling message request
160+ */
126161 private void writeSamplingMessage (CommandLine commandLine , SamplingMessageGrpc .SamplingMessageBlockingStub samplingMessageBlockingStub ) {
127162 String name = commandLine .getOptionValue ("name" );
128163 String content = commandLine .getOptionValue ("content" );
@@ -142,6 +177,17 @@ private void writeSamplingMessage(CommandLine commandLine, SamplingMessageGrpc.S
142177 log .info ("writeSamplingMessageResponse Status Code: " + response .getStatusCode ().name ());
143178 }
144179
180+ /**
181+ * Clear an existing sampling message of its content. The message is also invalidated.
182+ *
183+ * Expected arguments are:
184+ * -n for the name of the message to be cleared
185+ *
186+ * If the message name is unknown the server returns a status code NOT_FOUND and SUCCESS otherwise.
187+ *
188+ * @param commandLine CommandLine Object for argument extraction
189+ * @param samplingMessageBlockingStub The message stub that executes the sampling message request
190+ */
145191 private void clearSamplingMessage (CommandLine commandLine , SamplingMessageGrpc .SamplingMessageBlockingStub samplingMessageBlockingStub ) {
146192 String name = commandLine .getOptionValue ("name" );
147193 SamplingMessageGrpcService .ClearSamplingMessageRequest request =
@@ -154,6 +200,17 @@ private void clearSamplingMessage(CommandLine commandLine, SamplingMessageGrpc.S
154200 log .info ("writeSamplingMessageResponse Status Code: " + response .getStatusCode ().name ());
155201 }
156202
203+ /**
204+ * Read an existing sampling message.
205+ *
206+ * Expected arguments are:
207+ * -n for the name of the message to be read
208+ *
209+ * If the message name is unknown the server returns a status code NOT_FOUND and SUCCESS otherwise.
210+ *
211+ * @param commandLine CommandLine Object for argument extraction
212+ * @param samplingMessageBlockingStub The message stub that executes the sampling message request
213+ */
157214 private void readSamplingMessage (CommandLine commandLine , SamplingMessageGrpc .SamplingMessageBlockingStub samplingMessageBlockingStub ) {
158215 String name = commandLine .getOptionValue ("name" );
159216
@@ -169,6 +226,12 @@ private void readSamplingMessage(CommandLine commandLine, SamplingMessageGrpc.Sa
169226 log .info ("readSamplingMessageResponse Valid: " + response .getMessageIsValid ());
170227 }
171228
229+ /**
230+ * Read the current status of an existing sampling message.
231+ *
232+ * @param commandLine CommandLine Object for argument extraction
233+ * @param samplingMessageBlockingStub The message stub that executes the sampling message request
234+ */
172235 private void getSamplingMessageStatus (CommandLine commandLine , SamplingMessageGrpc .SamplingMessageBlockingStub samplingMessageBlockingStub ) {
173236 String name = commandLine .getOptionValue ("name" );
174237
@@ -182,6 +245,17 @@ private void getSamplingMessageStatus(CommandLine commandLine, SamplingMessageGr
182245 log .info ("getSamplingMessageStatusResponse Status Code: " + response .getStatusCode ().name ());
183246 }
184247
248+ /**
249+ * Delete an existing sampling message, permanently removing it from the server storage.
250+ *
251+ * Expected arguments are:
252+ * -n for the name of the message to be deleted
253+ *
254+ * If the message name is unknown the server returns a status code NOT_FOUND and SUCCESS otherwise.
255+ *
256+ * @param commandLine CommandLine Object for argument extraction
257+ * @param samplingMessageBlockingStub The message stub that executes the sampling message request
258+ */
185259 private void deleteSamplingMessage (CommandLine commandLine , SamplingMessageGrpc .SamplingMessageBlockingStub samplingMessageBlockingStub ) {
186260 String name = commandLine .getOptionValue ("name" );
187261
@@ -195,6 +269,12 @@ private void deleteSamplingMessage(CommandLine commandLine, SamplingMessageGrpc.
195269 log .info ("getSamplingMessageStatusResponse Status Code: " + response .getStatusCode ().name ());
196270 }
197271
272+
273+ /**
274+ * Prints an error message before exiting the program with an error exit code of 1.
275+ *
276+ * @param errorMessage Message to be displayed on the console during the program shutdown
277+ */
198278 private void exitWithError (String errorMessage ) {
199279 log .debug ("ParsingError: " , errorMessage );
200280 log .error (errorMessage );
0 commit comments