📄 set_one.java
字号:
// NAME// $RCSfile: set_one.java,v $// DESCRIPTION// [given below in javadoc format]// DELTA// $Revision: 1.7 $// CREATED// $Date: 2006/06/19 15:36:14 $// COPYRIGHT// Westhawk Ltd// TO DO///* * Copyright (C) 1996 - 2006 by Westhawk Ltd * * Permission to use, copy, modify, and distribute this software * for any purpose and without fee is hereby granted, provided * that the above copyright notices appear in all copies and that * both the copyright notice and this permission notice appear in * supporting documentation. * This software is provided "as is" without express or implied * warranty. * author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a> */package uk.co.westhawk.examplev2c;import uk.co.westhawk.snmp.stack.*;import uk.co.westhawk.snmp.pdu.*;import java.awt.*; import javax.swing.*;import java.awt.event.*;import java.util.*;import java.net.*;/** * <p> * The set_one application will display the parameters, as configured in the * properties file. It will retrieve the specified MIB variable. * </p> * * <p> * The name of the properties file can be passed as first argument to * this application. If there is no such argument, it will look for * <code>set_one.properties</code>. If this file does not exist, the * application will use default parameters. * </p> * * <p> * The user can set the required OID and perform a Get or GetNext * request. * </p> * * <p> * The user can also set a MIB variable by performing a Set request. * By default the value is set as a String type (using AsnOctets), * unless the value is a number (AsnInteger will then be used). * </p> * * @see uk.co.westhawk.snmp.stack.GetPdu * @see uk.co.westhawk.snmp.stack.SetPdu * * @author <a href="mailto:snmp@westhawk.co.uk">Birgit Arkesteijn</a> * @version $Revision: 1.7 $ $Date: 2006/06/19 15:36:14 $ */public class set_one extends JComponent implements Observer, ActionListener, WindowListener { private static final String version_id = "@(#)$Id: set_one.java,v 1.7 2006/06/19 15:36:14 birgit Exp $ Copyright Westhawk Ltd"; /** * sysContact is used as default oid */ public final static String sysContact = "1.3.6.1.2.1.1.4.0"; private String host; private int port; private String community; private String bindAddr; private String oid; private String value; private String socketType; private int version = SnmpConstants.SNMP_VERSION_2c; private SnmpContextPool context; private JTextField thost, toid, tcom, tbind, tport, tvalue, ttype; private JButton getButton, setButton, getNextButton; private JLabel lmessage; private JComboBox snmpVersion; private JComboBox socketTypeChoice; private Pdu pdu; private boolean pduInFlight; private Util util;/** * Constructor. * * @param propertiesFilename The name of the properties file. Can be * null. */public set_one(String propertiesFilename){ // AsnObject.setDebug(15); util = new Util(propertiesFilename, this.getClass().getName());}public void init () { host = util.getHost(); bindAddr = util.getBindAddress(); port = util.getPort(SnmpContextBasisFace.DEFAULT_PORT); socketType = util.getSocketType(); oid = util.getOid(sysContact); community = util.getCommunity(); pduInFlight = false; makeLayout(host, oid, port, community); sendGetRequest(host, port, community, oid, version, bindAddr, socketType);}public void actionPerformed(ActionEvent evt){ Object src = evt.getSource(); host = thost.getText(); port = Integer.valueOf(tport.getText()).intValue(); community = tcom.getText(); bindAddr = tbind.getText(); if (bindAddr.length() == 0) { bindAddr = null; } oid = toid.getText(); socketType = (String) socketTypeChoice.getSelectedItem(); String item = (String) snmpVersion.getSelectedItem(); if (item.equals("v2c")) { version = SnmpConstants.SNMP_VERSION_2c; } else { version = SnmpConstants.SNMP_VERSION_1; } try { SnmpContextFace context = createContext(version, host, port, bindAddr, socketType); if (src == getButton) { pdu = new GetPdu(context); pdu.addOid(oid); } else if (src == getNextButton) { pdu = new GetNextPdu(context); pdu.addOid(oid); } else if (src == setButton) { SetPdu setPdu = new SetPdu(context); String value = tvalue.getText(); AsnObject obj; if (Util.isNumber(value)) { obj = new AsnInteger(Util.getNumber(value)); } else { obj = new AsnOctets(value); } ttype.setText(obj.getRespTypeString()); setPdu.addOid(oid, obj); pdu = setPdu; } sendRequest(pdu); } catch (Exception exc) { exc.printStackTrace(); setErrorMessage("Exception: " + exc.getMessage()); }}/** * @since 4_14 */private void sendGetRequest(String host, int port, String community, String oid, int version, String bindAddr, String socketType){ try { SnmpContextFace context = createContext(version, host, port, bindAddr, socketType); pdu = new GetPdu(context); pdu.addOid(oid); sendRequest(pdu); } catch (java.io.IOException exc) { exc.printStackTrace(); setErrorMessage("IOException: " + exc.getMessage()); }}private void sendRequest(Pdu pdu){ boolean hadError = false; setButton.setEnabled(false); getButton.setEnabled(false); getNextButton.setEnabled(false); try { if (!pduInFlight) { pduInFlight = true; setMessage("Sending request ..: "); tvalue.setText(""); ttype.setText(""); pdu.addObserver(this); pdu.send(); } else { setErrorMessage("Pdu still in flight"); } } catch (PduException exc) { exc.printStackTrace(); setErrorMessage("PduException: " + exc.getMessage()); hadError = true; } catch (java.io.IOException exc) { exc.printStackTrace(); setErrorMessage("IOException: " + exc.getMessage()); hadError = true; } if (hadError == true) { pduInFlight = false; setButton.setEnabled(true); getButton.setEnabled(true); getNextButton.setEnabled(true); }}private SnmpContextFace createContext(int version, String host, int port, StringbindAddr, String socketType)throws java.io.IOException{ SnmpContextFace c = null; if (version == SnmpConstants.SNMP_VERSION_2c) { context = new SnmpContextv2cPool(host, port, community, bindAddr, socketType); c = context; } else { context = new SnmpContextPool(host, port, community, bindAddr, socketType); c = context; } // context.dumpContexts("Dump 1:"); return c;}/** * Implementing the Observer interface. Receiving the response from * the Pdu. * * @param obs the Pdu variable * @param ov the varbind * * @see uk.co.westhawk.snmp.stack.GetPdu * @see uk.co.westhawk.snmp.stack.SetPdu * @see uk.co.westhawk.snmp.stack.varbind */public void update(Observable obs, Object ov){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -