Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
cb6a59468d | |||
5fb6c3d0cc |
BIN
Calculator.pcapng
Normal file
BIN
Calculator.pcapng
Normal file
Binary file not shown.
BIN
Error.pcapng
Normal file
BIN
Error.pcapng
Normal file
Binary file not shown.
@ -2,35 +2,51 @@ import ch.sdi.calc.CalculatorGrpc;
|
|||||||
import ch.sdi.calc.CalculatorOuterClass;
|
import ch.sdi.calc.CalculatorOuterClass;
|
||||||
import io.grpc.ManagedChannel;
|
import io.grpc.ManagedChannel;
|
||||||
import io.grpc.ManagedChannelBuilder;
|
import io.grpc.ManagedChannelBuilder;
|
||||||
|
import io.grpc.StatusRuntimeException;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class CalculatorClient {
|
public class CalculatorClient {
|
||||||
|
|
||||||
private ManagedChannel channel;
|
private ManagedChannel channel;
|
||||||
private final CalculatorGrpc.CalculatorBlockingStub blockingStub;
|
private CalculatorGrpc.CalculatorBlockingStub blockingStub = null;
|
||||||
|
private static final Logger logger = Logger.getLogger(CalculatorClient.class.getName());
|
||||||
|
|
||||||
public CalculatorClient(String host, int port) {
|
public CalculatorClient(String host, int port) {
|
||||||
this(ManagedChannelBuilder.forAddress(host, port).usePlaintext());
|
this(ManagedChannelBuilder.forAddress(host, port).usePlaintext());
|
||||||
}
|
}
|
||||||
private CalculatorClient(ManagedChannelBuilder<?> channelBuilder) {
|
private CalculatorClient(ManagedChannelBuilder<?> channelBuilder) {
|
||||||
channel = channelBuilder.build();
|
try {
|
||||||
|
channel = channelBuilder.build();
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.log(Level.WARNING, "Could not connect to server: {0}", e.getMessage());
|
||||||
|
return;
|
||||||
|
}
|
||||||
blockingStub = CalculatorGrpc.newBlockingStub(channel);
|
blockingStub = CalculatorGrpc.newBlockingStub(channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
public double calculate(String expression) {
|
public double calculate(String expression) {
|
||||||
|
logger.info("Trying to do: " + expression + " ..." );
|
||||||
CalculatorOuterClass.CalculatorRequest request = CalculatorOuterClass.CalculatorRequest.newBuilder().setExpression(expression).build();
|
CalculatorOuterClass.CalculatorRequest request = CalculatorOuterClass.CalculatorRequest.newBuilder().setExpression(expression).build();
|
||||||
CalculatorOuterClass.CalculatorResponse response;
|
CalculatorOuterClass.CalculatorResponse response = null;
|
||||||
response = blockingStub.calculate(request);
|
try {
|
||||||
|
response = blockingStub.calculate(request);
|
||||||
|
} catch (StatusRuntimeException e) {
|
||||||
|
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
logger.info("Answer: " + response.getResult());
|
||||||
return response.getResult();
|
return response.getResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
CalculatorClient client = new CalculatorClient("calc.sdi.hevs.ch", 80);
|
CalculatorClient client = new CalculatorClient("calc.sdi.hevs.ch", 80);
|
||||||
|
|
||||||
for(int i = 0; i<10; i++) {
|
String expression = "1-a";
|
||||||
String expression = String.valueOf(i) + "+" + String.valueOf(i);
|
System.out.println("Expression: " + expression);
|
||||||
System.out.println("Expression: " + expression);
|
double answer = client.calculate(expression);
|
||||||
double answer = client.calculate(expression);
|
System.out.println("Answer: " + answer);
|
||||||
System.out.println("Answer: " + answer);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user