BidirectionalServiceImpl.java 1.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
package bidirectional;

import com.example.grpc.BidirectionalServiceGrpc.*;
import com.example.grpc.BidirectionalServiceOuterClass.*;
import io.grpc.stub.StreamObserver;


public class BidirectionalServiceImpl extends BidirectionalServiceImplBase {
    @Override
    public StreamObserver<ClientRequest> bidirectional(StreamObserver<ServerResponse> responseObserver){

        //it returns the stream that will be used by the clients to send messages. The client will write on this stream
        return new StreamObserver<ClientRequest>() {
            //receiving a message from the client
            public void onNext(ClientRequest clientRequest) {
                String clientStringRequest = clientRequest.getStringRequest();
                System.out.println("[FROM CLIENT] " + clientStringRequest);

                // sending the response to the client
                System.out.println("Sending the response to the client...\n");
                responseObserver.onNext(ServerResponse.newBuilder().setStringResponse("I've received this message: '" + clientStringRequest + "'").build());
            }

            public void onError(Throwable throwable) {
            }

            public void onCompleted() {
            }
        };
    }
}