- /*
- $Id: snmpPollerDemo.src,v 1.4 2002/09/09 05:35:19 tonyjpaul Exp $
- */
- /*
- * @(#)snmpPollerDemo.java
- * Copyright (c) 1996-2002 AdventNet, Inc. All Rights Reserved.
- * Please read the COPYRIGHTS file for more details.
- */
- /**
- * An example of using the SnmpPoller object in your code.
- * This is an applet example.
- * Please refer snmpPollerDemo.html for parameters.
- **/
- import java.awt.*;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.applet.Applet;
- import com.adventnet.snmp.beans.*;
- import com.adventnet.snmp.ui.*;
- import javax.swing.*;
- public class snmpPollerDemo extends Applet implements ActionListener {
- // Some AWT widgets we'll use
- Label l1;
- Button b1;
- JTextField t1;
- SnmpPoller poller; // The SNMP poller instance we'll use
- PropertySettings prop;
- /** The init method is first called for applets. **/
- public void init() {
- try {
- UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName());
- } catch (Exception e) {
- System.err.println("Couldn't use the system look and feel: "+ e);
- }
- prop = new PropertySettings(this);
- l1 = new Label ( getParameter("OID") );
- b1 = new Button();
- b1.setLabel("Stop Polling");
- t1 = new JTextField("The data from the agent is shown here");
- //Listen for actions on button
- b1.addActionListener(this);
- //Add Components using the default FlowLayout.
- add(prop);
- add(l1);
- add(t1);
- add(b1);
- }
- /** The start method is called when page with applet is visited. **/
- public void start() {
- try { // setup the poller object as desired - use applet parameters
- poller = new SnmpPoller(this);
- prop.addVetoableChangeListener(poller);
- // load MIB to allow us to use names
- showStatus("Loading MIBs..."); // show something on the status bar
- poller.loadMibs( getParameter("MIBS") );
- showStatus("Loading MIBs done.");
- poller.setTargetHost( getParameter("HOSTNAME"));
- poller.setCommunity( getParameter("COMMUNITY") );
- poller.setObjectID( getParameter("OID") );
- } catch (Exception ex) {
- System.err.println("Error in starting applet: "+ex+":"+ex.getMessage());
- }
- // We need to add a listener to listen for responses
- ResultAdapter listener = new ResultAdapter() {
- // This method will be invoked when the response is received
- public void setResult( ResultEvent e ) {
- try {
- t1.setText(e.getStringValue());
- } catch (DataException de) {
- t1.setText("Error in getting agent data: "+de +
- e.getErrorString());
- }
- }
- };
- poller.addResultListener(listener); // listen for response events
- }
- /** This method is called when the button is clicked **/
- public void actionPerformed(ActionEvent e) {
- if (poller.getPollingStatus()) { // if polling is going on, stop it
- poller.stopPolling();
- b1.setLabel("Start Polling");
- } else { // else restart it
- poller.restartPolling();
- b1.setLabel("Stop Polling");
- }
- }
- }