- /* $Id: rmiAppletDemo.src,v 1.3 2002/09/09 05:41:24 tonyjpaul Exp $ */
- /*
- * @(#)rmiAppletDemo.java
- * Copyright (c) 1996-2002 AdventNet, Inc. All Rights Reserved.
- * Please read the COPYRIGHTS file for more details.
- */
- /**
- * An example of using the RMI interfaces in a remote client Applet.
- * <p>
- * This example uses the RMI SnmpPoller interface.
- */
- import java.awt.*;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.applet.Applet;
- import java.rmi.*;
- import com.adventnet.snmp.rmi.*;
- import com.adventnet.snmp.beans.DataException;
- public class rmiAppletDemo extends Applet implements ActionListener, ResultListener {
- com.adventnet.snmp.rmi.SnmpFactory factory = null;
- com.adventnet.snmp.rmi.SnmpPoller poller = null; static final int USM_SECURITY_MODEL = 3;
- // Some AWT widgets we'll use
- Label l1;
- Button b1;
- TextField t1;
- /** The init method is first called for applets. **/
- public void init() {
- l1 = new Label ( getParameter("OID") );
- b1 = new Button();
- b1.setLabel("Stop Polling");
- t1 = new TextField("The data from the agent is shown here");
- //Listen for actions on button
- b1.addActionListener(this);
- //Add Components using the default FlowLayout.
- add(l1);
- add(t1);
- add(b1);
- }
- public void stop() {
- try{
- poller.stopPolling();
- }catch(Exception stopEx){}
- super.stop();
- }
- /** The start method is called when page with applet is visited. **/
- public void start() {
- try { // setup the poller object as desired - use applet parameters
- if(getParameter("RMISERVER") != null)
- factory = (com.adventnet.snmp.rmi.SnmpFactory)
- Naming.lookup( "rmi://" + getParameter("RMISERVER") + "/AdventnetSnmpFactory" );
- else
- factory = (com.adventnet.snmp.rmi.SnmpFactory)
- Naming.lookup( "rmi:///AdventnetSnmpFactory" );
- // Get a Poller object
- poller = factory.createPoller();
- // 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.");
- if( getParameter("HOSTNAME")!=null)
- poller.setTargetHost( getParameter("HOSTNAME") );
- if( getParameter("COMMUNITY")!=null)
- poller.setCommunity( getParameter("COMMUNITY") );
- if(getParameter("SNMP_PORT")!=null)
- poller.setTargetPort(Integer.parseInt(getParameter("SNMP_PORT")));
- if(getParameter("SNMP_VERSION")!=null)
- poller.setSnmpVersion(Integer.parseInt(getParameter("SNMP_VERSION")));
- if (getParameter("SNMP_VERSION") != null && getParameter("SNMP_VERSION").equals("3"))
- {
- poller.setSnmpVersion(Integer.parseInt(getParameter("SNMP_VERSION")));
- poller.setPrincipal( getParameter("USERNAME"));
- poller.setAuthPassword( getParameter("AUTHPASS"));
- String protocol = getParameter("AUTHPROTOCOL");
- if(protocol!=null)
- {
- if(protocol.equals("SHA"))
- poller.setAuthProtocol(poller.SHA_AUTH);
- else
- poller.setAuthProtocol(poller.MD5_AUTH);
- }
- if (getParameter("CONTEXTID") != null)
- poller.setContextID( getParameter("CONTEXTID"));
- if (getParameter("CONTEXTNAME") != null)
- poller.setContextName( getParameter("CONTEXTNAME"));
- if (getParameter("PRIVPASS") != null)
- poller.setPrivPassword( getParameter("PRIVPASS"));
- //poller.setSecurityLevel((byte)Integer.parseInt(getParameter("SEC_LEVEL"))); // Create the SNMPv3 tables.
- poller.create_v3_tables();
- }
- poller.setObjectID( getParameter("OID") );
- java.rmi.server.UnicastRemoteObject.exportObject(this);
- poller.addResultListener(this); // listen for response events
- } catch (Exception ex) {
- System.err.println("Error in starting applet: "+ex);
- t1.setText("Error: "+ex); // show something applet
- }
- }
- /** This method is called when the button is clicked **/
- public void actionPerformed(ActionEvent e) {
- try {
- 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");
- }
- } catch (Exception ex) {
- System.err.println("Error in starting applet: "+ex);
- }
- }
- // This method will be invoked when the response is received
- public void setResult( com.adventnet.snmp.beans.ResultEvent e ) {
- try {
- t1.setText(e.getStringValue());
- } catch (DataException de) {
- t1.setText("Error in getting agent data: "+de +
- e.getErrorString());
- }
- }
- public void setStringResult(String s ) { }
- public void setNumericResult(long i ) { }
- //public void setNumericResult(double i ) { }
- }