On Jul 23, 12:54 am, Benedikt Hallinger
<b.hallin...@skyforcesystems.de> wrote:
> Hello (and sorry for bothering) and thank you for your clarifying.
>
> > The "type" is simply transmitted as a tag on the wire. The wire format
> > of extensions is just like the wire format of a regular field - the
> > difference is that the containing type (Packet) need not know about
> > the extension fields in the .proto definition. Instead, each payload
> > type that you might include will have a unique extension number. When
> > you include one of the payload messages in your program, it will
> > register its extensions of Packet. Then when a packet message is
> > parsed, if it sees a tag that is not defined in the .proto but is a
> > valid extension, it looks up the extension information and parses it
> > appropriately.
>
> The idea is good and clear, but i have problems figuring out the code.
> The huge problem is, that the PacketManager should be designed protocol
> agnostic, so it
> can deal with all protocols designed in the extension way you described.
>
> This said, if i understood it right, i would write it as following:
> ---File: myProto.proto---
> message Packet {
> extensions n to m;
>
> }
>
> // Payload messages
> // the type field must be the same for all mesages, with differing tag
> number
> message TestMessage {
> extend Packet {
> optional TestMessage type = 10;}
>
> required string msg = 2;
>
> }
>
> -------------------------
>
> ---File: PacketHandler.java---
> class PacketHandler {
> ...
>
> // Gets called whenever a new message arrives
> // ByteBuffer is passed to this method containing
> // the raw data from the net.
> public void messageRecieved(ByteBuffer buffer) {
> byte[] bytes = buffer.array(); // fetch bytes from buffer
>
> // parse the header data;
> // this.header is passed in the constructor of PacketHandler;
> // it is an instance of a "Packet" message.
> ExtendableMessage header =
> this.header.newBuilderForType().mergeFrom(bytes).build();

You need to provide the ExtensionRegistry to the builder when invoking
mergeFrom() so that it knows how to parse the extended types.

>
> // lets see what tag number the extension has, this is the type identifier.
> // this is done via the extension registry.
> ExtensionRegistry extReg = ExtensionRegistry.newInstance();
> int type = extReg.findExtensionByName("type").descriptor().getNumber();

This doesn't access anything in the message that was transmitted - it
just sees if there was an extension registered with the name type.
Instead, you can use the reflection methods to determine which fields
are set in the parsed message - maybe something like:

Map<Descriptor.FieldDescriptor, Object> field_map = header.getAllFields
();
for (Map.Entry<Descriptor.FieldDescriptor, Object> field :
field_map.entrySet()) {
  HandleType(field.getKey().number(),    // this is the extension
number of the message that was transmitted over the wire
                       field.getValue());
}

>
> // invoke the right handler for that message, it will take care of the
> rest:
> getHandler(type).handle(header.getExtension(type));
>
> }
>
> ...}
>
> -------------------------
>
> However, as i tried that, it gave me errors i cant solve.
> As long as i work with concrete classes and names, all is okay, but that is
> not what i want here. What i want is to avoid that hardcoded "switch"
> statement on types, so it is easier to add new message types without having
> to fiddle around with much code.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to protobuf@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