Task 1 - develop a client
This commit is contained in:
parent
30d3216f9c
commit
0e1947feef
36
src/main/java/CalculatorClient.java
Normal file
36
src/main/java/CalculatorClient.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user