package demo.main.snmp;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;
import twaver.*;
import twaver.network.TNetwork;
import twaver.network.background.ColorBackground;
import twaver.tree.TTree;
public class DiscoveryDialog extends JDialog {
private TNetwork network = null;
private TTree tree = null;
private JButton btnStart = new JButton("Start");
private JButton btnCancel = new JButton("Hide");
private TNetwork gridNetwork = new TNetwork();
private Node centerNode = null;
private Vector<ResizableNode> balls = null;
private int threadCount = 10;
private int runningThreadCount = 0;
public DiscoveryDialog(Frame parent, TNetwork network, TTree tree) {
super(parent, true);
this.network = network;
this.tree = tree;
this.setTitle("Explore Network");
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
JPanel centerPane = new JPanel();
JPanel buttonPane = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10));
this.getContentPane().add(centerPane, BorderLayout.CENTER);
this.getContentPane().add(buttonPane, BorderLayout.SOUTH);
buttonPane.add(this.btnStart);
buttonPane.add(this.btnCancel);
this.btnStart.setMnemonic('S');
this.btnCancel.setMnemonic('C');
this.getRootPane().setDefaultButton(this.btnStart);
this.btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
centerPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 10));
centerPane.setLayout(new BorderLayout());
centerPane.add(gridNetwork, BorderLayout.CENTER);
gridNetwork.setToolbar(null);
gridNetwork.setInteractionMode(null);
gridNetwork.setNetworkBackground(new ColorBackground(Color.white));
for (int i = 1; i < 255; i++) {
Element element = createBall(i);
this.gridNetwork.getDataBox().addElement(element);
}
this.btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
discovery();
}
});
this.setSize(520, 260);
TWaverUtil.centerWindow(this);
}
private Element createBall(int index) {
int size = 12;
int gap = 15;
int inset = 10;
int columnCount = 30;
ResizableNode node = new ResizableNode(index);
node.setSize(size, size);
node.putCustomDraw(true);
node.putCustomDrawShapeFactory(TWaverConst.SHAPE_CIRCLE);
node.putCustomDrawAntialias(true);
node.putCustomDrawFill(true);
node.putCustomDrawGradient(true);
node.putCustomDrawGradientFactory(TWaverConst.GRADIENT_RADIAL_N);
node.putCustomDrawGradientColor(Color.white);
node.putCustomDrawFillColor(Color.orange);
node.putCustomDrawOutline(false);
node.putBorderVisible(false);
int row = index / columnCount;
int column = index % columnCount;
node.setLocation(inset + column * gap, inset + row * gap);
return node;
}
private String getNetworkSegmentPrefix() {
try {
InetAddress addr = InetAddress.getLocalHost();
String localHostName = addr.getHostAddress();
String netPrefix = localHostName.substring(0, localHostName.lastIndexOf("."));
return netPrefix;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
private synchronized ResizableNode pickBall() {
if (!balls.isEmpty()) {
int index = TWaverUtil.getRandomInt(balls.size());
ResizableNode ball = balls.remove(index);
return ball;
}
return null;
}
private synchronized void threadStart() {
runningThreadCount++;
}
private synchronized void threadStop() {
runningThreadCount--;
}
private synchronized boolean exploreFinished() {
return runningThreadCount == 0;
}
private void discovery() {
this.btnStart.setEnabled(false);
setupNetwork();
balls = new Vector(this.gridNetwork.getDataBox().getAllElements());
final String netPrefix = getNetworkSegmentPrefix();
for (int i = 0; i < threadCount; i++) {
Thread thread = new Thread() {
@Override
public void run() {
threadStart();
try {
ResizableNode ball = pickBall();
while (ball != null) {
final ResizableNode element = ball;
int index = (Integer) ball.getID();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
element.putCustomDrawFillColor(Color.red);
}
});
final String ipaddress = netPrefix + "." + index;
boolean pingOK = SnmpDemo.ping(ipaddress);
if (pingOK) {
final boolean snmpPingOK = SnmpDemo.snmpPing(ipaddress);
final String name = InetAddress.getByName(ipaddress).getHostName();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createComputer(ipaddress, snmpPingOK, name, element);
}
});
} else {
ball.putCustomDrawFillColor(Color.gray);
}
ball = pickBall();
}
threadStop();
//if finish, close window, layout network.
if (exploreFinished() && isShowing()) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (!btnStart.isEnabled()) {
btnStart.setEnabled(true);
dispose();
tree.expandAll();
}
}
});
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
};
thread.start();
}
}
private void createComputer(String ipaddress, boolean snmpPingOK, String name, ResizableNode element) {
ResizableNode node = new ResizableNode(ipaddress);
node.setImage("/demo/main/snmp/images/node.png");
node.addAttachment("winxp");
node.putAttachmentPosition(TWaverConst.POSITION_TOPLEFT);
if (snmpPingOK) {
node.addAttachment("snmp");
}
node.setName(name);
node.setToolTipText(ipaddress);
int x = TWaverUtil.getRandomInt(500);
int y = TWaverUtil.getRandomInt(500);
node.setLocation(new Point(x, y));
node.putBorderVisible(false);
network.getDataBox().addElement(node);
Link link = new Link(centerNode, node);
link.putLinkOutlineWidth(0);
link.putLinkWidth(1);
link.putBorderVisible(false);
link.putLinkColor(Color.cyan.darker());
network.getDataBox().addElement(link);
element.putCustomDrawFillColor(Color.green.darker());
}
private void setupNetwork() {
try {
network.getDataBox().cle