On Jul 15, 5:33 am, Kaj Bjurman <kaj.bjur...@gmail.com> wrote:
> I have just read the tutorials and the some threads in these forums,
> but there's one thing that I can't find an answer for. I'm currently
> using a router that is responsible to route different messages to
> different servers. The protocol that we are currently using is a very
> simple. Basically header:tag=value:tag=value: and so on.
> The router looks for a field with certain name (e.g. cookie) and
> routes a message based on the information in the value of that tag,
> and also modifies that field.
> Is it possible to write a similar router for PB? The router that we
> have shouldn't know about all message types. It just need to know that
> a field with a certain tag always should exist and that the field has
> a value. I don't parse the data of the wire into messages, do
> modifications and then write them back to the wire.
> So, in short. How to do that using PB?
I'm a little unclear about your requirement for arbitrary types. Do
your message types all define some common header values? The wire
format doesn't have a notion of tag names, simply tag numbers and
types. Therefore, unless all your messages define "cookie" to have the
same tag number, the router won't know what field to look for.
However, I think you can accomplish what you want by defining a header
message containing the fields your router needs to know about, and
include the payload as raw data that is interpreted by clients who
know what specific message type to read.
You can make sure that the field exists by labeling it "required" in
the message definition - then AbstractMessage.isInitialized() will
fail if the field has not been set.
> I'm also a bit curious on how to decode arbritrary messages, modify
> them and write tham back (even if I won't do that). How do you
> normally modify messages since they are immutable? Is there a way to
> get a builder for a certain message, and have the builder initialized
> so that it represents a copy of the original message?
Yes, the Builder class is what you want here. You can initialize a
builder using the original message, or better yet directly from the
serialized encoding from the wire (assuming you don't care about the
Foo f = Foo.newBuilder().mergeFrom(...).setCookie(newCookieVal).build
Hope that helps,
> I'm using Java if that makes a difference
You received this message because you are subscribed to the Google Groups
"Protocol Buffers" group.
To post to this group, send email to email@example.com
To unsubscribe from this group, send email to
For more options, visit this group at