SNMP Stack 6_1

uk.co.westhawk.snmp.stack
Class Pdu

java.lang.Object
  extended byjava.util.Observable
      extended byuk.co.westhawk.snmp.stack.Pdu
Direct Known Subclasses:
GetBulkPdu, GetNextPdu, GetPdu, InformPdu, MultiResponsePdu, ReportPdu, ResponsePdu, SetPdu, TrapPduv1, TrapPduv2

public abstract class Pdu
extends java.util.Observable

This class represents the ASN PDU object, this is the equivalent of a GetRequest PDU.

Version:
$Revision: 3.33 $ $Date: 2008/12/12 14:55:51 $
Author:
Tim Panton

Field Summary
protected  boolean added
           
protected  boolean answered
           
protected  SnmpContextBasisFace context
           
protected  byte[] encodedPacket
           
protected  int errind
           
protected  int errstat
           
protected  byte msg_type
           
protected  java.util.Vector reqVarbinds
           
protected  java.util.Vector respVarbinds
           
protected  java.lang.Integer snmpv3MsgId
           
 
Constructor Summary
Pdu(SnmpContextBasisFace con)
          Constructor.
 
Method Summary
 void addOid(AsnObjectId oid)
          Adds an OID (object identifier) to the PDU.
 void addOid(AsnObjectId oid, AsnObject val)
          Adds an OID (object identifier) to the PDU and the value that has to be set.
 void addOid(java.lang.String oid)
          Adds an OID (object identifier) to the PDU.
 void addOid(java.lang.String oid, AsnObject val)
          Adds an OID (object identifier) to the PDU and the value that has to be set.
 void addOid(varbind var)
          Adds an OID (object identifier) to the PDU.
protected  void addToTrans()
          Adds the PDU to its transmitter.
 SnmpContextBasisFace getContext()
          Returns the context of this PDU.
 int getErrorIndex()
          Returns the error index.
 int getErrorStatus()
          Returns the error status as indicated by the error-status field in the reponse PDU.
 java.lang.String getErrorStatusString()
          Returns the string representation of the error status.
 byte getMsgType()
          Returns the message type, this will indicate what kind of request we are dealing with.
 int getReqId()
          Returns the request id of the PDU.
 varbind[] getRequestVarbinds()
          Returns a copy of the varbinds used to build the request.
 varbind[] getResponseVarbinds()
          Returns a copy of the varbinds received in the response.
 int[] getRetryIntervals()
           
protected  boolean isExpectingResponse()
          Returns whether or not this type of PDU is expecting some kind of response.
 boolean isTimedOut()
          Returns whether or not this PDU is timed out, i.e. it did not get a response.
protected  void new_value(int n, varbind res)
          The value of the response is set.
 void notifyObservers(java.lang.Object arg)
          Notify all observers.
protected  java.lang.StringBuffer printVars(java.lang.String title, java.util.Vector vars)
          Returns the string representation of the varbinds of the PDU.
 boolean send()
          Sends the PDU.
protected  boolean send(int error_status, int error_index)
          Sends the PDU.
 boolean send(java.lang.String com)
          Deprecated. Community name has moved to SnmpContext. Use SnmpContext.setCommunity(String).
protected  boolean sendme()
          Sends the actual packet and updates the retries.
protected  void setErrorIndex(int ind)
          Sets the error index, this indicates which of the OIDs went wrong.
protected  void setErrorStatus(int err)
          Sets the error status, indicating what went wrong.
protected  void setErrorStatus(int err, PduException exc)
          Sets the error status and the exception, indicating what went wrong.
protected  void setMsgType(byte type)
          Sets the message type, this will indicate what kind of request we are dealing with.
 void setRetryIntervals(int[] rinterval)
          Sets the retry intervals of the PDU.
protected  void tell_them()
          This method notifies all observers.
 java.lang.String toString()
          Returns the string representation of the PDU.
protected  java.lang.String toString(boolean withRespVars)
          Returns the string representation of the PDU with or without the response varbinds.
 boolean waitForSelf()
          This method will wait until the answer is received, instead of continue with other stuff.
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

reqVarbinds

protected java.util.Vector reqVarbinds

respVarbinds

protected java.util.Vector respVarbinds

encodedPacket

protected byte[] encodedPacket

context

protected SnmpContextBasisFace context

added

protected boolean added

msg_type

protected byte msg_type

snmpv3MsgId

