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

📄 finallnode.java

📁 模拟网络通信
💻 JAVA
字号:
package com.node;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Set;

class FinalWorkThread extends Thread {
	private FinallNode fn;
	private Socket s;

	public FinalWorkThread(Socket s, FinallNode fn) {
		this.s = s;
		this.fn = fn;
	}

	public void run() {
		InputStream is;

		try {
			is = s.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			String line = null;
			while (true) {
				line = br.readLine();
				System.out.println("##finalNode get" + line);
				String sl[] = line.split(":");
				if (sl[0].equals("a")) {
					fn.add(sl[1], 1);
				} else {
					fn.add(sl[1], 0);
				}
			}
		} catch (IOException e) {

			e.printStackTrace();
		}

	}
}

class FSaveThread extends Thread {
	private FinallNode fn;
	private String path;
	private PrintWriter pw;

	public FSaveThread(FinallNode fn, String path) throws IOException {
		this.fn = fn;
		this.path = path;
		this.pw = null;
	}

	public void run() {

		while (true) {
			try {
				sleep(10000);
			} catch (InterruptedException e) {

				e.printStackTrace();
			}
			try {
				pw = new PrintWriter(new FileOutputStream(path), true);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			HashMap<String, Integer> ahm = fn.getAMap();
			HashMap<String, Integer> thm = fn.getTMap();
			Set<String> keys = ahm.keySet();
			for (String key : keys) {
				String str = key + "=" + ahm.get(key) + "=" + thm.get(key);
				System.out.println(str);
				pw.println(str);

			}
			pw.close();
		}
	}
}

public class FinallNode extends Thread {
	private HashMap<String, Integer> ahm;
	private HashMap<String, Integer> thm;
	private String path;
	private int port;

	public HashMap<String, Integer> getAMap() {
		return ahm;
	}

	public HashMap<String, Integer> getTMap() {
		return thm;
	}

	synchronized public void add(String routetrace, int mark) {
		if (thm.containsKey(routetrace)) {
			int newc = thm.get(routetrace) + 1;
			thm.remove(routetrace);
			thm.put(routetrace, newc);
			if (mark == 1) {
				newc = ahm.get(routetrace) + 1;
				ahm.remove(routetrace);
				ahm.put(routetrace, newc);
			}
		} else {
			thm.put(routetrace, 1);
			if (mark == 1) {
				ahm.put(routetrace, 1);
			} else {
				ahm.put(routetrace, 0);
			}
		}
	}

	public FinallNode(String path, int port) {
		this.path = path;
		this.port = port;
	}

	public void init() throws IOException {
		this.ahm = new HashMap<String, Integer>();
		this.thm = new HashMap<String, Integer>();
		BufferedReader br = new BufferedReader(new FileReader(path));
		String line = null;
		while ((line = br.readLine()) != null) {
			String sl[] = line.split("=");
			if (!ahm.containsKey(sl[0])) {
				ahm.put(sl[0], Integer.parseInt(sl[1]));
				thm.put(sl[0], Integer.parseInt(sl[2]));
			}
		}
		br.close();

	}

	public void run() {
		ServerSocket ss = null;
		try {
			ss = new ServerSocket(port);
		} catch (IOException e) {
			System.out.println("###final node get ss failed!!");
			e.printStackTrace();
		}
		if (ss != null) {
			try {
				FSaveThread fst = new FSaveThread(this, path);
				fst.start();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			while (true) {
				Socket s = null;
				try {
					s = ss.accept();
				} catch (IOException e) {
					e.printStackTrace();
					System.out.println("#final get socket err!!");
				}
				if (s != null) {
					FinalWorkThread fwt = new FinalWorkThread(s, this);
					fwt.start();
				}
			}

		}
	}
}

⌨️ 快捷键说明

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