changeset 55:f2f4b0398f04

update EchoClient.java
author Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
date Thu, 28 Jul 2011 18:06:46 +0900
parents 1d382eedc2b1
children 9f8463fb1d4d
files VncViewer.jar myVncClient.jar src/myVncClient/EchoClient.java src/myVncClient/MyVncClient.java src/myVncClient/WaitReply.java
diffstat 5 files changed, 122 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
Binary file VncViewer.jar has changed
Binary file myVncClient.jar has changed
--- a/src/myVncClient/EchoClient.java	Mon Jul 25 20:45:57 2011 +0900
+++ b/src/myVncClient/EchoClient.java	Thu Jul 28 18:06:46 2011 +0900
@@ -3,6 +3,8 @@
 import java.io.*;
 import java.net.*;
 
+import myVncClient.MulticastQueue.Client;
+
 
 public class EchoClient {
 	String name;
@@ -16,20 +18,22 @@
 	String treenum;// 自分の番号
 	String leaderflag;// リーダフラグ
 	Socket clientSocket = null;
+	MyVncClient client;
 	
 	
 
 	// WaitReplyに自分自身を渡している
 	public EchoClient() {
-	
 	}
 
 	// VncViewerから引数をもらってきてproxy役を認識する
-	public EchoClient(String _name) {
+	public EchoClient(String _name,MyVncClient client) {
+		this.client = client;
 		name = _name;
 	}
 	
-	public EchoClient(EchoClient echo) {
+	public EchoClient(EchoClient echo,MyVncClient client) {
+		this.client = client;
 		name = echo.name;
 		leaderflag = echo.leaderflag;
 		parent = echo.parent;
@@ -63,7 +67,7 @@
 
 	/**
 	 * @param args
-	 *            select at first connection or other
+	 *            select connect port 
 	 * @return
 	 */
 	EchoClient hostn(String args) {
@@ -103,8 +107,7 @@
 
 			}
 
-			waitReply = new WaitReply(treenum);
-			System.out.println(waitReply);
+			waitReply = new WaitReply(treenum,client);
 			waitReply.start();
 
 		}
@@ -114,20 +117,18 @@
 	
 	/**
 	 * Call at lost host 
-	 * @return
 	 */
-	void losthost() {
+	boolean losthost() {
 		if (echoSocket != null && os != null && is != null) {
 			try {
-
+				if(runflag){
+					return true;
+				}
 				// echoSocketの情報を呼び出す
 				if ("1".equals(leaderflag)) {
-					// 落ちた番号を報告
-					os.writeBytes("1\n");
-					os.writeBytes(parent + "\n");
+					senddataProxy("1",parent);
 				} else {
-					os.writeBytes("3\n");
-					os.writeBytes(parent + "\n");
+					senddataProxy("3",parent);
 				}
 
 				if ((responseLine = is.readLine()) != null) {
@@ -136,50 +137,93 @@
 				if ((parent = is.readLine()) != null) {
 					System.out.println("parent: " + parent);
 				}
-								
-				/*
+				
+				
+					
+				/**     
 				 * if ((value.treenum = is.readLine()) != null) {
 				 * System.out.println("treenum: " + value.treenum); }
 				 */
 				Thread.sleep(1000);
-				MyVncClient.main(this);
+				client.init(this);
+				client.start_threads();
+				client.start();
+//				MyVncClient.main(this);
 
 				// 開いたソケットなどをクローズ
 				os.close();
 				is.close();
 				echoSocket.close();
-				runflag = true;
+				//runflag = true;
 
 			} catch (UnknownHostException e) {
 				System.err.println("Trying to connect to unknown host: " + e);
 			} catch (IOException e) {
-				runflag = false;
+				
+				return false;
+				
+				/*
 				while (true) {
+					
 					try {
 						Thread.sleep(1000);
 						System.out.println("once connection proxy");
 					} catch (InterruptedException e1) {
-						// TODO Auto-generated catch block
 						e1.printStackTrace();
 					}
+					
 					openport();
 					losthost();
 					System.out.println("faild in this area");
 					System.err.println("IOException: " + e);
+					
 					if(runflag == true){
 						break;
-					}
+					}				
 				}
+				*/
 			} 
 			catch (InterruptedException e) {
-				// TODO Auto-generated catch block in order to thread sleep
 				e.printStackTrace();
 			}
 
 		}
-		return ;
+		return true;
 	}
+	
+	boolean notfoundParent() {
+		if (echoSocket != null && os != null && is != null) {
+			runflag = true;
+			try {
+				senddataProxy("2", parent);
 
+				if ((responseLine = is.readLine()) != null) {
+					System.out.println("Server: " + responseLine);
+				}
+				if ((parent = is.readLine()) != null) {
+					System.out.println("parent: " + parent);
+				}
+				if ((treenum = is.readLine()) != null) {
+					System.out.println("Server: " + treenum);
+				}
+				if ((leaderflag = is.readLine()) != null) {
+					System.out.println("parent: " + leaderflag);
+				}
+				client.init(this);
+				client.start_threads();
+				client.start();
+				os.close();
+				is.close();
+				echoSocket.close();
+			} catch (UnknownHostException e) {
+				System.err.println("Trying to connect to unknown host: " + e);
+			} catch (IOException e) {
+				System.err.println("IOException: " + e);
+			}
+		}
+		return true;
+	}
+/*
 	void lostchild() {
 
 		// ßSystem.out.println(name);
@@ -189,7 +233,7 @@
 				// echoSocketなどの情報を呼び出す(追加)
 
 				// 自分の番号を報告
-				os.writeBytes("2\n");
+				os.writeBytes("4\n");
 				os.writeBytes(treenum + "\n");
 
 				os.close();
@@ -203,7 +247,7 @@
 			}
 		}
 	}
-
+*/
 	EchoClient Interruption(Socket _clientSocket) {
 		clientSocket = _clientSocket;
 		BufferedReader lostis = null;//あとで修正する
@@ -247,4 +291,16 @@
 		}
 		return this;
 	}
+	
+	void senddataProxy(String type,String num) {
+		try {
+			os.writeBytes(type + "\n");
+			os.writeBytes(num + "\n");
+
+		} catch (UnknownHostException e) {
+			System.err.println("Trying to connect to unknown host: " + e);
+		} catch (IOException e) {
+			System.err.println("IOException: " + e);
+		}
+	}
 }
\ No newline at end of file
--- a/src/myVncClient/MyVncClient.java	Mon Jul 25 20:45:57 2011 +0900
+++ b/src/myVncClient/MyVncClient.java	Thu Jul 28 18:06:46 2011 +0900
@@ -20,6 +20,8 @@
 	Socket clientSocket = null;
 	String parent, treenum;
 	private String leaderflag;
+	boolean runflag = false;
+
 
 
 	//
@@ -131,29 +133,35 @@
 			 * this while reconnection  
 			 */
 			
+			int counter = 0;
+			//window を消してnullを突っ込んでGCで削除させる。
+			/*
+			vncFrame.setVisible(false);
+			vncFrame = null;
+			*/
 			while (true) {
 				/**
 				 * if my last node case reconnectoion stop    
 				 */
-				if(clientSocket != null) {
-					break;
-				}
+						
+				echo = new EchoClient(echo,this);
 				try {
 					Thread.sleep(ran);
-				} catch (InterruptedException e1) {
-					// TODO Auto-generated catch block
+				} catch (InterruptedException e1) {				
 					e1.printStackTrace();
 				}
-				if(clientSocket != null) {
+				
+				if(counter >= 3) {
+					echo.openport();
+					echo.notfoundParent();
+				}
+		
+				echo.openport();
+				//runflag = echo.losthost();
+				if(echo.losthost()) {
 					break;
 				}
-				echo = new EchoClient(echo);
-				echo.openport();
-				echo.losthost();
-				if(echo.runflag==true){
-					echo.runflag=false;
-					break;
-				}
+				counter++;
 			}
 			
 			// System.exit(0);
@@ -212,16 +220,20 @@
 					+ ":" + port, e);
 		} catch (EOFException e) {
 
-			// insert
 			//window を消してnullを突っ込んでGCで削除させる。
 			vncFrame.setVisible(false);
 			vncFrame = null;
 			//num4
 			// リーダーの子ノードがproxyに対して親が落ちたことを報告をする
-			if(leaderflag != null){
-				echo = new EchoClient(echo);
-				echo.openport();
-				echo.losthost();
+			if(leaderflag != null) {
+				while(true) {
+					echo = new EchoClient(echo,this);
+					echo.openport();
+					//runflag = echo.losthost();
+					if(echo.losthost()) {
+						break;
+					}
+				}
 			} else {
 
 				if (showOfflineDesktop) {
@@ -271,6 +283,7 @@
 	// Create a VncCanvas instance.
 	//
 
+
 	void createCanvas(int maxWidth, int maxHeight) throws IOException {
 		// Determine if Java 2D API is available and use a special
 		// version of VncCanvas if it is present.
@@ -703,7 +716,7 @@
 				String pHost;
 				if (mainArgs.length > 0) pHost = mainArgs[0];
 				else pHost = "cls080.ie.u-ryukyu.ac.jp";
-				echo = new EchoClient(pHost);
+				echo = new EchoClient(pHost,this);
 				echo.openport();
 				
 				value = echo.hostn("1");
@@ -1064,7 +1077,7 @@
 
 	public static void main(Socket _clientSocket) {
 		MyVncClient v = new MyVncClient();
-		v.clientSocket = _clientSocket;
+		//v.clientSocket = _clientSocket;
 		// v.mainArgs = argv;
 		v.inAnApplet = false;
 		v.inSeparateFrame = true;
--- a/src/myVncClient/WaitReply.java	Mon Jul 25 20:45:57 2011 +0900
+++ b/src/myVncClient/WaitReply.java	Thu Jul 28 18:06:46 2011 +0900
@@ -5,10 +5,11 @@
 
 
 public class WaitReply extends Thread {
-	
+	MyVncClient client;
 	private String treenum;
 
-	public WaitReply(String treenum) {
+	public WaitReply(String treenum,MyVncClient client) {
+		this.client = client;
 		this.treenum = treenum;
 	}
 
@@ -27,7 +28,11 @@
 			try {
 			clientSocket = echoServer.accept();
 			if(clientSocket != null){
-				MyVncClient.main(clientSocket);
+				client.clientSocket = clientSocket;
+				client.init(null);
+				client.start_threads();
+				client.start();
+				//MyVncClient.main(clientSocket);
 				//echo.Interruption(clientSocket);
 			}
 			
@@ -38,7 +43,6 @@
 			try {
 				Thread.sleep(100);
 			} catch (InterruptedException e) {
-				// TODO Auto-generated catch block
 				e.printStackTrace();
 			}
 			*/