view src/myVncClient/EchoClient.java @ 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
children 5867cac7efb0
line wrap: on
line source

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 ;
	}
}