Task 1 - develop a client

This commit is contained in:
Rémi Heredero 2023-11-08 09:42:09 +01:00
parent 30d3216f9c
commit 0e1947feef

View File

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