refactor. include state counts

This commit is contained in:
Doug MacEachern 2005-05-07 06:02:25 +00:00
parent 204041aed7
commit 9cfbd13722
1 changed files with 64 additions and 26 deletions

View File

@ -12,6 +12,10 @@ import java.util.StringTokenizer;
public class NetPortMap { public class NetPortMap {
private Sigar sigar;
private Map clients;
private int[] states;
private static final String SERVICE_FILE; private static final String SERVICE_FILE;
private static Map udpServices = null; private static Map udpServices = null;
@ -31,6 +35,10 @@ public class NetPortMap {
System.getProperty("sigar.net.services.file", defaultFile); System.getProperty("sigar.net.services.file", defaultFile);
} }
public NetPortMap() {
this.sigar = new Sigar();
}
public static class IpEntry { public static class IpEntry {
int count; int count;
@ -114,43 +122,52 @@ public class NetPortMap {
return (String)udpServices.get(new Long(port)); return (String)udpServices.get(new Long(port));
} }
/** public void stat() throws SigarException {
* Map listening tcp ports to connected remote addresses.
* key == Listening tcp port on the local machine.
* value == List of connected remote addresses.
*/
public static Map getTcpConnections(Sigar sigar)
throws SigarException {
int flags = int flags =
NetFlags.CONN_SERVER | NetFlags.CONN_TCP; NetFlags.CONN_SERVER | NetFlags.CONN_CLIENT |
NetFlags.CONN_TCP;
stat(flags);
}
Map map = new HashMap(); public void stat(int flags) throws SigarException {
this.clients = new HashMap();
this.states = new int[NetFlags.TCP_UNKNOWN];
for (int i=0; i<this.states.length; i++) {
this.states[i] = 0;
}
NetConnection[] connections = NetConnection[] connections =
sigar.getNetConnectionList(flags); this.sigar.getNetConnectionList(flags);
//first pass, get listening port numbers
for (int i=0; i<connections.length; i++) { for (int i=0; i<connections.length; i++) {
NetConnection conn = connections[i]; NetConnection conn = connections[i];
int state = conn.getState();
this.states[state]++;
//first pass, get listening port numbers
if (state == NetFlags.TCP_LISTEN) {
Long port = new Long(conn.getLocalPort()); Long port = new Long(conn.getLocalPort());
Map addresses = (Map)map.get(port); Map addresses = (Map)this.clients.get(port);
if (addresses == null) { if (addresses == null) {
addresses = new HashMap(); addresses = new HashMap();
map.put(port, addresses); this.clients.put(port, addresses);
}
} }
} }
//second pass, get addresses connected to listening ports //second pass, get addresses connected to listening ports
flags = NetFlags.CONN_CLIENT | NetFlags.CONN_TCP;
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];
if (conn.getState() == NetFlags.TCP_LISTEN) {
continue;
}
Long port = new Long(conn.getLocalPort()); Long port = new Long(conn.getLocalPort());
Map addresses = (Map)map.get(port);
Map addresses = (Map)this.clients.get(port);
if (addresses == null) { if (addresses == null) {
continue; continue;
@ -164,13 +181,27 @@ public class NetPortMap {
} }
entry.count++; entry.count++;
} }
}
return map; /**
* Map listening tcp ports to connected remote addresses.
* key == Listening tcp port on the local machine.
* value == List of connected remote addresses.
*/
public Map getTcpClientConnections() {
return this.clients;
}
public int[] getStates() {
return this.states;
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
Sigar sigar = new Sigar(); NetPortMap map = new NetPortMap();
Map ports = getTcpConnections(sigar); map.stat();
System.out.println("Client Connections...");
Map ports = map.getTcpClientConnections();
for (Iterator it = ports.entrySet().iterator(); for (Iterator it = ports.entrySet().iterator();
it.hasNext();) it.hasNext();)
@ -180,5 +211,12 @@ public class NetPortMap {
Map addresses = (Map)entry.getValue(); Map addresses = (Map)entry.getValue();
System.out.println(port + "=" + addresses); System.out.println(port + "=" + addresses);
} }
System.out.println("\nStates...");
int[] states = map.getStates();
for (int i=1; i<states.length; i++) {
System.out.println(NetConnection.getStateString(i) + "=" +
states[i]);
}
} }
} }