Just store the message as a string until you know what type it is, then
parse it.  There's really nothing useful that the protobuf library can do
with the bytes until it knows what type they are.

On Wed, Dec 23, 2009 at 4:04 AM, read.the news <read.the.n...@gmail.com>wrote:

> Hi,
>
> I have a simple question (which got rather long). For simplicity, we
> two message types supported by a server, M1 and M2. We can receive
> either of those two messages as "the first message", so we need an
> elegant way of instantiating a generic message and pass it further for
> processing.
>
> If we look from the designer perspective, this can be achieved easily
> by creating another message layer, M3, which should have either a M1
> or M2 inside. If we look from the developer perspective who just got a
> system that doesn't have such a M3 layer..
>
> As I wasn't able to find something in the API (probably I didn't
> searched enough), I was interested to see if there is some kind of
> "message builder" factory which, given a raw buffer, can hold a
> message inside a ::google::protobuf::Message. And then, in the worker
> thread, I have access to the message type, and I can cast it to the
> specific type that is needed, say.. M1.
> Uncompilable code below:
> ===
>  ::google::protobuf::Message *pMsgGeneric =
> MessageFactory.serializeFrom(inputRawMessageBuffer); // we just
> obtained a generic message instance from a buffer
>
> // later in the code when the worker thread actually processes the
> request and prepares a reply.
>  if (pMsgGeneric.getType() == M1) {
>    M1 *myConcreteM1Instance = (M1) pMsgGeneric;
>  }
> ===
> Looking from the other angle, message serialization logic probably can
> be decoupled from the message (so that one can obtain a message before
> instantiating it), meaning you will have to write something like:
> ===
>  // first, create the message serializer
>  ::google::protobuf::MessageSerializer msgSerializer = new
> SerializerFactory.getInstance("buffer serializer");
>
>  M1 *pM1 = new M1();
>
>  // set fields in pGenericMsg
>
>  // then serialize it:
>  msgSerializer.write( pM1 );
> ===
> However there are probably good reasons for doing things as they are,
> so please consider this last point only from a protobuf user
> perspective - I am probably not in the position to see the complete
> picture from this perspective.
>
> Thank you in advance for you help,
> Marius P.
>
> --
>
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To post to this group, send email to proto...@googlegroups.com.
> To unsubscribe from this group, send email to
> protobuf+unsubscr...@googlegroups.com<protobuf%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/protobuf?hl=en.
>
>
>

--

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


Reply via email to