📄 systemstatus.java
字号:
} // end of SystemStatus constructor private void sendSNMPStart(OidNode node) { System.out.println("---TRACE---SystemStatus:sendSNMPStart"); String requestString = ("START_REQ," + node.getDescriptorString() + "\n"); VPPTransactionWrapper connection = SNMPApplet.getConnectionInstance(); try { String response = connection.request("START_REQ", requestString); if ((response == null) || (response.equals(""))) { System.out.println("No response to Start request"); return; } else { try { decipherResponse("START_REQ", response); } catch (SAXException e1) { throw new VPPException(e1.getMessage() + " from SAXException"); } catch (IOException e2) { throw new VPPException(e2.getMessage() + " from IOException"); } } } catch (VPPException e) { VPPTransactionWrapper.showVPPException(e, "Start request failed"); } } private void decipherResponse(String requestType, String response) throws SAXException, IOException { if ((response == null) || (response.trim().equals(""))) { System.out.println("empty response to " + requestType); return; } BufferedReader reader = new BufferedReader(new StringReader(response)); SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser parser = null; try { parser = spf.newSAXParser(); } catch (ParserConfigurationException parsEx) { parsEx.printStackTrace(); } catch(SAXException saxEx) { saxEx.printStackTrace(); } parser.parse(new InputSource(reader), handler); } private void sendSNMPStop(OidNode node) { System.out.println("---TRACE---SystemStatus:sendSNMPStop"); String requestString = ("STOP_REQ," + node.getDescriptorString() + "\n"); System.out.println(requestString); VPPTransactionWrapper connection = SNMPApplet.getConnectionInstance(); try { String response = connection.request("STOP_REQ", requestString); if (response == null) { System.out.println("No response to STOP_REQ"); } else { try { decipherResponse("STOP_REQ", response); } catch (SAXException e1) { throw new VPPException(e1.getMessage() + " from SAXException"); } catch (IOException e2) { throw new VPPException(e2.getMessage() + " from IOException"); } } } catch (VPPException e) { VPPTransactionWrapper.showVPPException(e, "Stop request failed"); } } /** * Checks whether this parent has a child node with this identifier string. * * @param parent The parent whose children we are checking. * @param identifier The identifier we are checking for. * @return The child that contains identifier, or null if no child * contains identifier. */ public DefaultMutableTreeNode checkIdentifier(DefaultMutableTreeNode parent, String identifier) { DefaultMutableTreeNode node = null; for (Enumeration e = parent.children(); e.hasMoreElements(); ) { node = (DefaultMutableTreeNode) (e.nextElement()); if (node == null) { throw new RuntimeException("Current child is null"); } OidNode nd = (OidNode) node.getUserObject(); String nodeId = nd.getIdentifier(); if (nodeId.equals(identifier)) { return node; } } return null; } /** * createNodes : Builds the nodes dynamically * @param ip, ip address of the node (or could be hostname) * @param port, port of the node * @param type, name of the node * @param status, represents the status of the node */ public void createNodes(String ip, String port, String type, String status) { DefaultMutableTreeNode ipNode = null; DefaultMutableTreeNode portNode = null; DefaultMutableTreeNode statusNode = null; ipNode = checkIdentifier(top, ip); if (ipNode == null) { /* * create the new IP node */ ipNode = createAndAddChild(top, ip, "HOST:" + ip, OidNode.HOST); // make the ip node be displayed at the start m_tree.expandPath(new TreePath(top)); /* * create the new port node */ portNode = createAndAddChild(ipNode, port, type + ":" + port, OidNode.PORT); ((OidNode) (portNode.getUserObject())).setDescriptorString(ip, port, type); /* * create the new status node */ createAndAddChild(portNode, null, status, OidNode.STATUS); updateMessage(type, port, status, ip); } else { OidNode userObject = null; /* * MATCH FOUND FOR IP */ portNode = checkIdentifier(ipNode, port); if (portNode == null) { /* * create the new port node */ portNode = createAndAddChild(ipNode, port, type + ":" + port, OidNode.PORT); ((OidNode) (portNode.getUserObject())).setDescriptorString(ip, port, type); /* * create the new status node */ statusNode = createAndAddChild(portNode, null, status, OidNode.STATUS); userObject = (OidNode) (statusNode.getUserObject()); updateMessage(type, port, status, ip); } else { statusNode = (DefaultMutableTreeNode) (portNode.getChildAt(0)); userObject = (OidNode) statusNode.getUserObject(); /* * Check if the status has changed */ String oldStatus = userObject.toString(); if (oldStatus.equals(status)) { return; } /* * They are Different */ userObject.setDisplayString(status); System.out.println("\n status.setName() = " + userObject); if (statusNode != null) { m_model.nodeChanged(statusNode); } if (portNode != null) { m_model.nodeChanged(portNode); } if (ipNode != null) { m_model.nodeChanged(ipNode); } updateMessage(type, port, status, ip); } } } /** * * @param parent * @param id * @param displayString * @param type ROOT, HOST, PORT, or STATUS * * @return */ private DefaultMutableTreeNode createAndAddChild(DefaultMutableTreeNode parent, String id, String displayString, int type) { OidNode oNode = new OidNode(id, parent, type); oNode.setDisplayString(displayString); DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(oNode); m_model.insertNodeInto(childNode, parent, 0); return childNode; } /** * updateMessage : Update the GUI with the new messages received */ public void updateMessage(String type, String port, String status, String ip) { java.util.Calendar cal; String textStr = " "; String redString = " "; String DATE_FORMAT = "MM/dd/yyyy HH:mm:ss"; java.text.SimpleDateFormat sdf; cal = Calendar.getInstance(TimeZone.getDefault()); sdf = new java.text.SimpleDateFormat(DATE_FORMAT); sdf.setTimeZone(TimeZone.getDefault()); /* * Display the latest message in the text field of the GUI screen */ m_display.setText("Server name : " + type + " : " + port + " , Status : " + status); textStr = messagePane.getText(); /* * Append the new message in the message pane of the GUI screen */ messagePane.setText(sdf.format(cal.getTime()) + " --> " + ip + " " + type + " : " + port + " " + status + "\n" + textStr); } /** * interface to the SNMP manager * @param ip The IP address of the device that changed. * @param port The port number of the device that changed. * @param type The type of the device that changed, for example "ms" or "fs". * @param status The new status of the device that changed, * for example "active" or "inactive". */ public void changeEvent(String ip, String port, String type, String status) { createNodes(ip, port, type, status); } public void stop() { try { SNMPApplet.getConnectionInstance().close(); } catch (VPPException e) { System.out.println("Error closing connection: " + e.getMessage()); } run = false; if ((theApplet == null) || (theApplet.isStandalone)) { // uncomment for debug // System.out.println("CALLING SYSTEM EXIT"); System.exit(0); } // uncomment for debug // System.out.println("STOPPING APPLET"); theApplet.stop(); } public void start() throws SNMPClientException { run = true; VPPTransactionWrapper connection = SNMPApplet.getConnectionInstance(); String response; String request, message; request = "CONFIG_REQ"; while (run) { try { message = request + "," + versionNumber + ",0\n"; // uncomment for debugging // System.out.println("Sending request " + request + message); synchronized (this) { response = connection.request(request, message); } } catch (VPPException e) { VPPTransactionWrapper.showVPPException(e, "Status request failed"); return; } // if the connection timed out or the response could not be gotten for // some other reason, the response will be null. if (response == null) { System.out.println("---TRACE---SNMPApplet:init - no response to " + request + " " + message); } else { try { decipherResponse(request, response); } catch (SAXException e1) { throw new SNMPClientException(e1.getMessage() + " from SAXException"); } catch (IOException e2) { throw new SNMPClientException(e2.getMessage() + " from IOException"); } try { int time = 500; // uncomment for debugging // System.out.println("Sleeping for " + time + " ms"); Thread.sleep(time); } catch (InterruptedException e) { throw new SNMPClientException("sleep unexpectedly interrupted: " + e.getMessage()); } } // end else (response received) } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -