diff src/main/java/jp/ac/u_ryukyu/treevnc/MyRfbProto.java @ 174:2e1530139169

reorganization
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 22 Jun 2014 11:22:52 +0900
parents 2c36ea3f93da
children e1aa06b0d2ff
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/MyRfbProto.java	Sat Jun 21 17:28:59 2014 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/treevnc/MyRfbProto.java	Sun Jun 22 11:22:52 2014 +0900
@@ -4,6 +4,7 @@
 import java.net.BindException;
 import java.net.ServerSocket;
 import java.net.Socket;
+import java.net.UnknownHostException;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 import java.util.LinkedList;
@@ -14,6 +15,8 @@
 
 import jp.ac.u_ryukyu.treevnc.client.TreeVncProtocol;
 import jp.ac.u_ryukyu.treevnc.server.RequestScreenThread;
+import jp.ac.u_ryukyu.treevnc.server.TreeManagement;
+import jp.ac.u_ryukyu.treevnc.server.TreeRootFinderListener;
 import jp.ac.u_ryukyu.treevnc.server.VncProxyService;
 
 import com.glavsoft.exceptions.TransportException;
@@ -24,6 +27,7 @@
 import com.glavsoft.transport.Reader;
 import com.glavsoft.transport.Writer;
 import com.glavsoft.viewer.ViewerInterface;
+import com.glavsoft.viewer.swing.ConnectionParams;
 
 
 public abstract class MyRfbProto {
@@ -35,6 +39,7 @@
 	public MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MulticastQueue<LinkedList<ByteBuffer>>();
 	private RequestScreenThread rThread;
 	public int acceptPort = 0;
+    private String myAddress;
 	protected boolean readyReconnect = false;
 	private boolean cuiVersion;
 	private long counter = 0; // packet serial number
@@ -45,18 +50,19 @@
 
     private Inflater inflater = new Inflater();
     private Deflater deflater = new Deflater();
-    VncProxyService viewer;
+    ViewerInterface viewer;
 	private short id;
+    private boolean leader;
+    private TreeManagement treeManager;
+    private TreeVncCommandChannelListener acceptThread;
+    private boolean firstTime;
+    private TreeRootFinderListener getCast;
 
 
 	public MyRfbProto() {
 		rThread = new RequestScreenThread(this);
 	}
 	
-	public void setVncProxy(VncProxyService viewer) {
-	    this.viewer = viewer;
-	}
-
 	abstract public boolean isRoot() ;
 	
 	public ProtocolContext getContext() {
@@ -305,11 +311,11 @@
 	}
 	
 	public void setViewer(ViewerInterface v) {
-		treeProtocol.setViewer(v);
+		viewer = v;
 	}
 	
 	public ViewerInterface getViewer() {
-		return treeProtocol.getViewer();
+		return viewer;
 	}
 	
 	public TreeVncProtocol getEcho() {
@@ -357,7 +363,7 @@
 		}
 	}	
 
-	public synchronized void waitForReady(VncProxyService vncProxyService) throws InterruptedException {
+	public synchronized void waitForReady(ViewerInterface vncProxyService) throws InterruptedException {
 		while (!readyReconnect) {
 			wait();
 		}
@@ -395,7 +401,7 @@
     }
 
     public String getMyAddress() {
-        return treeProtocol.getMyAddress();
+        return myAddress;
     }
 
     /**
@@ -583,5 +589,84 @@
 		return id;
 	}
 
+    public void setMyAddress(String myHostName) {
+        this.myAddress = myHostName;
+        
+    }
+
+    public void setLeader(boolean leader) {
+        this.leader = leader;
+    }
+
+    public boolean isLeader() {
+        return leader;
+    }
+
+    public void setTreeManager(TreeManagement clients) {
+        treeManager = clients;
+    }
+    
+    public TreeManagement getTreeManger() {
+        return treeManager;
+    }
+
+    /**
+     * chnageVNCServer is called when host change.
+     * 
+     * @param vncProxyService 
+     * @param hostName
+     *            HostAddress
+     * @param width
+     *            FrameWidth
+     * @param height
+     *            FrameHeight
+     * @param id 
+     * @throws InterruptedException 
+     */
+    public void changeVNCServer(ViewerInterface vncProxyService, String hostName, int width, int height, short id)
+    		throws UnknownHostException, IOException, InterruptedException {
+    	// stop reader stop
+        stopReceiverTask();
+    	vncProxyService.inhelitClients(vncProxyService, hostName);
+    	orderRecconection(vncProxyService, hostName, id);
+    }
+
+    public void orderRecconection(ViewerInterface vncProxyService, String hostName, short id) throws UnknownHostException, IOException, InterruptedException {
+    	waitForReady(vncProxyService);
+    	sendDesktopSizeChange(id);
+    }
+
+    /**
+     * start accepting children
+     * run rootFinderListener if necessary
+     * @param vncProxyService TODO
+     */
+    public void createConnectionAndStart(ViewerInterface vncProxyService) {
+    	selectPort(ConnectionParams.DEFAULT_VNC_ROOT);
+    	if (treeManager!=null) {
+    	    treeManager.getList().getFirst().setPort(getAcceptPort());
+    	}
+    	acceptThread = new TreeVncCommandChannelListener(this, getAcceptPort());
+    	Thread thread = new Thread(acceptThread, "TreeVNC-accept");
+    	thread.start();
+    	if(firstTime) {
+    		getCast = new TreeRootFinderListener(vncProxyService);
+    		thread = new Thread(getCast, "tree-root-find-listener");
+    		thread.start();
+    		firstTime = false;
+    	}
+    }
+
+    public void initRoot(ViewerInterface vncProxyService, String hostName) {
+    	treeManager = new TreeManagement(hostName, ConnectionParams.DEFAULT_VNC_ROOT,this);
+        vncProxyService.setTreeManager(treeManager);
+        vncProxyService.initRootViewer(hostName);
+    	createConnectionAndStart(vncProxyService);
+    }
+
+    public TreeVncCommandChannelListener getAcceptThread() {
+        return acceptThread;
+    }
+
 	
 }