Mercurial > hg > Members > nobuyasu > tightVNCProxy
view src/myVncProxy/acceptClient.java @ 66:7632606406cb
create sendCheckMillis
author | e085711 |
---|---|
date | Tue, 26 Jul 2011 13:03:08 +0900 |
parents | 4864a7d1df00 |
children | 3ec4c9e94462 |
line wrap: on
line source
package myVncProxy; import java.net.Socket; import java.io.*; import java.net.*; import java.util.*; public class acceptClient extends Thread { ServerSocket echoServer=null; // ソケットや入出力用のストリームの宣言 String line,port,handover; BufferedReader is; PrintStream os; Socket clientSocket = null; int intv_time = 100; int i = 0 , p = 0,t = 0; //String[] sub = new String[100]; LinkedList<String> ls = new LinkedList<String>(); String request,name; int treebranch = 2;//treeの子ノードの数 String leaderflag="0",sendleaderflag="0"; boolean runflag =false,addrRegistor=true; BufferedReader temp = null;//一時的にisを保存する変数 CreateThread createThread; public acceptClient(String _name) { // TODO Auto-generated constructor stub name = _name; } public acceptClient() { new CreateThread(this); } public synchronized void gethost(BufferedReader is,PrintStream os) { //public synchronized void run () { /* while(true){ // ポート9999番を開く try { echoServer = new ServerSocket(9999); } catch (IOException e) { System.out.println(e); } // クライアントからの要求を受けるソケットを開く try { clientSocket = echoServer.accept(); is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); os = new PrintStream(clientSocket.getOutputStream()); */ // クライアントからのメッセージを待ち、受け取ったメッセージをそのまま返す try { while (true){ line = is.readLine(); port = is.readLine(); System.out.println("データーを受信しましたlin="+line+" port="+port); //自分の IPADRESSを取得する InetAddress addr = InetAddress.getLocalHost(); String add = new String(addr.getHostAddress()); if("1".equals(line)){ System.out.println("親が落ちましたmessage" + port); os.println(ls.getLast()); handover= String.valueOf((Integer.parseInt(port) - 1) / treebranch); String hidenchild=ls.getLast(); String newparent=ls.get(Integer.parseInt(handover)); ls.remove(Integer.parseInt(port)); ls.add(Integer.parseInt(port),ls.getLast()); ls.remove(Integer.parseInt(port)); i--; t++; runflag = true; if(t % treebranch == 0){ p--; } if((i-1)%treebranch==1){ // children in most young treenum have leaderflag 1 other 0 sendleaderflag = "1"; }else{ sendleaderflag = "0"; } child child = new child(); child.clost(hidenchild,newparent,os,port,handover,sendleaderflag,i); os.println(port); Thread.sleep(intv_time); is.close(); os.close(); }else if("2".equals(line)){ System.out.println("子供が落ちましたmessage" + port); //exit シグナルを送る Thread.sleep(intv_time); }else if("3".equals(line)){ System.out.println("落ちたのを確認しました"); os.println(ls.get(Integer.parseInt(port))); os.println(port); line=null; runflag = false; is.close(); os.close(); }else{ if(addrRegistor==true){ ls.add(add); addrRegistor = false; } System.out.println(p); if(line != null){ sock conf = new sock(); conf.arg(line,ls); i++; }else{ break; } if(i>=treebranch+1){ if((i-1)%treebranch==0){ leaderflag = "1"; }else{ leaderflag = "0"; } if((i-1)%treebranch==0){ p++; } // request = [p-1]; request = ls.get(p); os.println(request); System.out.println("pの値="+p); os.println(String.valueOf(p)); System.out.println("iの値="+i); os.println(String.valueOf(i)); System.out.println("leaderflag="+leaderflag + "\n"); os.println(leaderflag); }else{ //treeの親ノードに接続する人に接続する人を教える os.println(add);//あとで渡されたmainArgs[1](現在はname)を渡す予定 os.println("0"); os.println(String.valueOf(i)); os.println(leaderflag + "\n"); } Thread.sleep(intv_time); } } } catch (IOException e){ System.out.println(e); } catch(InterruptedException e){ e.printStackTrace(); } /* try{ echoServer.close(); } catch (IOException e){ System.out.println(e); } */ //} comment out while } } class sock{ void arg(String line,LinkedList<String> ls){ if(line != null){ ls.add(line); } int g=0; for(String bs: ls){ System.out.println(g+"番目"+bs); g++; } } } class child{ Socket echoSocket; DataOutputStream os = null; BufferedReader is = null; void clost(String hiddenchild,String newparent,PrintStream rep,String newtreenum,String newpnum,String newleaderflag,int i) throws IOException{ try { System.out.println(hiddenchild + "に接続します"); echoSocket = new Socket(hiddenchild, 10001 + (i + 1));//i+1は実験中に同じマシーンを使っていたのでportを変えて対応、本番時には取り除く予定。 os = new DataOutputStream(echoSocket.getOutputStream()); is = new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); os.writeBytes(newparent+"\n"); os.writeBytes(newpnum+"\n"); os.writeBytes(newtreenum+"\n"); os.writeBytes(newleaderflag+"\n"); } 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"); } } }