Search in sources :

Example 1 with TransferMsgProto

use of org.apache.ratis.proto.ExperimentsProtos.TransferMsgProto in project incubator-ratis by apache.

the class ClientProto method execProto.

public void execProto(int reps) throws Exception {
    System.out.println("Starting streaming with Protobuffers");
    StreamObserver<TransferMsgProto> requestObserver = asyncStubProto.sendData(new StreamObserver<TransferReplyProto>() {

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

        @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.allocate(1024 * 1024);
        if (bf.hasArray()) {
            Arrays.fill(bf.array(), (byte) 'a');
        }
        while (i < reps) {
            partId++;
            available.acquire();
            // using unsafewrap operations
            // creates a ByteString refrencing buffer data. Avoids Copying.
            // Something similar is missing in flatbuffers.
            TransferMsgProto msg = TransferMsgProto.newBuilder().setPartId(partId).setData(UnsafeByteOperations.unsafeWrap(bf)).build();
            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) TransferReplyProto(org.apache.ratis.proto.ExperimentsProtos.TransferReplyProto) TransferMsgProto(org.apache.ratis.proto.ExperimentsProtos.TransferMsgProto) ByteBuffer(java.nio.ByteBuffer)

Example 2 with TransferMsgProto

use of org.apache.ratis.proto.ExperimentsProtos.TransferMsgProto in project incubator-ratis by apache.

the class FileTransferProtobufs method sendData.

@Override
public StreamObserver<TransferMsgProto> sendData(final StreamObserver<TransferReplyProto> responseObserver) {
    return new StreamObserver<TransferMsgProto>() {

        private long rcvdDataSize = 0;

        public long getRcvdDataSize() {
            return rcvdDataSize;
        }

        @Override
        public void onNext(TransferMsgProto msg) {
            rcvdDataSize += msg.getData().size();
            TransferReplyProto rep = TransferReplyProto.newBuilder().setPartId(msg.getPartId()).setMessage("OK").build();
            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() {
            responseObserver.onCompleted();
        }
    };
}
Also used : StreamObserver(org.apache.ratis.thirdparty.io.grpc.stub.StreamObserver) Status(org.apache.ratis.thirdparty.io.grpc.Status) TransferReplyProto(org.apache.ratis.proto.ExperimentsProtos.TransferReplyProto) TransferMsgProto(org.apache.ratis.proto.ExperimentsProtos.TransferMsgProto)

Aggregations

TransferMsgProto (org.apache.ratis.proto.ExperimentsProtos.TransferMsgProto)2 TransferReplyProto (org.apache.ratis.proto.ExperimentsProtos.TransferReplyProto)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