changeset 134:128cce60c43c

where to connect command
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 07 Jun 2014 15:57:03 +0900
parents 70cbec526039
children ada4d850a820
files src/main/java/jp/ac/u_ryukyu/treevnc/CreateConnectionParam.java src/main/java/jp/ac/u_ryukyu/treevnc/MyRfbProto.java src/main/java/jp/ac/u_ryukyu/treevnc/client/FindRoot.java src/main/java/jp/ac/u_ryukyu/treevnc/client/GetHostClient.java src/main/java/jp/ac/u_ryukyu/treevnc/client/MyRfbProtoClient.java src/main/java/jp/ac/u_ryukyu/treevnc/client/ReconnectionWaiter.java src/main/java/jp/ac/u_ryukyu/treevnc/client/TreeVncProtocol.java src/main/java/jp/ac/u_ryukyu/treevnc/server/AcceptClient.java src/main/java/jp/ac/u_ryukyu/treevnc/server/GetBroadCastProxy.java src/main/java/jp/ac/u_ryukyu/treevnc/server/TreeRootFinderListener.java src/main/java/jp/ac/u_ryukyu/treevnc/server/TreeVNCNode.java src/main/java/jp/ac/u_ryukyu/treevnc/server/VncProxyService.java
diffstat 12 files changed, 418 insertions(+), 497 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/CreateConnectionParam.java	Sat Jun 07 12:54:44 2014 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/treevnc/CreateConnectionParam.java	Sat Jun 07 15:57:03 2014 +0900
@@ -1,10 +1,12 @@
 package jp.ac.u_ryukyu.treevnc;
 
+import java.io.IOException;
+
 import com.glavsoft.viewer.ViewerImpl;
 import com.glavsoft.viewer.swing.ConnectionParams;
 
+import jp.ac.u_ryukyu.treevnc.client.FindRoot;
 import jp.ac.u_ryukyu.treevnc.client.TreeVncProtocol;
-import jp.ac.u_ryukyu.treevnc.client.GetHostClient;
 
 public class CreateConnectionParam {
 	private String hostName;
@@ -16,18 +18,25 @@
 	}
 
 	public void findTreeVncRoot() throws InterruptedException {
-		GetHostClient getBcast = new GetHostClient(rfb.acceptPort,this);
-		getBcast.getHost();
+		FindRoot getBcast = new FindRoot(rfb.acceptPort,this);
+		getBcast.findRoot();
 		// wait for RootSelection
         wait();
 	}
 
