view src/myVncClient/echoClient.java @ 30:a335a1038a23

merge taninari's program
author e085711
date Wed, 06 Jul 2011 12:30:17 +0900
parents 750ecaa1e1b9
children 76f5994f2af2
line wrap: on
line source

package myVncClient;

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


public class echoClient {
    public static Revalue value = new Revalue();
	waitreply waitreply;// = new waitreply();
    String responseLine,treenum,parent,line;
    public static Socket echoSocket = null;
    BufferedReader lostis = null;
    public static DataOutputStream os = null;
    PrintStream lostos;
    public static BufferedReader is = null;
    Socket clientSocket = null;
    ServerSocket echoServer=null;
    public static String name;
    
    //waitreplyに自分自身を渡している
    public echoClient(){
    	waitreply = new waitreply(this);
    }
    
    
    //VncViewerから引数をもらってきてproxy役を認識する
    public echoClient(String _name){
    	name = _name;
    }

    //    Revalue hostn(String args){
    void openport(){
	// ソケットや入出力用のストリームの宣言
	
	// ポート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.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();
	}
*/



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

    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();

	    } catch (UnknownHostException e) {
		System.err.println("Trying to connect to unknown host: " + e);
	    } catch (IOException e) {
		System.err.println("IOException: " + e);
	    } 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;//リーダフラグ
}