Skip to content

Commit 67db405

Browse files
Parameterize the exit with help screen method and wrap the client execution in a try/catch block to prevent a stacktrace from appearing when no server is reachable
1 parent 1757fe8 commit 67db405

File tree

2 files changed

+30
-24
lines changed
  • sampling-message-client/src/main/java/de/dhbw/ravensburg/verteiltesysteme/client
  • sampling-message-server/src/main/java/de/dhbw/ravensburg/verteiltesysteme/server

2 files changed

+30
-24
lines changed

sampling-message-client/src/main/java/de/dhbw/ravensburg/verteiltesysteme/client/CommandLineClient.java

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import de.dhbw.ravensburg.verteiltesysteme.de.dhbw.ravensburg.verteiltesysteme.rpc.SamplingMessageGrpcService;
66
import io.grpc.ManagedChannel;
77
import io.grpc.ManagedChannelBuilder;
8+
import io.grpc.StatusRuntimeException;
89
import lombok.extern.slf4j.Slf4j;
910
import org.apache.commons.cli.*;
1011

@@ -89,7 +90,7 @@ public void run(String[] args) {
8990
}
9091

9192
if (commandLine.hasOption("help")) {
92-
exitWithHelpScreen();
93+
exitWithHelpScreen(0);
9394
}
9495

9596
final String address = commandLine.getOptionValue("address");
@@ -101,23 +102,28 @@ public void run(String[] args) {
101102
ManagedChannel managedChannel = managedChannelBuilder.build();
102103
SamplingMessageGrpc.SamplingMessageBlockingStub samplingMessageBlockingStub = SamplingMessageGrpc.newBlockingStub(managedChannel);
103104

104-
if (method == 0) {
105-
createSamplingMessage(commandLine, samplingMessageBlockingStub);
106-
} else if (method == 1) {
107-
writeSamplingMessage(commandLine, samplingMessageBlockingStub);
108-
} else if (method == 2) {
109-
clearSamplingMessage(commandLine, samplingMessageBlockingStub);
110-
} else if (method == 3) {
111-
readSamplingMessage(commandLine, samplingMessageBlockingStub);
112-
} else if (method == 4) {
113-
getSamplingMessageStatus(commandLine, samplingMessageBlockingStub);
114-
} else if (method == 5) {
115-
deleteSamplingMessage(commandLine, samplingMessageBlockingStub);
116-
} else {
117-
exitWithError("Unknown method: " + method);
105+
try {
106+
if (method == 0) {
107+
createSamplingMessage(commandLine, samplingMessageBlockingStub);
108+
} else if (method == 1) {
109+
writeSamplingMessage(commandLine, samplingMessageBlockingStub);
110+
} else if (method == 2) {
111+
clearSamplingMessage(commandLine, samplingMessageBlockingStub);
112+
} else if (method == 3) {
113+
readSamplingMessage(commandLine, samplingMessageBlockingStub);
114+
} else if (method == 4) {
115+
getSamplingMessageStatus(commandLine, samplingMessageBlockingStub);
116+
} else if (method == 5) {
117+
deleteSamplingMessage(commandLine, samplingMessageBlockingStub);
118+
} else {
119+
exitWithError("Unknown method: " + method);
120+
}
121+
} catch (StatusRuntimeException e) {
122+
exitWithError("Server not reachable");
123+
}
124+
finally {
125+
managedChannel.shutdown();
118126
}
119-
120-
managedChannel.shutdown();
121127
}
122128

123129
/**
@@ -288,11 +294,11 @@ private void deleteSamplingMessage(CommandLine commandLine, SamplingMessageGrpc.
288294
*/
289295
private void exitWithError(String errorMessage) {
290296
log.error(errorMessage);
291-
this.exitWithHelpScreen();
297+
this.exitWithHelpScreen(1);
292298
}
293299

294-
private void exitWithHelpScreen() {
300+
private void exitWithHelpScreen(int exitCode) {
295301
helpFormatter.printHelp("sampling-message-client", options);
296-
System.exit(0);
302+
System.exit(exitCode);
297303
}
298304
}

sampling-message-server/src/main/java/de/dhbw/ravensburg/verteiltesysteme/server/Main.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static void main(String[] args) {
2525
final CommandLineParser serverCommandLineParser = new DefaultParser();
2626
final CommandLine commandLine = serverCommandLineParser.parse(defaultOptions(), args);
2727
if (commandLine.hasOption("help")) {
28-
exitWithHelpScreen();
28+
exitWithHelpScreen(0);
2929
}
3030
serviceConfig = ServerCommandLineParser.fromCliArgs(commandLine);
3131
} catch (ParseException e) {
@@ -62,11 +62,11 @@ public static void main(String[] args) {
6262
*/
6363
private static void exitWithError(String errorMessage) {
6464
log.error(errorMessage);
65-
exitWithHelpScreen();
65+
exitWithHelpScreen(1);
6666
}
6767

68-
private static void exitWithHelpScreen() {
68+
private static void exitWithHelpScreen(int exitCode) {
6969
new HelpFormatter().printHelp("sampling-message-server", defaultOptions());
70-
System.exit(0);
70+
System.exit(exitCode);
7171
}
7272
}

0 commit comments

Comments
 (0)