changeset 169:4ee9cfadd8dc

update AcceptClient
author one
date Mon, 12 Sep 2011 16:36:40 +0900
parents 0d9a824dd12f
children 57904d72fc09
files src/myVncProxy/AcceptClient.java
diffstat 1 files changed, 132 insertions(+), 136 deletions(-) [+]
line wrap: on
line diff
--- a/src/myVncProxy/AcceptClient.java	Fri Sep 09 23:12:37 2011 +0900
+++ b/src/myVncProxy/AcceptClient.java	Mon Sep 12 16:36:40 2011 +0900
@@ -1,6 +1,7 @@
 package myVncProxy;
 
 import java.net.Socket;
+import java.net.UnknownHostException;
 import java.io.*;
 import java.net.*;
 import java.util.*;
@@ -12,9 +13,14 @@
 	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) {
-		name = _name;
+	public AcceptClient(String name) {
+		this.name = name;
 	}
 
 	public AcceptClient() {
@@ -24,141 +30,35 @@
 	// public synchronized void transferParentAddrerss(BufferedReader
 	// is,PrintStream os) {
 	public synchronized void transferParentAddrerss(BufferedReader is,PrintStream os) {
-		 String line, port;
-		 int intv_time = 100;
-		 String request;
-		 int treebranch = 2;// treeの子ノードの数 
-		 String leaderflag = "0", sendleaderflag = "0";
-		
-				// クライアントからのメッセージを待ち、受け取ったメッセージをそのまま返す
-				try {
-					while (true) {
-						line = is.readLine();
-						port = is.readLine();
-
-						System.out.println("データーを受信しましたlin=" + line
-								+ "       port=" + port);
-						// 自分の IPADRESSを取得する
-						InetAddress addr = InetAddress.getLocalHost();
-						String add = new String(addr.getHostAddress());
-
-						if ("1".equals(line)) {
-							System.out.println("親が落ちましたmessage" + port);
-							String checkRepetition = is.readLine();
-							os.println(ls.getLast());
-							parentnum = (Integer.parseInt(port) - 1)
-									/ treebranch;
-							String newparent = ls.get(parentnum);
-
-							counter--;
-							runflag = true;
-
-							sendleaderflag = decisionLeader(Integer.parseInt(port), treebranch);
-
-							Child report = new Child();
-							
-							passCheck = 1;
-							report.reportLastNode(ls.getLast(), newparent,port, 
-									String.valueOf(parentnum),sendleaderflag, counter);
-							
-							listupdate(port);
-
-							int g = 0;
-							for (String bs : ls) {
-								System.out.println(g + "番目" + bs);
-								g++;
-							}
-
-							os.println(port);
-							// os.println(leaderflag);
-
-							leaderflag = decisionLeader(
-									Integer.parseInt(checkRepetition),
-									treebranch);
-
-							if (Integer.parseInt(checkRepetition) == counter + 1) {
-								checkRepetition = "stop";
-							} else {
-								checkRepetition = "go";
-							}
-							os.println(checkRepetition);
-
-							// os.println(leaderflag);
-							Thread.sleep(intv_time);
-							is.close();
-							os.close();
-
-						} else if ("2".equals(line)) {
-							parentnum = (Integer.parseInt(port) - 1) / treebranch;
-							String newparent = ls.get(parentnum);
-							leaderflag = decisionLeader(Integer.parseInt(port),treebranch);
-							listupdate(port,newparent);
-							outputStream(os, newparent,String.valueOf(parentnum),port,leaderflag);
-							os.close();
-							is.close();
-						} else if ("3".equals(line)) {
-							String checkRepetition = is.readLine();
-							System.out.println("落ちたのを確認しました");
-
-							os.println(ls.get(Integer.parseInt(port)));
-							os.println(port);
-
-							if (checkRepetition.equals(ls.getLast())) {
-								checkRepetition = "stop";
-							} else {
-								//checkRepetition = "go";
-								if(ls.size()-1+passCheck == Integer.parseInt(checkRepetition)) {
-									checkRepetition = "skip";
-								}
-								passCheck = 0;
-							}
-							os.println(checkRepetition);
-
-							System.out.println("num4="
-									+ ls.get(Integer.parseInt(port)));
-							line = null;
-							runflag = false;
-							is.close();
-							os.close();
-						} else {
-							if (addrRegistor == true) {
-								ls.add(add);
-								addrRegistor = false;
-							}
-
-							if (line != null) {
-								addClientAdress(line, ls);
-								counter++;
-							} else {
-								break;
-							}
-
-							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, add, "0",
-										String.valueOf(counter), leaderflag);
-							}
-							Thread.sleep(intv_time);
-						}
+		// クライアントからのメッセージを待ち、受け取ったメッセージをそのまま返す
+		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();
-				}
-		
-		
+			}
+		} catch (IOException e) {
+			System.out.println(e);
+		} catch (InterruptedException e) {
+			e.printStackTrace();
+		}
 	}
 
 	/**
@@ -192,10 +92,14 @@
 	}
 
 	void addClientAdress(String line, LinkedList<String> ls) {
-		int g = 0;
 		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++;
@@ -210,6 +114,98 @@
 		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;
+	}
 
 }