001: /*_############################################################################
002: _##
003: _## SNMP4J-AgentX - AgentXContextPDU.java
004: _##
005: _## Copyright (C) 2005-2007 Frank Fock (SNMP4J.org)
006: _##
007: _## This program is free software; you can redistribute it and/or modify
008: _## it under the terms of the GNU General Public License version 2 as
009: _## published by the Free Software Foundation.
010: _##
011: _## This program is distributed in the hope that it will be useful,
012: _## but WITHOUT ANY WARRANTY; without even the implied warranty of
013: _## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: _## GNU General Public License for more details.
015: _##
016: _## You should have received a copy of the GNU General Public License
017: _## along with this program; if not, write to the Free Software
018: _## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
019: _## MA 02110-1301 USA
020: _##
021: _##########################################################################*/
022:
023: package org.snmp4j.agent.agentx;
024:
025: import java.io.IOException;
026: import java.nio.ByteBuffer;
027:
028: import org.snmp4j.smi.OctetString;
029:
030: public abstract class AgentXContextPDU extends AgentXPDU {
031:
032: protected OctetString context;
033:
034: protected AgentXContextPDU(byte type, OctetString context) {
035: super (type);
036: this .context = context;
037: }
038:
039: protected AgentXContextPDU(AgentXMessageHeader header) {
040: super (header);
041: }
042:
043: protected AgentXContextPDU(byte type, byte flags, int sessionID,
044: int transactionID, int packetID) {
045: super (type, flags, sessionID, transactionID, packetID);
046: }
047:
048: protected abstract void decodeAfterContext(ByteBuffer buf,
049: int length) throws IOException;
050:
051: protected abstract void encodeAfterContext(ByteBuffer buf);
052:
053: protected abstract int getAfterContextLength();
054:
055: public final void decodePayload(ByteBuffer buf, int length)
056: throws IOException {
057: if (isFlagSet(AgentXProtocol.FLAG_NON_DEFAULT_CONTEXT)) {
058: context = AgentXProtocol.decodeOctetString(buf);
059: } else {
060: context = new OctetString();
061: }
062: decodeAfterContext(buf, length);
063: }
064:
065: public final void encodePayload(ByteBuffer buf) {
066: if (isFlagSet(AgentXProtocol.FLAG_NON_DEFAULT_CONTEXT)) {
067: AgentXProtocol.encodeOctetString(buf, context);
068: }
069: encodeAfterContext(buf);
070: }
071:
072: public final int getPayloadLength() {
073: int length = 0;
074: if ((context != null) && (context.length() > 0)
075: && (AgentXProtocol.isNonDefaultContextsEnabled())) {
076: length = AgentXProtocol.getOctetStringLength(context);
077: }
078: length += getAfterContextLength();
079: return length;
080: }
081:
082: public OctetString getContext() {
083: return context;
084: }
085:
086: public void setContext(OctetString context) {
087: this .context = context;
088: }
089:
090: protected String toStringExtMembers() {
091: return super .toStringExtMembers() + ",context=" + context;
092: }
093:
094: /**
095: * Initialize flags and other things before a PDU is encoded.
096: */
097: protected void beforeEncode() {
098: if ((context != null) && (context.length() > 0)
099: && (AgentXProtocol.isNonDefaultContextsEnabled())) {
100: addFlag(AgentXProtocol.FLAG_NON_DEFAULT_CONTEXT);
101: }
102: }
103:
104: }
|