}
public void addToMap(final java.util.Map<InetAddress, PingMethod> hosts, final Map map) {
if (hosts == null || hosts.isEmpty() || map == null) return;
// TODO devices are added at [0,0], find way to layout properly!
Connection gToS = new Connection();
Device gateway = null;
Device aSwitch = null;
NetworkIF gatewayIF = null;
try { // try to find the gateway
gatewayIF = tryFindIP(map, InetAddress.getByName(subnet.getInfo().getLowAddress()));
} catch (UnknownHostException ex) {
Logger.debug("Unable to find gateway-interface", ex);
}
if (gatewayIF == null) { // create a gateway if none was found
gateway = new Host(Type.Router);
gatewayIF = new PhysicalIF(gateway, gToS, subnet.getInfo().getLowAddress());
map.addVertex(gateway);
gateway.addInterface(gatewayIF);
} else {
gateway = gatewayIF.getDevice();
}
try { // try to find the switch attached to the gateway
aSwitch = map.getOpposite(gateway, gatewayIF.getConnection());
// if the opposite is not a switch, reset
if (aSwitch != null && !aSwitch.getType().toString().toLowerCase().contains("switch")) aSwitch = null;
} catch (Exception e) {
Logger.debug("Unable to find opposite", e);
}
if (aSwitch == null) { // create a switch if necessary
aSwitch = new Host(Type.Switch);
map.addVertex(aSwitch);
aSwitch.addInterface(new TransparentIF(aSwitch, gToS, gatewayIF));
map.addEdge(gToS, gateway, aSwitch);
}
final Device gw = gateway, sw = aSwitch;
final NetworkIF gif = gatewayIF;
for (final InetAddress address : hosts.keySet()) {
Scheduler.execute(new Runnable() {
@Override
public void run() {
try {
if (!address.equals(gif.getAddress())) {
if (tryFindIP(map, address) != null) return;
Logger.debug("Adding Interface "+address);
Connection c = new Connection();
Device d = new Host();
d.setName(address.getHostName());
PhysicalIF pif = new PhysicalIF(d, c, address.getHostAddress());
pif.setPingMethod(hosts.get(address));
pif.setSubnet(subnet.getInfo().getNetmask());