import java.io.*;//this is so we can do file manipulation
import java.util.*;         //this is so we can use Vectors




public class ProcParser{
    private Vector Processes = new Vector();
    private Vector Sockets = new Vector();
    private Vector ProcessSockets = new Vector();

    public ProcParser(){
	getProcesses();         //these two are solely for debuging purposes at the moment
	getSockets();
	         //LinuxSocket ls = new LinuxSocket();
	         //ls.update();
    }

    public Vector getProcesses(){
	Process process = null;          //this is a Process object defined by Process.class
	Vector procies = new Vector();          //this is a Vector of Process objects
	File dir = new File("/proc");           //open the /proc directory for reading all the process numbers
	ProcessSockets.removeAllElements();         //remove all of the old Scokets
	String[] procs = dir.list();         //read the /proc directory
	for (int i = 0; i < procs.length;i++){          //for every entry in the directory get the process information
	    try {          //if the directory entry is not a number then it's not a PID so bail out
		Integer.valueOf(procs[i]);          //check to see if entry is a number throws Number format exception if it isn't
		process = new Process(procs[i]);         //if it passes the test make a new process useing only it's PID
		for (int j = 0; j < process.getSockets().size();j++){         //get the number of Sockets connected to the process
		    ProcessSockets.addElement(process.getSockets().elementAt(j));         //for each one add it to our vector of processes
		}
		procies.addElement(process);         //add each process to our vector of processes
		System.out.println(process);         //print out each process
	    }
	    catch (NumberFormatException nfe){
		         //this is our bail point if it's not a number. Don't do anything.
	    }
	}          //move on to the next directory entry
	
	System.out.println("Number of Processes = "+procies.size()+" Number or Sockets = "+Sockets.size());
	return procies;          //return our new  vector of processes
	
    }
    public Vector getDevices(){
	return new Vector();
    }
    public Vector getSockets(){
	Vector Inodes = new Vector();          //this is our vector of Inodes from /proc/net/[tcp,unix,raw,udp] we use this so we can close the file quickly
	Vector Sockets = new Vector();          //this is our internal vector of Sockets
	FileReader tcp = null;          //the file reader for the /proc/net/tcp file
	FileReader unix = null;         //the file reader for the /proc/net/unix file
	FileReader udp = null;         //the file reader for the /proc/net/udp file
	FileReader raw = null;         //the file reader for the /proc/net/raw file
	LineNumberReader lnr = null;         //A simple linenumber reader so we can work line by line easily
	String line = null;         //this is our temp string where the output of LineNumberReader.readLine() goes
	int i = 0;         //this is so we can keep track of where we are in the list of inodes
	try {          //this watches to make sure that the file we are looking fo is there
	    tcp = new FileReader("/proc/net/tcp");          //open file for reading
	    lnr = new LineNumberReader(tcp);         //wrap it in a line number reader
	    try {         // watch to make sure we have something more to read
		while (true){         //keep looping till we throw an exception
		    line = lnr.readLine();         //read a line
		    try {         //catch any text we might get from the header "inode" esp.
			Inodes.addElement(Integer.valueOf(line.substring(90,100).trim()));         //add an inode to the list
		    } catch (NumberFormatException nfe){          //do nothing if we get text
		    }
		}
		
	    } catch (IOException ioe){         //do nothing if we reach the end of the file automatically closes it
	    } catch (NullPointerException npe){         //make sure we have some data to read do nothing if we don't
	    }
	    for (;i<Inodes.size();i++)         //loop thru each inode so far
		Sockets.addElement(new LinuxSocket(((Integer)Inodes.elementAt(i)).intValue(),1));         //make a new socket out of each inode and add it to our list of sockets
	    udp = new FileReader("/proc/net/udp");         //open the udp table for reading
	    lnr = new LineNumberReader(udp);         //same as above, could put all this in one big for loop
	    try {
		while (true){
		    line = lnr.readLine();
		    try {
			Inodes.addElement(Integer.valueOf(line.substring(90,100).trim()));
		    } catch (NumberFormatException nfe){
		    }
		}
	    } catch (IOException ioe){
	    } catch (NullPointerException npe){
	    }
	    for (;i<Inodes.size();i++)
		Sockets.addElement(new LinuxSocket(((Integer)Inodes.elementAt(i)).intValue(),2));
	    raw = new FileReader("/proc/net/raw");
	    lnr = new LineNumberReader(raw);
	    try {
		while (true){
		    line = lnr.readLine();
		    try {
			Inodes.addElement(Integer.valueOf(line.substring(90,100).trim()));
		    } catch (NumberFormatException nfe){
		    }
		}
	    } catch (IOException ioe){
	    } catch (NullPointerException npe){
	    }
	    for (;i<Inodes.size();i++)
		Sockets.addElement(new LinuxSocket(((Integer)Inodes.elementAt(i)).intValue(),3));
	    unix = new FileReader("/proc/net/unix");
	    lnr = new LineNumberReader(unix);
	    try {
		while (true){
		    line = lnr.readLine();
		    try {
			Inodes.addElement(Integer.valueOf(line.substring(45,50).trim()));         //the unix protocal file has this in a differnt layout
		    } catch (NumberFormatException nfe){
		    }
		}
	    } catch (IOException ioe){
	    } catch (NullPointerException npe){
	    }
	    for (;i<Inodes.size();i++)
		Sockets.addElement(new LinuxSocket(((Integer)Inodes.elementAt(i)).intValue(),4));
	} catch (FileNotFoundException fnfe){         //bail out if the file we are looking for doesn't exist
	}
	
	System.out.println("Number of Sockets = "+Sockets.size()+" Inodes size = "+Inodes.size());
	return Sockets;         //  return all of our newly created sockets
    }
    public Vector getNetDevices(){
	return new Vector();
    }
    /*  public MemInfo getMemInfo(){
    }
    public CpuInfo getCpuInfo(){
    }
    public OsInfo getOsInfo(){
    }*/
}
