java - Efficient packet decoding -


i working on netty application snmp protocol. protocol has 3 different versions 1,2,3 , corresponding message format go versions. wondering efficient way decode these based on variations between packet formats.

snmpv1 message format: http://www.tcpipguide.com/free/t_snmpversion1snmpv1messageformat.htm

snmpv2 message format: http://www.tcpipguide.com/free/t_snmpversion2snmpv2messageformats-2.htm

as can see messages differ in format. post v3 differs lot can't post more 2 links.

my initial idea make reflection based decocder it's proving rather troublesome.

something along lines of (taken source on github):

    public <t> t decode(w buf, class<t> clazz) throws asn1exception {     if (typeencoders.containskey(clazz)) {         return clazz.cast(typeencoders.get(clazz).decode(buf));     }      t obj;     @suppresswarnings("unchecked")     map<string, object> sequence = (map<string, object>)annotatedtypeencoders.get(universaltype.sequence).decode(buf);     try {         obj = clazz.newinstance();         (map.entry<string, object> entry : sequence.entryset()) {             entry.getkey();             field field = clazz.getfield(entry.getkey());             field.setaccessible(true);             field.set(obj, entry.getvalue());         }     }     catch (exception e) {         throw new asn1exception("unable set fields on object", e);     }      return obj; } 

with mapped class of (my source):

public final class defaultsnmppdu implements snmpcommonpdu { @asn1type(type = asn1tag.integer) private snmpmessagetype type; @asn1type(type = asn1tag.integer) private int requestid; @asn1type(type = asn1tag.integer) private snmperrortype errortype; @asn1type(type = asn1tag.integer) private int errorindex; @asn1type(type = asn1tag.sequence) private map<string, object> variablebindings;  @override public snmpmessagetype gettype() {     return type; }  @override public int getrequestid() {     return requestid; }  @override public snmperrortype geterrortype() {     return errortype; }  @override public int geterrorindex() {     return errorindex; }  @override public map<string, object> getvariablebindings() {     return collections.unmodifiablemap(variablebindings); } } 

i open ideas, thank you.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -