- /* $Id: rmiRequestServerDemo.src,v 1.4 2002/09/09 05:41:24 tonyjpaul Exp $ */
- /*
- * @(#)rmiRequestServerDemo.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 SnmpRequestServer 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 rmiRequestServerDemo extends Applet implements ActionListener, ResultListener {
- com.adventnet.snmp.rmi.SnmpFactory factory = null;
- com.adventnet.snmp.rmi.SnmpRequestServer reqServer = null;
- static final int USM_SECURITY_MODEL = 3;
- // Some AWT widgets we'll use
- Label oidLabel;
- TextField oidBox;
- Button getResult;
- TextField resultBox;
- /** The init method is first called for applets. **/
- public void init() {
- oidLabel = new Label ( "OID" );
- oidBox = new TextField ( getParameter("OID") );
- getResult = new Button();
- getResult.setEnabled(false);
- getResult.setLabel("Get Result");
- resultBox = new TextField("The data from the agent is shown here");
- //Listen for actions on button
- getResult.addActionListener(this);
- //Add Components using the default FlowLayout.
- GridBagLayout gridbag = new GridBagLayout();
- GridBagConstraints constraints = new GridBagConstraints();
- setLayout(gridbag);
- gridbag.setConstraints(oidLabel,constraints);
- gridbag.setConstraints(oidBox,constraints);
- constraints.gridy = 1;
- gridbag.setConstraints(getResult,constraints);
- gridbag.setConstraints(resultBox,constraints);
- add(oidLabel);
- add(oidBox);
- add(getResult);
- add(resultBox);
- }
- /** 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
- reqServer = factory.createRequestServer();
- // load MIB to allow us to use names
- showStatus("Loading MIBs..."); // show something on the status bar
- reqServer.loadMibs( getParameter("MIBS") );
- showStatus("Loading MIBs done.");
- if( getParameter("HOSTNAME")!=null)
- reqServer.setTargetHost( getParameter("HOSTNAME") );
- if( getParameter("COMMUNITY")!=null)
- reqServer.setCommunity( getParameter("COMMUNITY") );
- if(getParameter("SNMP_PORT")!=null)
- reqServer.setTargetPort(Integer.parseInt(getParameter("SNMP_PORT")));
- if(getParameter("SNMP_VERSION")!=null)
- reqServer.setSnmpVersion(Integer.parseInt(getParameter("SNMP_VERSION")));
- if (getParameter("SNMP_VERSION").equals("3"))
- {
- reqServer.setPrincipal( getParameter("USERNAME"));
- reqServer.setAuthPassword( getParameter("AUTHPASS"));
- String protocol = getParameter("AUTHPROTOCOL");
- if(protocol!=null)
- {
- if(protocol.equals("SHA"))
- reqServer.setAuthProtocol(com.adventnet.snmp.beans.SnmpTarget.SHA_AUTH);
- else
- reqServer.setAuthProtocol(com.adventnet.snmp.beans.SnmpTarget.MD5_AUTH);
- }
- if (getParameter("CONTEXTID") != null)
- reqServer.setContextID( getParameter("CONTEXTID"));
- if (getParameter("CONTEXTNAME") != null)
- reqServer.setContextName( getParameter("CONTEXTNAME"));
- if (getParameter("PRIVPASS") != null)
- reqServer.setPrivPassword( getParameter("PRIVPASS")); // Create the SNMPv3 tables.
- reqServer.create_v3_tables();
- }
- getResult.setEnabled(true);
- java.rmi.server.UnicastRemoteObject.exportObject(this);
- reqServer.addResultListener(this); // listen for response events
- }
- catch (Exception ex) {
- System.err.println("Error in starting applet: "+ex);
- resultBox.setText("Error: "+ex); // show something applet
- }
- }
- /** This method is called when the button is clicked **/
- public void actionPerformed(ActionEvent e) {
- try {
- com.adventnet.snmp.beans.RequestEvent evt = // generate event to the SnmpRequestServer
- new com.adventnet.snmp.beans.RequestEvent(getResult, oidBox.getText(), com.adventnet.snmp.beans.RequestEvent.GET);
- reqServer.addRequest(evt);
- } 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 {
- resultBox.setText(e.getStringValue());
- } catch (DataException de) {
- resultBox.setText("Error in getting agent data: "+de +
- e.getErrorString());
- }
- }
- public void setStringResult(String s ) { }
- public void setNumericResult(long i ) { }
- //public void setNumericResult(double i ) { }
- }