⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 probemanager2.java

📁 利用Java Socket写的一段通讯协议
💻 JAVA
字号:
package com.ict.netcom2.kernel;

import java.io.*;
import java.util.*;
import com.ict.netcom2.task.*;

public class ProbeManager2{
	
	HashMap<String,Probe> probes;
	
	static final String dataFileName = "data.db";
	
	public ProbeManager2() {
		probes = new HashMap<String,Probe>();
		init();
	}
	
	private void init() {
		System.out.println("init ...........");
        try {
            File file = new File(dataFileName);
            if (!file.exists()) {
                return;
            }
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);
            
            int num = ois.readInt();
            for (int i=0; i<num; i++) {
            	String ip = (String)ois.readObject();
            	TaskManager tm = (TaskManager)ois.readObject();
            	Probe pb = new Probe(ip, tm);
            	probes.put(ip, pb);
            }
            ois.close();
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	private void store() {
		System.out.println("store........................");
		try {
			File f = new File(dataFileName);
			f.delete();
			FileOutputStream fos = new FileOutputStream(f);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			
			Set<Map.Entry<String, Probe>> set = probes.entrySet();
			Iterator<Map.Entry<String, Probe>> it = set.iterator();		
			oos.writeInt(set.size());
			while (it.hasNext()) {
				Probe pb = it.next().getValue();
				oos.writeObject(pb.getIp());
				oos.writeObject(pb.getTaskManager());
			}
			oos.close();
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public HashMap<String,Probe> getProbes() {
		return probes;
	}
	
	public Probe getProbe(String ip) {
		return probes.get(ip);
	}
	
	public synchronized void createProbe(String ip) {
		probes.put(ip, new Probe(ip));
		store();
	}
	
	public int register(String ip, int netproId, String netcomIp) {
		return probes.get(ip).register(netproId, netcomIp);
	}
	
	public void unRegister(String ip, String netcomIp) {
		probes.get(ip).unRegister(netcomIp);
	}
	
	public boolean checkLiving(String ip) {
		return probes.get(ip).checkLiving();
	}

	public int setComm(String ip, String old, String newComm) {
		return probes.get(ip).setComm(old, newComm);
	}
	
	public String querySysInfo(String ip) {
		return probes.get(ip).querySysInfo();
	}
	
	public String queryTask(String ip) {
		return probes.get(ip).queryTask();
	}
	
	public String queryTask(String ip, int type) {
		return probes.get(ip).queryTask(type);
	}
	
	public String queryThreads(String ip) {
		return probes.get(ip).queryThreads();
	} 
	
	public int[] addTask(String ip, long delay, 
			boolean isRoutine, TaskParam param) {
		int[] ret = probes.get(ip).addTask(delay, isRoutine, param);
		store();
		return ret;
	}
	
	public int[] stopTask(String ip, int taskId) {
		return probes.get(ip).stopTask(taskId);
	}
	
	public String getResult(String ip, int taskId) {
		return probes.get(ip).getResult(taskId);
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -