📄 pdu.java
字号:
* @see SnmpConstants
*/
public void setErrorStatus(int errorStatus) {
this.errorStatus.setValue(errorStatus);
}
/**
* Gets the error status of the PDU.
* @return
* a SNMP error status.
* @see SnmpConstants
*/
public int getErrorStatus() {
return errorStatus.getValue();
}
/**
* Gets a textual description of the error status.
* @return
* a String containing an element of the
* {@link SnmpConstants#SNMP_ERROR_MESSAGES} array for a valid error status.
* "Unknown error: <errorStatusNumber>" is returned for any other value.
*/
public String getErrorStatusText() {
return toErrorStatusText(errorStatus.getValue());
}
/**
* Returns textual description for the supplied error status value.
* @param errorStatus
* an error status.
* @return
* a String containing an element of the
* {@link SnmpConstants#SNMP_ERROR_MESSAGES} array for a valid error status.
* "Unknown error: <errorStatusNumber>" is returned for any other value.
* @since 1.7
*/
public static final String toErrorStatusText(int errorStatus) {
try {
return SnmpConstants.SNMP_ERROR_MESSAGES[errorStatus];
}
catch (ArrayIndexOutOfBoundsException iobex) {
return "Unknown error: "+errorStatus;
}
}
/**
* Sets the error index.
* @param errorIndex
* an integer value >= 0 where 1 denotes the first variable binding.
*/
public void setErrorIndex(int errorIndex) {
this.errorIndex.setValue(errorIndex);
}
/**
* Gets the error index.
* @return
* an integer value >= 0 where 1 denotes the first variable binding.
*/
public int getErrorIndex() {
return errorIndex.getValue();
}
/**
* Checks whether this PDU is a confirmed class PDU.
* @return boolean
*/
public boolean isConfirmedPdu() {
return ((type != PDU.REPORT) && (type != PDU.RESPONSE) &&
(type != PDU.TRAP) && (type != PDU.V1TRAP));
}
public int getBERLength() {
// header for data_pdu
int length = getBERPayloadLengthPDU();
length += BER.getBERLengthOfLength(length) + 1;
// assume maxmimum length here
return length;
}
public int getBERPayloadLength() {
return getBERPayloadLengthPDU();
}
public void decodeBER(BERInputStream inputStream) throws IOException {
BER.MutableByte pduType = new BER.MutableByte();
int length = BER.decodeHeader(inputStream, pduType);
int pduStartPos = (int)inputStream.getPosition();
switch (pduType.getValue()) {
case PDU.SET:
case PDU.GET:
case PDU.GETNEXT:
case PDU.GETBULK:
case PDU.INFORM:
case PDU.REPORT:
case PDU.TRAP:
case PDU.RESPONSE:
break;
default:
throw new IOException("Unsupported PDU type: "+pduType.getValue());
}
this.type = pduType.getValue();
requestID.decodeBER(inputStream);
errorStatus.decodeBER(inputStream);
errorIndex.decodeBER(inputStream);
pduType = new BER.MutableByte();
int vbLength = BER.decodeHeader(inputStream, pduType);
if (pduType.getValue() != BER.SEQUENCE) {
throw new IOException("Encountered invalid tag, SEQUENCE expected: "+
pduType.getValue());
}
// rest read count
int startPos = (int)inputStream.getPosition();
variableBindings = new Vector();
while (inputStream.getPosition() - startPos < vbLength) {
VariableBinding vb = new VariableBinding();
vb.decodeBER(inputStream);
variableBindings.add(vb);
}
if (inputStream.getPosition() - startPos != vbLength) {
throw new IOException("Length of VB sequence ("+vbLength+
") does not match real length: "+
((int)inputStream.getPosition()-startPos));
}
if (BER.isCheckSequenceLength()) {
BER.checkSequenceLength(length,
(int) inputStream.getPosition() - pduStartPos,
this);
}
}
protected int getBERPayloadLengthPDU() {
int length = 0;
// length for all vbs
for (int i = 0; i < variableBindings.size(); i++) {
length += ((VariableBinding)variableBindings.get(i)).getBERLength();
}
length += BER.getBERLengthOfLength(length) + 1;
// req id, error status, error index
Integer32 i32 = new Integer32(requestID.getValue());
length += i32.getBERLength();
i32 = errorStatus;
length += i32.getBERLength();
i32 = errorIndex;
length += i32.getBERLength();
i32 = null;
return length;
}
public void encodeBER(OutputStream outputStream) throws IOException {
BER.encodeHeader(outputStream, type, getBERPayloadLengthPDU());
requestID.encodeBER(outputStream);
errorStatus.encodeBER(outputStream);
errorIndex.encodeBER(outputStream);
int vbLength = 0;
for (int i=0; i<variableBindings.size(); i++) {
vbLength += ((VariableBinding)variableBindings.get(i)).getBERLength();
}
BER.encodeHeader(outputStream, BER.SEQUENCE, vbLength);
for (int i=0; i<variableBindings.size(); i++) {
((VariableBinding)variableBindings.get(i)).encodeBER(outputStream);
}
}
/**
* Removes all variable bindings from the PDU and sets the request ID to zero.
* This can be used to reuse a PDU for another request.
*/
public void clear() {
variableBindings.clear();
setRequestID(new Integer32(0));
}
/**
* Sets the PDU type.
* @param type
* the type of the PDU (e.g. GETNEXT, SET, etc.)
*/
public void setType(int type) {
this.type = type;
}
/**
* Gets the PDU type. The default is {@link PDU#GETNEXT}.
* @return
* the PDU's type.
*/
public int getType() {
return type;
}
public Object clone() {
return new PDU(this);
}
/**
* Gets the request ID associated with this PDU.
* @return
* an <code>Integer32</code> instance.
*/
public Integer32 getRequestID() {
return requestID;
}
/**
* Sets the request ID for this PDU. When the request ID is not set or set to
* zero, the message processing model will generate a unique request ID for
* the <code>PDU</code> when sent.
* @param requestID
* a unique request ID.
*/
public void setRequestID(Integer32 requestID) {
this.requestID = requestID;
}
/**
* Gets a string representation of the supplied PDU type.
* @param type
* a PDU type.
* @return
* a string representation of <code>type</code>, for example "GET".
*/
public static String getTypeString(int type) {
switch (type) {
case PDU.GET:
return "GET";
case PDU.SET:
return "SET";
case PDU.GETNEXT:
return "GETNEXT";
case PDU.GETBULK:
return "GETBULK";
case PDU.INFORM:
return "INFORM";
case PDU.RESPONSE:
return "RESPONSE";
case PDU.REPORT:
return "REPORT";
case PDU.TRAP:
return "TRAP";
case PDU.V1TRAP:
return "V1TRAP";
}
return "unknown";
}
/**
* Gets the PDU type identifier for a string representation of the type.
* @param type
* the string representation of a PDU type: <code>GET, GETNEXT, GETBULK,
* SET, INFORM, RESPONSE, REPORT, TRAP, V1TRAP)</code>.
* @return
* the corresponding PDU type constant, or <code>Integer.MIN_VALUE</code>
* of the supplied type is unknown.
*/
public static int getTypeFromString(String type) {
if (type.equals("GET")) {
return PDU.GET;
}
else if (type.equals("SET")) {
return PDU.SET;
}
else if (type.equals("GETNEXT")) {
return PDU.GETNEXT;
}
else if (type.equals("GETBULK")) {
return PDU.GETBULK;
}
else if (type.equals("INFORM")) {
return PDU.INFORM;
}
else if (type.equals("RESPONSE")) {
return PDU.RESPONSE;
}
else if (type.equals("TRAP")) {
return PDU.TRAP;
}
else if (type.equals("V1TRAP")) {
return PDU.V1TRAP;
}
else if (type.equals("REPORT")) {
return PDU.REPORT;
}
return Integer.MIN_VALUE;
}
/**
* Returns a string representation of the object.
*
* @return a string representation of the object.
*/
public String toString() {
StringBuffer buf = new StringBuffer();
buf.append(getTypeString(type));
buf.append("[requestID=");
buf.append(requestID);
buf.append(", errorStatus=");
buf.append(getErrorStatusText()+"("+errorStatus+")");
buf.append(", errorIndex=");
buf.append(errorIndex);
buf.append(", VBS[");
for (int i=0; i<variableBindings.size(); i++) {
buf.append(variableBindings.get(i));
if (i+1 < variableBindings.size()) {
buf.append("; ");
}
}
buf.append("]]");
return buf.toString();
}
/**
* Gets the maximum repetitions of repeatable variable bindings in GETBULK
* requests.
* @return
* an integer value >= 0.
*/
public int getMaxRepetitions() {
return errorIndex.getValue();
}
/**
* Sets the maximum repetitions of repeatable variable bindings in GETBULK
* requests.
* @param maxRepetitions
* an integer value >= 0.
*/
public void setMaxRepetitions(int maxRepetitions) {
this.errorIndex.setValue(maxRepetitions);
}
/**
* Gets the number of non repeater variable bindings in a GETBULK PDU.
* @return
* an integer value >= 0 and <= {@link #size()}
*/
public int getNonRepeaters() {
return errorStatus.getValue();
}
/**
* Sets the number of non repeater variable bindings in a GETBULK PDU.
* @param nonRepeaters
* an integer value >= 0 and <= {@link #size()}
*/
public void setNonRepeaters(int nonRepeaters) {
this.errorStatus.setValue(nonRepeaters);
}
/**
* Returns an array with the variable bindings of this PDU.
* @return
* an array of <code>VariableBinding</code> instances of this PDU in the
* same order as in the PDU.
*/
public VariableBinding[] toArray() {
VariableBinding[] vbs = new VariableBinding[this.variableBindings.size()];
this.variableBindings.toArray(vbs);
return vbs;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -