Mercurial > hg > Members > nobuyasu > tightVNCProxy
changeset 173:607f31debca1
modify AcceptClient.java
author | Yu Taninari <you@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 24 Oct 2011 20:15:24 +0900 |
parents | 1d16dcf966e1 |
children | eea9a1726edf |
files | src/myVncProxy/AcceptClient.java |
diffstat | 1 files changed, 142 insertions(+), 145 deletions(-) [+] |
line wrap: on
line diff
--- a/src/myVncProxy/AcceptClient.java Mon Oct 24 19:51:18 2011 +0900 +++ b/src/myVncProxy/AcceptClient.java Mon Oct 24 20:15:24 2011 +0900 @@ -1,6 +1,7 @@ package myVncProxy; import java.net.Socket; +import java.net.UnknownHostException; import java.io.*; import java.net.*; import java.util.*; @@ -11,11 +12,19 @@ boolean runflag = false; private String name; boolean addrRegistor = true; + int passCheck = 0; + int treebranch = 2;// treeの子ノードの数 + String newparent,request,myAddress; + String leaderflag = "0", sendleaderflag = "0"; + int intv_time = 100; + - public AcceptClient(String _name) { - name = _name; + + public AcceptClient(String name) { + this.name = name; } + public AcceptClient() { new CreateThread(this); } @@ -23,149 +32,35 @@ // public synchronized void transferParentAddrerss(BufferedReader // is,PrintStream os) { public synchronized void transferParentAddrerss(BufferedReader is,PrintStream os) { - String line, port; - int intv_time = 100; - String request; - int treebranch = 2;// treeの子ノードの数 - String leaderflag = "0", sendleaderflag = "0"; - - // クライアントからのメッセージを待ち、受け取ったメッセージをそのまま返す - try { - while (true) { - line = is.readLine(); - port = is.readLine(); - - System.out.println("データーを受信しましたlin=" + line - + " port=" + port); - // 自分の IPADRESSを取得する - InetAddress addr = InetAddress.getLocalHost(); - String myAddress = new String(addr.getHostAddress()); - - if ("1".equals(line)) { - System.out.println("親が落ちましたmessage" + port); - String checkRepetition = is.readLine(); - os.println(ls.getLast()); - parentnum = (Integer.parseInt(port) - 1) - / treebranch; - String newparent = ls.get(parentnum); - - counter--; - runflag = true; - - sendleaderflag = decisionLeader( - Integer.parseInt(port), treebranch); - - Child report = new Child(); - - report.reportLastNode(ls.getLast(), newparent, - port, String.valueOf(parentnum), - sendleaderflag, counter); - - listupdate(port); - - int g = 0; - for (String bs : ls) { - System.out.println(g + "番目" + bs); - g++; - } - - os.println(port); - // os.println(leaderflag); - - leaderflag = decisionLeader( - Integer.parseInt(checkRepetition), - treebranch); - - if (Integer.parseInt(checkRepetition) == counter + 1) { - checkRepetition = "stop"; - } else { - checkRepetition = "go"; - } - os.println(checkRepetition); - - // os.println(leaderflag); - 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)) { - String checkRepetition = is.readLine(); - System.out.println("落ちたのを確認しました"); - - os.println(ls.get(Integer.parseInt(port))); - os.println(port); - - if (checkRepetition.equals(ls.getLast())) { - checkRepetition = "stop"; - } else { -// checkRepetition = "go"; - if((Integer.parseInt(checkRepetition) -1)/treebranch - == Integer.parseInt(port)) - checkRepetition = "skip"; - } - os.println(checkRepetition); - - System.out.println("num4=" - + ls.get(Integer.parseInt(port))); - line = null; - runflag = false; - is.close(); - os.close(); - } else { - if (addrRegistor == true) { - ls.add(myAddress); - addrRegistor = false; - } - - if (line != null) { - addClientAdress(line, ls); - counter++; - } else { - break; - } - - if (counter >= treebranch + 1) { - - leaderflag = decisionLeader(counter, treebranch); - parentnum = (counter - 1) / treebranch; - - request = ls.get(parentnum); - System.out.println(parentnum); - - outputStream(os, request, - String.valueOf(parentnum), - String.valueOf(counter), leaderflag); - - checkParameter(parentnum, counter, leaderflag); - } else { - // treeの親ノードに接続する人に接続する人を教える - outputStream(os, myAddress, "0", - String.valueOf(counter), leaderflag); - } - Thread.sleep(intv_time); - } + // クライアントからのメッセージを待ち、受け取ったメッセージをそのまま返す + try { + while (true) { + String line = is.readLine(); + String port = is.readLine(); + myAddress = getMyAddress(); + if ("1".equals(line)) { + replyLeaderNode(os, is, port); + } else if ("2".equals(line)) { + replyNodeInformation(port); + listupdate(port, newparent); + outputStream(os, newparent, String.valueOf(parentnum), + port, leaderflag); + os.close(); + is.close(); + } else if ("3".equals(line)) { + replyNormalChildren(os, is, port); + line = null; + } else { + if(replyCreateTree(os, is, port, line)) { + break; } - - } catch (IOException e) { - System.out.println(e); } - - catch (InterruptedException e) { - e.printStackTrace(); - } - - + } + } catch (IOException e) { + System.out.println(e); + } catch (InterruptedException e) { + e.printStackTrace(); + } } /** @@ -177,6 +72,12 @@ ls.add(Integer.parseInt(port), ls.getLast()); ls.removeLast(); } + + void listupdate(String port,String myaddr) { + ls.remove(Integer.parseInt(port)); + ls.add(Integer.parseInt(port), myaddr); + ls.removeLast(); + } void outputStream(PrintStream os, String request, String parentnum, String treenum, String leaderflag) { @@ -193,10 +94,14 @@ } void addClientAdress(String line, LinkedList<String> ls) { - int g = 0; if (line != null) { ls.add(line); } + displyLinkedList(ls); + } + + void displyLinkedList( LinkedList<String> ls) { + int g = 0; for (String bs : ls) { System.out.println(g + "番目" + bs); g++; @@ -211,6 +116,98 @@ return "1"; } } + + String getMyAddress () throws UnknownHostException { + InetAddress addr = InetAddress.getLocalHost(); + return new String(addr.getHostAddress()); + } + + void replyNodeInformation (String port) { + parentnum = (Integer.parseInt(port) - 1) / treebranch; + newparent = ls.get(parentnum); + sendleaderflag = decisionLeader(Integer.parseInt(port), treebranch); + leaderflag = decisionLeader(Integer.parseInt(port),treebranch); + } + + void replyLeaderNode(PrintStream os,BufferedReader is,String port) throws IOException, InterruptedException { + String checkRepetition = is.readLine(); + os.println(ls.getLast()); + replyNodeInformation(port); + counter--; + passCheck = 1; + Child report = new Child(); + report.reportLastNode(ls.getLast(), newparent,port, String.valueOf(parentnum), + sendleaderflag, counter); + listupdate(port); + displyLinkedList(ls); + os.println(port); + runflag = true; + leaderflag = decisionLeader( + Integer.parseInt(checkRepetition), + treebranch); + if (Integer.parseInt(checkRepetition) == counter + 1) { + checkRepetition = "stop"; + } else { + checkRepetition = "go"; + } + os.println(checkRepetition); + Thread.sleep(intv_time); + is.close(); + os.close(); + } + + void replyNormalChildren(PrintStream os,BufferedReader is,String port) throws IOException { + String checkRepetition = is.readLine(); + System.out.println("落ちたのを確認しました"); + + os.println(ls.get(Integer.parseInt(port))); + os.println(port); + + + if(ls.size()-1+passCheck == Integer.parseInt(checkRepetition)) { + checkRepetition = "skip"; + } + passCheck = 0; + + os.println(checkRepetition); + + System.out.println("num4=" + + ls.get(Integer.parseInt(port))); + + runflag = false; + is.close(); + os.close(); + } + + boolean replyCreateTree(PrintStream os,BufferedReader is,String port,String line) throws InterruptedException { + if (addrRegistor == true) { + ls.add(myAddress); + addrRegistor = false; + } + + if (line != null) { + addClientAdress(line, ls); + counter++; + } else { + return true; + } + + if (counter >= treebranch + 1) { + leaderflag = decisionLeader(counter, treebranch); + parentnum = (counter - 1) / treebranch; + request = ls.get(parentnum); + System.out.println(parentnum); + outputStream(os, request,String.valueOf(parentnum), + String.valueOf(counter), leaderflag); + checkParameter(parentnum, counter, leaderflag); + } else { + // treeの親ノードに接続する人に接続する人を教える + outputStream(os, myAddress, "0", + String.valueOf(counter), leaderflag); + } + Thread.sleep(intv_time); + return false; + } } @@ -241,4 +238,4 @@ } } -} +} \ No newline at end of file