+	/**
+	 * To find parent, send WHERE_TO_CONNECT command to the TreeVNC root
+	 * Incoming CONNECT_TO message is handled in MyRFBProto
+	 * @param v
+	 */
 	public void createConnectionParam(ViewerImpl v) {
-		TreeVncProtocol echo = new TreeVncProtocol(hostName, 9999); 
+		TreeVncProtocol echo = new TreeVncProtocol(hostName,portNumber);
 		rfb.setEcho(echo);
-		rfb.setProxyAddr(hostName);
-		echo.getParentName();
-		v.setConnectionParam(echo.getParentsAddress(), portNumber);
+		try {
+            echo.whereToConnect(rfb.getMyAddress(),rfb.getAcceptPort());
+        } catch (IOException e) {
+            // cannot send where to connect
+        }
 	}
 	
 	public void runAcceptThread() {
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/MyRfbProto.java	Sat Jun 07 12:54:44 2014 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/treevnc/MyRfbProto.java	Sat Jun 07 15:57:03 2014 +0900
@@ -40,7 +40,6 @@
 	private RequestScreenThread rThread;
 	private boolean proxyFlag = true;
 	public TreeVncProtocol treeProtocol;
-	private String proxyAddr;
 	public int acceptPort = 0;
 	protected boolean readyReconnect = false;
 	private boolean cuiVersion;
@@ -121,12 +120,23 @@
 		    // this is a multicast message, cannot happen
 		    break;
         case ProtocolContext.WHERE_TO_CONNECT : 
+            handleWhereToConnect(port,hostname);
 		case ProtocolContext.LOST_PARENT :
 			System.out.println("get treeVNC command" + command);
 		}
 	}
 
 	/**
+	 * new clients ask root to where to connect
+	 * tell him his parent
+	 * @param port
+	 * @param hostname
+	 */
+	private void handleWhereToConnect(int port, String hostname) {
+        viewer.replyCreateTree(hostname,port);
+    }
+
+    /**
 	 * set new parent address
 	 * @param port
 	 * @param hostname
@@ -448,21 +458,16 @@
         context = workingProtocol;
     }
 
-
-	public Socket accept() throws IOException {
-		return null;
-	}
+    public Socket accept() throws IOException {
+        return servSock.accept();
+    }
 
-    public void initServSock(int port) throws IOException {
-        servSock = new ServerSocket(port);
-        acceptPort = port;
-    }
-	
     public int selectPort(int p) {
         int port = p;
         while (true) {
             try {
-                initServSock(port);
+                servSock = new ServerSocket(port);
+                acceptPort = port;
                 break;
             } catch (BindException e) {
                 port++;
@@ -524,10 +529,6 @@
 		return true;
 	}
 
-	public void setProxyAddr(String proxyAddr) {
-		this.proxyAddr = proxyAddr;
-	}
-
     void sendProxyFlag(OutputStream os) throws IOException {
         if (proxyFlag)
             os.write(1);
@@ -535,9 +536,7 @@
             os.write(0);
     }
 
-
-
-	public void close() {
+    public void close() {
 	    // none
 	}
 	
@@ -561,11 +560,7 @@
 	public void readCheckDelay(Reader reader) throws TransportException {
 		
 	}
-	
-	public String getProxyAddr() {
-		return proxyAddr;
-	}
-	
+
 	public synchronized void setReadyReconnect(boolean ready) {
 		readyReconnect = ready;
 		if (ready) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/jp/ac/u_ryukyu/treevnc/client/FindRoot.java	Sat Jun 07 15:57:03 2014 +0900
@@ -0,0 +1,168 @@
+package jp.ac.u_ryukyu.treevnc.client;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.DatagramPacket;
+import java.net.InetAddress;
+import java.net.MulticastSocket;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+import com.glavsoft.rfb.protocol.ProtocolContext;
+import com.glavsoft.viewer.swing.ConnectionParams;
+
+import jp.ac.u_ryukyu.treevnc.CreateConnectionParam;
+import jp.ac.u_ryukyu.treevnc.server.TreeRootFinderListener;
+
+public class FindRoot implements Runnable {
+	final int BUFSIZE = 1024;
+	private InetAddress mAddr;
+	private MulticastSocket soc;
+
+    private ServerSocket server = null;
+    private BufferedReader is;
+    private int port;
+    private boolean stopFlag;
+    private TreeVncRootSelectionPanel rootSelectionPanel = new TreeVncRootSelectionPanel();
+    private String proxyAddr;
+
+    /**
+     * To find vnc root, a client sends a multicast packet. 
+     * @param createConnectionParam 
+     * @param _str
+     */
+	public FindRoot(int _port, CreateConnectionParam createConnectionParam) {
+	    CreateConnectionParam cp = createConnectionParam;
+		port = _port;
+		rootSelectionPanel.setCp(cp);
+		createSocket();
+	}
+
+	public void createSocket() {		
+		try {
+			mAddr = InetAddress.getByName(TreeRootFinderListener.McastAddr);
+			soc = new MulticastSocket();
+			soc.setTimeToLive(1);
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+
+    /**
+     * send find root message.
+     * 
+     */
+	public void findRoot() {
+		ByteBuffer buf = ByteBuffer.allocate(12);
+		buf.order(ByteOrder.BIG_ENDIAN);
+		buf.put((byte) ProtocolContext.FIND_ROOT);
+		buf.put((byte) 0);
+		buf.put((byte) 0);
+		buf.put((byte) 0);
+		buf.putInt(4); // length
+		buf.putInt(port);
+		buf.flip();
+
+		DatagramPacket sendPacket = new DatagramPacket(buf.array(), buf.limit(), mAddr, ConnectionParams.DEFAULT_VNC_ROOT_FINDER);
+		try {
+			soc.send(sendPacket);
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		
+	}
+
+    
+    /**
+     * @return
+     * reply to findRootMassage
+     * 
+     */
+    public String textAddress() {
+        return rootSelectionPanel.getAddress();
+    }
+    
+    public String  textPort() {
+        return rootSelectionPanel.getPort();
+    }
+    
+    void socketClose() {
+        try {
+            rootSelectionPanel.unVisible();
+            is.close();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+    
+    /**
+     * TREEVNC_ROOT_FINDER is sent, wait for reply
+     */
+    @Override
+    public void run() {
+        try {
+            while(!stopFlag) {
+                Socket socket = server.accept();  
+                is = new BufferedReader(new InputStreamReader(
+                        socket.getInputStream()));
+                proxyAddr = is.readLine();
+                // proxyAddr format
+                // 5999:localhost:localhost:133.13.59.210:
+                // port:hostnae:hostname:ip address:
+                // GetBroadCastProxy create this string on VNC root
+                InetAddress adr = socket.getInetAddress();
+                String proxyAddr1 = adr.getHostName();
+                String proxyAddr2 = null;
+                if (proxyAddr1 != null) {
+                    int portIndex = proxyAddr.indexOf(":");
+                    int restIndex = proxyAddr.indexOf(":", portIndex+1);
+                    if (portIndex>0) {
+                        proxyAddr2 = proxyAddr.substring(0, portIndex)+":"+proxyAddr1 + ":" + proxyAddr.substring(restIndex+1);
+                    }
+                }
+                if(proxyAddr2!=null)
+                    rootSelectionPanel.checkBox(proxyAddr2);
+                rootSelectionPanel.setButton();
+                rootSelectionPanel.visible();
+            }
+            System.err.println("stop");
+        } catch (IOException e) {
+        }
+    }
+
+    public void setStopFlag(boolean stopFlag) {
+        this.stopFlag = stopFlag;
+        
+    }
+   
+    /**
+     * the thread is waiting accept system call,
+     * close the socket causes exception,
+     * which will terminated the thread.
+     * 
+     */
+    public void interrupt() {
+    	stopFlag = true;
+		try {
+			server.close();
+	    	soc.close();
+		} catch (IOException e) {
+		}
+        Thread.currentThread().interrupt();
+    }
+
+    public boolean isStopFlag() {
+        return stopFlag;
+    }
+    
+    public void ipRegister() {
+        rootSelectionPanel.ipRegister();
+    }
+
+    public String getProxyAddr() {
+        return proxyAddr;
+    }
+}
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/client/GetHostClient.java	Sat Jun 07 12:54:44 2014 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,172 +0,0 @@
-package jp.ac.u_ryukyu.treevnc.client;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.net.DatagramPacket;
-import java.net.InetAddress;
-import java.net.MulticastSocket;
-import java.net.ServerSocket;
-import java.net.Socket;
-import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
-
-import com.glavsoft.rfb.protocol.ProtocolContext;
-import com.glavsoft.viewer.swing.ConnectionParams;
-
-import jp.ac.u_ryukyu.treevnc.CreateConnectionParam;
-import jp.ac.u_ryukyu.treevnc.server.GetBroadCastProxy;
-
-public class GetHostClient implements Runnable {
-	final int BUFSIZE = 1024;
-	private InetAddress mAddr;
-	private MulticastSocket soc;
-
-    private ServerSocket server = null;
-    private BufferedReader is;
-    private int port;
-    private boolean stopFlag;
-    private TreeVncRootSelectionPanel rootSelectionPanel = new TreeVncRootSelectionPanel();
-    private String proxyAddr;
-
-    /**
-     * To find vnc root, a client sends a multicast packet. 
-     * @param createConnectionParam 
-     * @param _str
-     */
-	public GetHostClient(int _port, CreateConnectionParam createConnectionParam) {
-	    CreateConnectionParam cp = createConnectionParam;
-		port = _port;
-		rootSelectionPanel.setCp(cp);
-	}
-
-	public void createSocket() {		
-		try {
-			mAddr = InetAddress.getByName(GetBroadCastProxy.McastAddr);
-			soc = new MulticastSocket();
-			soc.setTimeToLive(1);
-		} catch (IOException e) {
-			e.printStackTrace();
-		}
-	}
-
-	public void getHost() {
-		createSocket();
-		findRoot();
-	}
-
-    /**
-     * send find root message.
-     * 
-     */
-	public void findRoot() {
-		ByteBuffer buf = ByteBuffer.allocate(12);
-		buf.order(ByteOrder.BIG_ENDIAN);
-		buf.put((byte) ProtocolContext.FIND_ROOT);
-		buf.put((byte) 0);
-		buf.put((byte) 0);
-		buf.put((byte) 0);
-		buf.putInt(4); // length
-		buf.putInt(port);
-		buf.flip();
-
-		DatagramPacket sendPacket = new DatagramPacket(buf.array(), buf.limit(), mAddr, ConnectionParams.DEFAULT_VNC_ROOT_FINDER);
-		try {
-			soc.send(sendPacket);
-		} catch (IOException e) {
-			e.printStackTrace();
-		}
-		
-	}
-
-    
-    /**
-     * @return
-     * reply to findRootMassage
-     * 
-     */
-    public String textAddress() {
-        return rootSelectionPanel.getAddress();
-    }
-    
-    public String  textPort() {
-        return rootSelectionPanel.getPort();
-    }
-    
-    void socketClose() {
-        try {
-            rootSelectionPanel.unVisible();
-            is.close();
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
-    }
-    
-    /**
-     * TREEVNC_ROOT_FINDER is sent, wait for reply
-     */
-    @Override
-    public void run() {
-        try {
-            while(!stopFlag) {
-                Socket socket = server.accept();  
-                is = new BufferedReader(new InputStreamReader(
-                        socket.getInputStream()));
-                proxyAddr = is.readLine();
-                // proxyAddr format
-                // 5999:localhost:localhost:133.13.59.210:
-                // port:hostnae:hostname:ip address:
-                // GetBroadCastProxy create this string on VNC root
-                InetAddress adr = socket.getInetAddress();
-                String proxyAddr1 = adr.getHostName();
-                String proxyAddr2 = null;
-                if (proxyAddr1 != null) {
-                    int portIndex = proxyAddr.indexOf(":");
-                    int restIndex = proxyAddr.indexOf(":", portIndex+1);
-                    if (portIndex>0) {
-                        proxyAddr2 = proxyAddr.substring(0, portIndex)+":"+proxyAddr1 + ":" + proxyAddr.substring(restIndex+1);
-                    }
-                }
-                if(proxyAddr2!=null)
-                    rootSelectionPanel.checkBox(proxyAddr2);
-                rootSelectionPanel.setButton();
-                rootSelectionPanel.visible();
-            }
-            System.err.println("stop");
-        } catch (IOException e) {
-        }
-    }
-
-    public void setStopFlag(boolean stopFlag) {
-        this.stopFlag = stopFlag;
-        
-    }
-   
-    /**
-     * the thread is waiting accept system call,
-     * close the socket causes exception,
-     * which will terminated the thread.
-     * 
-     */
-    public void interrupt() {
-    	stopFlag = true;
-		try {
-			server.close();
-	    	soc.close();
-		} catch (IOException e) {
-		}
-        Thread.currentThread().interrupt();
-    }
-
-    public boolean isStopFlag() {
-        return stopFlag;
-    }
-    
-    public void ipRegister() {
-        rootSelectionPanel.ipRegister();
-    }
-
-    public String getProxyAddr() {
-        return proxyAddr;
-    }
-}
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/client/MyRfbProtoClient.java	Sat Jun 07 12:54:44 2014 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/treevnc/client/MyRfbProtoClient.java	Sat Jun 07 15:57:03 2014 +0900
@@ -83,29 +83,32 @@
 		}
 	}
 	
+	/* 
+	 * measure client delay time using tree connection. It should be summed up in upward tree communication 
+	 */
 	@Override
 	public void readCheckDelay(Reader reader) throws TransportException {
-		ByteBuffer buf = multicastqueue.allocate(24);
-		reader.readBytes(buf.array(), 0, 24);
-		
-		LinkedList<ByteBuffer> sendData = new LinkedList<ByteBuffer>();
-		sendData.add(buf);
-		Socket echoSocket;
-		try {
-			echoSocket = new Socket(getProxyAddr(), 10002);
-			BufferedReader is = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
-			DataOutputStream os = new DataOutputStream(echoSocket.getOutputStream());
-			os.writeBytes("checkdelay\n");
-			os.writeBytes(String.valueOf(buf.getLong(16))+"\n");
-			System.out.println("delay"+ is.readLine());
-		} catch (UnknownHostException e) {
-			e.printStackTrace();
-		} catch (IOException e) {
-			e.printStackTrace();
-		}
-		buf.position(0);
-		
-		multicastqueue.put(sendData);
+//		ByteBuffer buf = multicastqueue.allocate(24);
+//		reader.readBytes(buf.array(), 0, 24);
+//		
+//		LinkedList<ByteBuffer> sendData = new LinkedList<ByteBuffer>();
+//		sendData.add(buf);
+//		Socket echoSocket;
+//		try {
+//			echoSocket = new Socket(getProxyAddr(), 10002);
+//			BufferedReader is = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
+//			DataOutputStream os = new DataOutputStream(echoSocket.getOutputStream());
+//			os.writeBytes("checkdelay\n");
+//			os.writeBytes(String.valueOf(buf.getLong(16))+"\n");
+//			System.out.println("delay"+ is.readLine());
+//		} catch (UnknownHostException e) {
+//			e.printStackTrace();
+//		} catch (IOException e) {
+//			e.printStackTrace();
+//		}
+//		buf.position(0);
+//		
+//		multicastqueue.put(sendData);
 		
 	}
 }
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/client/ReconnectionWaiter.java	Sat Jun 07 12:54:44 2014 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,48 +0,0 @@
-package jp.ac.u_ryukyu.treevnc.client;
-
-import java.io.IOException;
-import java.net.BindException;
-import java.net.ServerSocket;
-import java.net.Socket;
-
-
-/**
- *  Wait for reconnection message.
- *  Close current application and reopen it.
- */
-public class ReconnectionWaiter extends Thread {
-	private TreeVncProtocol echo;
-	
-	public ReconnectionWaiter(String treenum, TreeVncProtocol echo) {
-		this.echo = echo;
-	}
-
-	
-	public void run() {
-		ServerSocket echoServer = null;
-		try {
-			echoServer = new ServerSocket(10001);
-			while (true) {
-				Socket clientSocket = echoServer.accept();
-				if (clientSocket != null) {
-					echo.client.setTeminationType(true);
-					echo.client.closeApp();
-					echo.client.setSocket(echo.createSocketForClient(clientSocket,true));
-					echo.client.run();
-					clientSocket.close();
-				}
-			}
-		} catch (BindException e) {
-			return;
-		} catch (IOException e) {
-			System.out.println(e);
-		} finally {
-			try {
-				if (echoServer != null)
-					echoServer.close();
-			} catch (IOException e) {
-				return;
-			}
-		}
-	}
-}
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/client/TreeVncProtocol.java	Sat Jun 07 12:54:44 2014 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/treevnc/client/TreeVncProtocol.java	Sat Jun 07 15:57:03 2014 +0900
@@ -13,7 +13,6 @@
 	private DataOutputStream os = null;
 	private Socket echoSocket = null;
 	private boolean runflag = false;
-	private ReconnectionWaiter waitReply;
 	private Socket clientSocket = null;
 	private int echoPort;
 	public ViewerImpl client;
@@ -123,8 +122,6 @@
 			} catch (IOException e) {
 				System.err.println("IOException: " + e);
 			}
-			waitReply = new ReconnectionWaiter(treeNum, this);
-			waitReply.start();
 		}
 		return this;
 	}
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/server/AcceptClient.java	Sat Jun 07 12:54:44 2014 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/treevnc/server/AcceptClient.java	Sat Jun 07 15:57:03 2014 +0900
@@ -6,6 +6,9 @@
 import java.net.*;
 import java.util.*;
 
