KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*_############################################################################
2   _##
3   _## SNMP4J-AgentX - AgentXRegisterPDU.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.*;
26 import java.nio.*;
27
28 import org.snmp4j.smi.*;
29
30 public class AgentXRegisterPDU extends AgentXContextPDU {
31
32   private byte timeout = 0;
33   private byte priority;
34   private byte rangeSubID;
35   private OID subtree;
36   private int upperBound;
37
38   public AgentXRegisterPDU(byte flags,
39                            int sessionID, int transactionID, int packetID) {
40     super(AGENTX_REGISTER_PDU, flags, sessionID, transactionID, packetID);
41   }
42
43   public AgentXRegisterPDU(AgentXMessageHeader header) {
44     super(header);
45   }
46
47   public AgentXRegisterPDU(OctetString context, OID subtree,
48                            byte priority,
49                            byte rangeSubID,
50                            int upperBound) {
51     this(AGENTX_REGISTER_PDU,
52          context, subtree, priority, rangeSubID, upperBound);
53   }
54
55   protected AgentXRegisterPDU(byte type, OctetString context, OID subtree,
56                               byte priority,
57                               byte rangeSubID,
58                               int upperBound) {
59     super(type, context);
60     this.priority = priority;
61     this.subtree = subtree;
62     this.rangeSubID = rangeSubID;
63     this.upperBound = upperBound;
64   }
65
66
67   public void decodeAfterContext(ByteBuffer buf, int length) throws IOException {
68     timeout = buf.get();
69     priority = buf.get();
70     rangeSubID = buf.get();
71     buf.get(); // reserved
72
subtree = new OID();
73     AgentXProtocol.decodeOID(buf, subtree);
74     if (rangeSubID != 0) {
75       upperBound = buf.getInt();
76       if ((rangeSubID < 0) || (rangeSubID > subtree.size())) {
77         throw new IOException("Range sub-identifier "+rangeSubID+
78                               " is out of range of "+subtree);
79       }
80     }
81   }
82
83   public OctetString getContext() {
84     return context;
85   }
86
87   public byte getPriority() {
88     return priority;
89   }
90
91   public byte getRangeSubID() {
92     return rangeSubID;
93   }
94
95   public OID getSubtree() {
96     return subtree;
97   }
98
99   public byte getTimeout() {
100     return timeout;
101   }
102
103   public int getUpperBound() {
104     return upperBound;
105   }
106
107   public void setContext(OctetString context) {
108     this.context = context;
109   }
110
111   public void setPriority(byte priority) {
112     this.priority = priority;
113   }
114
115   public void setRangeSubID(byte rangeSubID) {
116     this.rangeSubID = rangeSubID;
117   }
118
119   public void setSubtree(OID subtree) {
120     this.subtree = subtree;
121   }
122
123   public void setTimeout(byte timeout) {
124     this.timeout = timeout;
125   }
126
127   public void setUpperBound(int upperBound) {
128     this.upperBound = upperBound;
129   }
130
131   public int getAfterContextLength() {
132     return AgentXProtocol.AGENTX_INT_SIZE +
133         AgentXProtocol.getOIDLength(subtree) +
134         ((rangeSubID != 0) ? AgentXProtocol.AGENTX_INT_SIZE : 0);
135   }
136
137   public AgentXRegion getRegion() {
138     OID lower = new OID(subtree);
139     OID upper = new OID(subtree);
140     AgentXRegion region = new AgentXRegion(lower, upper);
141     if (rangeSubID > 0) {
142       if (upper.get(rangeSubID-1) == upperBound) {
143         region.setSingleOID(true);
144         region.setUpperIncluded(true);
145       }
146       else {
147         upper.set(rangeSubID - 1, upperBound);
148         region.setRangeSubID(rangeSubID);
149       }
150     }
151     else if (isFlagSet(AgentXProtocol.FLAG_INSTANCE_REGISTRATION)) {
152       region.setSingleOID(true);
153       region.setUpperIncluded(true);
154     }
155     else {
156       region.setUpperBound(upper.nextPeer());
157     }
158     return region;
159   }
160
161   protected void encodeAfterContext(ByteBuffer buf) {
162     buf.put(timeout);
163     buf.put(priority);
164     buf.put(rangeSubID);
165     buf.put((byte)0); // reserved
166
AgentXProtocol.encodeOID(buf, subtree, false);
167     if (rangeSubID != 0) {
168       buf.putInt(upperBound);
169     }
170   }
171
172   protected String JavaDoc toStringExtMembers() {
173     return super.toStringExtMembers()+",timeout="+timeout+",priority="+priority+
174         ",rangeSubID="+rangeSubID+",subtree="+subtree+",upperBound="+upperBound
175         +",single="+isFlagSet(AgentXProtocol.FLAG_INSTANCE_REGISTRATION);
176   }
177 }
178
Popular Tags