view src/main/java/jp/ac/u_ryukyu/treevnc/client/EchoClient.java @ 33:9d3478d11d3b

Add the processing of client
author Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
date Tue, 04 Sep 2012 06:06:17 +0900
parents 758d025ee24b
children 1b81deb0abb3
line wrap: on
line source

package jp.ac.u_ryukyu.treevnc.client;

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

import jp.ac.u_ryukyu.treevnc.*;


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;
	private InterfaceForViewer client;
	private int echoPort = 9999;
	public String parentAddress;
	public String parentNum;
	public String treeNum;
	public String leaderFlag;


	public EchoClient() {
	}

	public EchoClient(EchoClient echo) {
		this.name = echo.name;
		this.leaderFlag = echo.leaderFlag;
		this.parentNum = echo.parentNum;
		this.treeNum = echo.treeNum;
		this.client = echo.client;
		this.waitReply = echo.waitReply;
		// checkMove = (MyVncClient)echo.client;
	}

	public EchoClient(String name, MyVncClient client) {
		this.client = (InterfaceForViewer) client;
		this.name = name;
	}

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

	public EchoClient(EchoClient echo, MyVncClient client) {
		this.client = (InterfaceForViewer) client;
		this.name = echo.name;
		leaderFlag = echo.leaderFlag;
		parentNum = echo.parentNum;
		treeNum = echo.treeNum;
		waitReply = echo.waitReply;
	}

	public void openport() {
		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 + " Connection Faild");
			System.exit(0);
		}

	}

	/**
	 * @param args
	 *            select connect port
	 * @return
	 */
	public EchoClient requestHostName(String args) {
		if (echoSocket != null && os != null && is != null) {
			try {

				InetAddress addr = InetAddress.getLocalHost();
				String add = new String(addr.getHostAddress());
				// add = getIpV6();

				os.writeBytes(add + "\n");
				os.writeBytes(args + "\n");
				getProxyData(is);

				streamClose();
			} 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() {
		if (echoSocket != null && os != null && is != null) {
			try {
				if (runflag) {
					return true;
				}
				sendDataProxy();
				String checkRepetition = getProxyData2(is);
				if (checkRepetition.equals("stop")) {
					return true;
				}
				if (!(waitReply.checkPath())) {
					Thread.sleep(1000);
					reConnectionMain(echoSocket);
					streamClose();
				}
			} catch (UnknownHostException e) {
				System.err.println("Trying to connect to unknown host: " + e);
			} catch (IOException e) {
				return false;
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (NullPointerException e) {
				openport();
				System.out.println("notFoundParents");
				notfoundParent();
			}
		}
		return true;
	}

	boolean notfoundParent() {
		if (echoSocket != null && os != null && is != null) {
			runflag = true;
			try {
				sendDataProxy("2", parentNum, null);
				getProxyData(is);
				reConnectionMain(echoSocket);
				streamClose();
			} catch (UnknownHostException e) {
				System.err.println("Trying to connect to unknown host: " + e);
			} catch (IOException e) {
				System.err.println("IOException: " + e);
			}
		}
		return true;
	}

	public EchoClient Interruption(Socket _clientSocket) {
		clientSocket = _clientSocket;
		BufferedReader lostis = null;

		try {
			lostis = new BufferedReader(new InputStreamReader(
					clientSocket.getInputStream()));
			getProxyData(lostis);
			clientSocket.close();
		} catch (IOException e) {
			System.out.println(e);
		}
		return this;
	}

	void getProxyData(BufferedReader is) throws IOException {
		if ((parentAddress = is.readLine()) != null) {
			System.out.println("Server: " + parentAddress);
		}
		if ((parentNum = is.readLine()) != null) {
			System.out.println("parent: " + parentNum);
		}
		if ((treeNum = is.readLine()) != null) {
			System.out.println("treenum: " + treeNum);
		}
		if ((leaderFlag = is.readLine()) != null) {
			System.out.println("leaderflag: " + leaderFlag);
		}
	}

	String getProxyData2(BufferedReader is) throws IOException {
		String checkRepetition;
		System.out.println("-------------------re----------------------------");
		if ((parentAddress = is.readLine()) != null) {
			System.out.println("Server: " + parentAddress);
		}
		if ((parentNum = is.readLine()) != null) {
			System.out.println("parent:test " + parentNum);
		}
		if ((checkRepetition = is.readLine()) != null) {
			System.out.println("checkRepetition: " + checkRepetition);
		}
		return checkRepetition;
	}

	void reConnectionMain(Socket echoSocket) {
		client.close();
		client.setClientSocket(echoSocket);
		client.run();
	}

	void streamClose() throws IOException {
		os.close();
		is.close();
		echoSocket.close();
	}

	void sendDataProxy() {
		if ("1".equals(leaderFlag)) {
			sendDataProxy("1", parentNum, treeNum);
			System.out.println("---------------------------------------------");
		} else {
			sendDataProxy("3", parentNum, treeNum);
			System.out.println("---------------------------------------------");
		}
	}

	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);
		}
	}
	
	public void getParentName() {
		if (clientSocket == null) {
			// echo = new EchoClient(pHost, this);
			openport();
			requestHostName("1");
		} else {
			Interruption(clientSocket);
		}
	}
}