demultiplexing protobuf messages

Trustin Lee (이희승) trustin at gmail.com
Tue Nov 3 04:00:27 EST 2009


On Mon, Nov 2, 2009 at 9:04 PM, sandstorm_sh <sandstorm_sh at abv.bg> wrote:
>
> How can we demultiplex protobuf messages, given the fact that a message has
> not any type defining header information in itself, and the protocol we are
> trying to define is message-flexible so to say, i.e. has not got any fixed
> message sequence hardcoded into the protocol definition.
>
> one solution is to prepend the type header like a lengthfieldprepender
> prepends a length field, and to decode the message with an appropriate
> protobuf decoder.
>
> we can maintain a collection of decoders for the corresponding types, but
> the problem is that the decode method is protected, not public, and we can
> not invoke it.
> so we should use the pipeline somehow, and my question is what is the
> standard idiom for this particular situation: do we have to replace handlers
> in the pipeline, based on the incoming message type ?
>
> what is the standard thing to do ?

I would use DecoderEmbedder to decode the message whose type/length
prefix stripped out.  It could look like this:

public class MyDecoder extends FrameDecoder {
    private volatile boolean readingHeader = true;
    private volatile int bodyLength;
    private volatile DecoderEmbedder bodyDecoder;

    protected Object decode(ctx, ch, buf) {
        if (readingHeader) {
            if (buf.readableByte() < 8) {
                return null;
            }
            int messageType = buf.readInt();
            bodyLength = buf.readInt();
            switch (messageType) {
            case 1:
                bodyDecoder = new DecoderEmbedder(new MessageTypeOneDecoder());
                break;
            ....
            }
            readingHeader = false;
        }

        if (!readingHeader) {
            if (buf.readableBytes() < bodyLength) {
                return;
            }
            bodyDecoder.offer(buf.readBytes(bodyLength));
            readingHeader = true;
            return bodyDecoder.poll();
        }
    }
}

I'm not sure the code above is perfect as I didn't run it by myself,
but it should show you some basic idea.

I also like Nicholas's idea, so I'd recommend it, too, if it's a
viable option for you.

HTH

— Trustin Lee, http://gleamynode.net/


>
>
> example code in the netty distribution contains protobuf examples, but there
> is one message type incoming and outgoing..
>
> im a bit newbie in netty, so i'd like some elaboration on this topic, thanks
> --
> View this message in context: http://n2.nabble.com/demultiplexing-protobuf-messages-tp3931395p3931395.html
> Sent from the Netty User Group mailing list archive at Nabble.com.
> _______________________________________________
> netty-users mailing list
> netty-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/netty-users
>



More information about the netty-users mailing list