Client-Side Implementation of Client Streaming RPC
Write the client implementation of the addFile method.
We'll cover the following...
- addFile method in FTPServiceClient class
- Flow diagram
- Create StreamObserver for sending request
- Check for existence of source file
- Build and send metadata–onNext method
- Read the file in chunks
- Calculate checksum
- Send file chunk - onNext method
- Notify the server of completion–onCompleted method
- Exception handling - OnError method
- Close the connection
In this lesson we show the client side implementation of the client streaming addFile
RPC. The client side is implemented in the ftp-service-client
module and it is called using the ftp-service-cli-client
module.
addFile
method in FTPServiceClient
class
The addFile
method is used to transfer a file from the local system to the server’s destination directory. It contains the logic of breaking down larger files into chunks and computing checksums to ensure data integrity during transmission. Each chunk of the file is sent as a separate message. Open the FTPServiceClient
class from the src/main/java/io/datajek/ftpservice/client
package in the ftp-service-client
module. We will write the addFile
method in this class for the client-side implementation of the add file RPC.
public void addFile(String sourceFilePath, String destFileName, StreamObserver<Result> resultStreamObserver) {}
The method will take three input parameters:
sourceFilePath
: This represents the source file that we want to send to the server. This represents the name of the file in the local file system.destFileName
: This specifies the name that the file should have in the server’s destination directory. It is the desired name for the file on the server.resultStreamObserver
: TheStreamObserver
callback interface allows us to handle messages asynchronously and receive status updates and the final result of the add file operation. TheResult
object contains information about the status of the file transfer.resultStreamObserver
is the observer that the client provides when making a call to the server. It’s used by the server to send responses back to the client.
The server-side implementation of addFile
discussed in the previous lesson has another observer, StreamObserver<AddFileRequest>
, which is used by the server to receive client requests. Compare this to the implementation of the deletefile
method, which only required the resultStreamObserver
.
A unary RPC has
a responseObserver
to receive server’s response.
A client streaming RPC has
a requestObserver
to send requests and a responseObserver
to receive server’s response.
Flow diagram
The diagram below shows the steps of sending a file to the server.
We first create a StreamObserver
for sending the stream of messages to the server. The StreamObserver
has three methods which we will override.
The
onNext
method is used to send the file metadata and file chunks.The ...