📄 snmp.java
字号:
}
timer.cancel();
for (Iterator it = pendingRequests.values().iterator(); it.hasNext(); ) {
PendingRequest pending = (PendingRequest) it.next();
ResponseEvent e =
new ResponseEvent(this, null, pending.pdu, null, pending.userObject,
new InterruptedException(
"Snmp session has been closed"));
pending.listener.onResponse(e);
}
// close all notification listeners
if (notificationDispatcher != null) {
notificationDispatcher.closeAll();
}
}
/**
* Sends a GET request to a target. This method sets the PDU's type to
* {@link PDU#GET} and then sends a synchronous request to the supplied
* target.
* @param pdu
* a <code>PDU</code> instance. For SNMPv3 messages, the supplied PDU
* instance has to be a <code>ScopedPDU</code> instance.
* @param target
* the Target instance representing the target SNMP engine where to send
* the <code>pdu</code>.
* @return
* the received response encapsulated in a <code>ResponseEvent</code>
* instance. To obtain the received response <code>PDU</code> call
* {@link ResponseEvent#getResponse()}. If the request timed out,
* that method will return <code>null</code>.
* @throws IOException
* if the PDU cannot be sent to the target.
* @since 1.1
*/
public ResponseEvent get(PDU pdu, Target target) throws IOException {
pdu.setType(PDU.GET);
return send(pdu, target);
}
/**
* Asynchronously sends a GET request <code>PDU</code> to the given target.
* The response is then returned by calling the supplied
* <code>ResponseListener</code> instance.
*
* @param pdu
* the PDU instance to send.
* @param target
* the Target instance representing the target SNMP engine where to send
* the <code>pdu</code>.
* @param userHandle
* an user defined handle that is returned when the request is returned
* via the <code>listener</code> object.
* @param listener
* a <code>ResponseListener</code> instance that is called when
* <code>pdu</code> is a confirmed PDU and the request is either answered
* or timed out.
* @throws IOException
* if the PDU cannot be sent to the target.
* @since 1.1
*/
public void get(PDU pdu, Target target, Object userHandle,
ResponseListener listener) throws IOException {
pdu.setType(PDU.GET);
send(pdu, target, userHandle, listener);
}
/**
* Sends a GETNEXT request to a target. This method sets the PDU's type to
* {@link PDU#GETNEXT} and then sends a synchronous request to the supplied
* target. This method is a convenience wrapper for the
* {@link #send(PDU pdu, Target target)} method.
* @param pdu
* a <code>PDU</code> instance. For SNMPv3 messages, the supplied PDU
* instance has to be a <code>ScopedPDU</code> instance.
* @param target
* the Target instance representing the target SNMP engine where to send
* the <code>pdu</code>.
* @return
* the received response encapsulated in a <code>ResponseEvent</code>
* instance. To obtain the received response <code>PDU</code> call
* {@link ResponseEvent#getResponse()}. If the request timed out,
* that method will return <code>null</code>.
* @throws IOException
* if the PDU cannot be sent to the target.
* @since 1.1
*/
public ResponseEvent getNext(PDU pdu, Target target) throws IOException {
pdu.setType(PDU.GETNEXT);
return send(pdu, target);
}
/**
* Asynchronously sends a GETNEXT request <code>PDU</code> to the given
* target. The response is then returned by calling the supplied
* <code>ResponseListener</code> instance.
*
* @param pdu
* the PDU instance to send.
* @param target
* the Target instance representing the target SNMP engine where to send
* the <code>pdu</code>.
* @param userHandle
* an user defined handle that is returned when the request is returned
* via the <code>listener</code> object.
* @param listener
* a <code>ResponseListener</code> instance that is called when
* <code>pdu</code> is a confirmed PDU and the request is either answered
* or timed out.
* @throws IOException
* if the PDU cannot be sent to the target.
* @since 1.1
*/
public void getNext(PDU pdu, Target target, Object userHandle,
ResponseListener listener) throws IOException {
pdu.setType(PDU.GETNEXT);
send(pdu, target, userHandle, listener);
}
/**
* Sends a GETBULK request to a target. This method sets the PDU's type to
* {@link PDU#GETBULK} and then sends a synchronous request to the supplied
* target. This method is a convenience wrapper for the
* {@link #send(PDU pdu, Target target)} method.
* @param pdu
* a <code>PDU</code> instance. For SNMPv3 messages, the supplied PDU
* instance has to be a <code>ScopedPDU</code> instance.
* @param target
* the Target instance representing the target SNMP engine where to send
* the <code>pdu</code>.
* @return
* the received response encapsulated in a <code>ResponseEvent</code>
* instance. To obtain the received response <code>PDU</code> call
* {@link ResponseEvent#getResponse()}. If the request timed out,
* that method will return <code>null</code>.
* @throws IOException
* if the PDU cannot be sent to the target.
* @since 1.1
*/
public ResponseEvent getBulk(PDU pdu, Target target) throws IOException {
pdu.setType(PDU.GETBULK);
return send(pdu, target);
}
/**
* Asynchronously sends a GETBULK request <code>PDU</code> to the given
* target. The response is then returned by calling the supplied
* <code>ResponseListener</code> instance.
*
* @param pdu
* the PDU instance to send.
* @param target
* the Target instance representing the target SNMP engine where to send
* the <code>pdu</code>.
* @param userHandle
* an user defined handle that is returned when the request is returned
* via the <code>listener</code> object.
* @param listener
* a <code>ResponseListener</code> instance that is called when
* <code>pdu</code> is a confirmed PDU and the request is either answered
* or timed out.
* @throws IOException
* if the PDU cannot be sent to the target.
* @since 1.1
*/
public void getBulk(PDU pdu, Target target, Object userHandle,
ResponseListener listener) throws IOException {
pdu.setType(PDU.GETBULK);
send(pdu, target, userHandle, listener);
}
/**
* Sends an INFORM request to a target. This method sets the PDU's type to
* {@link PDU#INFORM} and then sends a synchronous request to the supplied
* target. This method is a convenience wrapper for the
* {@link #send(PDU pdu, Target target)} method.
* @param pdu
* a <code>PDU</code> instance. For SNMPv3 messages, the supplied PDU
* instance has to be a <code>ScopedPDU</code> instance.
* @param target
* the Target instance representing the target SNMP engine where to send
* the <code>pdu</code>.
* @return
* the received response encapsulated in a <code>ResponseEvent</code>
* instance. To obtain the received response <code>PDU</code> call
* {@link ResponseEvent#getResponse()}. If the request timed out,
* that method will return <code>null</code>.
* @throws IOException
* if the inform request could not be send to the specified target.
* @since 1.1
*/
public ResponseEvent inform(PDU pdu, Target target) throws IOException {
pdu.setType(PDU.INFORM);
return send(pdu, target);
}
/**
* Asynchronously sends an INFORM request <code>PDU</code> to the given
* target. The response is then returned by calling the supplied
* <code>ResponseListener</code> instance.
*
* @param pdu
* the PDU instance to send.
* @param target
* the Target instance representing the target SNMP engine where to send
* the <code>pdu</code>.
* @param userHandle
* an user defined handle that is returned when the request is returned
* via the <code>listener</code> object.
* @param listener
* a <code>ResponseListener</code> instance that is called when
* <code>pdu</code> is a confirmed PDU and the request is either answered
* or timed out.
* @throws IOException
* if the PDU cannot be sent to the target.
* @since 1.1
*/
public void inform(PDU pdu, Target target, Object userHandle,
ResponseListener listener) throws IOException {
pdu.setType(PDU.GETBULK);
send(pdu, target, userHandle, listener);
}
/**
* Sends a SNMPv1 trap to a target. This method sets the PDU's type to
* {@link PDU#V1TRAP} and then sends it to the supplied target. This method
* is a convenience wrapper for the {@link #send(PDU pdu, Target target)}
* method.
* @param pdu
* a <code>PDUv1</code> instance.
* @param target
* the Target instance representing the target SNMP engine where to send
* the <code>pdu</code>. The selected SNMP protocol version for the
* target must be {@link SnmpConstants#version1}.
* @throws IOException
* if the trap cannot be sent.
* @since 1.1
*/
public void trap(PDUv1 pdu, Target target) throws IOException {
if (target.getVersion() != SnmpConstants.version1) {
throw new IllegalArgumentException(
"SNMPv1 trap PDU must be used with SNMPv1");
}
pdu.setType(PDU.V1TRAP);
send(pdu, target);
}
/**
* Sends a SNMPv2c or SNMPv3 notification to a target. This method sets the
* PDU's type to {@link PDU#NOTIFICATION} and then sends it to the supplied
* target. This method is a convenience wrapper for the
* {@link #send(PDU pdu, Target target)} method.
* @param pdu
* a <code>PDUv1</code> instance.
* @param target
* the Target instance representing the target SNMP engine where to send
* the <code>pdu</code>. The selected SNMP protocol version for the
* target must be {@link SnmpConstants#version2c} or
* {@link SnmpConstants#version2c}.
* @throws IOException
* if the notification cannot be sent.
* @since 1.1
*/
public void notify(PDU pdu, Target target) throws IOException {
if (target.getVersion() == SnmpConstants.version1) {
throw new IllegalArgumentException(
"Notifications PDUs cannot be used with SNMPv1");
}
pdu.setType(PDU.NOTIFICATION);
send(pdu, target);
}
/**
* Sends a SET request to a target. This method sets the PDU's type to
* {@link PDU#SET} and then sends a synchronous request to the supplied
* target.
* @param pdu
* a <code>PDU</code> instance. For SNMPv3 messages, the supplied PDU
* instance has to be a <code>ScopedPDU</code> instance.
* @param target
* the Target instance representing the target SNMP engine where to send
* the <code>pdu</code>.
* @return
* the received response encapsulated in a <code>ResponseEvent</code>
* instance. To obtain the received response <code>PDU</code> call
* {@link ResponseEvent#getResponse()}. If the request timed out,
* that method will return <code>null</code>.
* @since 1.1
*/
public ResponseEvent set(PDU pdu, Target target) {
pdu.setType(PDU.SET);
try {
return send(pdu, target);
}
catch (IOException ex) {
return new ResponseEvent(this, null, pdu, null, target, ex);
}
}
/**
* Asynchronously sends a SET request <code>PDU</code> to the given target.
* The response is then returned by calling the supplied
* <code>ResponseListener</code> instance.
*
* @param pdu
* the PDU instance to send.
* @param target
* the Target instance representing the target SNMP engine where to send
* the <code>pdu</code>.
* @param userHandle
* an user defined handle that is returned when the request is returned
* via the <code>listener</code> object.
* @param listener
* a <code>ResponseListener</code> instance that is called when
* <code>pdu</code> is a confirmed PDU and the request is either answered
* or timed out.
* @throws IOException
* if the PDU cannot be sent to the target.
* @since 1.1
*/
public void set(PDU pdu, Target target, Object userHandle,
ResponseListener listener) throws IOException {
pdu.setType(PDU.SET);
send(pdu, target, userHandle, listener);
}
public ResponseEvent send(PDU pdu, Target target) throws IOException {
return send(pdu, target, null);
}
/**
* Sends a <code>PDU</code> to the given target and if the <code>PDU</code>
* is a confirmed request, then the received response is returned
* synchronously.
* @param pdu
* a <code>PDU</code> instance. When sending a SNMPv1 trap PDU, the
* supplied PDU instance must be a <code>PDUv1</code>. For all types of
* SNMPv3 messages, the supplied PDU instance has to be a
* <code>ScopedPDU</code> instance.
* @param target
* the Target instance representing the target SNMP engine where to send
* the <code>pdu</code>.
* @param transport
* specifies the <code>TransportMapping</code> to be used when sending
* the PDU. If <code>transport</code> is <code>null</code>, the associated
* message dispatcher will try to determine the transport mapping by the
* <code>target</code>'s address.
* @return
* the received response encapsulated in a <code>ResponseEvent</code>
* instance. To obtain the received response <code>PDU</code> call
* {@link ResponseEvent#getResponse()}. If the request timed out,
* that method will return <code>null</code>. If the sent <code>pdu</code>
* is an unconfirmed PDU (notification, response, or report), then
* <code>null</code> will be returned.
* @throws IOException
* if the message could not be sent.
* @see PDU
* @see ScopedPDU
* @see PDUv1
*/
public ResponseEvent send(PDU pdu, Target target,
TransportMapping transport) throws IOException {
if (!pdu.isConfirmedPdu()) {
sendMessage(pdu, target, null);
return null;
}
SyncResponseListener syncResponse = new SyncResponseListener();
synchronized (syncResponse) {
synchronized (sync) {
PduHandle handle = sendMessage(pdu, target, null);
PendingRequest request =
new PendingRequest(handle, syncResponse, target, pdu, target,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -