polymorphism - Deserializing a Message multiple times with Google Protocol Buffers -
i working system extremely modular. messages can determined by 3-tuple of src, dest, type.
i'm looking reimplementing our messages protocol buffers. i've read through protocol buffer polymorphism, what's right way polymorphism protocol buffers? , http://www.indelible.org/ink/protobuf-polymorphism/
what wondering has implement solution where:
message header { required string src = 1 required string dest = 2 required string type = 3 }
and creating separate messages where:
message foo { required header h = 1 required string = 2 }
separate file:
message bar { required header h = 1 required uint32 num = 2 }
in receiving code have like:
message.parsefromstring(msgstr) if (message.type == foo) { foomessage.parsefromstring(msgstr) } else { barmessage.parsefromstring(msgstr) }
if approach has been used, better or worse what's described in above links?
only way found serialize/deserialize message body byte array. needed level of abstraction:
message header { required string src = 1; required string dest = 2; required string type = 3; } message foo { required string = 2; } message bar { required uint32 num = 2; } message request { required header h; required bytes data; // serialize foo or bar here }
then code (this pseudo-code):
request req = request.parsefromstring(buffer); if (req.h().type() == "foo") { foo msg = foo.parsefromstring(req.data); // process foo msg } else if (req.h().type() == "bar") { bar msg = bar.parsefromstring(req.data); // process bar msg }
Comments
Post a Comment