protected java.lang.Integer snmpv3MsgId

errstat

protected int errstat

errind

protected int errind

answered

protected boolean answered
Constructor Detail

Pdu

public Pdu(SnmpContextBasisFace con)
Constructor.

Parameters:
con - The context of the PDU
See Also:
SnmpContext, SnmpContextv2c, SnmpContextv3
Method Detail

new_value

protected void new_value(int n,
                         varbind res)
The value of the response is set. This will be called by Pdu.fillin().


tell_them

protected void tell_them()
This method notifies all observers. This will be called by Pdu.fillin().

The Object to the update() method of the Observer will be a varbind, unless an exception occurred. In the case of an exception, that exception will be passed. So watch out casting!


getContext

public SnmpContextBasisFace getContext()
Returns the context of this PDU.

Returns:
The context

setRetryIntervals

public void setRetryIntervals(int[] rinterval)
Sets the retry intervals of the PDU. The length of the array corresponds with the number of retries. Each entry in the array is the number of milliseconds of each try.

If used, please set before sending!

The default is {500, 1000, 2000, 5000, 5000}. It is good practice to make the interval bigger with each retry, if the numbers are the same the chance of collision is higher.

Parameters:
rinterval - The interval in msec of each retry

getRetryIntervals

public int[] getRetryIntervals()

send

public boolean send()
             throws java.io.IOException,
                    PduException
Sends the PDU. Note that all properties of the context have to be set before this point.

Throws:
java.io.IOException
PduException

send

protected boolean send(int error_status,
                       int error_index)
                throws java.io.IOException,
                       PduException
Sends the PDU. This method accommodates the GetBulk request.

Parameters:
error_status - The value of the error_status field.
error_index - The value of the error_index field.
Throws:
java.io.IOException
PduException
See Also:
send()

addToTrans

protected void addToTrans()
Adds the PDU to its transmitter. The transmitter is the thread that will be sent the PDU and then waits for the answer.

See Also:
send()

sendme

protected boolean sendme()
Sends the actual packet and updates the retries.

See Also:
AbstractSnmpContext.sendPacket(byte[] p)

send

public boolean send(java.lang.String com)
             throws java.io.IOException,
                    PduException
Deprecated. Community name has moved to SnmpContext. Use SnmpContext.setCommunity(String).

Sends the PDU. For backwards compatibility only. Please use send() instead. The community name will be passed to the SnmpContext. If using SnmpContextv3, the community name will be ignored.

Parameters:
com - The community name of the PDU in SNMPv1 and SNMPv2c.
Throws:
java.io.IOException
PduException
See Also:
SnmpContext.setCommunity(java.lang.String), send()

addOid

public void addOid(java.lang.String oid)
Adds an OID (object identifier) to the PDU. The OID indicates which MIB variable we request for or which MIB variable should be set.

Parameters:
oid - The oid
See Also:
addOid(varbind), varbind

addOid

public void addOid(AsnObjectId oid)
Adds an OID (object identifier) to the PDU. The OID indicates which MIB variable we request for or which MIB variable should be set.

Parameters:
oid - The oid
Since:
4_12
See Also:
addOid(varbind), varbind

addOid

public void addOid(java.lang.String oid,
                   AsnObject val)
Adds an OID (object identifier) to the PDU and the value that has to be set. This method has moved from SetPdu to this class in version 4_12.

Parameters:
oid - The oid
val - The value
Since:
4_12
See Also:
addOid(java.lang.String), varbind

addOid

public void addOid(AsnObjectId oid,
                   AsnObject val)
Adds an OID (object identifier) to the PDU and the value that has to be set.

Thanks to Eli Bishop (eli@graphesthesia.com) for the suggestion.

Parameters:
oid - The oid
val - The value
Since:
4_12
See Also:
addOid(java.lang.String), varbind

addOid

public void addOid(varbind var)
Adds an OID (object identifier) to the PDU.

Parameters:
var - The varbind
See Also:
addOid(String)

getRequestVarbinds

public varbind[] getRequestVarbinds()
Returns a copy of the varbinds used to build the request.

Returns:
the request varbinds of this PDU.

getResponseVarbinds

public varbind[] getResponseVarbinds()
                              throws PduException
Returns a copy of the varbinds received in the response. If there was no response (yet), null will be returned.

