Mercurial > hg > Members > riono > TreeVNC_ja_comment
changeset 223:3189e210a7ed
indent fix.
author | oc |
---|---|
date | Sun, 06 Jul 2014 12:46:20 +0900 |
parents | 92969b3c0846 |
children | ccbc3a951961 |
files | src/main/java/jp/ac/u_ryukyu/treevnc/TreeManagement.java src/main/java/jp/ac/u_ryukyu/treevnc/TreeRFBProto.java src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncCommandChannelListener.java src/viewer_swing/java/com/glavsoft/viewer/TreeViewer.java |
diffstat | 4 files changed, 548 insertions(+), 548 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/TreeManagement.java Sun Jul 06 10:36:30 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/TreeManagement.java Sun Jul 06 12:46:20 2014 +0900 @@ -7,85 +7,85 @@ public class TreeManagement { - private LinkedList<TreeVNCNode> nodeList = new LinkedList<TreeVNCNode>(); - boolean runflag = false; - private final int treebranch = 2; - private boolean showTreeNode = false; - private byte[] netmask; - private byte[] netaddress; + private LinkedList<TreeVNCNode> nodeList = new LinkedList<TreeVNCNode>(); + boolean runflag = false; + private final int treebranch = 2; + private boolean showTreeNode = false; + private byte[] netmask; + private byte[] netaddress; - public TreeManagement(String hostName, int vncport, boolean showTree) { - TreeVNCNode me = new TreeVNCNode(hostName, vncport,"localhost"); - showTreeNode = showTree; - me.setTreeNum(0); - nodeList.add(me); - } - -// -// 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; -// } -// } + public TreeManagement(String hostName, int vncport, boolean showTree) { + TreeVNCNode me = new TreeVNCNode(hostName, vncport,"localhost"); + showTreeNode = showTree; + me.setTreeNum(0); + nodeList.add(me); + } + + // + // 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; + // } + // } - /** - * a parent is lost, remove from the list and move last one into here - * @param nodeNum - * parent value - */ - private void moveLastNodeToLostNodePosition(int nodeNum) { - nodeList.remove(nodeNum); - TreeVNCNode node = nodeList.removeLast(); - node.setTreeNum(nodeNum); - nodeList.add(nodeNum, node) ; - } + /** + * a parent is lost, remove from the list and move last one into here + * @param nodeNum + * parent value + */ + private void moveLastNodeToLostNodePosition(int nodeNum) { + nodeList.remove(nodeNum); + TreeVNCNode node = nodeList.removeLast(); + node.setTreeNum(nodeNum); + nodeList.add(nodeNum, node) ; + } - private void checkParameter(int parent, int counter, int leaderflag2) { - System.out.println("number p =" + parent); - System.out.println("number i =" + counter); - System.out.println("leaderflag=" + leaderflag2 + "\n"); - } + private void checkParameter(int parent, int counter, int leaderflag2) { + System.out.print("number p =" + parent); + System.out.print(" number i =" + counter); + System.out.println(" leaderflag=" + leaderflag2 + "\n"); + } - private TreeVNCNode getParentNode(int nodeNum) { - int parentnum = (nodeNum - 1) / treebranch; + private TreeVNCNode getParentNode(int nodeNum) { + int parentnum = (nodeNum - 1) / treebranch; return nodeList.get(parentnum); - } - - - public TreeVNCNode getChildNode(TreeVNCNode newparent, int i) { - int child = newparent.getTreeNum() * treebranch + i + 1; - if (child >= nodeList.size()) { - return(null); - } else { - return(nodeList.get(child)); - } - - } - - /** - * A parent is lost, move last node which has no child to the position - * @param hostname - * @param port - * @param myHostName - */ + } + + + public TreeVNCNode getChildNode(TreeVNCNode newparent, int i) { + int child = newparent.getTreeNum() * treebranch + i + 1; + if (child >= nodeList.size()) { + return(null); + } else { + return(nodeList.get(child)); + } + + } + + /** + * A parent is lost, move last node which has no child to the position + * @param hostname + * @param port + * @param myHostName + */ public void fixLostParent(String hostname, int port, String myHostName) { TreeVNCNode lostParentNode = lookup(nodeList,hostname,port); if (lostParentNode == null ) return; // some thing wrong int treeNumber = lostParentNode.getTreeNum(); TreeVNCNode deadParent = getParentNode(treeNumber); - TreeVNCNode me = nodeList.getFirst(); - me.setHostName(myHostName); + TreeVNCNode me = nodeList.getFirst(); + me.setHostName(myHostName); if (deadParent.getTreeNum() == 0) { - // if dead root, connect me. - connectTo(me, lostParentNode); - return; + // if dead root, connect me. + connectTo(me, lostParentNode); + return; } if (getChildNode(deadParent, 0) == null) return; moveLastNodeToLostNodePosition(deadParent.getTreeNum()); @@ -99,115 +99,115 @@ * @param newparent */ private void lostNodeConnection(TreeVNCNode oldParent) { - TreeVNCNode newparent = nodeList.get(oldParent.getTreeNum()); - TreeVNCNode grandfather = getParentNode(newparent.getTreeNum()); - - connectTo(grandfather, newparent); - + TreeVNCNode newparent = nodeList.get(oldParent.getTreeNum()); + TreeVNCNode grandfather = getParentNode(newparent.getTreeNum()); + + connectTo(grandfather, newparent); + for(int i=0; i < treebranch; i++) { - TreeVNCNode child = getChildNode(newparent, i); + TreeVNCNode child = getChildNode(newparent, i); if (child != null) { connectTo(newparent, child); } } - + if (showTreeNode) { - showTreeNode(); + showTreeNode(); } } - public void connectTo(TreeVNCNode newparent, TreeVNCNode n) { - TreeVncProtocol vc1 = new TreeVncProtocol(n.getHostname(),n.getPort()); - try { - short nodeId = (short) n.getTreeNum(); - vc1.connectTo(newparent.getHostname(), newparent.getPort(), isLeader(n), nodeId); - } catch (IOException e) { - // log - } - } + public void connectTo(TreeVNCNode newparent, TreeVNCNode n) { + TreeVncProtocol vc1 = new TreeVncProtocol(n.getHostname(),n.getPort()); + try { + short nodeId = (short) n.getTreeNum(); + vc1.connectTo(newparent.getHostname(), newparent.getPort(), isLeader(n), nodeId); + } catch (IOException e) { + // log + } + } private int isLeader(TreeVNCNode n) { return ( n.getTreeNum() % treebranch == 1) ? 1 : 0; } private TreeVNCNode lookup(LinkedList<TreeVNCNode> list, String hostname, int port) { - for (TreeVNCNode r : list) { - if (r.getHostname().equals(hostname) && r.getPort() == port ) - return r; - } - return null; + for (TreeVNCNode r : list) { + if (r.getHostname().equals(hostname) && r.getPort() == port ) + return r; + } + return null; } - /** - * Determine tree topology and send CONNECT_TO command - * @param hostname - * @param port - */ - public void decideWhereToConnect(String hostname, int port, String localhostname) { + /** + * Determine tree topology and send CONNECT_TO command + * @param hostname + * @param port + */ + public void decideWhereToConnect(String hostname, int port, String localhostname) { TreeVNCNode node = new TreeVNCNode(hostname,port,localhostname); node.setTreeNum(nodeList.size()); nodeList.add(node); - if (nodeList.size() >= treebranch + 1) { - TreeVNCNode parent = getParentNode(node.getTreeNum()); - checkParameter(parent.getTreeNum(), nodeList.size(), isLeader(node)); - connectTo(parent, node); - } else { - // connect to me - TreeVNCNode me = nodeList.getFirst(); - me.setHostName(localhostname); - connectTo(me, node); - } - if (showTreeNode) { - showTreeNode(); - } - } - - /** - * show Tree Node. - */ - public void showTreeNode() { - int nl = 0, pow = 2; + if (nodeList.size() >= treebranch + 1) { + TreeVNCNode parent = getParentNode(node.getTreeNum()); + checkParameter(parent.getTreeNum(), nodeList.size(), isLeader(node)); + connectTo(parent, node); + } else { + // connect to me + TreeVNCNode me = nodeList.getFirst(); + me.setHostName(localhostname); + connectTo(me, node); + } + if (showTreeNode) { + showTreeNode(); + } + } + + /** + * show Tree Node. + */ + public void showTreeNode() { + int nl = 0, pow = 2; for (int i=0; i<nodeList.size(); i++) { - TreeVNCNode treeNode = nodeList.get(i); - System.out.print(treeNode.getTreeNum() + ":" + treeNode.getPort() + ":" + treeNode.getHostname()); - if (i==nl) { - System.out.println(); - nl = nl + pow; - pow = pow * pow; - } else { - System.out.print(" "); - } + TreeVNCNode treeNode = nodeList.get(i); + System.out.print(treeNode.getTreeNum() + ":" + treeNode.getPort() + ":" + treeNode.getHostname()); + if (i==nl) { + System.out.println(); + nl = nl + pow; + pow = pow * pow; + } else { + System.out.print(" "); + } } System.out.println(); - } + } - public LinkedList<TreeVNCNode> getList() { - return nodeList; - } + public LinkedList<TreeVNCNode> getList() { + return nodeList; + } - public void setList(LinkedList<TreeVNCNode> _ls) { - nodeList = _ls; - } + public void setList(LinkedList<TreeVNCNode> _ls) { + nodeList = _ls; + } - public int getTreeBranch() { - return treebranch; - } + public int getTreeBranch() { + return treebranch; + } - public void setNetMask(byte[] netmask,byte[] netaddress) { - this.netmask = netmask; - this.netaddress = netaddress; - } - - public boolean onTheSameNetwork(InetAddress adr ) { - byte [] byteadr = adr.getAddress(); - if (byteadr.length != netmask.length) return false; - for(int i=0; i < netmask.length; i++) { - if ((netmask[i] & byteadr[i])!=netaddress[i]) return false; - } - return true; - } + public void setNetMask(byte[] netmask,byte[] netaddress) { + this.netmask = netmask; + this.netaddress = netaddress; + } + + public boolean onTheSameNetwork(InetAddress adr ) { + byte [] byteadr = adr.getAddress(); + if (byteadr.length != netmask.length) return false; + for(int i=0; i < netmask.length; i++) { + if ((netmask[i] & byteadr[i])!=netaddress[i]) return false; + } + return true; + } } \ No newline at end of file
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/TreeRFBProto.java Sun Jul 06 10:36:30 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/TreeRFBProto.java Sun Jul 06 12:46:20 2014 +0900 @@ -27,18 +27,18 @@ public class TreeRFBProto { - final static int FramebufferUpdateRequest = 3; - final static int CheckDelay = 11; - protected final static int FramebufferUpdate = 0; - protected ProtocolContext context; - private int clients = 0; - public MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MulticastQueue<LinkedList<ByteBuffer>>(); - private RequestScreenThread rThread; - public int acceptPort = 0; + final static int FramebufferUpdateRequest = 3; + final static int CheckDelay = 11; + protected final static int FramebufferUpdate = 0; + protected ProtocolContext context; + private int clients = 0; + 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 + protected boolean readyReconnect = false; + private boolean cuiVersion; + private long counter = 0; // packet serial number public ServerSocket servSock; private boolean permitChangeScreen = true; private static final int INFLATE_BUFSIZE = 1024 * 100; @@ -46,228 +46,228 @@ private Inflater inflater = new Inflater(); private Deflater deflater = new Deflater(); ViewerInterface viewer; - private short id; // my tree node id ( = 0 in root ) + private short id; // my tree node id ( = 0 in root ) private boolean leader; private TreeVncCommandChannelListener acceptThread; private boolean firstTime = true; private TreeRootFinderListener getCast; private CreateConnectionParam cp; private boolean hasViewer = false; - public boolean showTreeNode = false; + public boolean showTreeNode = false; private boolean reconnecting; private short reconnectingId; // Change Server Request to id's node VNC server private TreeVNCNetwork nets = new TreeVNCNetwork(); - private boolean normalTermination; - private TreeVncRootSelectionPanel rootSelectionPanel; - private boolean isTreeManager; - private String vncInterface; + private boolean normalTermination; + private TreeVncRootSelectionPanel rootSelectionPanel; + private boolean isTreeManager; + private String vncInterface; + + public TreeRFBProto(boolean isTreeManager) { + rThread = new RequestScreenThread(this); + nets.setMyRfb(this); + this.isTreeManager = isTreeManager; + } + + public boolean isTreeManager() { + return isTreeManager; + } - public TreeRFBProto(boolean isTreeManager) { - rThread = new RequestScreenThread(this); - nets.setMyRfb(this); - this.isTreeManager = isTreeManager; - } - - public boolean isTreeManager() { - return isTreeManager; - } - - public ProtocolContext getContext() { - return context; - } - - /** - * handle new client accept - * it also handle TreeVNC Command - * @param acceptThread - * @param newCli - * @param os - * @param is - * @throws IOException - * @throws TransportException - */ - public void newClient(final Socket newCli,final Writer os, final Reader is) { + public ProtocolContext getContext() { + return context; + } + + /** + * handle new client accept + * it also handle TreeVNC Command + * @param acceptThread + * @param newCli + * @param os + * @param is + * @throws IOException + * @throws TransportException + */ + public void newClient(final Socket newCli,final Writer os, final Reader is) { - final int myId = clients; - final MulticastQueue.Client<LinkedList<ByteBuffer>> c = multicastqueue.newClient(); - final AtomicInteger writerRunning = new AtomicInteger(); - writerRunning.set(1); - /** - * Timeout thread. If a client is suspended, it has top of queue - * indefinitely, which caused memory overflow. After the timeout, we - * poll the queue and discard it. Start long wait if writer is running. - */ - final Runnable timer = new Runnable() { - public void run() { - int count = 0; - for (;;) { - long timeout = 50000 / 8; - try { - synchronized (this) { - int state, flag; - writerRunning.set(0); - wait(timeout); - flag = 0; - while ((state = writerRunning.get()) == 0) { - c.poll(); // discard, should be timeout - count++; - if (flag == 0) { - System.out.println("Discarding " + myId - + " count=" + count); - flag = 1; - } - wait(10); // if this is too short, writer cannot - // take the poll, if this is too - // long, memory will overflow... - } - if (flag == 1) - System.out.println("Resuming " + myId - + " count=" + count); - if (state != 1) { - System.out.println("Client died " + myId); - break; - } - } - } catch (InterruptedException e) { - } - } - } - }; - new Thread(timer, "timer-discard-multicastqueue").start(); - /** - * handle command from lower node - */ - final Runnable reader = new Runnable() { + final int myId = clients; + final MulticastQueue.Client<LinkedList<ByteBuffer>> c = multicastqueue.newClient(); + final AtomicInteger writerRunning = new AtomicInteger(); + writerRunning.set(1); + /** + * Timeout thread. If a client is suspended, it has top of queue + * indefinitely, which caused memory overflow. After the timeout, we + * poll the queue and discard it. Start long wait if writer is running. + */ + final Runnable timer = new Runnable() { + public void run() { + int count = 0; + for (;;) { + long timeout = 50000 / 8; + try { + synchronized (this) { + int state, flag; + writerRunning.set(0); + wait(timeout); + flag = 0; + while ((state = writerRunning.get()) == 0) { + c.poll(); // discard, should be timeout + count++; + if (flag == 0) { + System.out.println("Discarding " + myId + + " count=" + count); + flag = 1; + } + wait(10); // if this is too short, writer cannot + // take the poll, if this is too + // long, memory will overflow... + } + if (flag == 1) + System.out.println("Resuming " + myId + + " count=" + count); + if (state != 1) { + System.out.println("Client died " + myId); + break; + } + } + } catch (InterruptedException e) { + } + } + } + }; + new Thread(timer, "timer-discard-multicastqueue").start(); + /** + * handle command from lower node + */ + final Runnable reader = new Runnable() { public void run() { - for (;;) { - try { - final byte b[] = new byte[4096]; - final int c = is.readByte(b); - if (c <= 0) - throw new IOException(); - if (isTreeManager()) { - if (b[0] == ClientToServerMessage.SERVER_CHANGE_REQUEST) { - if (permitChangeScreen()) { - ByteBuffer buf = ByteBuffer.wrap(b); - buf.order(ByteOrder.BIG_ENDIAN); - short id = buf.getShort(2); - int length = buf.getInt(4); - if (length == 0) - continue; - String newHostName = new String(b, 8, length); - System.out.println("Root server change request :" + newHostName); - // please remove these numbers. - if (viewer != null) { - changeVNCServer(viewer, newHostName, 3200, 1980, id); - } - } else { - continue; - } - } - } else if (b[0] == ClientToServerMessage.SERVER_CHANGE_REQUEST) { - ClientToServerMessage sc = new ClientToServerMessage() { + for (;;) { + try { + final byte b[] = new byte[4096]; + final int c = is.readByte(b); + if (c <= 0) + throw new IOException(); + if (isTreeManager()) { + if (b[0] == ClientToServerMessage.SERVER_CHANGE_REQUEST) { + if (permitChangeScreen()) { + ByteBuffer buf = ByteBuffer.wrap(b); + buf.order(ByteOrder.BIG_ENDIAN); + short id = buf.getShort(2); + int length = buf.getInt(4); + if (length == 0) + continue; + String newHostName = new String(b, 8, length); + System.out.println("Root server change request :" + newHostName); + // please remove these numbers. + if (viewer != null) { + changeVNCServer(viewer, newHostName, 3200, 1980, id); + } + } else { + continue; + } + } + } else if (b[0] == ClientToServerMessage.SERVER_CHANGE_REQUEST) { + ClientToServerMessage sc = new ClientToServerMessage() { @Override public void send(Writer writer) throws TransportException { writer.write(b,0,c); } - }; - context.sendMessage(sc); - } - // System.out.println("client read "+c); - } catch (Exception e) { - try { - writerRunning.set(2); - os.close(); - is.close(); - break; - } catch (IOException e1) { - } catch (TransportException e1) { - e1.printStackTrace(); - } - return; - } - } - } + }; + context.sendMessage(sc); + } + // System.out.println("client read "+c); + } catch (Exception e) { + try { + writerRunning.set(2); + os.close(); + is.close(); + break; + } catch (IOException e1) { + } catch (TransportException e1) { + e1.printStackTrace(); + } + return; + } + } + } - }; - /** - * send packets to a client (one thread for each client ) - */ - Runnable sender = new Runnable() { - public void run() { - writerRunning.set(1); - try { - requestThreadNotify(); + }; + /** + * send packets to a client (one thread for each client ) + */ + Runnable sender = new Runnable() { + public void run() { + writerRunning.set(1); + try { + requestThreadNotify(); - // after this, we discard upward packet. - new Thread(reader, "upward-packet-processing").start(); + // after this, we discard upward packet. + new Thread(reader, "upward-packet-processing").start(); - for (;;) { - LinkedList<ByteBuffer> bufs = c.poll(); - int inputIndex = 0; - ByteBuffer header = bufs.get(inputIndex); - if (header == null) - continue; - else if (header.get(0) == CheckDelay) { - writeToClient(os, bufs, inputIndex); - continue; - } else if (header.get(0) == FramebufferUpdate) { - //System.out.println("client "+ myId); - } - /* - * if(i%20==0){ sendDataCheckDelay(); } i++; - */ - writeToClient(os, bufs, inputIndex); - writerRunning.set(1); // yes my client is awaking. - } - } catch (Exception e) { - try { - writerRunning.set(2); - os.close(); - } catch (IOException e1) { - System.out.println("root writer close faild :" + e1); - } - System.out.println("root writer faild :" + e); - /* if socket closed cliList.remove(newCli); */ - } - } + for (;;) { + LinkedList<ByteBuffer> bufs = c.poll(); + int inputIndex = 0; + ByteBuffer header = bufs.get(inputIndex); + if (header == null) + continue; + else if (header.get(0) == CheckDelay) { + writeToClient(os, bufs, inputIndex); + continue; + } else if (header.get(0) == FramebufferUpdate) { + //System.out.println("client "+ myId); + } + /* + * if(i%20==0){ sendDataCheckDelay(); } i++; + */ + writeToClient(os, bufs, inputIndex); + writerRunning.set(1); // yes my client is awaking. + } + } catch (Exception e) { + try { + writerRunning.set(2); + os.close(); + } catch (IOException e1) { + System.out.println("root writer close faild :" + e1); + } + System.out.println("root writer faild :" + e); + /* if socket closed cliList.remove(newCli); */ + } + } - public void writeToClient(final Writer os, - LinkedList<ByteBuffer> bufs, int inputIndex) - throws TransportException { - while (inputIndex < bufs.size()) { - ByteBuffer b = bufs.get(inputIndex++); - os.write(b.array(), b.position(), b.limit()); - } - os.flush(); - bufs = null; - multicastqueue.heapAvailable(); - } - }; - clients++; - new Thread(sender, "writer-to-lower-node").start(); + public void writeToClient(final Writer os, + LinkedList<ByteBuffer> bufs, int inputIndex) + throws TransportException { + while (inputIndex < bufs.size()) { + ByteBuffer b = bufs.get(inputIndex++); + os.write(b.array(), b.position(), b.limit()); + } + os.flush(); + bufs = null; + multicastqueue.heapAvailable(); + } + }; + clients++; + new Thread(sender, "writer-to-lower-node").start(); - } + } - + public boolean permitChangeScreen() { return permitChangeScreen; } - + public void setPermitChangeScreen(boolean v) { - permitChangeScreen = v; + permitChangeScreen = v; } - - public void requestThreadNotify() { - rThread.reStart(); - } - - + + public void requestThreadNotify() { + rThread.reStart(); + } + + public void setProtocolContext(Protocol workingProtocol) { context = workingProtocol; } @@ -298,131 +298,131 @@ - public void writeFramebufferUpdateRequest(int x, int y, int w, int h, - boolean incremental) throws TransportException { - byte[] b = new byte[10]; + public void writeFramebufferUpdateRequest(int x, int y, int w, int h, + boolean incremental) throws TransportException { + byte[] b = new byte[10]; + + b[0] = (byte) FramebufferUpdateRequest; // 3 is FrameBufferUpdateRequest + b[1] = (byte) (incremental ? 1 : 0); + b[2] = (byte) ((x >> 8) & 0xff); + b[3] = (byte) (x & 0xff); + b[4] = (byte) ((y >> 8) & 0xff); + b[5] = (byte) (y & 0xff); + b[6] = (byte) ((w >> 8) & 0xff); + b[7] = (byte) (w & 0xff); + b[8] = (byte) ((h >> 8) & 0xff); + b[9] = (byte) (h & 0xff); + } - b[0] = (byte) FramebufferUpdateRequest; // 3 is FrameBufferUpdateRequest - b[1] = (byte) (incremental ? 1 : 0); - b[2] = (byte) ((x >> 8) & 0xff); - b[3] = (byte) (x & 0xff); - b[4] = (byte) ((y >> 8) & 0xff); - b[5] = (byte) (y & 0xff); - b[6] = (byte) ((w >> 8) & 0xff); - b[7] = (byte) (w & 0xff); - b[8] = (byte) ((h >> 8) & 0xff); - b[9] = (byte) (h & 0xff); - } - - public void setViewer(ViewerInterface v) { - viewer = v; - } - - public ViewerInterface getViewer() { - return viewer; - } + public void setViewer(ViewerInterface v) { + viewer = v; + } + + public ViewerInterface getViewer() { + return viewer; + } + + + void sendInitData(OutputStream os) throws IOException { + os.write(context.getInitData()); + } + + + public void setTerminationType(boolean setType) { + normalTermination = setType; + } + + public boolean getTerminationType() { + return normalTermination; + } - void sendInitData(OutputStream os) throws IOException { - os.write(context.getInitData()); - } + public void addHostToSelectionPanel(int port, String hostname,String myHostName) { + if (rootSelectionPanel != null) { + rootSelectionPanel.checkBox(Integer.toString(port) + ":" + hostname + ":" + myHostName); + rootSelectionPanel.setButton(); + rootSelectionPanel.visible(); + } + } - - public void setTerminationType(boolean setType) { - normalTermination = setType; - } - - public boolean getTerminationType() { - return normalTermination; - } - - - public void addHostToSelectionPanel(int port, String hostname,String myHostName) { - if (rootSelectionPanel != null) { - rootSelectionPanel.checkBox(Integer.toString(port) + ":" + hostname + ":" + myHostName); - rootSelectionPanel.setButton(); - rootSelectionPanel.visible(); - } - } - - public void createRootSelectionPanel(CreateConnectionParam cp) { - rootSelectionPanel = new TreeVncRootSelectionPanel(); - rootSelectionPanel.setCp(cp); - } - + public void createRootSelectionPanel(CreateConnectionParam cp) { + rootSelectionPanel = new TreeVncRootSelectionPanel(); + rootSelectionPanel.setCp(cp); + } + public void close() { - // none - } - - public int getAcceptPort() { - return acceptPort; - } - - public boolean getReadyReconnect() { - return readyReconnect; - } + // none + } + + public int getAcceptPort() { + return acceptPort; + } + + public boolean getReadyReconnect() { + return readyReconnect; + } - public boolean getCuiVersion() { - return cuiVersion; - } - - public void setCuiVersion(boolean flag) { - cuiVersion = flag; - } + public boolean getCuiVersion() { + return cuiVersion; + } - public void readCheckDelay(Reader reader) throws TransportException { - - } + public void setCuiVersion(boolean flag) { + cuiVersion = flag; + } + + public void readCheckDelay(Reader reader) throws TransportException { + + } - public synchronized void vncConnected(boolean ready) { - if (ready && reconnecting) { - Socket vncSocket = viewer.getVNCSocket(); - NetworkInterface ni = nets.getInterface(vncSocket); - if (ni!=null) { - vncInterface = ni.getName(); - System.out.println("VNCNetworkInterface :" + vncInterface); - } - sendDesktopSizeChange(reconnectingId); - reconnecting = false; - if (reconnectingId == 0) { - viewer.setVisible(false); - } - } - if (reconnectingId!=0) { - readyReconnect = ready; - if (ready) { - notifyAll(); - } - } - } + public synchronized void vncConnected(boolean ready) { + if (ready && reconnecting) { + Socket vncSocket = viewer.getVNCSocket(); + NetworkInterface ni = nets.getInterface(vncSocket); + if (ni!=null) { + vncInterface = ni.getName(); + System.out.println("VNCNetworkInterface :" + vncInterface); + } + sendDesktopSizeChange(reconnectingId); + reconnecting = false; + if (reconnectingId == 0) { + viewer.setVisible(false); + } + } + if (reconnectingId!=0) { + readyReconnect = ready; + if (ready) { + notifyAll(); + } + } + } - public synchronized void waitForVNCConnection() throws InterruptedException { - if (reconnectingId!=0) { - while (!readyReconnect) { - wait(); - } - } - } + public synchronized void waitForVNCConnection() throws InterruptedException { + if (reconnectingId!=0) { + while (!readyReconnect) { + wait(); + } + } + } - public void sendDesktopSizeChange(short id) { - LinkedList<ByteBuffer> desktopSize = new LinkedList<ByteBuffer>(); - int width = context.getFbWidth(); - int height = context.getFbHeight(); - desktopSize.add(new UpdateRectangleMessage(width, height, EncodingType.INIT_DATA, context.getInitData(),id).getMessage()); - addSerialNumber(desktopSize); - multicastqueue.put(desktopSize); - } + public void sendDesktopSizeChange(short id) { + LinkedList<ByteBuffer> desktopSize = new LinkedList<ByteBuffer>(); + int width = context.getFbWidth(); + int height = context.getFbHeight(); + desktopSize.add(new UpdateRectangleMessage(width, height, EncodingType.INIT_DATA, context.getInitData(),id).getMessage()); + addSerialNumber(desktopSize); + multicastqueue.put(desktopSize); + } - public void addSerialNumber(LinkedList<ByteBuffer> bufs) { - ByteBuffer serialNum = multicastqueue.allocate(8); - serialNum.putLong(counter++); - serialNum.flip(); - bufs.addFirst(serialNum); - } + public void addSerialNumber(LinkedList<ByteBuffer> bufs) { + ByteBuffer serialNum = multicastqueue.allocate(8); + serialNum.putLong(counter++); + serialNum.flip(); + bufs.addFirst(serialNum); + } public void resetDecoder() { @@ -505,7 +505,7 @@ */ public int unzip(Inflater inflater, LinkedList<ByteBuffer> inputs, int inputIndex, byte[] bytes, int bufSize) - throws DataFormatException { + throws DataFormatException { int position = 0; int limit = bytes.length; while (inputIndex < inputs.size()) { @@ -518,9 +518,9 @@ int len0 = inflater.inflate(bytes, position, limit-position); if (len0 > 0) { - position += len0; + position += len0; if (position > limit) { - throw new DataFormatException(); + throw new DataFormatException(); } } } while (!inflater.needsInput()); @@ -563,8 +563,8 @@ inputs.add(inputData); header.putInt(12, EncodingType.ZRLEE.getId()); // means - // recompress - // every time + // recompress + // every time // using new Deflecter every time is incompatible with the // protocol, clients have to be modified. Deflater nDeflater = deflater; // new Deflater(); @@ -608,17 +608,17 @@ // here. } - public void setId(short id) { - this.id = id; - } + public void setId(short id) { + this.id = id; + } - public short getId() { - return id; - } + public short getId() { + return id; + } public void setMyAddress(String myHostName) { this.myAddress = myHostName; - + } public void setLeader(boolean leader) { @@ -630,9 +630,9 @@ } public void setTreeManager(String intf, TreeManagement clients) { - nets.setTreeManager(intf, clients); + nets.setTreeManager(intf, clients); } - + public TreeManagement getTreeManager(String intf) { return nets.getTreeManager(intf); } @@ -651,16 +651,16 @@ * @throws InterruptedException */ public void changeVNCServer(ViewerInterface vncProxyService, String hostName, int width, int height, short id) - throws UnknownHostException, IOException, InterruptedException { - // stop reader stop + throws UnknownHostException, IOException, InterruptedException { + // stop reader stop stopReceiverTask(); reconnectingId = id; - vncProxyService.inhelitClients(vncProxyService, hostName); + vncProxyService.inhelitClients(vncProxyService, hostName); // after connecting VNC server, rfb send SEND_INIT_DATA command and wakes me up if necessary - reconnecting = true; - if (reconnectingId!=0) { - waitForVNCConnection(); - } + reconnecting = true; + if (reconnectingId!=0) { + waitForVNCConnection(); + } } /** @@ -668,20 +668,20 @@ * run rootFinderListener if necessary */ public void createConnectionAndStart(ViewerInterface v) { - selectPort(ConnectionParams.DEFAULT_VNC_ROOT); + selectPort(ConnectionParams.DEFAULT_VNC_ROOT); startTreeVncCommandListener(); - if(isTreeManager() && firstTime) { - getCast = new TreeRootFinderListener(v); - Thread thread = new Thread(getCast, "tree-root-find-listener"); - thread.start(); - firstTime = false; - } + if(isTreeManager() && firstTime) { + getCast = new TreeRootFinderListener(v); + Thread thread = new Thread(getCast, "tree-root-find-listener"); + thread.start(); + firstTime = false; + } } - public void startTreeVncCommandListener() { + public void startTreeVncCommandListener() { acceptThread = new TreeVncCommandChannelListener(this, getAcceptPort()); - Thread thread = new Thread(acceptThread, "TreeVNC-accept"); - thread.start(); + Thread thread = new Thread(acceptThread, "TreeVNC-accept"); + thread.start(); } public TreeVncCommandChannelListener getAcceptThread() { @@ -708,14 +708,14 @@ reconnecting = b; } - public int getReconnectingId() { - return reconnectingId; - } - + public int getReconnectingId() { + return reconnectingId; + } + - public void setShowTree(boolean showTree) { - showTreeNode = showTree; - } + public void setShowTree(boolean showTree) { + showTreeNode = showTree; + } - + }
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncCommandChannelListener.java Sun Jul 06 10:36:30 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncCommandChannelListener.java Sun Jul 06 12:46:20 2014 +0900 @@ -24,7 +24,7 @@ LinkedBlockingQueue<TreeVncCommand> cmdQueue = new LinkedBlockingQueue<TreeVncCommand>(); private Thread acceptThread; protected final static String versionMsg_3_856 = "RFB 003.856\n"; - + public TreeVncCommandChannelListener(TreeRFBProto _rfb, int p) { rfb = _rfb; port = p; @@ -32,23 +32,23 @@ } public void commandMainLoop() { - acceptThread = new Thread(new Runnable() { - @Override - public void run() { - TreeVncCommand cmd = null; - do { - try { - cmd = cmdQueue.poll(1000, TimeUnit.MILLISECONDS); - } catch (InterruptedException e) { - continue; - } - if (cmd!=null) { - cmd.handleTreeVncCommand(); - } - } while (cmd == null || cmd.getCommand() != TreeCommand.QUIT_LOOP); - } - },"root-command-loop"); - acceptThread.start(); + acceptThread = new Thread(new Runnable() { + @Override + public void run() { + TreeVncCommand cmd = null; + do { + try { + cmd = cmdQueue.poll(1000, TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + continue; + } + if (cmd!=null) { + cmd.handleTreeVncCommand(); + } + } while (cmd == null || cmd.getCommand() != TreeCommand.QUIT_LOOP); + } + },"root-command-loop"); + acceptThread.start(); } public void run() { @@ -153,12 +153,12 @@ } return new TreeVncCommand(rfb, myHostName, command, port, hostname, intf, value); } - + void sendRfbVersion(Writer writer) throws IOException, TransportException { writer.write(versionMsg_3_856.getBytes()); } - + byte[] readVersionMsg(Reader reader, Writer writer) throws IOException, TransportException { byte[] b = new byte[HandshakeState.PROTOCOL_STRING_LENGTH ]; @@ -175,7 +175,7 @@ } int rfbMajor = (b[4] - '0') * 100 + (b[5] - '0') * 10 + (b[6] - '0'); -// int rfbMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0'); + // int rfbMinor = (b[8] - '0') * 100 + (b[9] - '0') * 10 + (b[10] - '0'); if (rfbMajor < 3) { throw new IOException( @@ -184,13 +184,13 @@ return null; } - - + + void readSecType(Reader reader) throws TransportException { byte[] b = new byte[1]; reader.read(b); } - + void sendSecurityType(Writer os) throws TransportException { // number-of-security-types os.writeInt(1); @@ -203,19 +203,19 @@ * os.flush(); */ } - + void sendSecResult(Writer os) throws TransportException { - ByteBuffer b = ByteBuffer.allocate(4); - b.order(ByteOrder.BIG_ENDIAN); - b.putInt(0); - os.write(b.array()); + ByteBuffer b = ByteBuffer.allocate(4); + b.order(ByteOrder.BIG_ENDIAN); + b.putInt(0); + os.write(b.array()); } void readClientInit(Reader in) throws TransportException { byte[] b = new byte[0]; in.readBytes(b); } - + byte initData[] = {7, -128, 4, 56, 32, 24, 0, 1, 0, -1, 0, -1, 0, -1, 16, 8, 0, 0, 0, 0, 0, 0, 0, 7, 103, 105, 114, 101, 102, 108, 121}; void sendInitData(Writer os) throws TransportException { // In case of "-d" we have no context
--- a/src/viewer_swing/java/com/glavsoft/viewer/TreeViewer.java Sun Jul 06 10:36:30 2014 +0900 +++ b/src/viewer_swing/java/com/glavsoft/viewer/TreeViewer.java Sun Jul 06 12:46:20 2014 +0900 @@ -13,30 +13,30 @@ private boolean permitChangeScreen = true; private boolean showTree = false; - public static void main(String[] args) { - new TreeViewer().vncStart(args); - } + public static void main(String[] args) { + new TreeViewer().vncStart(args); + } - private void vncStart(String[] args) { - modeSelect(args); - if (treeProxy && !viewer) { - // start treeManager only ( -p flag ) - VncProxyService vps = new VncProxyService(); - rootStart(args,vps); - return; - } - if (viewer) { - ViewerInterface v = cui ? new CuiViewer() : new Viewer(); - if (treeProxy) { - // this viewer also works as tree root ( display mode -d flag ) - // start with no connection waiting server change request - rootStart(args, v); - } else { + private void vncStart(String[] args) { + modeSelect(args); + if (treeProxy && !viewer) { + // start treeManager only ( -p flag ) + VncProxyService vps = new VncProxyService(); + rootStart(args,vps); + return; + } + if (viewer) { + ViewerInterface v = cui ? new CuiViewer() : new Viewer(); + if (treeProxy) { + // this viewer also works as tree root ( display mode -d flag ) + // start with no connection waiting server change request + rootStart(args, v); + } else { v.startTreeViewer(hostName,cui); - } - } + } + } - } + } public void rootStart(String[] args, ViewerInterface v) { v.setNoConnection(noConnection); // should we accept host name here?