Client-Side Implementation of Bidirectional Streaming RPC
Write the client implementation of the getFileAttributes method.
We'll cover the following...
When users want to retrieve file attributes, they pass the name of the file and the server address as command line arguments. We parse these arguments and, if certain conditions are met, call the getFileAttributes method of the SampleCLIClient class. This method, in turn, calls the getFileAttributes method of the FTPServiceClient class.
In this lesson, we demonstrate the client-side implementation of the getFileAttributes RPC. The client-side logic is implemented in the ftp-service-client module and is invoked using the ftp-service-cli-client module.
getFileAttributes method in FTPServiceClient class
The client invokes the getFileAttributes method to send a stream of requests to the server with the filenames for which it wants to retrieve attributes. The server responds with the file attributes for each file name.
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 getFileAttributes method in this class. It takes two input parameters:
filenames: This parameter is an array of strings representing the names of the files for which we want to retrieve attributes from the server.resultStreamObserver: It is aStreamObserverthat allows the client to receive asynchronous responses from the server. It will handle the server’s response asFileAttributesResultmessages.
public void getFileAttributes(String[] fileNames, StreamObserver<FileAttributesResult> resultStreamObserver) {}
In a bidirectional streaming RPC, the client sends a stream of messages to the server using requestObserver and receives the server’s response using resultStreamObserver.
Create StreamObserver for sending request
First, we will create a stream observer named requestObserver that will stream data by sending the MetaData messages to the server. The asyncStub is used to call the getFileAttributes method. It is an asynchronous stub for making non-blocking RPC calls.
public void getFileAttributes(String[] fileNames, StreamObserver<FileAttributesResult> resultStreamObserver) {StreamObserver<MetaData> requestObserver = asyncStub.getFileAttributes(resultStreamObserver);}
Build and send metadata
The client sends the file names as command line arguments. The method iterates over each file name in the fileNames array in line 5. For each file name: