Receiving multiple message types

2,850 views
Skip to first unread message

Jens Rantil

unread,
Mar 18, 2012, 6:41:34 PM3/18/12
to Protocol Buffers
Hi,

I'm a newcomer to protobuf and am trying to wrap my head around some
things. Let's say I have a message receiver of some kind. It can
receive different types requests. All messages are encoded in protobuf
and my receiver has all *.proto message definitions necessary be able
to decode all incoming requests.

My question is, does protobuf come with any bells and whistles that
let me decode a random incoming message, given all possible message
definitions? If not, how do you generally cope with this situation?
Because I feel it's a common one.

I guess one possible solution is to create another message definition:
message GenericMessage {
enum MessageType {
SET_NAME = 0;
CREATE_CUSTOMER = 1;
}
required MessageType type = 0;
required bytes protobuf_data = 1;
}

However, this would require me to do two deserializations. Is this the
prefered way to go? I'm curious to hear you input. Please pinpoint me
if this has been answered previously in forum or in the documentation.

Regards,
Jens

Jim Morris

unread,
Mar 19, 2012, 3:46:16 PM3/19/12
to prot...@googlegroups.com
There are several ways to do it, on eis called unions and the other is to use extensions. My blog entry here covers the latter and has links to the union method.

http://blog.wolfman.com/articles/2011/11/23/how-to-implement-polymorphic-protocol-buffers-in-java

You are on the right track though you just put all the messages as optional in the one message with the header, so you only need to decode once.

Jens Rantil

unread,
Mar 19, 2012, 3:46:14 PM3/19/12
to Protocol Buffers
Hi again,

I seem to have found the answer myself:
http://code.google.com/apis/protocolbuffers/docs/techniques.html#union

Regards,

Jens
> --
> You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
> To post to this group, send email to prot...@googlegroups.com.
> To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.
>
>

--
Want to know how full my inbox is? Or how to get in touch with me faster? Or tell me your e-mail is not that important? Then check this out: http://courteous.ly/4WtfZY
Reply all
Reply to author
Forward
0 new messages