changeset 79:c76e43a12796

modify MyVncClient
author e085711
date Wed, 31 Aug 2011 13:34:32 +0900
parents fe94e353d595
children 3bfe8845684c
files src/myVncClient/CuiMyVncClient.java src/myVncClient/EchoClient.java src/myVncClient/InterfaceForViewer.java src/myVncClient/MyVncClient.java src/myVncClient/WaitReply.java
diffstat 5 files changed, 128 insertions(+), 178 deletions(-) [+]
line wrap: on
line diff
--- a/src/myVncClient/CuiMyVncClient.java	Tue Aug 30 18:33:24 2011 +0900
+++ b/src/myVncClient/CuiMyVncClient.java	Wed Aug 31 13:34:32 2011 +0900
@@ -14,7 +14,7 @@
 
 	public static void main(String[] argv) {
 		CuiMyVncClient v = new CuiMyVncClient();
-		v.echo = null;
+		v.echoValue = null;
 		v.runClient(argv, v);
 
 	}
@@ -82,7 +82,9 @@
 	boolean runflag = false;
 	boolean first = true;
 
-	EchoClient echo;
+	EchoClient echoValue;
+	int echoPort;
+	
 	
 	void checkArgs(String[] argv) {
 		if (argv.length > 3) {
@@ -182,7 +184,7 @@
 				 * if my last node case reconnectoion stop
 				 */
 
-				echo = new EchoClient(echo, this);
+				echoValue = new EchoClient(echoValue, this);
 				try {
 					Thread.sleep(ran);
 				} catch (InterruptedException e1) {
@@ -190,13 +192,13 @@
 				}
 
 				if (counter >= 3) {
-					echo.openport();
-					echo.notfoundParent();
+					echoValue.openport();
+					echoValue.notfoundParent();
 				}
 
-				echo.openport();
+				echoValue.openport();
 				// runflag = echo.losthost();
-				if (echo.losthost()) {
+				if (echoValue.losthost()) {
 					break;
 				}
 				counter++;
@@ -221,10 +223,10 @@
 
 			if (leaderflag != null) {
 				while (true) {
-					echo = new EchoClient(echo, this);
-					echo.openport();
+					echoValue = new EchoClient(echoValue, this);
+					echoValue.openport();
 					// runflag = echo.losthost();
-					if (echo.losthost()) {
+					if (echoValue.losthost()) {
 						break;
 					}
 				}
@@ -308,13 +310,13 @@
 		if(rfb.serverMinor == 998) {
 			byte[] b = new byte[4];
 			b = rfb.readEchoPort();
-			int echoPort = castByteInt(b);
+			echoPort = castByteInt(b);
 
 			String hs = host;
 			InetAddress addr = InetAddress.getByName(host);
 			String h = new String(addr.getHostAddress());
 			
-			getParentname(echo, echoPort);
+			getParentName();
 			if(!(h.equals(host))) {
 				rfb.changeParent(host, port);
 				rfb.readVersionMsg();
@@ -371,6 +373,24 @@
 		showConnectionStatus("Using RFB protocol version " + rfb.clientMajor
 				+ "." + rfb.clientMinor);
 
+		if(rfb.serverMinor == 998) {
+			byte[] b = new byte[4];
+			b = rfb.readEchoPort();
+			echoPort = castByteInt(b);
+
+			String hs = host;
+			InetAddress addr = InetAddress.getByName(host);
+			String h = new String(addr.getHostAddress());
+			
+			getParentName();
+			if(!(h.equals(host))) {
+				rfb.changeParent(host, port);
+				rfb.readVersionMsg();
+				rfb.writeVersionMsg();
+			}
+		}
+		
+		
 		int secType = rfb.negotiateSecurity();
 		int authType;
 		if (secType == RfbProto.SecTypeTight) {
@@ -971,8 +991,8 @@
 		vncFrame.setVisible(false);
 		vncFrame.dispose();
 	}
-	void getParentname(EchoClient value, int echoPort) {
-		if (value == null) {
+	public void getParentName() {
+		if (echoValue == null) {
 
 			if (clientSocket == null) {
 				String pHost;
@@ -981,74 +1001,46 @@
 				else
 					pHost = "cls080.ie.u-ryukyu.ac.jp";
 //				echo = new EchoClient(pHost, this);
-				echo = new EchoClient(pHost, this, echoPort);
-				echo.openport();
+				echoValue = new EchoClient(pHost, this, echoPort);
+				echoValue.openport();
 
-				value = echo.hostn("1");
+				echoValue = echoValue.hostn("1");
 			} else {
-				echo = new EchoClient();
-				value = echo.Interruption(clientSocket);
+				echoValue = new EchoClient();
+				echoValue = echoValue.Interruption(clientSocket);
 			}
 		}	
 
 		// proxyからの返信で接続先を決定する
-		host = value.responseLine;
-		parent = value.parent;
-		if (value.treenum != null) {
-			treenum = value.treenum;
+		host = echoValue.responseLine;
+		parent = echoValue.parent;
+		if (echoValue.treenum != null) {
+			treenum = echoValue.treenum;
 		} else {
-			treenum = echo.treenum;
+			treenum = echoValue.treenum;
 		}
-		if (value.leaderflag != null) {
-			leaderflag = value.leaderflag;
+		if (echoValue.leaderflag != null) {
+			leaderflag = echoValue.leaderflag;
 		} else {
-			leaderflag = echo.leaderflag;
+			leaderflag = echoValue.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;
-
-
 //		port = 5999;		
 		
 	}
+	public void setEchoValue(EchoClient value) {
+		this.echoValue = value;
+	}	
+
 	
 	int castByteInt(byte[] b) {
 		ByteBuffer bb = ByteBuffer.wrap(b);
-		int value = bb.getInt();
-		return value;
+		int echoValue = bb.getInt();
+		return echoValue;
 	}
 	
-	
-	
-	public static void main(Socket _clientSocket) {
-		MyVncClient v = new MyVncClient();
-		// v.clientSocket = _clientSocket;
-		// v.mainArgs = argv;
-		v.inAnApplet = false;
-		v.inSeparateFrame = true;
-
-		v.echo = null;
-		
-		v.init();
-		v.start_threads();
-		v.start();
-	}
-
-	public static void main(EchoClient value) {
-		MyVncClient v = new MyVncClient();
-		v.inAnApplet = false;
-		v.inSeparateFrame = true;
-
-		v.echo = value;
-		
-		v.init();
-		v.start_threads();
-		v.start();
-
-	}	
-	
 }
--- a/src/myVncClient/EchoClient.java	Tue Aug 30 18:33:24 2011 +0900
+++ b/src/myVncClient/EchoClient.java	Wed Aug 31 13:34:32 2011 +0900
@@ -176,6 +176,8 @@
 				 */
 				Thread.sleep(1000);
 				client.init();
+				client.setEchoValue(this);
+				client.getParentName();
 				client.start_threads();
 				client.start();
 //				MyVncClient.main(this);
--- a/src/myVncClient/InterfaceForViewer.java	Tue Aug 30 18:33:24 2011 +0900
+++ b/src/myVncClient/InterfaceForViewer.java	Wed Aug 31 13:34:32 2011 +0900
@@ -9,9 +9,10 @@
 	public void start_threads();
 	public void start();
 	
-	
+	public void setEchoValue(EchoClient value);
 	public String readParameter(String name, boolean required);
 	
+	public void getParentName();
 	// synchronized
 	public void disconnect();
 	public void fatalError(String str);
--- a/src/myVncClient/MyVncClient.java	Tue Aug 30 18:33:24 2011 +0900
+++ b/src/myVncClient/MyVncClient.java	Wed Aug 31 13:34:32 2011 +0900
@@ -23,7 +23,10 @@
 	boolean runflag = false;
 	boolean first = true;
 
-	EchoClient echo;
+	EchoClient echoValue;
+	int echoPort;
+	String pHost;
+
 	//
 	// main() is called when run as a java program from the command line.
 	// It simply runs the applet inside a newly-created frame.
@@ -31,7 +34,7 @@
 
 	public static void main(String[] argv) {
 		MyVncClient v = new MyVncClient();
-		v.echo = null;
+		v.echoValue = null;
 		v.runClient(argv);
 	}
 
@@ -41,9 +44,9 @@
 		inSeparateFrame = true;
 
 		if (mainArgs.length > 0)
-			host = mainArgs[0];
+			pHost = mainArgs[0];
 		else
-			host = "cls080.ie.u-ryukyu.ac.jp";
+			pHost = "cls080.ie.u-ryukyu.ac.jp";
 		if (mainArgs.length > 1)
 			port = Integer.parseInt(mainArgs[1]);
 		else
@@ -164,7 +167,7 @@
 				 * if my last node case reconnectoion stop
 				 */
 
-				echo = new EchoClient(echo, this);
+				echoValue = new EchoClient(echoValue, this);
 				try {
 					Thread.sleep(ran);
 				} catch (InterruptedException e1) {
@@ -172,13 +175,13 @@
 				}
 
 				if (counter >= 3) {
-					echo.openport();
-					echo.notfoundParent();
+					echoValue.openport();
+					echoValue.notfoundParent();
 				}
 
-				echo.openport();
+				echoValue.openport();
 				// runflag = echo.losthost();
-				if (echo.losthost()) {
+				if (echoValue.losthost()) {
 					break;
 				}
 				counter++;
@@ -246,10 +249,10 @@
 			// リーダーの子ノードがproxyに対して親が落ちたことを報告をする
 			if (leaderflag != null) {
 				while (true) {
-					echo = new EchoClient(echo, this);
-					echo.openport();
+					echoValue = new EchoClient(echoValue, this);
+					echoValue.openport();
 					// runflag = echo.losthost();
-					if (echo.losthost()) {
+					if (echoValue.losthost()) {
 						break;
 					}
 				}
@@ -360,7 +363,7 @@
 
 		showConnectionStatus("Connecting to " + host + ", port " + port + "...");
 
-		rfb = new MyRfbProto(host, port, this);
+		rfb = new MyRfbProto(pHost, port, this);
 		showConnectionStatus("Connected to server");
 
 		rfb.readVersionMsg();
@@ -371,22 +374,23 @@
 		showConnectionStatus("Using RFB protocol version " + rfb.clientMajor
 				+ "." + rfb.clientMinor);
 
+
 		if(rfb.serverMinor == 998) {
 			byte[] b = new byte[4];
 			b = rfb.readEchoPort();
-			int echoPort = castByteInt(b);
+			echoPort = castByteInt(b);
 
-			InetAddress addr = InetAddress.getByName(host);
+			InetAddress addr = InetAddress.getByName(pHost);
 			String h = new String(addr.getHostAddress());
 			
-			getParentname(echo, echoPort);
+			getParentName();
 			if(!(h.equals(host))) {
 				rfb.changeParent(host, port);
 				rfb.readVersionMsg();
 				rfb.writeVersionMsg();
 			}
 		}
-		
+
 		
 		int secType = rfb.negotiateSecurity();
 		int authType;
@@ -451,7 +455,7 @@
 			InetAddress addr = InetAddress.getByName(host);
 			String h = new String(addr.getHostAddress());
 			
-			getParentname(echo, echoPort);
+			getParentName();
 			if(!(h.equals(host))) {
 				rfb.changeParent(host, port);
 				rfb.readVersionMsg();
@@ -1131,93 +1135,54 @@
 	public void windowDeiconified(WindowEvent evt) {
 	}
 	
-	void getParentname(EchoClient value, int echoPort) {
-	if (value == null) {
+	public void getParentName() {
+	if (echoValue == null) {
 
 		if (clientSocket == null) {
-			String pHost;
-			if (mainArgs.length > 0)
-				pHost = mainArgs[0];
-			else
-				pHost = "cls080.ie.u-ryukyu.ac.jp";
+
 //			echo = new EchoClient(pHost, this);
-			echo = new EchoClient(pHost, this, echoPort);
-			echo.openport();
+			echoValue = new EchoClient(pHost, this, echoPort);
+			echoValue.openport();
 
-			value = echo.hostn("1");
+			echoValue = echoValue.hostn("1");
 		} else {
-			echo = new EchoClient();
-			value = echo.Interruption(clientSocket);
+			echoValue = new EchoClient();
+			echoValue = echoValue.Interruption(clientSocket);
 		}
 	}
 	// proxyからの返信で接続先を決定する
-	host = value.responseLine;
-	parent = value.parent;
-	if (value.treenum != null) {
-		treenum = value.treenum;
+	host = echoValue.responseLine;
+	parent = echoValue.parent;
+	if (echoValue.treenum != null) {
+		treenum = echoValue.treenum;
 	} else {
-		treenum = echo.treenum;
+		treenum = echoValue.treenum;
 	}
-	if (value.leaderflag != null) {
-		leaderflag = value.leaderflag;
+	if (echoValue.leaderflag != null) {
+		leaderflag = echoValue.leaderflag;
 	} else {
-		leaderflag = echo.leaderflag;
+		leaderflag = echoValue.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();
-		if (host.equals("")) {
-			fatalError("HOST parameter not specified");
-		}
-	}
-//	port = 5999;
-	
 	}
 
+	public void setEchoValue(EchoClient value) {
+		this.echoValue = value;
+	}
 	int castByteInt(byte[] b) {
 		ByteBuffer bb = ByteBuffer.wrap(b);
 		int value = bb.getInt();
 		return value;
 	}
-	
-	
-	
-	public static void main(Socket _clientSocket) {
-		MyVncClient v = new MyVncClient();
-		// v.clientSocket = _clientSocket;
-		// v.mainArgs = argv;
-		v.inAnApplet = false;
-		v.inSeparateFrame = true;
 
-		v.echo = null;
-		
-		v.init();
-		v.start_threads();
-		v.start();
-	}
-
-	public static void main(EchoClient value) {
-		MyVncClient v = new MyVncClient();
-		v.inAnApplet = false;
-		v.inSeparateFrame = true;
-
-		v.echo = value;
-		
-		v.init();
-		v.start_threads();
-		v.start();
-
-	}
-	
 	public void setClientSocket(Socket sock) {
 		clientSocket = sock;
 	}
+	
 	public void close() {
 		rfb.close();
 		vncFrame.setVisible(false);
--- a/src/myVncClient/WaitReply.java	Tue Aug 30 18:33:24 2011 +0900
+++ b/src/myVncClient/WaitReply.java	Wed Aug 31 13:34:32 2011 +0900
@@ -3,63 +3,53 @@
 import java.net.*;
 import java.io.*;
 
-
 public class WaitReply extends Thread {
-//	MyVncClient client;
+	// MyVncClient client;
 	InterfaceForViewer client;
 	private String treenum;
 
+	/*
+	 * public WaitReply(String treenum,MyVncClient client) { this.client =
+	 * client; this.treenum = treenum; }
+	 */
 
-/*	
-	public WaitReply(String treenum,MyVncClient client) {
-		this.client = client;
-		this.treenum = treenum;
-	}
-*/
-
-	public WaitReply(String treenum,InterfaceForViewer client) {
+	public WaitReply(String treenum, InterfaceForViewer client) {
 		this.client = client;
 		this.treenum = treenum;
 	}
 
-	
-	public void run(){
+	public void run() {
 		Socket clientSocket = null;
-		ServerSocket echoServer=null;
-		//boolean mainFlag;
-		
-		while(true){
+		ServerSocket echoServer = null;
+		// boolean mainFlag;
+
+		while (true) {
 			try {
 				echoServer = new ServerSocket(10001 + Integer.parseInt(treenum));
-			}
-			catch (IOException e) {
+			} catch (IOException e) {
 				System.out.println(e);
 			}
 			try {
-			clientSocket = echoServer.accept();
-			if(clientSocket != null){
-				client.close();
-				
-//				client.clientSocket = clientSocket;
-				client.setClientSocket(clientSocket);
-				client.init();
-				client.start_threads();
-				client.start();
-				//MyVncClient.main(clientSocket);
-				//echo.Interruption(clientSocket);
-			}
-			
-			}catch (IOException e){
+				clientSocket = echoServer.accept();
+				if (clientSocket != null) {
+					client.close();
+					// client.clientSocket = clientSocket;
+					client.setClientSocket(clientSocket);
+					client.init();
+					client.start_threads();
+					client.start();
+					// MyVncClient.main(clientSocket);
+					// echo.Interruption(clientSocket);
+					echoServer.close();
+				}
+
+			} catch (IOException e) {
 				System.out.println(e);
 			}
 			/*
-			try {
-				Thread.sleep(100);
-			} catch (InterruptedException e) {
-				e.printStackTrace();
-			}
-			*/
+			 * try { Thread.sleep(100); } catch (InterruptedException e) {
+			 * e.printStackTrace(); }
+			 */
 		}
 	}
 }
-