KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > agentx > AgentXContextPDU


1 /*_############################################################################
2   _##
3   _## SNMP4J-AgentX - AgentXContextPDU.java
4   _##
5   _## Copyright (C) 2005-2007 Frank Fock (SNMP4J.org)
6   _##
7   _## This program is free software; you can redistribute it and/or modify
8   _## it under the terms of the GNU General Public License version 2 as
9   _## published by the Free Software Foundation.
10   _##
11   _## This program is distributed in the hope that it will be useful,
12   _## but WITHOUT ANY WARRANTY; without even the implied warranty of
13   _## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14   _## GNU General Public License for more details.
15   _##
16   _## You should have received a copy of the GNU General Public License
17   _## along with this program; if not, write to the Free Software
18   _## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19   _## MA 02110-1301 USA
20   _##
21   _##########################################################################*/

22
23 package org.snmp4j.agent.agentx;
24
25 import java.io.IOException JavaDoc;
26 import java.nio.ByteBuffer JavaDoc;
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 JavaDoc buf, int length)
49       throws IOException JavaDoc;
50
51   protected abstract void encodeAfterContext(ByteBuffer JavaDoc buf);
52
53   protected abstract int getAfterContextLength();
54
55   public final void decodePayload(ByteBuffer JavaDoc buf, int length)
56       throws IOException JavaDoc
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 JavaDoc 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 JavaDoc toStringExtMembers() {
93     return super.toStringExtMembers()+",context="+context;
94   }
95
96   /**
97    * Initialize flags and other things before a PDU is encoded.
98    */

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