changeset 50:c07bec8c8617

change echoClient.java EchoClient.java
author Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
date Thu, 21 Jul 2011 23:53:48 +0900
parents a9e0284cc29c
children 5867cac7efb0
files VncViewer.jar myVncClient.jar src/myVncClient/EchoClient.java src/myVncClient/MyVncClient.java src/myVncClient/VncViewer.java src/myVncClient/WaitReply.java src/myVncClient/echoClient.java
diffstat 7 files changed, 298 insertions(+), 295 deletions(-) [+]
line wrap: on
line diff
Binary file VncViewer.jar has changed
Binary file myVncClient.jar has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/myVncClient/EchoClient.java	Thu Jul 21 23:53:48 2011 +0900
@@ -0,0 +1,262 @@
+package myVncClient;
+
+import java.io.*;
+import java.net.*;
+import java.util.Random;
+
+public class EchoClient {
+	String name;
+	BufferedReader is = null;
+	DataOutputStream os = null;
+	Socket echoSocket = null;
+	boolean runflag = false;
+	WaitReply waitReply;// = new WaitReply();
+	String responseLine;
+	String parent;// 親の番号
+	String treenum;// 自分の番号
+	String leaderflag;// リーダフラグ
+	
+	String line;
+	BufferedReader lostis = null;//あとで修正する
+	PrintStream lostos;//あとで修正する
+	Socket clientSocket = null;
+	ServerSocket echoServer = null;
+
+	
+	
+
+	// WaitReplyに自分自身を渡している
+	public EchoClient() {
+		waitReply = new WaitReply("0");
+	}
+
+	// VncViewerから引数をもらってきてproxy役を認識する
+	public EchoClient(String _name) {
+		name = _name;
+	}
+
+	// void hostn(String args){
+	void openport() {
+		//  ソケットや入出力用のストリームの宣言
+		/*
+		Random rnd = new Random();
+		long ran = rnd.nextInt(1000) + 1000;
+*/
+		// ポート9999番に接続
+		try {
+			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) {
+			System.err.println("Don't know about host: localhost");
+		} catch (IOException e) {
+			System.out.println("接続に失敗しました");
+			System.err
+			.println("Couldn't get I/O for the connection to: localhost");
+		}
+	}
+
+	/*
+	 * 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
+	 * @return
+	 */
+	void hostn(String args) {
+		// サーバーにメッセージを送る
+		if (echoSocket != null && os != null && is != null) {
+			try {
+				// ip情報を取得する
+				InetAddress addr = InetAddress.getLocalHost();
+				// System.out.println(addr.getHostAddress());
+				String add = new String(addr.getHostAddress());
+
+				// メッセージを送ります
+				os.writeBytes(add + "\n");
+				os.writeBytes(args + "\n");
+
+				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("treenum: " + treenum);
+				}
+				if ((leaderflag = is.readLine()) != null) {
+					System.out.println("leaderflag: " + leaderflag);
+				}
+
+				// 開いたソケットなどをクローズ
+				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);
+
+			}
+
+			waitReply = new WaitReply(treenum);
+			System.out.println(waitReply);
+			waitReply.start();
+
+		}
+		return ;
+	}
+
+	
+	/**
+	 * Call at lost host 
+	 * @return
+	 */
+	void losthost() {
+		if (echoSocket != null && os != null && is != null) {
+			try {
+
+				// echoSocketの情報を呼び出す
+				if ("1".equals(leaderflag)) {
+					// 落ちた番号を報告
+					os.writeBytes("1\n");
+					os.writeBytes(parent + "\n");
+				} else {
+					os.writeBytes("3\n");
+					os.writeBytes(parent + "\n");
+				}
+
+				if ((responseLine = is.readLine()) != null) {
+					System.out.println("Server: " + responseLine);
+				}
+				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);
+
+				// 開いたソケットなどをクローズ
+				os.close();
+				is.close();
+				echoSocket.close();
+				runflag = true;
+
+			} catch (UnknownHostException e) {
+				System.err.println("Trying to connect to unknown host: " + e);
+			} catch (IOException e) {
+				runflag = 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 ;
+	}
+
+	void lostchild() {
+
+		// ßSystem.out.println(name);
+		if (echoSocket != null && os != null && is != null) {
+			try {
+
+				// echoSocketなどの情報を呼び出す(追加)
+
+				// 自分の番号を報告
+				os.writeBytes("2\n");
+				os.writeBytes(treenum + "\n");
+
+				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);
+			}
+		}
+	}
+
+	void Interruption(Socket _clientSocket) {
+		clientSocket = _clientSocket;
+
+		try {
+			lostis = new BufferedReader(new InputStreamReader(
+					clientSocket.getInputStream()));
+			lostos = new PrintStream(clientSocket.getOutputStream());
+			// while (true){
+			// line = lostis.readLine();
+			// System.out.println(line);
+
+			try {
+				if ((responseLine = lostis.readLine()) != null) {
+					System.out.println("newServer: " + responseLine);
+				}
+				if ((parent = lostis.readLine()) != null) {
+					System.out.println("newtparent: " + parent);
+				}
+				if ((treenum = lostis.readLine()) != null) {
+					System.out.println("newtreenum: " + treenum);
+				}
+				if ((leaderflag = lostis.readLine()) != null) {
+					System.out.println("newreaderflag: " + 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 {
+			clientSocket.close();// WaitReplyのacceptを終了させる
+		} catch (IOException e) {
+			System.out.println(e);
+		}
+		return ;
+	}
+}
+
+
--- a/src/myVncClient/MyVncClient.java	Tue Jul 12 13:41:54 2011 +0900
+++ b/src/myVncClient/MyVncClient.java	Thu Jul 21 23:53:48 2011 +0900
@@ -11,24 +11,26 @@
 public class MyVncClient extends VncViewer implements java.lang.Runnable,
 		WindowListener {
 
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 1L;
 	boolean inAnApplet = true;
 	boolean inSeparateFrame = false;
-	static Socket clientSocket = null;
+	Socket clientSocket = null;
 	String parent, treenum;
-	Revalue value; // include trenumber, parentstnumber,etc
+//	Revalue value; // include information trenumber, parentstnumber,etc
+	private String leaderflag;
+
 
 	//
 	// main() is called when run as a java program from the command line.
 	// It simply runs the applet inside a newly-created frame.
 	//
 
-	public static void main(String[] argv) {
-		MyVncClient v = new MyVncClient();
-		v.runClient(argv);
-	}
 
-	private void runClient(String[] argv) {
-		mainArgs = argv;
+	private void runClient(EchoClient echoClient) {
+
 		inAnApplet = false;
 		inSeparateFrame = true;
 
@@ -41,7 +43,7 @@
 	// init()
 	//
 
-	public void init(Revalue value) {
+	public void init(EchoClient value) {
 
 		// readParameters();
 		readParameters(value);
@@ -122,7 +124,7 @@
 			Random rnd = new Random();
 			long ran = rnd.nextInt(5000) + 5000;
 			System.out.println(ran);
-			echoClient.runflag = false;
+			boolean runflag = false;
 			while (true) {
 				try {
 					Thread.sleep(ran);
@@ -130,10 +132,11 @@
 					// TODO Auto-generated catch block
 					e1.printStackTrace();
 				}
-				echo = new echoClient();
+				echo = new EchoClient();
 				echo.openport();
 				echo.losthost();
-				if(echoClient.runflag==true){ 
+				if(runflag==true){
+					echo.runflag=true;
 					break;
 				}
 			}
@@ -199,8 +202,8 @@
 			vncFrame.setVisible(false);
 			vncFrame = null;
 			// リーダーの子ノードがproxyに対して親が落ちたことを報告をする
-			if(echoClient.value.leaderflag != null){
-				echo = new echoClient();
+			if(leaderflag != null){
+				echo = new EchoClient();
 				echo.openport();
 				echo.losthost();
 			/*
@@ -687,7 +690,7 @@
 	 those expected in the html applet tag source.
 	*/
 
-	void readParameters(Revalue value) {
+	void readParameters(EchoClient value) {
 		/*
 		 * host = readParameter("HOST", !inAnApplet);
 		 * 
@@ -702,13 +705,13 @@
 				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);
 				echo.openport();
 				
-				value = echo.hostn("1");
+				echo.hostn("1");
 			} else {
-				echo = new echoClient();
-				value = echo.Interruption(clientSocket);
+				echo = new EchoClient();
+				echo.Interruption(clientSocket);
 			}
 		}
 		// proxyからの返信で接続先を決定する
@@ -1051,8 +1054,8 @@
 
 	public static void main(Socket _clientSocket) {
 		// TODO Auto-generated method stub
-		clientSocket = _clientSocket;
 		MyVncClient v = new MyVncClient();
+		v.clientSocket = _clientSocket;
 		// v.mainArgs = argv;
 		v.inAnApplet = false;
 		v.inSeparateFrame = true;
@@ -1062,10 +1065,9 @@
 		v.start();
 	}
 
-	public static void main(Revalue value) {
+	public static void main(EchoClient value) {
 		// TODO Auto-generated method stub
 		MyVncClient v = new MyVncClient();
-		v.value = value;
 		v.inAnApplet = false;
 		v.inSeparateFrame = true;
 
--- a/src/myVncClient/VncViewer.java	Tue Jul 12 13:41:54 2011 +0900
+++ b/src/myVncClient/VncViewer.java	Thu Jul 21 23:53:48 2011 +0900
@@ -10,6 +10,7 @@
 
 	boolean inAnApplet = true;
 	boolean inSeparateFrame = false;
+	protected String[] mainArgs;
 
 	//
 	// main() is called when run as a java program from the command line.
@@ -26,12 +27,12 @@
 		v.start();
 	}
 
-	String[] mainArgs;
+	String[] echoClient;
 
 	MyRfbProto rfb;
 	Thread rfbThread;
 	Thread accThread;
-    echoClient echo;
+    EchoClient echo;
 
 	Frame vncFrame;
 	Container vncContainer;
--- a/src/myVncClient/WaitReply.java	Tue Jul 12 13:41:54 2011 +0900
+++ b/src/myVncClient/WaitReply.java	Thu Jul 21 23:53:48 2011 +0900
@@ -5,20 +5,21 @@
 
 
 public class WaitReply extends Thread {
-	echoClient echo;
-	Socket clientSocket = null;
-	ServerSocket echoServer=null;
-	boolean mainFlag;
 	
-	WaitReply(echoClient _echo){
-		echo = _echo;
+	private String treenum;
+
+	public WaitReply(String treenum) {
+		this.treenum = treenum;
 	}
-	
+
 	public void run(){
+		Socket clientSocket = null;
+		ServerSocket echoServer=null;
+		//boolean mainFlag;
 		
 		while(true){
 			try {
-				echoServer = new ServerSocket(10001 + Integer.parseInt(echoClient.value.treenum));
+				echoServer = new ServerSocket(10001 + Integer.parseInt(treenum));
 			}
 			catch (IOException e) {
 				System.out.println(e);
--- a/src/myVncClient/echoClient.java	Tue Jul 12 13:41:54 2011 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,263 +0,0 @@
-package myVncClient;
-
-import java.io.*;
-import java.net.*;
-import java.util.Random;
-
-public class echoClient {
-	static Revalue value = new Revalue();
-	static String name;
-	static BufferedReader is = null;
-	static DataOutputStream os = null;
-	static Socket echoSocket = null;
-	static boolean runflag = false;
-	WaitReply waitReply;// = new WaitReply();
-	String responseLine, treenum, parent, line;
-	BufferedReader lostis = null;//あとで修正する
-	PrintStream lostos;//あとで修正する
-	Socket clientSocket = null;
-	ServerSocket echoServer = null;
-
-	
-	
-
-	// WaitReplyに自分自身を渡している
-	public echoClient() {
-		waitReply = new WaitReply(this);
-	}
-
-	// VncViewerから引数をもらってきてproxy役を認識する
-	public echoClient(String _name) {
-		name = _name;
-	}
-
-	// Revalue hostn(String args){
-	void openport() {
-		//  ソケットや入出力用のストリームの宣言
-		/*
-		Random rnd = new Random();
-		long ran = rnd.nextInt(1000) + 1000;
-*/
-		// ポート9999番に接続
-		try {
-			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) {
-			System.err.println("Don't know about host: localhost");
-		} catch (IOException e) {
-			System.out.println("接続に失敗しました");
-			System.err
-			.println("Couldn't get I/O for the connection to: localhost");
-		}
-	}
-
-	/*
-	 * 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
-	 * @return
-	 */
-	Revalue hostn(String args) {
-		// サーバーにメッセージを送る
-		if (echoSocket != null && os != null && is != null) {
-			try {
-				// ip情報を取得する
-				InetAddress addr = InetAddress.getLocalHost();
-				// System.out.println(addr.getHostAddress());
-				String add = new String(addr.getHostAddress());
-
-				// メッセージを送ります
-				os.writeBytes(add + "\n");
-				os.writeBytes(args + "\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);
-				}
-				if ((value.leaderflag = is.readLine()) != null) {
-					System.out.println("leaderflag: " + value.leaderflag);
-				}
-
-				// 開いたソケットなどをクローズ
-				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);
-
-			}
-
-			waitReply = new WaitReply(this);
-			System.out.println(waitReply);
-			waitReply.start();
-
-		}
-		return value;
-	}
-
-	
-	/**
-	 * Call at lost host 
-	 * @return
-	 */
-	Revalue losthost() {
-		if (echoSocket != null && os != null && is != null) {
-			try {
-
-				// echoSocketの情報を呼び出す
-				if ("1".equals(value.leaderflag)) {
-					// 落ちた番号を報告
-					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();
-				is.close();
-				echoSocket.close();
-				runflag = true;
-
-			} catch (UnknownHostException e) {
-				System.err.println("Trying to connect to unknown host: " + e);
-			} catch (IOException e) {
-				runflag = 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 value;
-	}
-
-	void lostchild() {
-
-		// ßSystem.out.println(name);
-		if (echoSocket != null && os != null && is != null) {
-			try {
-
-				// echoSocketなどの情報を呼び出す(追加)
-
-				// 自分の番号を報告
-				os.writeBytes("2\n");
-				os.writeBytes(value.treenum + "\n");
-
-				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);
-			}
-		}
-	}
-
-	Revalue Interruption(Socket _clientSocket) {
-		clientSocket = _clientSocket;
-
-		try {
-			lostis = new BufferedReader(new InputStreamReader(
-					clientSocket.getInputStream()));
-			lostos = new PrintStream(clientSocket.getOutputStream());
-			// 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 {
-			clientSocket.close();// WaitReplyのacceptを終了させる
-		} catch (IOException e) {
-			System.out.println(e);
-		}
-		return value;
-	}
-}
-
-class Revalue {
-	public String responseLine;
-	public String parent;// 親の番号
-	public String treenum;// 自分の番号
-	public String leaderflag;// リーダフラグ
-}