When you change these parameters, the changes are applied to all active messages. CODE EXAMPLE 19-1 shows the instantiation and configuration of an SnmpPeer object representing a remote agent using port 8085 on host summer.
SNMP Session
The SNMP manager session is controlled by an instance of the SnmpSession class. An SnmpSession object creates and manages SNMP requests to multiple peers. The SnmpSession object can be instantiated with a default peer so that all requests created without specifying a peer use the default. The default peer can be set while the SnmpSession object is running.
| CODE EXAMPLE 19-2 Instantiating and Configuring an SnmpSession |
SNMP Session Options
SNMP sessions can be configured for specific situations using methods provided in the SnmpOptions object. Setting these options affects all subsequent requests. Existing requests are also affected, depending on the nature of the option. The SNMP session options are:
The SnmpRequest object creates a request that is used to perform one or more of the following SNMP operations:
The request object provides the waitForCompletion method. The waitForCompletion method is used to specify synchronous mode and the interval to wait for the response. This is done by blocking the user's thread for the required period. The user thread is notified whenever a request reaches completion, irrespective of its success or failure. Invoking the waitForCompletion method with an interval of zero blocks the request object until a response is received. CODE EXAMPLE 19-3 shows the instantiation of a synchronous request.
| CODE EXAMPLE 19-3 Instantiating a Synchronous Request |
Asynchronous Mode
In asynchronous mode, management applications are able to send multiple requests and wait for individual responses. Asynchronous mode also permits polling, as described in "Polling" on page 255.
Requests are automatically retried if a response does not arrive within a specified interval. If the agent responds with an error, the SnmpRequest object uses the options defined in the SnmpOptions object to determine the subsequent actions. CODE EXAMPLE 19-4 shows the instantiation of an asynchronous request.
| CODE EXAMPLE 19-4 Instantiating an Asynchronous Request |
Loading Metadata Into the MibStore
Metadata generated by the mibgen compiler can be loaded into the MibStore for use by SNMP manager applications. CODE EXAMPLE 19-5 shows two ways of loading a metadata file generated from a MIB called RFC1213.
| CODE EXAMPLE 19-5 Loading Metadata into the MibStore |
SNMP Traps
The SNMP manager API provides classes that enable you to receive SNMP v1 and SNMP v2 trap PDUs. SNMP v1 Traps
To receive SNMP v1 trap PDUs, you must instantiate an SnmpTrapAgent. The SnmpTrapAgent allows you to create a receiver/dispatcher for SNMP traps. It is run as a thread. The SnmpTrapListener interface defines one callback method that must be implemented for classes that are required to receive SNMP v1 trap PDUs. CODE EXAMPLE 19-6 shows the instantiation of a trap listener.
| CODE EXAMPLE 19-6 Instantiating a Trap Listener |
A class implementing the SnmpTrapListener interface is added to the SnmpTrapAgent. It is the callback object that is called when a valid SNMP v1 trap PDU is present. CODE EXAMPLE 19-7 shows the code for a class implementing the SnmpTrapListener interface.
| CODE EXAMPLE 19-7 Implementing an SNMP v1 Trap Callback Class |
SNMP v2 Traps
To receive SNMP v2 trap PDUs, you must instantiate an SnmpTrapAgent. The SnmpTrapAgent allows you to create a receiver/dispatcher for SNMP traps. It is run as a thread. The SnmpV2TrapListener interface defines one callback method that must be implemented for classes that are required to receive SNMP v2 trap PDUs. CODE EXAMPLE 19-8 shows the instantiation of a trap listener.
| CODE EXAMPLE 19-8 Instantiating a Trap Listener |
A class implementing the SnmpV2TrapListener interface is added to the SnmpTrapAgent. It is the callback object that is called when a valid trap PDU is present. CODE EXAMPLE 19-9 shows the code for a class implementing the SnmpV2TrapListener interface.
| CODE EXAMPLE 19-9 Implementing an SNMP v2 Trap Callback Class |
Polling
The SNMP manager API provides a polling capability through the SnmpPollRequest class. The SnmpPollRequest class extends the SnmpRequest class. The polling operation is stopped if an error of any type is encountered.