Package muduo.rpc.proto.RpcProto

Examples of muduo.rpc.proto.RpcProto.RpcMessage


    public Object encode(ChannelHandlerContext ctx, Channel channel, Object obj)
            throws Exception {
        if (!(obj instanceof RpcMessage)) {
            return obj;
        }
        RpcMessage message = (RpcMessage) obj;
        int size = message.getSerializedSize();
        ChannelBuffer buffer = new BigEndianHeapChannelBuffer(4 + size + 4);
        buffer.writeBytes("RPC0".getBytes());
        int writerIndex = buffer.writerIndex();
        CodedOutputStream output = CodedOutputStream.newInstance(
                buffer.array(), buffer.writerIndex(), buffer.writableBytes() - 4);
        message.writeTo(output);
        output.checkNoSpaceLeft();

        buffer.writerIndex(writerIndex + size);
        Adler32 checksum = new Adler32();
        checksum.update(buffer.array(), buffer.arrayOffset(), buffer.readableBytes());
View Full Code Here


    public void disconnect() {
        channel.disconnect();
    }

    public void messageReceived(ChannelHandlerContext ctx, final MessageEvent e) {
        RpcMessage message = (RpcMessage) e.getMessage();
        assert e.getChannel() == channel;
        // System.out.println(message);
        if (message.getType() == MessageType.REQUEST) {
            doRequest(message);
        } else if (message.getType() == MessageType.RESPONSE) {
            Outstanding o = outstandings.remove(message.getId());
            // System.err.println("messageReceived " + this);
            if (o != null) {
                Message resp = fromByteString(o.responsePrototype, message.getResponse());
                o.done.run(resp);
            } else {
                System.err.println("Unknown id " + message.getId());
            }
        }
    }
View Full Code Here

            }
        } else {
            errorBuilder.setError(ErrorCode.NO_SERVICE);
        }
        if (!succeed) {
            RpcMessage resp = errorBuilder.build();
            channel.write(resp);
        }
    }
View Full Code Here

        return true;
    }

    protected void done(Message response, long id) {
        if (response != null) {
            RpcMessage resp = RpcMessage.newBuilder()
                    .setType(MessageType.RESPONSE)
                    .setId(id)
                    .setResponse(response.toByteString())
                    .build();
            channel.write(resp);
        } else {
            RpcMessage resp = RpcMessage.newBuilder()
                    .setType(MessageType.ERROR)
                    .setId(id)
                    .setError(ErrorCode.INVALID_RESPONSE)
                    .build();
            channel.write(resp);
View Full Code Here

    @Override
    public void callMethod(MethodDescriptor method, RpcController controller, Message request,
            Message responsePrototype, RpcCallback<Message> done) {
        long callId = id.getAndIncrement();
        RpcMessage message = RpcMessage.newBuilder()
                .setType(MessageType.REQUEST)
                .setId(callId)
                .setService(method.getService().getFullName())
                .setMethod(method.getName())
                .setRequest(request.toByteString())
View Full Code Here

public class RpcTest {

    @Test
    public void testEncoder() throws Exception {
        RpcEncoder encoder = new RpcEncoder();
        RpcMessage message = RpcMessage.newBuilder().setType(MessageType.REQUEST).setId(1).build();
        encoder.encode(null, null, message);
    }
View Full Code Here

    @Test
    public void testEncoder2() throws Exception {
        RpcEncoder encoder = new RpcEncoder();
        EchoRequest request = EchoRequest.newBuilder().setPayload("Hello").build();
        RpcMessage message = RpcMessage.newBuilder().setType(MessageType.REQUEST).setId(1)
                .setService(EchoService.getDescriptor().getFullName())
                .setMethod(EchoService.getDescriptor().getMethods().get(0).getName())
                .setRequest(request.toByteString()).build();
        encoder.encode(null, null, message);
    }
View Full Code Here

    @Test
    public void testDecoder() throws Exception {
        RpcEncoder encoder = new RpcEncoder();
        SudokuRequest request = SudokuRequest.newBuilder().setCheckerboard("001010").build();
        RpcMessage message = RpcMessage.newBuilder().setType(MessageType.REQUEST).setId(1)
                .setService(SudokuService.getDescriptor().getFullName())
                .setMethod(SudokuService.getDescriptor().getMethods().get(0).getName())
                .setRequest(request.toByteString()).build();
        ChannelBuffer buffer = (ChannelBuffer) encoder.encode(null, null, message);

        RpcDecoder decoder = new RpcDecoder();
        RpcMessage decodedMessage = (RpcMessage) decoder.decode(null, null, buffer);
        assertEquals(1, decodedMessage.getId());
    }
View Full Code Here

    }

    @Test
    public void testDecoder2() throws Exception {
        RpcEncoder encoder = new RpcEncoder();
        RpcMessage message = RpcMessage.newBuilder().setType(MessageType.REQUEST).setId(2).build();
        ChannelBuffer buffer = (ChannelBuffer) encoder.encode(null, null, message);
        ChannelBuffer buf2 = new BigEndianHeapChannelBuffer(buffer.readableBytes() + 8);
        buf2.writeInt(123);
        buf2.writeBytes(buffer);

        buf2.readInt();
        RpcDecoder decoder = new RpcDecoder();
        RpcMessage decodedMessage = (RpcMessage) decoder.decode(null, null, buf2);
        assertEquals(2, decodedMessage.getId());
    }
View Full Code Here

            public void run(SudokuResponse response) {
                // System.out.println(parameter);
                gotResponse = response;
            }
        });
        RpcMessage message = (RpcMessage) mockChannel.message;
        assertEquals(1, message.getId());
        assertEquals(MessageType.REQUEST, message.getType());
        assertEquals(remoteService.getDescriptorForType().getFullName(), message.getService());
        assertEquals("Solve", message.getMethod());

        SudokuResponse sudokuResponse = SudokuResponse.newBuilder()
                .setSolved(true)
                .setCheckerboard("123456")
                .build();
        RpcMessage response = RpcMessage.newBuilder()
                .setType(MessageType.RESPONSE)
                .setId(1)
                .setResponse(sudokuResponse.toByteString())
                .build();
        channel.messageReceived(null, new UpstreamMessageEvent(mockChannel, response, null));
View Full Code Here

TOP

Related Classes of muduo.rpc.proto.RpcProto.RpcMessage

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.