📄 monitorasteriskgraph.java
字号:
_mayLoopStart = false; _oldActiveChannelMap = _activeChannelMap; _activeChannelMap = new HashMap(); _activeChannelNameMap = new HashMap(); getActiveChannels(_context, null); } try { Thread.sleep(SLEEPTIME); } catch (InterruptedException ix) { ; } }}/** * Implementing the Observer interface. * Receiving the response from getChannelTypes() or getActiveChannels(). * * @param obs the pdu variable * @param ov the array of varbind (not used) * * @see AsteriskChanTypeTablePdu * @see AsteriskChanTablePdu * @see #getChannelTypes * @see #getActiveChannels */public void update(Observable obs, Object ov){ if (obs instanceof AsteriskChanTypeTablePdu) { handleChannelType(obs, ov); } else { handleActiveChannel(obs, ov); }}protected void handleChannelType(Observable obs, Object ov){ AsteriskChanTypeTablePdu prev; int errStatus = _atPdu.getErrorStatus(); int errIndex = _atPdu.getErrorIndex(); if (errStatus == AsnObject.SNMP_ERR_NOERROR) { prev = _atPdu; // System.out.println(_atPdu.toString()); String name = _atPdu.getAstChanTypeName(); Node cTypeNode = _graph.addNode(); cTypeNode.setString(CTYPE, name); cTypeNode.setBoolean(ISACT, false); _channelTypeMap.put(name, new Integer(cTypeNode.getRow())); getChannelTypes(_context, prev); } else { // This table should always be there, so when this error occurs, // we either got the wrong host or we've looped outside the // table. /* System.out.println("handleChannelType(): " + _atPdu.getErrorStatusString() + " @ errIndex " + errIndex); */ // printNodeTableInfo(); _viz.run(FONT); _viz.run(LAYOUT); _viz.run(COLOUR); _viz.run(REPAINT); startThread(); }}protected void handleActiveChannel(Observable obs, Object ov){ // got answer back on the active channels int errStatus = _aPdu.getErrorStatus(); int errIndex = _aPdu.getErrorIndex(); if (errStatus == AsnObject.SNMP_ERR_NOERROR) { handleActiveChannelUp(obs, ov); } else if (errStatus == AsnObject.SNMP_ERR_NOSUCHNAME) { // It's not easy to distinguish between // - the table isn't there (i.e. not active calls) // - we walked out off the table // - the table is there, but some rows are missing if (errIndex == 0) { // Cannot get the value of astNumChannels, // Mib isn't implemented _mayLoopStart = true; } else if (errIndex == 1) { // Cannot get value of astChanIndex: Integer value = _aPdu.getAstNumChannels(); if (value.intValue() == 0) { // System.out.println("handleActiveChannel(): no active channels"); } else { // we walked out off the table, since if the table // exists, astChanIndex should be available } _mayLoopStart = true; } else { // I assume that just some rows are missing handleActiveChannelUp(obs, ov); } } else { /* System.out.println("handleActiveChannel(): " + _aPdu.getErrorStatusString() + " @ errIndex " + errIndex); */ _mayLoopStart = true; } if (_mayLoopStart == true) { // remove the old ones from the graph, and add the new ones redrawActiveChannelsInGraph(); // System.out.println("handleActiveChannel(): going to sleep"); }}protected void handleActiveChannelUp(Observable obs, Object ov){ AsteriskChanTablePdu prev; prev = _aPdu; // System.out.println(_aPdu.toString()); Integer chanIndexI = _aPdu.getAstChanIndex(); String channelName = _aPdu.getAstChanName(); _activeChannelMap.put(chanIndexI, _aPdu); getActiveChannels(_context, prev);}protected void redrawActiveChannelsInGraph(){ // remove the old active channel nodes IntIterator it; Table nodesTable = _graph.getNodeTable(); it = nodesTable.rows(_activeChannelPred); while (it.hasNext()) { int row = it.nextInt(); _graph.removeNode(row); } // remove all the edges it = _graph.edgeRows(); while (it.hasNext()) { int edge = it.nextInt(); _graph.removeEdge(edge); } // Add the new active channels. // Per active channel: // - find the channel type node // - add the active channel node // - create the edge between channel and type Set keyset; Iterator i; keyset = _activeChannelMap.keySet(); i = keyset.iterator(); while (i.hasNext()) { Integer index = (Integer) i.next(); AsteriskChanTablePdu pdu = (AsteriskChanTablePdu) _activeChannelMap.get(index); // find the channel type node String channelType = pdu.getAstChanType(); Integer channelTypeNodeRowId = (Integer) _channelTypeMap.get(channelType); Node cTypeNode = _graph.getNode(channelTypeNodeRowId.intValue()); // add the active channel node String channelName = pdu.getAstChanName(); Node aChanNode = _graph.addNode(); aChanNode.setString(ACHAN, channelName); aChanNode.setBoolean(ISACT, true); int thisNodeIndex = aChanNode.getRow(); _activeChannelNameMap.put(channelName, new Integer(thisNodeIndex)); // create the edge between channel and type // source, target Edge edge1 = _graph.addEdge(aChanNode, cTypeNode); edge1.setBoolean(ISACT, false); } // create the edges between the active channels // I can only do that after all the (new) nodes have been created keyset = _activeChannelMap.keySet(); i = keyset.iterator(); while (i.hasNext()) { Integer index = (Integer) i.next(); AsteriskChanTablePdu pdu = (AsteriskChanTablePdu) _activeChannelMap.get(index); String channelName = pdu.getAstChanName(); String bridgedChan = pdu.getAstChanBridge(); Object nameNodeO = _activeChannelNameMap.get(channelName); Object bridgedChanO = _activeChannelNameMap.get(bridgedChan); // create the edge between two active channels if (nameNodeO != null && bridgedChanO != null) { Integer nameNodeInd = (Integer) nameNodeO; Integer bridgedChanInd = (Integer) bridgedChanO; Node nodeA = _graph.getNode(nameNodeInd.intValue()); Node nodeB = _graph.getNode(bridgedChanInd.intValue()); Edge edge2 = _graph.addEdge(nodeA, nodeB); edge2.setBoolean(ISACT, false); Edge edge3 = _graph.addEdge(nodeB, nodeA); edge3.setBoolean(ISACT, false); } } _viz.run(FONT); _viz.run(LAYOUT); _viz.run(COLOUR); _viz.run(REPAINT);}protected void showActiveChannelInfo(String channelName){ Set keyset; Iterator i; keyset = _activeChannelMap.keySet(); i = keyset.iterator(); while (i.hasNext()) { Integer index = (Integer) i.next(); AsteriskChanTablePdu pdu = (AsteriskChanTablePdu) _activeChannelMap.get(index); String thisChannelName = pdu.getAstChanName(); if (channelName.equals(thisChannelName)) { System.out.println(pdu.toString()); } }}private void printNodeTableInfo(){ Table nodesTable = _graph.getNodeTable(); TableIterator it = nodesTable.iterator(); System.out.println(getClass().getName() + ".printNodeTableInfo():"); while (it.hasNext()) { int row = it.nextInt(); Tuple t = nodesTable.getTuple(row); int cno = t.getColumnCount(); System.out.println("row=" + row + ", #col=" + cno); for (int col=0; col<cno; col++) { String name = t.getColumnName(col); Class cl = t.getColumnType(col); System.out.println("row=" + row + ", col=" + col + ": name=" + name + ", class=" + cl.getName()); } } int nno = _graph.getNodeCount(); for (int i=0; i<nno; i++) { int ind = _graph.getNodeIndex(i); Node n = _graph.getNodeFromKey(i); System.out.println("i=" + i + ", ind=" + ind + ", node=" + n.toString()); }}private void printMap(Map map){ Set keyset = map.keySet(); Iterator i = keyset.iterator(); while (i.hasNext()) { Object key = i.next(); Object value = map.get(key); System.out.println(key.toString() + " = " + value.toString()); }}public void freeResources(){ if (_context != null) { _mayLoopStart = false; _context.destroy(); _context = null; }}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);}/** * Main. To use a properties file different from * <code>MonitorAsteriskGraph.properties</code>, pass the name as first argument. */public static void main(String[] args){ String propFileName = null; if (args.length > 0) { propFileName = args[0]; } MonitorAsteriskGraph application = new MonitorAsteriskGraph(propFileName); JFrame frame = new JFrame(); frame.setTitle(application.getClass().getName()); frame.getContentPane().add(application, BorderLayout.CENTER); application.init(); frame.addWindowListener(application); Dimension dim = new Dimension(600, 600); frame.setSize(dim); application.setSize(dim); frame.setVisible(true); application.start();}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -