1 22 23 package org.snmp4j.agent.agentx; 24 25 import java.io.IOException ; 26 import java.nio.ByteBuffer ; 27 28 import org.snmp4j.smi.OctetString; 29 30 public abstract class AgentXContextPDU extends AgentXPDU { 31 32 protected OctetString context; 33 34 protected AgentXContextPDU(byte type, OctetString context) { 35 super(type); 36 this.context = context; 37 } 38 39 protected AgentXContextPDU(AgentXMessageHeader header) { 40 super(header); 41 } 42 43 protected AgentXContextPDU(byte type, byte flags, int sessionID, 44 int transactionID, int packetID) { 45 super(type, flags, sessionID, transactionID, packetID); 46 } 47 48 protected abstract void decodeAfterContext(ByteBuffer buf, int length) 49 throws IOException ; 50 51 protected abstract void encodeAfterContext(ByteBuffer buf); 52 53 protected abstract int getAfterContextLength(); 54 55 public final void decodePayload(ByteBuffer buf, int length) 56 throws IOException 57 { 58 if (isFlagSet(AgentXProtocol.FLAG_NON_DEFAULT_CONTEXT)) { 59 context = AgentXProtocol.decodeOctetString(buf); 60 } 61 else { 62 context = new OctetString(); 63 } 64 decodeAfterContext(buf, length); 65 } 66 67 public final void encodePayload(ByteBuffer buf) { 68 if (isFlagSet(AgentXProtocol.FLAG_NON_DEFAULT_CONTEXT)) { 69 AgentXProtocol.encodeOctetString(buf, context); 70 } 71 encodeAfterContext(buf); 72 } 73 74 public final int getPayloadLength() { 75 int length = 0; 76 if ((context != null) && (context.length() > 0) && 77 (AgentXProtocol.isNonDefaultContextsEnabled())) { 78 length = AgentXProtocol.getOctetStringLength(context); 79 } 80 length += getAfterContextLength(); 81 return length; 82 } 83 84 public OctetString getContext() { 85 return context; 86 } 87 88 public void setContext(OctetString context) { 89 this.context = context; 90 } 91 92 protected String toStringExtMembers() { 93 return super.toStringExtMembers()+",context="+context; 94 } 95 96 99 protected void beforeEncode() { 100 if ((context != null) && (context.length() > 0) && 101 (AgentXProtocol.isNonDefaultContextsEnabled())) { 102 addFlag(AgentXProtocol.FLAG_NON_DEFAULT_CONTEXT); 103 } 104 } 105 106 } 107 | Popular Tags |