view src/myVncClient/EchoClient.java @ 87:6c43d40abfe6

modify EchoClient
author one
date Thu, 08 Sep 2011 16:03:16 +0900
parents 29e43be288ca
children d3e806372657 3ea33e683522
line wrap: on
line source

package myVncClient;

import java.io.*;
import java.net.*;

import myVncClient.MulticastQueue.Client;


public class EchoClient {
	private String name;
	private BufferedReader is = null;
	private DataOutputStream os = null;
	private Socket echoSocket = null;
	private boolean runflag = false;
	private WaitReply waitReply;
	private Socket clientSocket = null;
//	MyVncClient client;
	private InterfaceForViewer client;
	private int echoPort = 9999;
	MyVncClient checkMove;
	VncViewer vncV;
	String responseLine;
	String parent;// 親の番号
	String treenum;// 自分の番号
	String leaderflag;// リーダフラグ
	boolean passflag;
	

	// WaitReplyに自分自身を渡している
	public EchoClient() {
	}

	
	public EchoClient(EchoClient echo) {
		this.name = echo.name;
		leaderflag= echo.leaderflag;
		parent = echo.parent;
		treenum = echo.treenum;
		client = echo.client;
		checkMove = (MyVncClient)echo.client;
	}
	// VncViewerから引数をもらってきてproxy役を認識する
	public EchoClient(String name,MyVncClient client) {
		this.client = client;
		this.name = name;
	}

	public EchoClient(String name,MyVncClient client, int echoPort) {
		this.client = client;
		this.name = name;
		this.echoPort = echoPort;
	}
	
	
	
	public EchoClient(EchoClient echo,MyVncClient client) {
		this.client = client;
		this.name = echo.name;
		leaderflag = echo.leaderflag;
		parent = echo.parent;
		treenum = echo.treenum;
		waitReply = echo.waitReply;
	}

	public EchoClient(String name,CuiMyVncClient client) {
		this.client = client;
		this.name = name;
	}
	
	public EchoClient(String name,CuiMyVncClient client, int echoPort) {
		this.client = client;
		this.name = name;
		this.echoPort = echoPort;
	}

	public EchoClient(EchoClient echo,CuiMyVncClient client) {
		this.client = client;
		this.name = echo.name;
		leaderflag = echo.leaderflag;
		parent = echo.parent;
		treenum = echo.treenum;
	}

	// 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, echoPort);
			} else {
				echoSocket = new Socket("133.13.48.18", echoPort);
			}
			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(name + " への接続に失敗しました");
			System.exit(0);
		}
	}

	/**
	 * @param args
	 *            select connect port 
	 * @return
	 */
	EchoClient 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,client);
			waitReply.start();

		}
		return this;
	}

	
	/**
	 * Call at lost host 
	 */
	boolean losthost() {
		String checkRepetition; 
		if (echoSocket != null && os != null && is != null) {
			try {
				if(runflag){
					return true;
				}
				// echoSocketの情報を呼び出す
				if ("1".equals(leaderflag)) {
					senddataProxy("1",parent,treenum);
				} else {
					senddataProxy("3",parent,treenum);
				}

				if ((responseLine = is.readLine()) != null) {
					System.out.println("Server: " + responseLine);
				}
				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); }
				 */
				

				if(!(checkRepetition.equals("skip")) || "1".equals(leaderflag)) {

					Thread.sleep(1000);
					client.init();
					client.setEchoValue(this);
					client.getParentName();
					client.start_threads();
					client.start();
					//				MyVncClient.main(this);

					// 開いたソケットなどをクローズ
					os.close();
					is.close();
					echoSocket.close();
					//runflag = true;
				}

				
				Thread.sleep(1000);
				if(!(checkMove.vncFrame.isShowing())&&"skip".equals(checkRepetition)) {
					System.out.println("in");
					openport();
					notfoundParent();					
				}
				

			} catch (UnknownHostException e) {
				System.err.println("Trying to connect to unknown host: " + e);
			} catch (IOException e) {
				
				return false;
				
				/*
				while (true) {
					
					try {
						Thread.sleep(1000);
						System.out.println("once connection proxy");
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
					
					openport();
					losthost();
					System.out.println("faild in this area");
					System.err.println("IOException: " + e);
					
					if(runflag == true){
						break;
					}				
				}
				*/
			} 
			catch (InterruptedException e) {
				e.printStackTrace();
			}

		}
		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("treenum: " + treenum);
				}
				if ((leaderflag = is.readLine()) != null) {
					System.out.println("leaderflag: " + leaderflag);
				}
				client.init();
				client.setEchoValue(this);
				client.getParentName();
				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);
		if (echoSocket != null && os != null && is != null) {
			try {

				// echoSocketなどの情報を呼び出す(追加)

				// 自分の番号を報告
				os.writeBytes("4\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);
			}
		}
	}
*/
	EchoClient Interruption(Socket _clientSocket) {
		clientSocket = _clientSocket;
		BufferedReader lostis = null;//あとで修正する


		try {
			lostis = new BufferedReader(new InputStreamReader(
					clientSocket.getInputStream()));
			// 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 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);
		}
	}
}