Package systeminformationmonitor.system

Source Code of systeminformationmonitor.system.NetStat

/*
*  Copyright 2009 Leo Xu.
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*  under the License.
*/
package systeminformationmonitor.system;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hyperic.sigar.NetConnection;
import org.hyperic.sigar.NetFlags;
import org.hyperic.sigar.SigarException;
import systeminformationmonitor.swing.model.ConnectionTableModel;
import systeminformationmonitor.system.object.NetStatObject;

/**
* NetStat class generates information regarding current network status.
* @author Leo Xu
*/
/**
* Display network connections.
*/
public class NetStat extends AbstractSystemUpdater {

    private static final int LADDR_LEN = 20;
    private static final int RADDR_LEN = 35;
    private static final boolean isNumeric = false;
    private static final boolean wantPid = false;

    /**
     * Construct an empty NetStat Object
     */
    public NetStat() {
  
    }

    @Override
    public void update() {
        ConnectionTableModel instace = ConnectionTableModel.getInstance();
        Vector<NetStatObject> temp = getNetStat();

        threadLock.lock();
        try {           
            instace.setRowData(temp);
            instace.fireTableDataChanged();
        } finally {
            threadLock.unlock();           
        }
    }

    private Vector<NetStatObject> getNetStat() {
        //default flag
        int flags = NetFlags.CONN_CLIENT | NetFlags.CONN_PROTOCOLS;
        NetConnection[] connections = new NetConnection[0];
        Vector netVector = new Vector<NetStatObject>(30);
        try {
            connections = sigar.getNetConnectionList(flags);
        } catch (SigarException ex) {
            Logger.getLogger(NetStat.class.getName()).log(Level.SEVERE, null, ex);
        }

        for (int i = 0; i < connections.length; i++) {
            NetConnection conn = connections[i];
            NetStatObject netObj = new NetStatObject();

            String proto = conn.getTypeString();
            String state;
            if (conn.getType() == NetFlags.CONN_UDP) {
                state = "";
            } else {
                state = conn.getStateString();
            }


            netObj.setType(proto);
            netObj.setLocalAddress(formatAddress(conn.getType(),
                    conn.getLocalAddress(),
                    conn.getLocalPort(),
                    LADDR_LEN));
            netObj.setRemoteAddress(formatAddress(conn.getType(),
                    conn.getRemoteAddress(),
                    conn.getRemotePort(),
                    RADDR_LEN));
            netObj.setState(state);

            String process = null;
            if (wantPid &&
                    //XXX only works w/ listen ports
                    (conn.getState() == NetFlags.TCP_LISTEN)) {
                try {
                    long pid =
                            sigar.getProcPort(conn.getType(),
                            conn.getLocalPort());
                    if (pid != 0) { //XXX another bug
                        String name =
                                sigar.getProcState(pid).getName();
                        process = pid + "/" + name;
                    }
                } catch (SigarException e) {
                }
            }

            if (process == null) {
                process = "";
            }

            netObj.setProcess(process);

            netVector.add(netObj);
        }       
        return netVector;
    }

    private String formatPort(int proto, long port) {
        if (port == 0) {
            return "*";
        }
        if (!isNumeric) {
            String service = sigar.getNetServicesName(proto, port);
            if (service != null) {
                return service;
            }
        }
        return String.valueOf(port);
    }

    private String formatAddress(int proto, String ip,
            long portnum, int max) {

        String port = formatPort(proto, portnum);
        String address;

        if (NetFlags.isAnyAddress(ip)) {
            address = "*";
        } else if (isNumeric) {
            address = ip;
        } else {
            try {
                address = InetAddress.getByName(ip).getHostName();
            } catch (UnknownHostException e) {
                address = ip;
            }
        }

        max -= port.length() + 1;
        if (address.length() > max) {
            address = address.substring(0, max);
        }

        return address + ":" + port;
    }
}

TOP

Related Classes of systeminformationmonitor.system.NetStat

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.