55import de .dhbw .ravensburg .verteiltesysteme .de .dhbw .ravensburg .verteiltesysteme .rpc .SamplingMessageGrpcService ;
66import io .grpc .ManagedChannel ;
77import io .grpc .ManagedChannelBuilder ;
8+ import io .grpc .StatusRuntimeException ;
89import lombok .extern .slf4j .Slf4j ;
910import 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}
0 commit comments