package com.sohu.tv.mq.cloud.task.server.data;

import org.apache.commons.lang3.math.NumberUtils;

/**
 * tcp连接
 * 
 * @Description:
 * @author yongfeigao
 * @date 2018年7月18日
 */
public class Connection implements LineParser {
    public static final String FLAG = "TCP";
    // 链接数量
    private int established;
    // 等待数量
    private int timeWait;
    // 孤儿数量
    private int orphan;

    /**
     * line format: TCP: inuse 454 orphan 0 tw 159620 alloc 454 mem 79
     */
    public void parse(String line, String timeKey) throws Exception {
        if (line.startsWith(FLAG)) {
            String[] items = line.split("\\s+");
            for (int i = 0; i < items.length; ++i) {
                if (items[i].equals("alloc")) {
                    established = NumberUtils.toInt(items[i + 1]);
                } else if (items[i].equals("orphan")) {
                    orphan = NumberUtils.toInt(items[i + 1]);
                } else if (items[i].equals("tw")) {
                    timeWait = NumberUtils.toInt(items[i + 1]);
                }
            }
        }
    }

    public int getEstablished() {
        return established;
    }

    public int getTimeWait() {
        return timeWait;
    }

    public int getOrphan() {
        return orphan;
    }
}
