Mercurial > hg > Members > nobuyasu > tightVNCProxy
view src/myVncProxy/AcceptClient.java @ 191:b2f0cd0cff6c default tip
Added tag Version-1.0 for changeset 79046b4e5990
author | Yu Taninari <you@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 29 Nov 2011 15:52:44 +0900 |
parents | d69a89f6d14a |
children |
line wrap: on
line source
package myVncProxy; import java.net.Socket; import java.net.UnknownHostException; import java.io.*; import java.net.*; import java.util.*; public class AcceptClient { int counter = 0, parentnum = 0/* 落ちたときの親の番号をカウントするためのもの */; LinkedList<String> ls = new LinkedList<String>(); 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) { this.name = name; } public AcceptClient() { new CreateThread(this); } // public synchronized void transferParentAddrerss(BufferedReader // is,PrintStream os) { public synchronized void transferParentAddrerss(BufferedReader is,PrintStream os) { // クライアントからのメッセージを待ち、受け取ったメッセージをそのまま返す try { while (true) { String line = is.readLine(); String port = is.readLine(); myAddress = getMyAddress(); if ("1".equals(line)) { //Parents lost replyLeaderNode(os, is, port); } else if ("2".equals(line)) { //not Found Parents replyNodeInformation(port); listupdate(port, newparent); outputStream(os, newparent, String.valueOf(parentnum), port, leaderflag); os.close(); is.close(); } else if ("3".equals(line)) { //lost time send to not leader children replyNormalChildren(os, is, port); line = null; } else if (line!=null){ //connection First time if(checkAddress(line)){ outputStream(os, ls.getFirst(),"0","0", "0"); break; }else { if(replyCreateTree(os, port, line)) { break; } else { break; } } } } } catch (IOException e) { System.out.println(e); } catch (InterruptedException e) { e.printStackTrace(); } } private boolean checkAddress(String line){ String test[] = line.split("\\."); int a = Integer.parseInt(test[0]); int b = Integer.parseInt(test[1]); if((192==a&&b==168)||(172==a&&(b>15||b<32))||10==a) { return true; } else{ return false; } } /** * @param port * parent value */ private void listupdate(String port) { ls.remove(Integer.parseInt(port)); ls.add(Integer.parseInt(port), ls.getLast()); ls.removeLast(); } private void listupdate(String port,String myaddr) { ls.remove(Integer.parseInt(port)); ls.add(Integer.parseInt(port), myaddr); ls.removeLast(); } private void outputStream(PrintStream os, String request, String parentnum, String treenum, String leaderflag) { os.println(request); os.println(parentnum); os.println(treenum); os.println(leaderflag); } private void checkParameter(int parent, int counter, String leaderflag) { System.out.println("pの値=" + parentnum); System.out.println("iの値=" + counter); System.out.println("leaderflag=" + leaderflag + "\n"); } private void addClientAdress(String line, LinkedList<String> ls) { if (line != null) { ls.add(line); } displyLinkedList(ls); } private void displyLinkedList( LinkedList<String> ls) { int g = 0; for (String bs : ls) { System.out.println(g + "番目" + bs); g++; } } private String decisionLeader(int counter, int treebranch) { if ((counter - 1) % treebranch == 1) { // children in most young treenum // have leaderflag 1 other 0 return "0"; } else { return "1"; } } String getMyAddress () { InetAddress addr = null; try { addr = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace(); } return new String(addr.getHostAddress()); } private 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); } private 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(); } private void replyNormalChildren(PrintStream os,BufferedReader is,String port) throws IOException { String checkRepetition = is.readLine(); 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(); } private boolean replyCreateTree(PrintStream os,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; } } 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"); } } }