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.
> 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;
> }
> ===
The inputRawMessageBuffer does not have any type information for the
entire message buffer to say the buffer contains a message of type M1 --
it just has a sequence of fields that belong to M1 (or M2, or some other
message type). The reason for the M3 layer is to provide a tag
describing the enclosing message type.
Michael Poole
--
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.