changeset 30:a335a1038a23

merge taninari's program
author e085711
date Wed, 06 Jul 2011 12:30:17 +0900
parents 750ecaa1e1b9
children bf1126d1e721
files src/myVncClient/MyVncClient.java src/myVncClient/echoClient.java src/myVncClient/waitreply.java
diffstat 3 files changed, 189 insertions(+), 83 deletions(-) [+]
line wrap: on
line diff
--- a/src/myVncClient/MyVncClient.java	Wed Jul 06 11:56:15 2011 +0900
+++ b/src/myVncClient/MyVncClient.java	Wed Jul 06 12:30:17 2011 +0900
@@ -12,7 +12,9 @@
 
 	boolean inAnApplet = true;
 	boolean inSeparateFrame = false;
-
+    static Socket clientSocket = null;
+    String parent,treenum;
+    Revalue value; //include trenumber, parentstnumber,etc
 	//
 	// main() is called when run as a java program from the command line.
 	// It simply runs the applet inside a newly-created frame.
@@ -24,7 +26,7 @@
 		v.inAnApplet = false;
 		v.inSeparateFrame = true;
 
-		v.init();
+		v.init(null);
 		v.start();
 	}
 
@@ -34,9 +36,10 @@
 	// init()
 	//
 
-	public void init() {
+	public void init(Revalue value) {
 
-		readParameters();
+//		readParameters();
+		readParameters(value);
 
 		refApplet = this;
 
@@ -158,7 +161,6 @@
 
 		
 		try {
-
 			if (showControls)
 				buttonPanel.enableButtons();
 
@@ -175,10 +177,24 @@
 					+ ":" + port, e);
 		} catch (EOFException e) {
 
-		    //insert
-		    echo = new echoClient();
-		    echo.openport();
-		    echo.losthost();
+		    //insert                                                                                                                                                       
+		    //リーダーの子ノードがproxyに対して親が落ちたことを報告をする                                                                                                  
+		    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();
+			}
+			echo = new echoClient();
+			echo.openport();
+			echo.losthost();
+		    }else{
+
 
 			if (showOfflineDesktop) {
 				e.printStackTrace();
@@ -203,6 +219,7 @@
 			} else {
 				fatalError("Network error: remote side closed connection", e);
 			}
+		    }
 		} catch (IOException e) {
 			String str = e.getMessage();
 			if (str != null && str.length() != 0) {
@@ -642,21 +659,44 @@
 	// those expected in the html applet tag source.
 	//
 
-	void readParameters() {
-	    if (mainArgs.length > 0){
-		host = mainArgs[0];
-		echo = new echoClient(host);
+	void readParameters(Revalue value) {
+	    /*
+		host = readParameter("HOST", !inAnApplet);
+
+		if (host == null) { host = getCodeBase().getHost(); if
+		(host.equals("")) { fatalError("HOST parameter not specified"); } }
+		 
+		port = readIntParameter("PORT", 5550);
+	    */
+
+		if(value==null){
+		if(clientSocket==null)
+		{
+			echo = new echoClient(mainArgs[0]);
+			echo.openport();
+			//	    value = echo.hostn(mainArgs[0]);
+			value = echo.hostn("1");
 	    }else{
-		host = "cls080.ie.u-ryukyu.ac.jp";
-		echo = new echoClient();
+	    	echo = new echoClient();
+	    	value = echo.Interruption(clientSocket);
 	    }
-
+		}
+	    //proxyからの返信で接続先を決定する
+	    host=value.responseLine;
+	    parent=value.parent;
+	    treenum=value.treenum;
+	    System.out.println("親は"+parent);
+	    System.out.println("自分の番号"+treenum);
+	    System.out.println("接続先hostは" + host);
 
-		if (mainArgs.length > 1)
-			port = Integer.parseInt(mainArgs[1]);
-		else
-			port = 5550;
-
+	    if (host == null) {
+		host = getCodeBase().getHost();
+		if (host.equals("")) {
+		    fatalError("HOST parameter not specified");
+		}
+	    }
+	    port = 5550;
+		// Read "ENCPASSWORD" or "PASSWORD" parameter if specified.
 		readPasswordParameters();
 
 		String str;
@@ -977,4 +1017,27 @@
 
 	public void windowDeiconified(WindowEvent evt) {
 	}
+	public static void main(Socket _clientSocket) {
+		// TODO Auto-generated method stub
+		clientSocket = _clientSocket;
+		MyVncClient v = new MyVncClient();
+//		v.mainArgs = argv;
+		v.inAnApplet = false;
+		v.inSeparateFrame = true;
+
+		v.init(null);
+		v.start();
+	}
+	public static void main(Revalue value) {
+		// TODO Auto-generated method stub
+		MyVncClient v = new MyVncClient();
+		v.value = value;
+		v.inAnApplet = false;
+		v.inSeparateFrame = true;
+
+		v.init(value);
+		v.start();
+		
+	}
 }
+	
\ No newline at end of file
--- a/src/myVncClient/echoClient.java	Wed Jul 06 11:56:15 2011 +0900
+++ b/src/myVncClient/echoClient.java	Wed Jul 06 12:30:17 2011 +0900
@@ -5,25 +5,27 @@
 
 
 public class echoClient {
-    Revalue value = new Revalue();
-	waitreply wr;// = new waitreply();
+    public static Revalue value = new Revalue();
+	waitreply waitreply;// = new waitreply();
     String responseLine,treenum,parent,line;
-    Socket echoSocket = null;
+    public static Socket echoSocket = null;
     BufferedReader lostis = null;
-    DataOutputStream os = null;
+    public static DataOutputStream os = null;
     PrintStream lostos;
-    BufferedReader is = null;
+    public static BufferedReader is = null;
     Socket clientSocket = null;
     ServerSocket echoServer=null;
-
-    String host;
+    public static String name;
     
-    echoClient(){
-    	wr = new waitreply(this);
+    //waitreplyに自分自身を渡している
+    public echoClient(){
+    	waitreply = new waitreply(this);
     }
-    echoClient(String _host){
-    	wr = new waitreply(this);
-	host  =  _host;
+    
+    
+    //VncViewerから引数をもらってきてproxy役を認識する
+    public echoClient(String _name){
+    	name = _name;
     }
 
     //    Revalue hostn(String args){
@@ -32,13 +34,11 @@
 	
 	// ポート9999番を開く
 	try {
-	    if(host == null){
-		echoSocket = new Socket("133.13.48.18", 9999);
-		System.out.println();
-	    }
-	    else{
-		echoSocket = new Socket(host, 9999);
-	    }
+		if(name != null){
+			echoSocket = new Socket(name, 9999);	
+		}else{
+			echoSocket = new Socket("133.13.48.18", 9999);
+		}
 	    os = new DataOutputStream(echoSocket.getOutputStream());
 	    is = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
 	} catch (UnknownHostException e) {
@@ -80,7 +80,6 @@
 
 
     Revalue hostn(String args){
-	
 	// サーバーにメッセージを送る
 	if (echoSocket != null && os != null && is != null) {
 	    try {
@@ -102,6 +101,9 @@
 		if ((value.treenum = is.readLine()) != null) {
 		    System.out.println("treenum: " + value.treenum);
 		}
+		if ((value.leaderflag = is.readLine()) != null) {
+		    System.out.println("leaderflag: " + value.leaderflag);
+		}
 
 		// 開いたソケットなどをクローズ
 		os.close();
@@ -113,7 +115,11 @@
 		System.err.println("IOException: " + e);
 
 	    }
-		wr.start();	
+	   
+	    waitreply = new waitreply(this);
+	    System.out.println(waitreply);
+		waitreply.start();
+			
 	}
 	return value;
     }
@@ -122,21 +128,28 @@
 	if (echoSocket != null && os != null && is != null) {
 	    try {
 		
+	    //echoSocketの情報を呼び出す
+	    if("1".equals(value.leaderflag)){
 		//落ちた番号を報告
-		os.writeBytes("1\n");
-		os.writeBytes(value.parent + "\n");
-		
-
+	    	os.writeBytes("1\n");
+	    	os.writeBytes(value.parent + "\n");
+	    }
+	    else{
+	    	os.writeBytes("3\n");
+	    	os.writeBytes(value.parent + "\n");
+	    }
 
 		if ((value.responseLine = is.readLine()) != null) {
 		    System.out.println("Server: " + value.responseLine);
 		}
 		if ((value.parent = is.readLine()) != null) {
 		    System.out.println("parent: " + value.parent);
-		}
+		}/*
 		if ((value.treenum = is.readLine()) != null) {
 		    System.out.println("treenum: " + value.treenum);
-		}
+		}*/
+		Thread.sleep(1000);
+		MyVncClient.main(value);
 
 		// 開いたソケットなどをクローズ
 		os.close();
@@ -147,14 +160,23 @@
 		System.err.println("Trying to connect to unknown host: " + e);
 	    } catch (IOException e) {
 		System.err.println("IOException: " + e);
-	    }
+	    } catch (InterruptedException e) {
+			// TODO Auto-generated catch block in order to thread sleep
+			e.printStackTrace();
+		}
+	    
 	}
-	return value;
+    return value;
     }
 
     void lostchild(){
+    	
+    //ßSystem.out.println(name);
 	if (echoSocket != null && os != null && is != null) {
 	    try {
+	    	
+	    //echoSocketなどの情報を呼び出す(追加)
+	    
 
 		//自分の番号を報告
 		os.writeBytes("2\n");
@@ -172,46 +194,54 @@
 	}
     }
 
-    Revalue Interruption(Socket clientSocket){  
+    Revalue Interruption(Socket _clientSocket){
+    	clientSocket = _clientSocket;
     	
 		try {
-			clientSocket = echoServer.accept();
 			lostis = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
 			lostos = new PrintStream(clientSocket.getOutputStream());
-			while (true){
-				line = lostis.readLine();
-			}	    
+			//while (true){
+				//line = lostis.readLine();
+				//System.out.println(line);
+				
+				try{
+				    if ((value.responseLine = lostis.readLine()) != null) {
+					System.out.println("newServer: " + value.responseLine);
+				    }
+				    if ((value.parent = lostis.readLine()) != null) {
+						System.out.println("newtparent: " + value.parent);
+					}
+				    if ((value.treenum = lostis.readLine()) != null) {
+						System.out.println("newtreenum: " + value.treenum);
+					}
+				    if ((value.leaderflag = lostis.readLine()) != null) {
+						System.out.println("newreaderflag: " + value.leaderflag);
+					}
+				 
+				}	  
+				catch (UnknownHostException e) {
+				    System.err.println("Trying to connect to unknown host: " + e);
+				} catch (IOException e) {
+				    System.err.println("IOException: " + e);
+				}
+				
+		//	}	    
 		}catch (IOException e){
 			System.out.println(e);
 		}
 		try{
-		    echoServer.close();
+		    clientSocket.close();//waitreplyのacceptを終了させる
 		}
 		catch (IOException e){
 		    System.out.println(e);
 		}    	
-	try{
-	    if ((value.responseLine = lostis.readLine()) != null) {
-		System.out.println("Server: " + value.responseLine);
-	    }
-	    if ((value.parent = lostis.readLine()) != null) {
-		System.out.println("parent: " + value.parent);
-	    }
-	    if ((value.treenum = lostis.readLine()) != null) {
-		System.out.println("treenum: " + value.treenum);
-	    }
-	}	  
-	catch (UnknownHostException e) {
-	    System.err.println("Trying to connect to unknown host: " + e);
-	} catch (IOException e) {
-	    System.err.println("IOException: " + e);
-	}
-	return value;
+		return value;
     }
 }
 
 class Revalue{
     public String responseLine;
-    public String parent;
-    public String treenum;
+    public String parent;//親の番号
+    public String treenum;//自分の番号
+    public String leaderflag;//リーダフラグ
 }
--- a/src/myVncClient/waitreply.java	Wed Jul 06 11:56:15 2011 +0900
+++ b/src/myVncClient/waitreply.java	Wed Jul 06 12:30:17 2011 +0900
@@ -4,7 +4,7 @@
 import java.io.*;
 
 
-public class waitreply extends Thread{
+public class waitreply extends Thread {
 	echoClient echo;
 	Socket clientSocket = null;
 	ServerSocket echoServer=null;
@@ -13,20 +13,33 @@
 	waitreply(echoClient _echo){
 		echo = _echo;
 	}
+	
 	public void run(){
-		try {
-			echoServer = new ServerSocket(9998);
-		}
-		catch (IOException e) {
-			System.out.println(e);
-		}
+		
 		while(true){
 			try {
+				echoServer = new ServerSocket(10001 + Integer.parseInt(echoClient.value.treenum));
+			}
+			catch (IOException e) {
+				System.out.println(e);
+			}                                                                                                                
+			try {
 			clientSocket = echoServer.accept();
-			echo.Interruption(clientSocket);
+			if(clientSocket != null){
+				MyVncClient.main(clientSocket);
+				//echo.Interruption(clientSocket);
+			}
 			}catch (IOException e){
 				System.out.println(e);
 			}
+			/*
+			try {
+				Thread.sleep(100);
+			} catch (InterruptedException e) {
+				// TODO Auto-generated catch block
+				e.printStackTrace();
+			}
+			*/
 		}
 	}
 }