changeset 59:d086ec1b4a7a

merge
author Yu Taninari <e085734@ie.u-ryukyu.ac.jp>
date Fri, 29 Jul 2011 17:26:17 +0900
parents 2588865ec41e (current diff) e65934744da8 (diff)
children 18a19d8a09f4
files VncViewer.jar myVncClient.jar src/myVncClient/acceptThread.java
diffstat 7 files changed, 204 insertions(+), 121 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/myVncClient/AcceptThread.java	Fri Jul 29 17:26:17 2011 +0900
@@ -0,0 +1,31 @@
+package myVncClient;
+import java.net.Socket;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import myVncClient.MyRfbProto;
+
+public class AcceptThread implements Runnable {
+    MyRfbProto rfb;
+    byte[] imageBytes;
+
+    AcceptThread(MyRfbProto _rfb ) {
+	rfb = _rfb;
+    }
+    public void run() {
+	rfb.selectPort();
+	while (true) {
+	    try {
+		Socket newCli = rfb.accept();
+		
+		OutputStream os = newCli.getOutputStream();
+		InputStream is = newCli.getInputStream();
+		rfb.newClient(this, newCli, os, is);
+	    } catch (IOException e) {
+		e.printStackTrace();
+		System.out.println(e);
+	    }
+	}
+    }
+}
\ No newline at end of file
--- a/src/myVncClient/EchoClient.java	Fri Jul 29 17:25:08 2011 +0900
+++ b/src/myVncClient/EchoClient.java	Fri Jul 29 17:26:17 2011 +0900
@@ -2,7 +2,9 @@
 
 import java.io.*;
 import java.net.*;
-import java.util.Random;
+
+import myVncClient.MulticastQueue.Client;
+
 
 public class EchoClient {
 	String name;
@@ -10,24 +12,33 @@
 	DataOutputStream os = null;
 	Socket echoSocket = null;
 	boolean runflag = false;
-	WaitReply waitReply;// = new WaitReply();
+	WaitReply waitReply;
 	String responseLine;
 	String parent;// 親の番号
 	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,MyVncClient client) {
+		this.client = client;
+		name = echo.name;
+		leaderflag = echo.leaderflag;
+		parent = echo.parent;
+		treenum = echo.treenum;
+	}
 
 	// void hostn(String args){
 	void openport() {
@@ -54,23 +65,9 @@
 		}
 	}
 
-	/*
-	 * try { echoServer = new ServerSocket(9998); } catch (IOException e) {
-	 * System.out.println(e); }
-	 * 
-	 * try { //clientSocket = echoServer.accept(); lostis = new
-	 * BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
-	 * lostos = new PrintStream(clientSocket.getOutputStream()); while (true){
-	 * line = is.readLine(); } } catch (IOException e){ System.out.println(e); }
-	 */
-
-	/*
-	 * catch(InterruptedException e){ e.printStackTrace(); }
-	 */
-
 	/**
 	 * @param args
-	 *            select at first connection or other
+	 *            select connect port 
 	 * @return
 	 */
 	EchoClient hostn(String args) {
@@ -110,8 +107,7 @@
 
 			}
 
-			waitReply = new WaitReply(treenum);
-			System.out.println(waitReply);
+			waitReply = new WaitReply(treenum,client);
 			waitReply.start();
 
 		}
@@ -121,20 +117,19 @@
 	
 	/**
 	 * Call at lost host 
-	 * @return
 	 */
-	void losthost() {
+	boolean losthost() {
+		String checkRepetition; 
 		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,treenum);
 				} else {
-					os.writeBytes("3\n");
-					os.writeBytes(parent + "\n");
+					senddataProxy("3",parent,treenum);
 				}
 
 				if ((responseLine = is.readLine()) != null) {
@@ -142,49 +137,98 @@
 				}
 				if ((parent = is.readLine()) != null) {
 					System.out.println("parent: " + parent);
-				}/*
+				}
+				if ((checkRepetition = is.readLine()) != null) {
+					System.out.println("checkRepetition: " + checkRepetition);
+				}
+				if(checkRepetition.equals("stop")){
+					return true;
+				}
+					
+				/**     
 				 * 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,null);
 
+				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);
@@ -194,7 +238,7 @@
 				// echoSocketなどの情報を呼び出す(追加)
 
 				// 自分の番号を報告
-				os.writeBytes("2\n");
+				os.writeBytes("4\n");
 				os.writeBytes(treenum + "\n");
 
 				os.close();
@@ -208,16 +252,15 @@
 			}
 		}
 	}
-
-	void Interruption(Socket _clientSocket) {
+*/
+	EchoClient Interruption(Socket _clientSocket) {
 		clientSocket = _clientSocket;
 		BufferedReader lostis = null;//あとで修正する
-		PrintStream lostos;//あとで修正する
+
 
 		try {
 			lostis = new BufferedReader(new InputStreamReader(
 					clientSocket.getInputStream()));
-			lostos = new PrintStream(clientSocket.getOutputStream());
 			// while (true){
 			// line = lostis.readLine();
 			// System.out.println(line);
@@ -251,8 +294,24 @@
 		} catch (IOException e) {
 			System.out.println(e);
 		}
-		return ;
+		return this;
 	}
