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); + } + } +}