# HG changeset patch # User Yu Taninari # Date 1311843168 -32400 # Node ID 6e703f1000e75b680913234e04fe551130ccc96f # Parent 4423c9c5ab5b732a6aa6ff25a370a6ce4efeae5e# Parent 343e0a107dfc1f780451c16525ff45673f35faa8 merge diff -r 343e0a107dfc -r 6e703f1000e7 src/myVncProxy/AcceptClient.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/myVncProxy/AcceptClient.java Thu Jul 28 17:52:48 2011 +0900 @@ -0,0 +1,221 @@ +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/*落ちたときの親の番号をカウントするためのもの*/; + 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; + 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()); + parentnum = (Integer.parseInt(port) -1) / treebranch; + String hidenchild=ls.getLast(); + String newparent=ls.get(parentnum); + listupdate(port); + counter--; + runflag = true; + + if((counter-1)%treebranch==1) { // children in most young treenum have leaderflag 1 other 0 + sendleaderflag = "1"; + } else { + sendleaderflag = "0"; + } + + Child report = new Child(); + + report.reportLastNode(hidenchild,newparent,port,String.valueOf(parentnum), + sendleaderflag,counter); + + os.println(port); + Thread.sleep(intv_time); + is.close(); + os.close(); + + } else if("2".equals(line)) { + parentnum = (Integer.parseInt(port) -1) / treebranch; + String newparent=ls.get(parentnum); + + outputStream(os,newparent,String.valueOf(parentnum), + String.valueOf(counter),leaderflag); + + os.close(); + is.close(); + } 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"; + } + + parentnum = (counter - 1) / treebranch; + // request = [p-1]; + request = ls.get(parentnum); + + outputStream(os,request,String.valueOf(parentnum), + String.valueOf(counter),leaderflag); + + checkParameter(parentnum,counter,leaderflag); + } else { + //treeの親ノードに接続する人に接続する人を教える + outputStream(os,name,"0",String.valueOf(counter), + 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 treenum,String leaderflag) { + os.println(request); + os.println(parentnum); + os.println(treenum); + 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{ + + void reportLastNode(String hiddenchild,String newparent,String newtreenum,String newpnum,String newleaderflag,int i) throws IOException{ + try { + Socket echoSocket; + System.out.println(hiddenchild + "に接続します"); + echoSocket = new Socket(hiddenchild, 10001 + (i + 1));//i+1は実験中に同じマシーンを使っていたのでportを変えて対応、本番時には取り除く予定。 + + DataOutputStream os = new DataOutputStream(echoSocket.getOutputStream()); + + os.writeBytes(newparent+"\n"); + os.writeBytes(newpnum+"\n"); + os.writeBytes(newtreenum+"\n"); + os.writeBytes(newleaderflag+"\n"); + + os.close(); + } 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 6e703f1000e7 src/myVncProxy/AcceptThread.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/myVncProxy/AcceptThread.java Thu Jul 28 17:52:48 2011 +0900 @@ -0,0 +1,31 @@ +package myVncProxy; +import java.net.ServerSocket; +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); + } + } + } +} + diff -r 343e0a107dfc -r 6e703f1000e7 src/myVncProxy/CreateThread.java --- a/src/myVncProxy/CreateThread.java Tue Jul 26 16:03:18 2011 +0900 +++ b/src/myVncProxy/CreateThread.java Thu Jul 28 17:52:48 2011 +0900 @@ -11,10 +11,10 @@ public class CreateThread implements Runnable { ServerSocket echoServer; - acceptClient acceptClient; + AcceptClient acceptClient; - public CreateThread(acceptClient _acc){ + public CreateThread(AcceptClient _acc){ acceptClient = _acc; } diff -r 343e0a107dfc -r 6e703f1000e7 src/myVncProxy/MyRfbProto.java --- a/src/myVncProxy/MyRfbProto.java Tue Jul 26 16:03:18 2011 +0900 +++ b/src/myVncProxy/MyRfbProto.java Thu Jul 28 17:52:48 2011 +0900 @@ -516,7 +516,7 @@ } - void newClient(acceptThread acceptThread, final Socket newCli, + void newClient(AcceptThread acceptThread, final Socket newCli, final OutputStream os, final InputStream is) throws IOException { // createBimgFlag = true; // rfb.addSockTmp(newCli); diff -r 343e0a107dfc -r 6e703f1000e7 src/myVncProxy/VncProxyService.java --- a/src/myVncProxy/VncProxyService.java Tue Jul 26 16:03:18 2011 +0900 +++ b/src/myVncProxy/VncProxyService.java Thu Jul 28 17:52:48 2011 +0900 @@ -28,7 +28,7 @@ Thread accThread; Thread clientThread; CreateThread geth; - acceptClient acc; + AcceptClient acc; Frame vncFrame; Container vncContainer; @@ -106,7 +106,7 @@ }catch(Exception e){} rfbThread = new Thread(this); - accThread = new Thread(new acceptThread(rfb)); + accThread = new Thread(new AcceptThread(rfb)); } @@ -198,7 +198,7 @@ // void connectAndAuthenticate() throws Exception { - acc = new acceptClient(mainArgs[0]); + acc = new AcceptClient(mainArgs[0]); geth = new CreateThread(acc); Thread thread = new Thread(geth); thread.start(); diff -r 343e0a107dfc -r 6e703f1000e7 src/myVncProxy/VncViewer.java --- a/src/myVncProxy/VncViewer.java Tue Jul 26 16:03:18 2011 +0900 +++ b/src/myVncProxy/VncViewer.java Thu Jul 28 17:52:48 2011 +0900 @@ -136,7 +136,7 @@ rfbThread = new Thread(this); rfbThread.start(); - accThread = new Thread(new acceptThread(rfb)); + accThread = new Thread(new AcceptThread(rfb)); accThread.start(); } diff -r 343e0a107dfc -r 6e703f1000e7 src/myVncProxy/acceptClient.java --- a/src/myVncProxy/acceptClient.java Tue Jul 26 16:03:18 2011 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,239 +0,0 @@ -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 6e703f1000e7 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); - } - } - } -}