+import jp.ac.u_ryukyu.treevnc.MyRfbProto;
+import jp.ac.u_ryukyu.treevnc.client.TreeVncProtocol;
+
 public class AcceptClient {
 	private int nodeCounter = 0, parentnum = 0;
 	private LinkedList<TreeVNCNode> nodeList = new LinkedList<TreeVNCNode>();
@@ -13,11 +16,14 @@
 	boolean runflag = false;
 	private final int treebranch = 2;
 	private String newparent, request, myAddress;
-	private String leaderflag = "0", sendleaderflag = "0";
+	private int leaderflag = 0;
+	private int sendleaderflag = 0;
 	private final int intv_time = 100;
 	private String defaultVNCport = "5999";
+    private MyRfbProto rfb;
 
-	public AcceptClient() {
+	public AcceptClient(MyRfbProto rfb) {
+	    this.rfb = rfb;
 	}
 	
 	public void transferParentAddrerss(BufferedReader is, PrintStream os) {
@@ -29,13 +35,13 @@
 				if ("1".equals(line) || "3".equals(line)) {
 					String treeNumber = is.readLine();
 					// reply to Parents lost node
-					replyLeaderNode(os, is, port, Integer.parseInt(treeNumber));
+					replyLeaderNode(os, is, Integer.parseInt(port), Integer.parseInt(treeNumber));
 				} else if ("2".equals(line)) {
 					// reply to not Found Parents
-					replyNodeInformation(port);
+					replyNodeInformation(Integer.parseInt(port));
 					listupdate(port, newparent);
 					outputStream(os, newparent, String.valueOf(parentnum),
-							port, leaderflag);
+							port, leaderflag==1?"1":"0");
 					os.close();
 					is.close();
 				} else if (line != null) {
@@ -44,11 +50,11 @@
 						outputStream(os, myAddress, "0", "0", "0");
 						break;
 					} else {
-						if (replyCreateTree(os, port, line)) {
-							break;
-						} else {
-							break;
-						}
+//						if (replyCreateTree(os, port, line)) {
+//							break;
+//						} else {
+//							break;
+//						}
 					}
 				}
 			}