Returns:
the response varbinds of this PDU.
Throws:
PduException - An agent or decoding exception occurred whilst receiving the response.
See Also:
getErrorStatus(), notifyObservers(java.lang.Object)

getReqId

public int getReqId()
Returns the request id of the PDU.

Returns:
The ID

getErrorIndex

public int getErrorIndex()
Returns the error index. The error index indicates which of the OIDs went wrong.

Returns:
the error index
See Also:
getErrorStatus()

getErrorStatus

public int getErrorStatus()
Returns the error status as indicated by the error-status field in the reponse PDU. The error index will indicated which OID caused the error. In case of a decoding exception the error status will be set to one of the decoding errors:

The actual exception will be passed to your update(Observable ob, Object arg) method via the the parameter arg.

Returns:
the error status
See Also:
notifyObservers(java.lang.Object), getResponseVarbinds(), SnmpConstants.SNMP_ERR_NOERROR, SnmpConstants.SNMP_ERR_DECODING_EXC, SnmpConstants.SNMP_ERR_DECODINGASN_EXC, SnmpConstants.SNMP_ERR_DECODINGPKTLNGTH_EXC, getErrorStatusString(), getErrorIndex()

getErrorStatusString

public java.lang.String getErrorStatusString()
Returns the string representation of the error status.

Returns:
the error string
See Also:
getErrorStatus()

isTimedOut

public boolean isTimedOut()
Returns whether or not this PDU is timed out, i.e. it did not get a response. Its errorStatus will be set to AsnObject.SNMP_ERR_GENERR.

Note that a SNMP agent can respond with an errorStatus of AsnObject.SNMP_ERR_GENERR as well, so getting a AsnObject.SNMP_ERR_GENERR does not necessarily mean that the request is timed out!

Returns:
true is the PDU was timed out
See Also:
getErrorStatus(), SnmpConstants.SNMP_ERR_GENERR

waitForSelf

public boolean waitForSelf()
This method will wait until the answer is received, instead of continue with other stuff.


toString

public java.lang.String toString()
Returns the string representation of the PDU.

Returns:
The string of the PDU

toString

protected java.lang.String toString(boolean withRespVars)
Returns the string representation of the PDU with or without the response varbinds.

Parameters:
withRespVars - Include the response varbinds or not
Returns:
The string of the PDU

printVars

protected java.lang.StringBuffer printVars(java.lang.String title,
                                           java.util.Vector vars)
Returns the string representation of the varbinds of the PDU.

Since:
4_14
See Also:
toString(boolean)

getMsgType

public byte getMsgType()
Returns the message type, this will indicate what kind of request we are dealing with. By default it will be set to the GET_REQ_MSG

Returns:
The message type

setMsgType

protected void setMsgType(byte type)
Sets the message type, this will indicate what kind of request we are dealing with. By default it will be set to the GET_REQ_MSG

Parameters:
type - The message type

setErrorStatus

protected void setErrorStatus(int err)
Sets the error status, indicating what went wrong.

Parameters:
err - the error status
See Also:
getErrorIndex(), getErrorStatusString(), getErrorStatus()

setErrorStatus

protected void setErrorStatus(int err,
                              PduException exc)
Sets the error status and the exception, indicating what went wrong.

Parameters:
err - the error status
exc - the PDU Exception that was thrown whilst decoding
See Also:
getErrorIndex(), getErrorStatusString(), getErrorStatus()

setErrorIndex

protected void setErrorIndex(int ind)
Sets the error index, this indicates which of the OIDs went wrong.

Parameters:
ind - the error index
See Also:
setErrorStatus(int), getErrorIndex()

isExpectingResponse

protected boolean isExpectingResponse()
Returns whether or not this type of PDU is expecting some kind of response. This method is used in AbstractSnmpContext to help determine whether or not to start a thread that listens for a response when sending this PDU. The default is true.

Returns:
true if a response is expected, false if not.
Since:
4_14

notifyObservers

public void notifyObservers(java.lang.Object arg)
Notify all observers. If a decoding exception had occurred, the argument will be replaced with the exception.

In the case of an exception, the error status will be set to one of the decoding errors (see getErrorStatus) and passed as the parameter arg in the update(Observable obs, Object arg) method.

Parameters:
arg - The argument passed to update, can be a PduException.
Since:
4.5
See Also:
SnmpConstants.SNMP_ERR_DECODING_EXC, getErrorStatus(), getResponseVarbinds()

SNMP Stack 6_1