...

/

Client-Side Implementation of Server Streaming RPC

Client-Side Implementation of Server Streaming RPC

Implement the client side of a server streaming readFile RPC in the FTP service.

When users want to read a file, they pass the name of the file and the server address as command line arguments. We will parse the arguments and if certain conditions are met, call the readFile method of the SampleCLIClient class. This method in turn calls the readFile method of the FTPServiceClient class. In this lesson, we show the client-side implementation of the readFile RPC. The client side is implemented in the ftp-service-client module and it is called using the ftp-service-cli-client module.

readFile method in FTPServiceClient class

The ftp-service-client module contains the client implementation of the FTP service. Open the FTPServiceClient class from the src/main/java/io/datajek/ftpservice/client package. We will create a readFile method in this class. The client sends a request to the server with the filename to read. The server responds with the file contents.

The method takes two input parameters:

  1. filenameToRead: This parameter is a string that represents the name of the file we want to read from the server.

  2. resultStreamObserver: It is a StreamObserver that allows the client to receive asynchronous responses from the server. In this case, it is used to handle the server’s response to the read file request.

public void readFile(String filenameToRead, StreamObserver<ReadFileResult> resultStreamObserver) {
}
readFile method in FTPServiceClient class

Inside the readFile method, we will send the file name to read as a MetaData message. The message is created in lines 3-5.

public void readFile(String filenameToRead, StreamObserver<ReadFileResult> resultStreamObserver) {
MetaData request = MetaData.newBuilder()
.setName(filenameToRead)
.build();
asyncStub.readFile(request, resultStreamObserver);
}
readFile method

The asyncStub is used to make an asynchronous call to the server which means that the client sends the read request asynchronously and handles the response from the server when it becomes available. ...