Generic Message Dispatch and Message Handler

689 views
Skip to first unread message

maninder batth

unread,
Oct 26, 2010, 3:45:06 PM10/26/10
to Protocol Buffers
Is any one aware of a Sample code that would allow messages to be
dispatched in a generic fashion on the client side and receive and
locate the correct Message Handler on the server side in Java, given
client and server are remote.
I have a naive approach as follow

message AParticularRequest {
// fields
required string messageType = x;
}

Using Message interface, i can write any Message to bytes and send
over the network.

On Client Side,
My generic Handler would create a GeneratedMessage and look for the
field messageType. Based on the value of the messageType, a particular
handler will be invoked.

I am wondering if there are better approaches out there.

Evan Jones

unread,
Oct 27, 2010, 7:27:23 AM10/27/10
to maninder batth, Protocol Buffers
On Oct 26, 2010, at 15:45 , maninder batth wrote:
> My generic Handler would create a GeneratedMessage and look for the
> field messageType. Based on the value of the messageType, a particular
> handler will be invoked.

This is basically what I have done, for my protobuf RPC
implementation. If you only need to choose between a limited set of
types, you may want a union type or extensions instead:

http://code.google.com/apis/protocolbuffers/docs/techniques.html#union

http://code.google.com/apis/protocolbuffers/docs/proto.html#extensions

Evan

--
Evan Jones
http://evanjones.ca/

Jimm

unread,
Oct 27, 2010, 11:36:39 AM10/27/10
to Protocol Buffers
Evan,
How are you parsing arbitrary PB bytes into a Generated Message ? I am
finding no class in API that can deserialize PB byte buffer into
GeneratedMessage?

Evan Jones

unread,
Oct 27, 2010, 5:52:44 PM10/27/10
to Jimm, Protocol Buffers
On Oct 27, 2010, at 11:36 , Jimm wrote:
> How are you parsing arbitrary PB bytes into a Generated Message ? I am
> finding no class in API that can deserialize PB byte buffer into
> GeneratedMessage?

I'm using the generic Service API that is included with protocol
buffers, so I'm not using GeneratedMessage. Rather, I'm using a
message instance itself. The "register" does something ilke this:

serviceRegister.registerCall(MyCustomMessage.getDefaultInstance());


Then you can parse this with code like the following:

Message requestPrototype = ...; // stored in registerCall
implementation
Message.Builder builder = requestPrototype.newBuilderForType();
builder.mergeFrom(requestByteString);


My code is actually available in the following hg repository. I don't
recommend that people use it directly, since it is a bit hacky, but it
could serve as an example:

http://people.csail.mit.edu/evanj/hg/index.cgi/javatxn/file/tip/src/ca/evanjones/protorpc/ServiceRegistry.java
http://people.csail.mit.edu/evanj/hg/index.cgi/javatxn/file/tip/src/ca/evanjones/protorpc/ProtoMethodInvoker.java


Good luck,

Reply all
Reply to author
Forward
0 new messages