diff src/main/java/jp/ac/u_ryukyu/treevnc/MyRfbProto.java @ 133:70cbec526039

connection handling
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 07 Jun 2014 12:54:44 +0900
parents 95f53663295c
children 128cce60c43c
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/MyRfbProto.java	Sat Jun 07 01:57:58 2014 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/treevnc/MyRfbProto.java	Sat Jun 07 12:54:44 2014 +0900
@@ -1,11 +1,12 @@
 package jp.ac.u_ryukyu.treevnc;
 
 import java.io.IOException;
-
 import java.io.OutputStream;
 import java.net.BindException;
 import java.net.ServerSocket;
 import java.net.Socket;
+import java.net.SocketException;
+import java.net.UnknownHostException;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 import java.util.LinkedList;
@@ -38,7 +39,7 @@
 	public MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MulticastQueue<LinkedList<ByteBuffer>>();
 	private RequestScreenThread rThread;
 	private boolean proxyFlag = true;
-	private TreeVncProtocol echo;
+	public TreeVncProtocol treeProtocol;
 	private String proxyAddr;
 	public int acceptPort = 0;
 	protected boolean readyReconnect = false;
@@ -87,8 +88,10 @@
 	 * @param b   12byte header ( command 4byte, length 4byte, port 4byte, option String )
 	 * @param is
 	 * @param os
+	 * @throws TransportException 
+	 * @throws IOException 
 	 */
-	private void treeVncCommand(byte[] b, Reader is, Writer os) {
+	private void treeVncCommand(byte[] b, Reader is, Writer os) throws TransportException, IOException {
 		ByteBuffer buf = ByteBuffer.wrap(b);
         int command = buf.get()&0xff;
         buf.position(buf.position()+3);
@@ -108,14 +111,38 @@
 		case ProtocolContext.FIND_ROOT_REPLY :
 			handleFindRootReply(port,hostname);
 			break;
-		case ProtocolContext.FIND_ROOT : 
-		case ProtocolContext.WHERE_TO_CONNECT : 
+        case ProtocolContext.CONNECT_TO_AS_LEADER :
+            handleConnectTo(port,hostname,true);
+            break;
+        case ProtocolContext.CONNECT_TO :
+            handleConnectTo(port,hostname,false);
+            break;
+		case ProtocolContext.FIND_ROOT :
+		    // this is a multicast message, cannot happen
+		    break;
+        case ProtocolContext.WHERE_TO_CONNECT : 
 		case ProtocolContext.LOST_PARENT :
 			System.out.println("get treeVNC command" + command);
 		}
 	}
 
 	/**
+	 * set new parent address
+	 * @param port
+	 * @param hostname
+	 * @param leader
+	 * @throws IOException 
+	 * @throws SocketException 
+	 * @throws UnknownHostException 
+	 */
+	private void handleConnectTo(int port, String hostname, boolean leader) throws UnknownHostException, SocketException, IOException {
+        if (isRoot()) {
+            return; // we don't have parent
+        }
+        treeProtocol.connectToParenet(port, hostname,leader);
+    }
+
+    /**
 	 * Accept FIND_ROOT_REPLY
 	 *     add replying TreeVNC root to RootSelection Panel
 	 * @param port
@@ -135,15 +162,21 @@
 	 * @throws IOException
 	 * @throws TransportException
 	 */
-	public void newClient(AcceptThread acceptThread, final Socket newCli,
-			final Writer os, final Reader is) throws IOException, TransportException {
+	public void newClient(AcceptThread acceptThread, final Socket newCli,final Writer os, final Reader is) { 
 
-		
-		if (initialConnection(os, is)) {
-		    // TreeVNC command is processed
-		    newCli.close();
-		    return; 
-		}
+		try {
+            if (initialConnection(os, is)) {
+                // TreeVNC command is processed
+                newCli.close();
+                return; 
+            }
+        } catch (Exception e) {
+            try {
+                newCli.close();
+            } catch (IOException e1) {
+                // log
+            }
+        }
 			
 		final int myId = clients;
 		final MulticastQueue.Client<LinkedList<ByteBuffer>> c = multicastqueue.newClient();
@@ -467,19 +500,19 @@
 	}
 
 	public void setEcho(TreeVncProtocol _echo) {
-		echo = _echo;
+		treeProtocol = _echo;
 	}
 	
 	public void setViewer(ViewerImpl v) {
-		echo.setViewer(v);
+		treeProtocol.setViewer(v);
 	}
 	
 	public ViewerImpl getViewer() {
-		return echo.getViewer();
+		return treeProtocol.getViewer();
 	}
 	
 	public TreeVncProtocol getEcho() {
-		return echo;
+		return treeProtocol;
 	}
 
 	public void setTerminationType(boolean setType) {
@@ -578,7 +611,7 @@
     }
 
     public String getMyAddress() {
-        return echo.getMyAddress();
+        return treeProtocol.getMyAddress();
     }
 
     /**