@@ -75,9 +81,9 @@
 	 * @param port
 	 *            parent value
 	 */
-	private synchronized void listupdate(String port) {
-		nodeList.remove(Integer.parseInt(port));
-		nodeList.add(Integer.parseInt(port), nodeList.getLast());
+	private synchronized void listupdate(int port) {
+		nodeList.remove(port);
+		nodeList.add(port, nodeList.getLast());
 		nodeList.removeLast();
 	}
 
@@ -95,25 +101,24 @@
 		os.println(leaderflag);
 	}
 
-	private void checkParameter(int parent, int counter, String leaderflag) {
+	private void checkParameter(int parent, int counter, int leaderflag2) {
 		System.out.println("number p =" + parentnum);
 		System.out.println("number i =" + counter);
-		System.out.println("leaderflag=" + leaderflag + "\n");
+		System.out.println("leaderflag=" + leaderflag2 + "\n");
 	}
 
 	private synchronized void addClientAdress(String line, LinkedList<TreeVNCNode> nodeList2) {
 		if (line != null) {
 			nodeList2.add(new TreeVNCNode(line));
 		}
-		// displyLinkedList(ls);
 	}
 
-	private String decisionLeader(int counter, int treebranch) {
+	private int decisionLeader(int counter, int treebranch) {
 		if ((counter - 1) % treebranch == 1) { // children in most young treenum
 												// have leaderflag 1 other 0
-			return "0";
+			return 0;
 		} else {
-			return "1";
+			return 1;
 		}
 	}
 
@@ -127,13 +132,13 @@
 		return addr.getHostAddress();
 	}
 
-	private void replyNodeInformation(String port) {
-		parentnum = (Integer.parseInt(port) - 1) / treebranch;
+	private void replyNodeInformation(int port) {
+		parentnum = (port - 1) / treebranch;
 		newparent = nodeList.get(parentnum).getHostname();
-		sendleaderflag = decisionLeader(Integer.parseInt(port), treebranch);
+		sendleaderflag = decisionLeader(port, treebranch);
 	}
 
-	private synchronized void replyLeaderNode(PrintStream os, BufferedReader is, String port, int treeNumber) throws IOException,
+	private synchronized void replyLeaderNode(PrintStream os, BufferedReader is, int port, int treeNumber) throws IOException,
 			InterruptedException {
 		String lastNode = nodeList.getLast().getHostname();
 		String checkSameHost = nodeList.get(treeNumber).getHostname();
@@ -142,7 +147,7 @@
 		}
 		replyNodeInformation(port);
 		nodeCounter--;
-		reportLastNode(lastNode, newparent, port, String.valueOf(parentnum), sendleaderflag);
+		reportLastNode(lastNode, newparent, port, parentnum, sendleaderflag);
 		listupdate(port);
 		if(lastNode != checkSameHost) {
 			os.println(this.defaultVNCport);
@@ -175,38 +180,32 @@
 		}
 	}
 	
-	private synchronized boolean replyCreateTree(PrintStream os, String port,
-			String line) throws InterruptedException {
-		if (addrRegistor == true) {
-			nodeList.add(new TreeVNCNode(myAddress));
-			addrRegistor = false;
-		}
-
-		if (line != null) {
-			addClientAdress(line, nodeList);
-			nodeCounter++;
-		} else {
-			return true;
-		}
-
-		if (nodeCounter >= treebranch + 1) {
-			leaderflag = decisionLeader(nodeCounter, treebranch);
-			parentnum = (nodeCounter - 1) / treebranch;
-			request = nodeList.get(parentnum).getHostname();
-			System.out.println(parentnum);
-			outputStream(os, request, String.valueOf(parentnum),
-					String.valueOf(nodeCounter), leaderflag);
-			checkParameter(parentnum, nodeCounter, leaderflag);
-		} else {
-			outputStream(os, myAddress, "0", String.valueOf(nodeCounter),
-					leaderflag);
-		}
-		Thread.sleep(intv_time);
-		return false;
+	public void replyCreateTree(String hostname, int port) {
+	    if (addrRegistor == true) {
+	        nodeList.add(new TreeVNCNode(hostname,port));
+	        addrRegistor = false;
+	    }
+	    addClientAdress(hostname, nodeList);
+	    nodeCounter++;
+	    TreeVncProtocol treeProtocol = new TreeVncProtocol(hostname,port);
+	    try {
+	        if (nodeCounter >= treebranch + 1) {
+	            leaderflag = decisionLeader(nodeCounter, treebranch);
+	            parentnum = (nodeCounter - 1) / treebranch;
+	            request = nodeList.get(parentnum).getHostname();
+	            checkParameter(parentnum, nodeCounter, leaderflag);
+	            treeProtocol.connectTo(request,port,leaderflag);
+	        } else {
+	            // connect to me
+	            treeProtocol.connectTo(rfb.getMyAddress(),rfb.getAcceptPort(),leaderflag);
+	        }
+	    } catch (IOException e) {
+	        // log
+	    }
 	}
 
-	void reportLastNode(String newchild, String newparent, String newtreenum,
-			String newpnum, String newleaderflag) throws IOException {
+	void reportLastNode(String newchild, String newparent, int newtreenum,
+			int newpnum, int newleaderflag) throws IOException {
 		try {
 			System.out.println(newchild + "connect");
 			Socket echoSocket = new Socket(newchild, 10001);
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/server/GetBroadCastProxy.java	Sat Jun 07 12:54:44 2014 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,162 +0,0 @@
-package jp.ac.u_ryukyu.treevnc.server;
-
-import java.io.IOException;
-import java.io.PrintStream;
-import java.net.DatagramPacket;
-import java.net.InetAddress;
-import java.net.MulticastSocket;
-import java.net.Socket;
-import java.net.SocketAddress;
-import java.net.UnknownHostException;
-
-import jp.ac.u_ryukyu.treevnc.client.TreeVncProtocol;
-
-import com.glavsoft.rfb.protocol.ProtocolContext;
-import com.glavsoft.viewer.swing.ConnectionParams;
-
-//import TextBoxProxy;
-
-public class GetBroadCastProxy implements Runnable {
-	public static final String McastAddr = "224.0.0.1";
-	static final int BufSize = 1024;
-	private boolean stopFlag = false;
-	private VncProxyService vps;
-	public static String MULTICAST_FIND_TREEVNC_ROOT = "TREEVNC-who:";
-	private MulticastSocket soc;
-    private String bCast;
-
-	public GetBroadCastProxy(VncProxyService _vps,String desktopName,String host){
-	    String myaddress = getMyAddress();
-	    // getMyAddress is not always connectable eg. in private segment.
-		vps = _vps;
-		bCast = vps.getRfb().getAcceptPort()+":"+host+":"
-				+desktopName+":"+myaddress+":";
-	}
-	
-	/**
-	 * To find TreeVNC root, a client sends me a multicast, reply our address to him.
-	 *  It contains a port to receive, so multiple TREEVNC clients can run on a PC. 
-	 */
-	private void replyToRootSearchMulticast() {
-		byte[] buf = new byte[BufSize];
-		try {
-			InetAddress mAddr = InetAddress.getByName(McastAddr);
-			soc = new MulticastSocket(ConnectionParams.DEFAULT_VNC_ROOT_FINDER);
-			DatagramPacket recvPacket = new DatagramPacket(buf, BufSize);
-			soc.joinGroup(mAddr);
-			while (!stopFlag) {
-				soc.receive(recvPacket);
-//				String address = getAddress(recvPacket.getSocketAddress());
-		        byte[] reply = recvPacket.getData();
-				int len = recvPacket.getLength();
-				if (len != 12) {
-					continue;
-				}
-				if ((reply[0]&0xff) != ProtocolContext.FIND_ROOT) {
-					continue;
-				}
-				int port = 0;
-				port = reply[8];
-				port = port * 256 + reply[9];
-				port = port * 256 + reply[10];
-				port = port * 256 + reply[11];
-				
-				TreeVncProtocol t = new TreeVncProtocol("", port);
-				try {
-					t.findRootReply(getMyAddress(), vps.getRfb().getAcceptPort());
-				} catch (IOException e) {
-					e.printStackTrace();
-				}
-				if(stopFlag) break;
-			}
-		} catch (Exception e) {
-			
-		}
-	}
-	
-	public int parse_code( byte[] bs,int offset,int len )
-	{
-	    int intval = 0;
-	    for( int i = offset; i < len ; i++ ) {
-	        if  (bs[i]==0 ) return intval;
-	        intval = intval * 10 + ( bs[ i ] - '0' );
-	    }
-	    return intval;
-	}
-	
-
-    /**
-     * To find me (TreeVNC root) ,a client send a broadcast with port number.
-     * Send  the parameter to connect to him.
-     * The client's GetDataClient will receive this message.
-     * 
-     * @param addr
-     * @param port
-     * @param str 
-     */
-	private void replyToClient(String addr, int port_,String str_) {
-	    final String address = addr;
-	    final int port = port_;
-	    final String str = str_;
-
-		TreeVncProtocol t = new TreeVncProtocol("", port_);
-		try {
-			t.findRootReply(str_, port_);
-		} catch (IOException e) {
-			e.printStackTrace();
-		}
-	    
-	    Runnable sender = new Runnable() {
-			public void run() {
-			        try {
-			            Thread.sleep(1000);
-			            Socket socket = new Socket(address, port);  // This is a TCP stream to reply
-			            PrintStream os = new PrintStream(socket.getOutputStream());
-			            os.println(str);
-			            os.print(Integer.toString(port));
-			            os.close();
-			            socket.close();
-			        } catch (IOException e) {
-			            System.out.println("Connection faild");
-			        } catch (InterruptedException e) {
-			            System.out.println("Connection faild");
-			        }
-			}
-		};
-		
-		new Thread(sender).start();
-	}
-
-	private String getAddress(SocketAddress addr) {
-		String str = addr.toString();
-		str = str.substring(1,str.indexOf(":"));
-		return str;
-	}
-	
-	public void run() {
-		replyToRootSearchMulticast();
-	}
-
-	public void setStopFlag(boolean stopFlag) {
-		this.stopFlag = stopFlag;
-	}
-
-	public boolean isStopFlag() {
-		return stopFlag;
-	}
-	
-	/**
-	 *  getLocalHost() returns hostname's address. It may not connectable, but
-	 *  it gives readable hostname. Do not use it to connect.
-	 * @return
-	 */
-	String getMyAddress () {
-		InetAddress addr = null;
-		try {
-			addr = InetAddress.getLocalHost();
-		} catch (UnknownHostException e) {
-			return "localhost";
-		}
-		return addr.getHostAddress();
-	}
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/jp/ac/u_ryukyu/treevnc/server/TreeRootFinderListener.java	Sat Jun 07 15:57:03 2014 +0900
@@ -0,0 +1,102 @@
+package jp.ac.u_ryukyu.treevnc.server;
+
+import java.io.IOException;
+import java.net.DatagramPacket;
+import java.net.InetAddress;
+import java.net.MulticastSocket;
+import java.net.UnknownHostException;
+
+import jp.ac.u_ryukyu.treevnc.client.TreeVncProtocol;
+
+import com.glavsoft.rfb.protocol.ProtocolContext;
+import com.glavsoft.viewer.swing.ConnectionParams;
+
+public class TreeRootFinderListener implements Runnable {
+	public static final String McastAddr = "224.0.0.1";
+	static final int BufSize = 1024;
+	private boolean stopFlag = false;
+	private VncProxyService vps;
+	private MulticastSocket soc;
+
+	public TreeRootFinderListener(VncProxyService _vps) {
+		vps = _vps;
+	}
+	
+	/**
+	 * To find TreeVNC root, a client sends me a multicast, reply our address to him.
+	 *  It contains a port to receive, so multiple TREEVNC clients can run on a PC. 
+	 */
+	private void replyToRootSearchMulticast() {
+		byte[] buf = new byte[BufSize];
+		try {
+			InetAddress mAddr = InetAddress.getByName(McastAddr);
+			soc = new MulticastSocket(ConnectionParams.DEFAULT_VNC_ROOT_FINDER);
+			DatagramPacket recvPacket = new DatagramPacket(buf, BufSize);
+			soc.joinGroup(mAddr);
+			while (!stopFlag) {
+				soc.receive(recvPacket);
+		        byte[] reply = recvPacket.getData();
+				int len = recvPacket.getLength();
+				if (len != 12) {
+					continue;
+				}
+				if ((reply[0]&0xff) != ProtocolContext.FIND_ROOT) {
+					continue;
+				}
+				int port = 0;
+				port = reply[8];
+				port = port * 256 + reply[9];
+				port = port * 256 + reply[10];
+				port = port * 256 + reply[11];
+				
+				TreeVncProtocol t = new TreeVncProtocol("", port);
+				try {
+					t.findRootReply(getMyAddress(), vps.getRfb().getAcceptPort());
+				} catch (IOException e) {
+					e.printStackTrace();
+				}
+				if(stopFlag) break;
+			}
+		} catch (Exception e) {
+			
+		}
+	}
+	
+	public int parse_code( byte[] bs,int offset,int len )
+	{
+	    int intval = 0;
+	    for( int i = offset; i < len ; i++ ) {
+	        if  (bs[i]==0 ) return intval;
+	        intval = intval * 10 + ( bs[ i ] - '0' );
+	    }
+	    return intval;
+	}
+	
+
+    public void run() {
+		replyToRootSearchMulticast();
+	}
+
+	public void setStopFlag(boolean stopFlag) {
+		this.stopFlag = stopFlag;
+	}
+
+	public boolean isStopFlag() {
+		return stopFlag;
+	}
+	
+	/**
+	 *  getLocalHost() returns hostname's address. It may not connectable, but
+	 *  it gives readable hostname. Do not use it to connect.
+	 * @return
+	 */
+	String getMyAddress () {
+		InetAddress addr = null;
+		try {
+			addr = InetAddress.getLocalHost();
+		} catch (UnknownHostException e) {
+			return "localhost";
+		}
+		return addr.getHostAddress();
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/jp/ac/u_ryukyu/treevnc/server/TreeVNCNode.java	Sat Jun 07 15:57:03 2014 +0900
@@ -0,0 +1,25 @@
+package jp.ac.u_ryukyu.treevnc.server;
+
+import com.glavsoft.viewer.swing.ConnectionParams;
+
+
+
+public class TreeVNCNode {
+    String hostname;
+    int port;
+    
+    public TreeVNCNode(String myaddr) {
+        hostname = myaddr;
+        port = ConnectionParams.DEFAULT_VNC_ROOT;
+    }
+
+    public TreeVNCNode(String hostname, int port) {
+        this.hostname = hostname;
+        this.port = port;
+    }
+
+    public String getHostname() {
+        return hostname;
+    }
+
+}
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/server/VncProxyService.java	Sat Jun 07 12:54:44 2014 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/treevnc/server/VncProxyService.java	Sat Jun 07 15:57:03 2014 +0900
@@ -30,7 +30,7 @@
 	private boolean forceReconnection;
 	private String reconnectionReason;
 	private AcceptThread acceptThread;
-	private GetBroadCastProxy getCast;
+	private TreeRootFinderListener getCast;
 	private AcceptClient clients;
 	public int opendPort;
 	static VncProxyService currentVps;
@@ -76,7 +76,7 @@
 	private void initProxy1(String hostName) {
 		myRfb = new MyRfbProtoProxy();
 		myRfb.setVncProxy(this);
-		clients = new AcceptClient();
+		clients = new AcceptClient(myRfb);
 		isApplet = false;
 		setIsTreeVNC(true);
 		setConnectionParam(hostName,vncport);
@@ -108,16 +108,17 @@
 		createConnectionAndStart();
 	}
 
+	/**
+	 * start accepting children
+	 * run rootFinderListener if necessary
+	 */
 	public void createConnectionAndStart() {
-		Thread thread;
 		opendPort = myRfb.selectPort(ConnectionParams.DEFAULT_VNC_ROOT);
 		acceptThread = new AcceptThread(myRfb, opendPort);
-		thread = new Thread(acceptThread, "TreeVNC-accept");
+		Thread thread = new Thread(acceptThread, "TreeVNC-accept");
 		thread.start();
 		if(firstTime) {
-			getCast = new GetBroadCastProxy(this,
-					this.connectionParams.getHostName(),
-					this.connectionParams.getHostName());
+			getCast = new TreeRootFinderListener(this);
 			thread = new Thread(getCast);
 			thread.start();
 		}
@@ -219,4 +220,8 @@
 	public void createRootSelectionPanel() {
 		rootSelectionPanel = new TreeVncRootSelectionPanel();
 	}
+
+    public void replyCreateTree(String hostname, int port) {
+        clients.replyCreateTree(hostname,port);
+    }
 }