use Map rather than List to keep track of addresses

This commit is contained in:
Doug MacEachern 2005-03-15 23:19:07 +00:00
parent 9abeb7ef2b
commit db88e7f04a
1 changed files with 24 additions and 5 deletions

View File

@ -28,6 +28,18 @@ public class NetPortMap {
} }
} }
public static class IpEntry {
int count;
IpEntry() {
this.count = 0;
}
public String toString() {
return "count:" + this.count;
}
}
private static void parseServices(String type, Map services) { private static void parseServices(String type, Map services) {
File file = new File(SERVICE_FILE); File file = new File(SERVICE_FILE);
if (!file.exists()) { if (!file.exists()) {
@ -119,10 +131,10 @@ public class NetPortMap {
for (int i=0; i<connections.length; i++) { for (int i=0; i<connections.length; i++) {
NetConnection conn = connections[i]; NetConnection conn = connections[i];
Long port = new Long(conn.getLocalPort()); Long port = new Long(conn.getLocalPort());
List addresses = (List)map.get(port); Map addresses = (Map)map.get(port);
if (addresses == null) { if (addresses == null) {
addresses = new ArrayList(); addresses = new HashMap();
map.put(port, addresses); map.put(port, addresses);
} }
} }
@ -130,17 +142,24 @@ public class NetPortMap {
//second pass, get addresses connected to listening ports //second pass, get addresses connected to listening ports
flags = NetFlags.CONN_CLIENT | NetFlags.CONN_TCP; flags = NetFlags.CONN_CLIENT | NetFlags.CONN_TCP;
connections = sigar.getNetConnectionList(flags); connections = sigar.getNetConnectionList(flags);
IpEntry key = new IpEntry();
for (int i=0; i<connections.length; i++) { for (int i=0; i<connections.length; i++) {
NetConnection conn = connections[i]; NetConnection conn = connections[i];
Long port = new Long(conn.getLocalPort()); Long port = new Long(conn.getLocalPort());
List addresses = (List)map.get(port); Map addresses = (Map)map.get(port);
if (addresses == null) { if (addresses == null) {
continue; continue;
} }
addresses.add(conn.getRemoteAddress()); String ip = conn.getRemoteAddress();
IpEntry entry = (IpEntry)addresses.get(ip);
if (entry == null) {
entry = new IpEntry();
addresses.put(ip, entry);
}
entry.count++;
} }
return map; return map;
@ -155,7 +174,7 @@ public class NetPortMap {
{ {
Map.Entry entry = (Map.Entry)it.next(); Map.Entry entry = (Map.Entry)it.next();
Long port = (Long)entry.getKey(); Long port = (Long)entry.getKey();
List addresses = (List)entry.getValue(); Map addresses = (Map)entry.getValue();
System.out.println(port + "=" + addresses); System.out.println(port + "=" + addresses);
} }
} }