view src/myVncProxy/AcceptClient.java @ 171:ffc16faf011a

merge
author Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
date Fri, 21 Oct 2011 17:02:00 +0900
parents 57904d72fc09 1c0af90f7f59
children 54ad8e0654b5
line wrap: on
line source

package myVncProxy;

import java.net.Socket;
import java.net.UnknownHostException;
import java.io.*;
import java.net.*;
import java.util.*;

public class AcceptClient {
	int counter = 0, parentnum = 0/* 落ちたときの親の番号をカウントするためのもの */;
	LinkedList<String> ls = new LinkedList<String>();
	boolean runflag = false;
	private String name;
	boolean addrRegistor = true;
	int passCheck = 0;
	int treebranch = 2;// treeの子ノードの数 
	String newparent,request,myAddress;
	String leaderflag = "0", sendleaderflag = "0";
	int intv_time = 100;
	


	public AcceptClient(String name) {
		this.name = name;
	}


	public AcceptClient() {
		new CreateThread(this);
	}

	// public synchronized void transferParentAddrerss(BufferedReader
	// is,PrintStream os) {
	public synchronized void transferParentAddrerss(BufferedReader is,PrintStream os) {
		// クライアントからのメッセージを待ち、受け取ったメッセージをそのまま返す
		try {
			while (true) {
				String line = is.readLine();
				String port = is.readLine();
				myAddress = getMyAddress();
				if ("1".equals(line)) {
					replyLeaderNode(os, is, port);
				} else if ("2".equals(line)) {
					replyNodeInformation(port);
					listupdate(port, newparent);
					outputStream(os, newparent, String.valueOf(parentnum),
							port, leaderflag);
					os.close();
					is.close();
				} else if ("3".equals(line)) {
					replyNormalChildren(os, is, port);
					line = null;
				} else {
					if(replyCreateTree(os, is, port, line)) {
						break;
					}
				}
			}
		} catch (IOException e) {
			System.out.println(e);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param port
	 *            parent value
	 */
	void listupdate(String port) {
		ls.remove(Integer.parseInt(port));
		ls.add(Integer.parseInt(port), ls.getLast());
		ls.removeLast();
	}

	void outputStream(PrintStream os, String request, String parentnum,
			String treenum, String leaderflag) {
		os.println(request);
		os.println(parentnum);
		os.println(treenum);
		os.println(leaderflag);
	}

	void checkParameter(int parent, int counter, String leaderflag) {
		System.out.println("pの値=" + parentnum);
		System.out.println("iの値=" + counter);
		System.out.println("leaderflag=" + leaderflag + "\n");
	}

	void addClientAdress(String line, LinkedList<String> ls) {
		if (line != null) {
			ls.add(line);
		}
		displyLinkedList(ls);
	}
	
	void displyLinkedList( LinkedList<String> ls) {
		int g = 0;
		for (String bs : ls) {
			System.out.println(g + "番目" + bs);
			g++;
		}
	}

	String decisionLeader(int counter, int treebranch) {
		if ((counter - 1) % treebranch == 1) { // children in most young treenum
												// have leaderflag 1 other 0
			return "0";
		} else {
			return "1";
		}
	}
	
	String getMyAddress () throws UnknownHostException {
		InetAddress addr = InetAddress.getLocalHost();
		return new String(addr.getHostAddress());
	}
	
	void replyNodeInformation (String port) {
		parentnum = (Integer.parseInt(port) - 1) / treebranch;
		newparent = ls.get(parentnum);
		sendleaderflag = decisionLeader(Integer.parseInt(port), treebranch);
		leaderflag = decisionLeader(Integer.parseInt(port),treebranch);
	}
	
	void replyLeaderNode(PrintStream os,BufferedReader is,String port) throws IOException, InterruptedException {
		String checkRepetition = is.readLine();
		os.println(ls.getLast());
		replyNodeInformation(port);
		counter--;
		passCheck = 1;
		Child report = new Child();
		report.reportLastNode(ls.getLast(), newparent,port, String.valueOf(parentnum),
				sendleaderflag, counter);
		listupdate(port);
		displyLinkedList(ls);
		os.println(port);
		runflag = true;
		leaderflag = decisionLeader(
				Integer.parseInt(checkRepetition),
				treebranch);
		if (Integer.parseInt(checkRepetition) == counter + 1) {
			checkRepetition = "stop";
		} else {
			checkRepetition = "go";
		}
		os.println(checkRepetition);
		Thread.sleep(intv_time);
		is.close();
		os.close();
	}
	
	void replyNormalChildren(PrintStream os,BufferedReader is,String port) throws IOException {
		String checkRepetition = is.readLine();
		System.out.println("落ちたのを確認しました");

		os.println(ls.get(Integer.parseInt(port)));
		os.println(port);

		
		if(ls.size()-1+passCheck == Integer.parseInt(checkRepetition)) {
			checkRepetition = "skip";
		}
		passCheck = 0;

		os.println(checkRepetition);

		System.out.println("num4="
				+ ls.get(Integer.parseInt(port)));

		runflag = false;
		is.close();
		os.close();
	}
	
	boolean replyCreateTree(PrintStream os,BufferedReader is,String port,String line) throws InterruptedException {
		if (addrRegistor == true) {
			ls.add(myAddress);
			addrRegistor = false;
		}

		if (line != null) {
			addClientAdress(line, ls);
			counter++;
		} else {
			return true;
		}
		
		if (counter >= treebranch + 1) {
			leaderflag = decisionLeader(counter, treebranch);
			parentnum = (counter - 1) / treebranch;
			request = ls.get(parentnum);
			System.out.println(parentnum);
			outputStream(os, request,String.valueOf(parentnum),
					String.valueOf(counter), leaderflag);
			checkParameter(parentnum, counter, leaderflag);
		} else {
			// treeの親ノードに接続する人に接続する人を教える
			outputStream(os, myAddress, "0",
					String.valueOf(counter), leaderflag);
		}
		Thread.sleep(intv_time);
		return false;
	}

}

class Child {

	void reportLastNode(String hiddenchild, String newparent,
			String newtreenum, String newpnum, String newleaderflag, int i)
			throws IOException {
		try {
			Socket echoSocket;
			System.out.println(hiddenchild + "に接続します");
			echoSocket = new Socket(hiddenchild, 10001 + (i + 1));// i+1は実験中に同じマシーンを使っていたのでportを変えて対応、本番時には取り除く予定。

			DataOutputStream os = new DataOutputStream(
					echoSocket.getOutputStream());

			os.writeBytes(newparent + "\n");
			os.writeBytes(newpnum + "\n");
			os.writeBytes(newtreenum + "\n");
			os.writeBytes(newleaderflag + "\n");

			os.close();
		} catch (UnknownHostException e) {
			System.err.println("Don't know about host: localhost");
		} catch (IOException e) {
			System.err
					.println("Couldn't get I/O for the connection to: localhost");
		}

	}
}