changeset 177:2a9dd76dde5f

merge 167
author Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
date Tue, 25 Oct 2011 00:22:51 +0900
parents 189af4af9b2b (current diff) ec3842bc0aeb (diff)
children 0f0d3193017c
files src/myVncProxy/VncProxyService.java
diffstat 2 files changed, 174 insertions(+), 149 deletions(-) [+]
line wrap: on
line diff
--- a/src/myVncProxy/AcceptClient.java	Mon Oct 24 20:23:16 2011 +0900
+++ b/src/myVncProxy/AcceptClient.java	Tue Oct 25 00:22:51 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.*;
@@ -11,11 +12,19 @@
 	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) {
-		name = _name;
+
+	public AcceptClient(String name) {
+		this.name = name;
 	}
 
+
 	public AcceptClient() {
 		new CreateThread(this);
 	}
@@ -23,149 +32,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 myAddress = 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();
-
-							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);
-
-							outputStream(os, newparent,
-									String.valueOf(parentnum),
-									String.valueOf(counter), 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((Integer.parseInt(checkRepetition) -1)/treebranch
-										== Integer.parseInt(port))
-									checkRepetition = "skip";
-							}
-							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(myAddress);
-								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, myAddress, "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();
+		}
 	}
 
 	/**
@@ -177,6 +72,12 @@
 		ls.add(Integer.parseInt(port), ls.getLast());
 		ls.removeLast();
 	}
+	
+	void listupdate(String port,String myaddr) {
+		ls.remove(Integer.parseInt(port));
+		ls.add(Integer.parseInt(port), myaddr);
+		ls.removeLast();
+	}
 
 	void outputStream(PrintStream os, String request, String parentnum,
 			String treenum, String leaderflag) {
@@ -193,10 +94,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++;
@@ -211,6 +116,98 @@
 			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;
+	}
 
 }
 
@@ -241,4 +238,4 @@
 		}
 
 	}
-}
+}
\ No newline at end of file
--- a/src/myVncProxy/VncProxyService.java	Mon Oct 24 20:23:16 2011 +0900
+++ b/src/myVncProxy/VncProxyService.java	Tue Oct 25 00:22:51 2011 +0900
@@ -9,7 +9,7 @@
 	public static void main(String[] argv) {
 		VncProxyService v = new VncProxyService();
 
-		v.checkArgs(argv);
+		//v.checkArgs(argv);
 		
 		v.mainArgs = argv;
 
@@ -29,6 +29,7 @@
 	Thread clientThread;
     CreateThread geth;
     AcceptClient acc;
+    private TextBox getHost; 
 
 	Frame vncFrame;
 	Container vncContainer;
@@ -268,7 +269,11 @@
 	//
 
 	void connectAndAuthenticate() throws Exception {
-	    acc = new AcceptClient(host);
+		if(mainArgs.length==0)
+		    acc = new AcceptClient(getHost.getAddress());
+		//acc = new AcceptClient();
+		else
+		    acc = new AcceptClient(mainArgs[0]);
 	    geth = new CreateThread(acc , echoPort);
 	    Thread thread = new Thread(geth);
 	    thread.start();
@@ -352,7 +357,11 @@
 		vncContainer.remove(authPanel);
 		 */
 		showConnectionStatus("ask password...");
-		String pw = mainArgs[2];
+		String pw;
+		if(mainArgs.length!=0)
+			pw = mainArgs[2];
+		else
+			pw = getHost.getPassword();
 		return pw;
 	}
 
@@ -614,6 +623,21 @@
 	//
 
 	void readParameters() {
+		if(mainArgs.length==0){
+			getHost = new TextBox();
+			getHost.ipRegister();
+			host = getHost.getAddress();
+		} else {
+			host = mainArgs[0];
+		}
+		
+		if(mainArgs.length < 2){
+			port = Integer.parseInt(getHost.getPort());
+//			port = 5900;
+		}else{
+			port = Integer.parseInt(mainArgs[1]);
+		}
+		
 
 		// Read "ENCPASSWORD" or "PASSWORD" parameter if specified.
 //		readPasswordParameters();
@@ -659,8 +683,12 @@
 	//
 
 	private void readPasswordParameters() {
+		String encPasswordParam;
 		// String encPasswordParam = readParameter("ENCPASSWORD", false);
-		String encPasswordParam = mainArgs[2];
+		if(mainArgs.length!=0)
+			encPasswordParam = mainArgs[2];
+		else
+			encPasswordParam = getHost.getPassword();
 
 		if (encPasswordParam == null) {
 			// passwordParam = readParameter("PASSWORD", false);