📄 agent.java
字号:
Node testNode = (Node) _testList.elementAt(i); ListeningContextPool lcontext = _target.createListeningContext(testNode); _lcontextList.addElement(lcontext); lcontext.addUnhandledRawPduListener(this); lcontext.addRawPduListener(this); SnmpContextBasisFace scontext = _target.createContext(testNode); _scontextList.addElement(scontext); if (scontext.getVersion() == SnmpConstants.SNMP_VERSION_3) { SnmpContextv3Basis s3context = (SnmpContextv3Basis) scontext; s3context.setUsmAgent(this); } scontext.addRequestPduListener(this, scontext.getPort()); scontext.addTrapListener(this); } } catch (IOException exc) { // the xml file should have the proper settings .. _writer.println("Agent.start(): IOException " + exc.getMessage()); exc.printStackTrace(_writer); } }}public void trapReceived(TrapEvent evt){ _writer.println(getClass().getName() + ".trapReceived():"); int port = evt.getHostPort(); Pdu trapPdu = evt.getPdu(); int reqId = trapPdu.getReqId(); SnmpContextBasisFace rcontext = trapPdu.getContext(); int version = rcontext.getVersion(); String host = rcontext.getHost(); _writer.println("\ttrap id " + reqId + ", v " + SnmpUtilities.getSnmpVersionString(version) + " from host " + host + ", sent from port " + port); _writer.println("\ttrap " + trapPdu.toString());}public void requestPduReceived(RequestPduEvent evt){ Pdu receivedPdu = evt.getPdu(); _writer.println("\nrequestPduReceived():" + " received decoded request " + receivedPdu.toString()); // test sending a response back if (receivedPdu.getMsgType() == SnmpConstants.GET_REQ_MSG) { varbind[] varbinds = receivedPdu.getRequestVarbinds(); if (varbinds != null && varbinds.length == 1) { varbind var = varbinds[0]; AsnObjectId oid = var.getOid(); ScalarNode sNode = (ScalarNode) _scalarHash.get(oid.toString()); if (sNode != null) { tryToReply(evt, sNode); } } }}public void tryToReply(RequestPduEvent evt, ScalarNode sNode){ Pdu receivedPdu = evt.getPdu(); try { SnmpContextBasisFace rcnt = createReplyContext(evt); Pdu respPdu = new ResponsePdu(rcnt, receivedPdu); respPdu.addOid(sNode._oid, new AsnOctets(sNode._value)); _writer.println("\tsending response: " + respPdu.toString()); respPdu.send(); } catch(PduException pexc) { _writer.println("Agent.tryToReply(): PduException " + pexc.getMessage()); pexc.printStackTrace(_writer); } catch(java.io.IOException iexc) { _writer.println("Agent.tryToReply(): IOException " + iexc.getMessage()); iexc.printStackTrace(_writer); }}public SnmpContextBasisFace createReplyContext(RequestPduEvent evt) throws IOException{ SnmpContextBasisFace newContext = null; int port = evt.getHostPort(); Pdu pdu = evt.getPdu(); SnmpContextBasisFace rcontext = pdu.getContext(); int version = rcontext.getVersion(); String host = rcontext.getHost(); String typeSocket = rcontext.getTypeSocket(); if (version == SnmpConstants.SNMP_VERSION_2c) { SnmpContextv2c rcontextv2c = (SnmpContextv2c) rcontext; _poolv2c = new SnmpContextv2cPool(host, port, rcontextv2c.getCommunity(), typeSocket); newContext = _poolv2c; } else if (version == SnmpConstants.SNMP_VERSION_3) { SnmpContextv3 rcontextv3 = (SnmpContextv3) rcontext; _poolv3 = new SnmpContextv3Pool(host, port, typeSocket); rcontextv3.cloneParameters(_poolv3); newContext = _poolv3; } else { SnmpContext rcontextv1 = (SnmpContext) rcontext; _poolv1 = new SnmpContextPool(host, port, rcontextv1.getCommunity(), typeSocket); newContext = _poolv1; } return newContext;}public void freeResources(){ if (_testStarted == true) { int len1 = _lcontextList.size(); for (int i=len1-1; i>=0; i--) { ListeningContextPool lcontext = (ListeningContextPool) _lcontextList.remove(i); lcontext.removeUnhandledRawPduListener(this); lcontext.removeRawPduListener(this); } try { int len2 = _scontextList.size(); for (int i=len2-1; i>=0; i--) { SnmpContextBasisFace scontext = (SnmpContextBasisFace) _scontextList.remove(i); scontext.removeRequestPduListener(this, scontext.getPort()); } } catch (IOException exc) { // the xml file should have the proper settings .. _writer.println("Agent.freeResources(): IOException " + exc.getMessage()); exc.printStackTrace(_writer); } if (_poolv1 != null) { _poolv1.destroyPool(); } if (_poolv2c != null) { _poolv2c.destroyPool(); } if (_poolv3 != null) { _poolv3.destroyPool(); } _testStarted = false; }}public void rawPduReceived(RawPduEvent evt){ int version = evt.getVersion(); String host = evt.getHostAddress(); _writer.println("\nrawPduReceived():" + " received unhandled or raw undecoded pdu v" + version + " from host " + host);}public java.lang.String getSnmpEngineId(){ return _engineID;}public int getSnmpEngineBoots(){ return _engineBoots;}public int getSnmpEngineTime(){ long thisTime = (new Date()).getTime(); long deltaMillis = thisTime - _startTime; int seconds = (int) (deltaMillis / 1000); return seconds;}public long getUsmStatsUnknownEngineIDs(){ return 0;}public long getUsmStatsNotInTimeWindows(){ return 0;}public void setSnmpContext(SnmpContextv3Basis context){}public void windowOpened(WindowEvent e) {}public void windowClosed(WindowEvent e) {}public void windowIconified(WindowEvent e) {}public void windowDeiconified(WindowEvent e) {}public void windowActivated(WindowEvent e) {}public void windowDeactivated(WindowEvent e) {}public void windowClosing(WindowEvent e) { this.freeResources(); System.exit(0);}/** * The main method in order to run this as an application. */public static void main(String[] argv){ try { int len = argv.length; if (len > 0) { Agent.XML_FILE = argv[0]; } Agent testS = new Agent(); testS._isStandAlone = true; JFrame frame = new JFrame("Agent Test"); uk.co.westhawk.tablelayout.TableLayout tLayout = new uk.co.westhawk.tablelayout.TableLayout(); frame.addWindowListener(testS); java.awt.Dimension dim = new java.awt.Dimension(500, 150); frame.setSize(dim); frame.setLocation(50, 50); frame.getContentPane().setLayout(tLayout); frame.setVisible(true); testS.init(); testS.start(); } catch (Exception exc) { exc.printStackTrace(); usage(); }}public void printUriDetails(URI uri){ _writer.println("uri.getScheme(): " + uri.getScheme()); _writer.println("uri.getSchemeSpecificPart(): " + uri.getSchemeSpecificPart()); _writer.println("uri.getRawSchemeSpecificPart(): " + uri.getRawSchemeSpecificPart()); _writer.println("uri.getAuthority(): " + uri.getAuthority()); _writer.println("uri.getRawAuthority(): " + uri.getRawAuthority()); _writer.println("uri.getUserInfo() : " + uri.getUserInfo() ); _writer.println("uri.getRawUserInfo(): " + uri.getRawUserInfo()); _writer.println("uri.getHost(): " + uri.getHost()); _writer.println("uri.getPort(): " + uri.getPort()); _writer.println("uri.getPath(): " + uri.getPath()); _writer.println("uri.getRawPath(): " + uri.getRawPath()); _writer.println("uri.getQuery(): " + uri.getQuery()); _writer.println("uri.getRawQuery(): " + uri.getRawQuery()); _writer.println("uri.getFragment(): " + uri.getFragment()); _writer.println("uri.getRawFragment(): " + uri.getRawFragment());}/** * Prints the usage of this application. */public static void usage(){ System.err.println("Usage:"); System.err.println("\t Agent [<xml file>]");}class ScalarNode{ String _name, _oid, _value; public ScalarNode(Element element) { _name = element.getAttribute(NAME); _oid = element.getAttribute(OID); _value = Util.getCDataValue(element, VALUE); } public ScalarNode(String name, String oid, String value) { _name = name; _oid = oid; _value = value; } public String toString() { StringBuffer buffer = new StringBuffer(getClass().getName()); buffer.append("["); buffer.append("name=").append(_name); buffer.append(", oid=").append(_oid); buffer.append(", value=").append(_value); buffer.append("]"); return buffer.toString(); }}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -