package org.bench4q.monitor.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.ExecutionException;
import java.text.SimpleDateFormat;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.hyperic.sigar.SigarException;

import com.google.gson.annotations.Expose;

@XmlRootElement(name = "history")
public class MonitorMain {
	@XmlElement
	private String date;
	@XmlElement(name = "processor_info")
	private ProcessorModel processorModel;
	@XmlElement(name = "memory_info")
	private MemoryModel memoryModel;
	@XmlElement(name = "disk_info")
	private PhysicalDiskModel physicalDiskModel;
	@Expose
	@XmlElement(name = "network_info")
	private NetworkInterfaceModel networkInterfaceModel;
	@XmlElement(name = "process_info")
	private ProcessModel processModel;
	private SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd-HH-mm-ss");

	public MonitorMain() throws SigarException, InterruptedException,
			ExecutionException {
		dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		this.date = dateFormat.format(new Date());
		long startTime = System.currentTimeMillis();
		List<Thread> threadList = new ArrayList<Thread>();
		threadList.add(new Thread(new ProcessModel(this)));
		threadList.add(new Thread(new PhysicalDiskModel(this)));
		threadList.add(new Thread(new ProcessorModel(this)));
		threadList.add(new Thread(new NetworkInterfaceModel(this)));
		for (Thread thread : threadList)
			thread.start();
		memoryModel = new MemoryModel();
		boolean threadIsAlive = true;
		while (threadIsAlive) {
			threadIsAlive = false;
			for (Thread thread : threadList) {
				if (thread.isAlive())
					threadIsAlive = true;
			}
		}
		System.out.println(System.currentTimeMillis() - startTime);
	}

	public void setProcesssModel(ProcessModel processModel) {
		this.processModel = processModel;
	}

	public void setPhysicalDiskModel(PhysicalDiskModel physicalDiskModel) {
		this.physicalDiskModel = physicalDiskModel;
	}

	public void setProcessorModel(ProcessorModel processorModel) {
		this.processorModel = processorModel;
	}

	public void setNetworkInterfaceModel(
			NetworkInterfaceModel networkInterfaceModel) {
		this.networkInterfaceModel = networkInterfaceModel;
	}
}
