# HG changeset patch # User Nobuyasu Oshiro # Date 1311827103 -32400 # Node ID c92ec725895300308ff10fd20aca44da93d9eb8a # Parent 343e0a107dfc1f780451c16525ff45673f35faa8 change name AcceptThread diff -r 343e0a107dfc -r c92ec7258953 src/myVncProxy/AcceptThread.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/myVncProxy/AcceptThread.java Thu Jul 28 13:25:03 2011 +0900 @@ -0,0 +1,239 @@ +package myVncProxy; + +import java.net.Socket; +import java.io.*; +import java.net.*; +import java.util.*; + +public class acceptClient extends Thread { + int counter = 0 , parentnum = 0,t = 0/*落ちたときの親の番号をカウントするためのもの*/; + LinkedList ls = new LinkedList(); + boolean runflag =false; + private String name; + boolean addrRegistor=true; + + public acceptClient(String _name) { + name = _name; + } + public acceptClient() { + new CreateThread(this); + } + + public synchronized void gethost(BufferedReader is,PrintStream os) { + String line,port,handover; + int intv_time = 100; + String request; + int treebranch = 2;//treeの子ノードの数 + String leaderflag="0",sendleaderflag="0";//Socket send standardization String + + + // クライアントからのメッセージを待ち、受け取ったメッセージをそのまま返す + 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)); + listupdate(port); + /* + ls.remove(Integer.parseInt(port)); + ls.add(Integer.parseInt(port),ls.getLast()); + ls.remove(Integer.parseInt(port)); + */ + counter--; + t++; + runflag = true; + /* + if(t % treebranch == 0){ + parentnum--; + } + */ + parentnum = (Integer.parseInt(port) -1) / treebranch; + + if((counter-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,port,handover,sendleaderflag,counter); + 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); + int g = 0; + for(String bs: ls){ + System.out.println(g+"番目"+bs); + g++; + } + System.out.println("num4=" + ls.get(Integer.parseInt(port))); + line=null; + runflag = false; + is.close(); + os.close(); + }else{ + if(addrRegistor==true){ + ls.add(add); + addrRegistor = false; + } + System.out.println(parentnum); + if(line != null){ + arg(line,ls); + counter++; + }else{ + break; + } + + if(counter>=treebranch+1){ + if((counter-1)%treebranch==0){ + leaderflag = "1"; + }else{ + leaderflag = "0"; + } + /* + if((counter-1)%treebranch==0){ + parentnum++; + } + */ + parentnum = (counter - 1) / treebranch; + // request = [p-1]; + request = ls.get(parentnum); + /* + os.println(request); + os.println(String.valueOf(parentnum)); + os.println(String.valueOf(counter)); + os.println(leaderflag); + */ + outputStream(os,request,String.valueOf(parentnum),String.valueOf(counter),leaderflag); + checkParameter(parentnum,counter,leaderflag); + }else{ + //treeの親ノードに接続する人に接続する人を教える + outputStream(os,name,"0",String.valueOf(counter),leaderflag); + /* + os.println(name);//あとで渡されたmainArgs[1](現在はname)を渡す予定,addに変更 + os.println("0"); + os.println(String.valueOf(counter)); + os.println(leaderflag); + */ + } + 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 + } + + /** + * @param port + * parent value + */ + void listupdate(String port) { + ls.remove(Integer.parseInt(port)); + ls.add(Integer.parseInt(port),ls.getLast()); + ls.removeLast(); + } + + void outputStream(PrintStream os,String request,String parentnum,String counter,String leaderflag) { + os.println(request); + os.println(parentnum); + os.println(counter); + os.println(leaderflag); + } + + void checkParameter(int parent,int counter,String leaderflag) { + System.out.println("pの値="+parentnum); + System.out.println("iの値="+counter); + System.out.println("leaderflag="+leaderflag + "\n"); + } + + void arg(String line,LinkedList ls) { + if(line != null){ + ls.add(line); + } + int g=0; + + for(String bs: ls){ + System.out.println(g+"番目"+bs); + g++; + } + } + +} + +/* +class sock { + void arg(String line,LinkedList 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,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"); + } + + } +} diff -r 343e0a107dfc -r c92ec7258953 src/myVncProxy/acceptThread.java --- a/src/myVncProxy/acceptThread.java Tue Jul 26 16:03:18 2011 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,31 +0,0 @@ -package myVncProxy; - -import java.net.Socket; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -public class acceptThread implements Runnable { - MyRfbProto rfb; - byte[] imageBytes; - - acceptThread(MyRfbProto _rfb) { - rfb = _rfb; - } - - public void run() { - rfb.selectPort(); - while (true) { - try { - Socket newCli = rfb.accept(); - - OutputStream os = newCli.getOutputStream(); - InputStream is = newCli.getInputStream(); - rfb.newClient(this, newCli, os, is); - } catch (IOException e) { - e.printStackTrace(); - System.out.println(e); - } - } - } -}