From 0e1947feef199c7f72e7bba9db11553189e90c11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Wed, 8 Nov 2023 09:42:09 +0100 Subject: [PATCH] Task 1 - develop a client --- src/main/java/CalculatorClient.java | 36 +++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/main/java/CalculatorClient.java diff --git a/src/main/java/CalculatorClient.java b/src/main/java/CalculatorClient.java new file mode 100644 index 0000000..0d86333 --- /dev/null +++ b/src/main/java/CalculatorClient.java @@ -0,0 +1,36 @@ +import ch.sdi.calc.CalculatorGrpc; +import ch.sdi.calc.CalculatorOuterClass; +import io.grpc.ManagedChannel; +import io.grpc.ManagedChannelBuilder; + +public class CalculatorClient { + + private ManagedChannel channel; + private final CalculatorGrpc.CalculatorBlockingStub blockingStub; + + public CalculatorClient(String host, int port) { + this(ManagedChannelBuilder.forAddress(host, port).usePlaintext()); + } + private CalculatorClient(ManagedChannelBuilder channelBuilder) { + channel = channelBuilder.build(); + blockingStub = CalculatorGrpc.newBlockingStub(channel); + } + + public double calculate(String expression) { + CalculatorOuterClass.CalculatorRequest request = CalculatorOuterClass.CalculatorRequest.newBuilder().setExpression(expression).build(); + CalculatorOuterClass.CalculatorResponse response; + response = blockingStub.calculate(request); + return response.getResult(); + } + + public static void main(String[] args) { + CalculatorClient client = new CalculatorClient("calc.sdi.hevs.ch", 80); + + for(int i = 0; i<10; i++) { + String expression = String.valueOf(i) + "+" + String.valueOf(i); + System.out.println("Expression: " + expression); + double answer = client.calculate(expression); + System.out.println("Answer: " + answer); + } + } +}