-}
+	
+	void senddataProxy(String type,String num,String treenum) {
+		try {
+			if(treenum!=null) {
+				os.writeBytes(type + "\n");
+				os.writeBytes(num + "\n");
+				os.writeBytes(treenum + "\n");
+			} else {
+				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/MyRfbProto.java	Fri Jul 29 17:25:08 2011 +0900
+++ b/src/myVncClient/MyRfbProto.java	Fri Jul 29 17:26:17 2011 +0900
@@ -20,7 +20,7 @@
 import myVncClient.MulticastQueue.Client;
 
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
+//import java.util.concurrent.Executors;
 import java.io.OutputStream;
 
 class MyRfbProto extends RfbProto {
@@ -41,7 +41,7 @@
 	private byte initData[];
 	private LinkedList<Socket> cliListTmp;
 	private LinkedList<Socket> cliList;
-	private LinkedList<Thread> sendThreads;
+	//private LinkedList<Thread> sendThreads;
 	boolean createBimgFlag;
 
 	ExecutorService executor;
@@ -434,7 +434,7 @@
 		}
 	}
 
-	void newClient(acceptThread acceptThread, final Socket newCli,
+	void newClient(AcceptThread acceptThread, final Socket newCli,
 			final OutputStream os, final InputStream is) throws IOException {
 		// createBimgFlag = true;
 		// rfb.addSockTmp(newCli);
--- a/src/myVncClient/MyVncClient.java	Fri Jul 29 17:25:08 2011 +0900
+++ b/src/myVncClient/MyVncClient.java	Fri Jul 29 17:26:17 2011 +0900
@@ -6,7 +6,6 @@
 import java.net.*;
 import java.util.Random;
 
-import myVncClient.acceptThread;
 
 public class MyVncClient extends VncViewer implements java.lang.Runnable,
 		WindowListener {
@@ -20,6 +19,8 @@
 	Socket clientSocket = null;
 	String parent, treenum;
 	private String leaderflag;
+	boolean runflag = false;
+
 
 
 	//
@@ -47,7 +48,7 @@
 	//
 
 	public void init(EchoClient value) {
-
+		
 		// readParameters();
 		readParameters(value);
 
@@ -93,8 +94,7 @@
 		rfbThread.start();
 	}
 	
-	public void run() {
-
+	public void run() { 
 		gridbag = new GridBagLayout();
 		vncContainer.setLayout(gridbag);
 
@@ -111,7 +111,7 @@
 		try {
 
 			connectAndAuthenticate();
-			accThread = new Thread(new acceptThread(rfb));
+			accThread = new Thread(new AcceptThread(rfb));
 			accThread.start();
 
 			doProtocolInitialisation();
@@ -127,20 +127,40 @@
 			Random rnd = new Random();
 			long ran = rnd.nextInt(5000) + 5000;
 			System.out.println(ran);
+			//親がいない場合の処理はここに書く!!!!
+			/**
+			 * this while reconnection  
+			 */
+			
+			int counter = 0;
+			//window を消してnullを突っ込んでGCで削除させる。
+			/*
+			vncFrame.setVisible(false);
+			vncFrame = null;
+			*/
 			while (true) {
+				/**
+				 * if my last node case reconnectoion stop    
+				 */
+						
+				echo = new EchoClient(echo,this);
 				try {
 					Thread.sleep(ran);
-				} catch (InterruptedException e1) {
-					// TODO Auto-generated catch block
+				} catch (InterruptedException e1) {				
 					e1.printStackTrace();
 				}
-				echo = new EchoClient();
+				
+				if(counter >= 3) {
+					echo.openport();
+					echo.notfoundParent();
+				}
+		
 				echo.openport();
-				echo.losthost();
-				if(echo.runflag==true){
-					echo.runflag=false;
+				//runflag = echo.losthost();
+				if(echo.losthost()) {
 					break;
 				}
+				counter++;
 			}
 			
 			// System.exit(0);
@@ -199,33 +219,20 @@
 					+ ":" + port, e);
 		} catch (EOFException e) {
 
-			// insert
 			//window を消してnullを突っ込んでGCで削除させる。
 			vncFrame.setVisible(false);
 			vncFrame = null;
+			//num4
 			// リーダーの子ノードがproxyに対して親が落ちたことを報告をする
-			if(leaderflag != null){
-				echo = new EchoClient();
-				echo.openport();
-				echo.losthost();
-			/*
-			if ("1".equals(echoClient.value.leaderflag)) {
-				echo = new echoClient();
-				echo.openport();
-				echo.losthost();
-			} else if ("0".equals(echoClient.value.leaderflag)) {
-				
-				try {
-					Thread.sleep(1000);
-				} catch (InterruptedException e1) {
-					// TODO Auto-generated catch block
-					e1.printStackTrace();
+			if(leaderflag != null) {
+				while(true) {
+					echo = new EchoClient(echo,this);
+					echo.openport();
+					//runflag = echo.losthost();
+					if(echo.losthost()) {
+						break;
+					}
 				}
-
-				echo = new echoClient();
-				echo.openport();
-				echo.losthost();
-				*/
 			} else {
 
 				if (showOfflineDesktop) {
@@ -275,6 +282,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.
@@ -707,22 +715,34 @@
 				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");
 			} else {
 				echo = new EchoClient();
-				echo.Interruption(clientSocket);
+				value = echo.Interruption(clientSocket);
 			}
 		}
 		// proxyからの返信で接続先を決定する
 		host = value.responseLine;
 		parent = value.parent;
-		treenum = value.treenum;
+		if(value.treenum != null) {
+			treenum = value.treenum;
+		} else {
+			treenum = echo.treenum;
+		}
+		if(value.leaderflag != null) {
+			leaderflag = value.leaderflag;
+		} else {
+			leaderflag = echo.leaderflag;
+		}
 		System.out.println("Parent =" + parent);
 		System.out.println("mynumber =" + treenum);
 		System.out.println("connect host =" + host);
+		System.out.println("leaderflag(boolean) = " + leaderflag);
+		
+		echo = value;
 
 		if (host == null) {
 			host = getCodeBase().getHost();
@@ -1056,7 +1076,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/VncViewer.java	Fri Jul 29 17:25:08 2011 +0900
+++ b/src/myVncClient/VncViewer.java	Fri Jul 29 17:26:17 2011 +0900
@@ -108,7 +108,7 @@
 
 		rfbThread = new Thread(this);
 		rfbThread.start();
-		accThread = new Thread(new acceptThread(rfb)); 
+		accThread = new Thread(new AcceptThread(rfb)); 
 		accThread.start();
 		
 		
--- a/src/myVncClient/WaitReply.java	Fri Jul 29 17:25:08 2011 +0900
+++ b/src/myVncClient/WaitReply.java	Fri Jul 29 17:26:17 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();
 			}
 			*/
--- a/src/myVncClient/acceptThread.java	Fri Jul 29 17:25:08 2011 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,31 +0,0 @@
-package myVncClient;
-import java.net.Socket;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-
-import myVncClient.MyRfbProto;
-
-public class acceptThread implements Runnable {
-	MyRfbProto rfb;
-	byte[] imageBytes;
-
-	acceptThread(MyRfbProto _rfb ) {
-		rfb = _rfb;
-	}
-	public void run() {
-		rfb.selectPort();
-		while (true) {
-			try {
-				Socket newCli = rfb.accept();
-				
-				OutputStream os = newCli.getOutputStream();
-				InputStream is = newCli.getInputStream();
-				rfb.newClient(this, newCli, os, is);
-				} catch (IOException e) {
-				e.printStackTrace();
-				System.out.println(e);
-			}
-		}
-	}
-}
\ No newline at end of file