Search in sources :

Example 1 with FlatBufferBuilder

use of org.apache.ratis.thirdparty.com.google.flatbuffers.FlatBufferBuilder in project incubator-ratis by apache.

the class ClientFlat method execFlatClient.

public void execFlatClient(int reps) throws Exception {
    System.out.println("Starting streaming with Flatbuffers");
    StreamObserver<TransferMsg> requestObserver = asyncStubFlat.sendData(new StreamObserver<TransferReply>() {

        @Override
        public void onNext(TransferReply msg) {
            recv[0]++;
            available.release();
        }

        @Override
        public void onError(Throwable t) {
            Status status = Status.fromThrowable(t);
            System.out.println(status);
            System.out.println("Finished streaming with errors");
        }

        @Override
        public void onCompleted() {
            System.out.println("Finished streaming");
        }
    });
    try {
        int i = 0;
        // allocate a byte buffer containing message data.
        ByteBuffer bf = ByteBuffer.allocateDirect(1024 * 1024);
        if (bf.hasArray()) {
            Arrays.fill(bf.array(), (byte) 'a');
        }
        while (i < reps) {
            partId++;
            available.acquire();
            // start a builder and reset position of databuffer to 0.
            FlatBufferBuilder builder = new FlatBufferBuilder();
            bf.position(0).limit(bf.capacity());
            // create string of the message data
            // the datacopy happens here, significant CPU time spent.
            int dataOff = builder.createString(bf);
            // Use flatbuffers generated message builder.
            int off = TransferMsg.createTransferMsg(builder, partId, dataOff);
            builder.finish(off);
            TransferMsg msg = TransferMsg.getRootAsTransferMsg(builder.dataBuffer());
            requestObserver.onNext(msg);
            i++;
        }
    } catch (Exception e) {
        System.out.println(e);
    }
    requestObserver.onCompleted();
    Thread.sleep(1000 * 100);
    if (recv[0] == partId) {
        System.out.println("Transfer Successfull....");
    } else {
        System.out.println("Some error occurred...");
    }
}
Also used : Status(org.apache.ratis.thirdparty.io.grpc.Status) TransferReply(org.apache.ratis.flatbufs.TransferReply) FlatBufferBuilder(org.apache.ratis.thirdparty.com.google.flatbuffers.FlatBufferBuilder) TransferMsg(org.apache.ratis.flatbufs.TransferMsg) ByteBuffer(java.nio.ByteBuffer)

Example 2 with FlatBufferBuilder

use of org.apache.ratis.thirdparty.com.google.flatbuffers.FlatBufferBuilder in project incubator-ratis by apache.

the class FileTransferFlatbufs method sendData.

@Override
public StreamObserver<TransferMsg> sendData(final StreamObserver<TransferReply> responseObserver) {
    return new StreamObserver<TransferMsg>() {

        private long rcvdDataSize = 0;

        public long getRcvdDataSize() {
            return rcvdDataSize;
        }

        @Override
        public void onNext(TransferMsg msg) {
            rcvdDataSize += msg.dataLength();
            FlatBufferBuilder builder = new FlatBufferBuilder();
            int off = TransferReply.createTransferReply(builder, msg.partId(), builder.createString("OK"));
            builder.finish(off);
            TransferReply rep = TransferReply.getRootAsTransferReply(builder.dataBuffer());
            responseObserver.onNext(rep);
        }

        @Override
        public void onError(Throwable t) {
            Status status = Status.fromThrowable(t);
            System.out.println(status);
            System.out.println("Finished streaming with errors");
        }

        @Override
        public void onCompleted() {
            System.out.println(rcvdDataSize);
            responseObserver.onCompleted();
        }
    };
}
Also used : StreamObserver(org.apache.ratis.thirdparty.io.grpc.stub.StreamObserver) Status(org.apache.ratis.thirdparty.io.grpc.Status) FlatBufferBuilder(org.apache.ratis.thirdparty.com.google.flatbuffers.FlatBufferBuilder) TransferReply(org.apache.ratis.flatbufs.TransferReply) TransferMsg(org.apache.ratis.flatbufs.TransferMsg)

Aggregations

TransferMsg (org.apache.ratis.flatbufs.TransferMsg)2 TransferReply (org.apache.ratis.flatbufs.TransferReply)2 FlatBufferBuilder (org.apache.ratis.thirdparty.com.google.flatbuffers.FlatBufferBuilder)2 Status (org.apache.ratis.thirdparty.io.grpc.Status)2 ByteBuffer (java.nio.ByteBuffer)1 StreamObserver (org.apache.ratis.thirdparty.io.grpc.stub.StreamObserver)1