# HG changeset patch # User oc # Date 1404291535 -32400 # Node ID 2b3eb4a9492f74eafe9175d16e0469c63d140247 # Parent 614db20d8fab379e379a396cd59d7080ac8c9f33 MyRfbProto reorganization diff -r 614db20d8fab -r 2b3eb4a9492f build.gradle --- a/build.gradle Wed Jul 02 16:31:25 2014 +0900 +++ b/build.gradle Wed Jul 02 17:58:55 2014 +0900 @@ -1,8 +1,8 @@ apply plugin:'java' apply plugin: 'eclipse' -sourceCompatibility = 1.8 -targetCompatibility = 1.8 +sourceCompatibility = 1.7 +targetCompatibility = 1.7 version = '2.7.2' project.ext.baseName = 'tightvnc-jviewer' diff -r 614db20d8fab -r 2b3eb4a9492f src/main/java/com/glavsoft/rfb/protocol/ReceiverTask.java --- a/src/main/java/com/glavsoft/rfb/protocol/ReceiverTask.java Wed Jul 02 16:31:25 2014 +0900 +++ b/src/main/java/com/glavsoft/rfb/protocol/ReceiverTask.java Wed Jul 02 17:58:55 2014 +0900 @@ -47,7 +47,7 @@ import java.util.logging.Logger; import jp.ac.u_ryukyu.treevnc.MyRfbProto; -import jp.ac.u_ryukyu.treevnc.client.TreeVncProtocol; +import jp.ac.u_ryukyu.treevnc.TreeVncProtocol; public class ReceiverTask implements Runnable { private static final byte FRAMEBUFFER_UPDATE = 0; diff -r 614db20d8fab -r 2b3eb4a9492f src/main/java/jp/ac/u_ryukyu/treevnc/CreateConnectionParam.java --- a/src/main/java/jp/ac/u_ryukyu/treevnc/CreateConnectionParam.java Wed Jul 02 16:31:25 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/CreateConnectionParam.java Wed Jul 02 17:58:55 2014 +0900 @@ -5,9 +5,6 @@ import com.glavsoft.viewer.ViewerInterface; import com.glavsoft.viewer.swing.ConnectionParams; -import jp.ac.u_ryukyu.treevnc.client.FindRoot; -import jp.ac.u_ryukyu.treevnc.client.TreeVncProtocol; - public class CreateConnectionParam { private String hostName; private int portNumber = ConnectionParams.DEFAULT_VNC_ROOT; diff -r 614db20d8fab -r 2b3eb4a9492f src/main/java/jp/ac/u_ryukyu/treevnc/FindRoot.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/FindRoot.java Wed Jul 02 17:58:55 2014 +0900 @@ -0,0 +1,155 @@ +package jp.ac.u_ryukyu.treevnc; + +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.TreeCommand; +import com.glavsoft.viewer.swing.ConnectionParams; + +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) TreeCommand.FIND_ROOT.cmd); + 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(); + } + + } + + 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; + } +} diff -r 614db20d8fab -r 2b3eb4a9492f src/main/java/jp/ac/u_ryukyu/treevnc/MyRfbProto.java --- a/src/main/java/jp/ac/u_ryukyu/treevnc/MyRfbProto.java Wed Jul 02 16:31:25 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/MyRfbProto.java Wed Jul 02 17:58:55 2014 +0900 @@ -1,6 +1,7 @@ package jp.ac.u_ryukyu.treevnc; import java.io.IOException; +import java.io.OutputStream; import java.net.BindException; import java.net.InetAddress; import java.net.NetworkInterface; @@ -18,10 +19,6 @@ import java.util.zip.Deflater; import java.util.zip.Inflater; -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 com.glavsoft.exceptions.TransportException; import com.glavsoft.rfb.client.ClientToServerMessage; import com.glavsoft.rfb.encoding.EncodingType; @@ -33,7 +30,7 @@ import com.glavsoft.viewer.swing.ConnectionParams; -public abstract class MyRfbProto { +public class MyRfbProto { final static int FramebufferUpdateRequest = 3; final static int CheckDelay = 11; protected final static int FramebufferUpdate = 0; @@ -60,16 +57,21 @@ private TreeRootFinderListener getCast; private CreateConnectionParam cp; private boolean hasViewer = 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 isTreeManager; - public MyRfbProto() { + public MyRfbProto(boolean isTreeManager) { rThread = new RequestScreenThread(this); nets.setMyRfb(this); + this.isTreeManager = isTreeManager; } - abstract public boolean isTreeManager() ; + public boolean isTreeManager() { + return isTreeManager; + } public ProtocolContext getContext() { return context; @@ -320,15 +322,37 @@ return viewer; } - public void setTerminationType(boolean setType) { - /*nop*/ + + void sendInitData(OutputStream os) throws IOException { + os.write(context.getInitData()); } + + @Override + public void setTerminationType(boolean setType) { + normalTermination = setType; + } + + @Override public boolean getTerminationType() { - /*nop*/ - return true; + 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(); + } + } + + @Override + public void createRootSelectionPanel(CreateConnectionParam cp) { + rootSelectionPanel = new TreeVncRootSelectionPanel(); + rootSelectionPanel.setCp(cp); + } + public void close() { // none } @@ -696,6 +720,11 @@ public int getReconnectingId() { return reconnectingId; } + + + public void setShowTree(boolean showTree) { + showTreeNode = showTree; + } } diff -r 614db20d8fab -r 2b3eb4a9492f src/main/java/jp/ac/u_ryukyu/treevnc/MyVncClient.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/MyVncClient.java Wed Jul 02 17:58:55 2014 +0900 @@ -0,0 +1,113 @@ +package jp.ac.u_ryukyu.treevnc; + +import java.awt.event.WindowEvent; +import java.awt.event.WindowListener; +import java.util.logging.Logger; + +import com.glavsoft.core.SettingsChangedEvent; +import com.glavsoft.rfb.IChangeSettingsListener; +import com.glavsoft.rfb.IRfbSessionListener; +import com.glavsoft.rfb.protocol.ProtocolSettings; +import com.glavsoft.viewer.Viewer; +import com.glavsoft.viewer.cli.Parser; +import com.glavsoft.viewer.swing.ParametersHandler; + +// "Viewer.java" location is src/viewer_swing/java/com/glavsoft/viewer. +public class MyVncClient extends Viewer implements IRfbSessionListener, + WindowListener, IChangeSettingsListener { + + /** + * + */ + private static final long serialVersionUID = 1L; + public static Logger logger = Logger.getLogger("com.glavsoft"); + private final ProtocolSettings settings; + + + public MyVncClient() { + settings = ProtocolSettings.getDefaultSettings(); + //treeConnectionManager = new TreeConnectionManager(this, isApplet); + } + + public MyVncClient(Parser parser) { + this(); + ParametersHandler.completeSettingsFromCLI(parser, connectionParams, settings, uiSettings); + passwordFromParams = parser.getValueFor(ParametersHandler.ARG_PASSWORD); + logger.info("TightVNC Viewer version " + ver()); + isApplet = false; + } + + public void treeVncClient(String[] argv) { + new MyVncClient(); + } + + public static void main(String[] argv) { + String[] mainArgs = argv; + System.out.println(mainArgs.length); + Parser parser = new Parser(); + ParametersHandler.completeParserOptions(parser); + + parser.parse(argv); + if (parser.isSet(ParametersHandler.ARG_HELP)) { + printUsage(parser.optionsUsage()); + System.exit(0); + } + MyVncClient myClient = new MyVncClient(parser); + new Thread(myClient).start(); + } + + public void startClient(String[] argv) { + String[] mainArgs = argv; + System.out.println(mainArgs.length); + Parser parser = new Parser(); + ParametersHandler.completeParserOptions(parser); + + parser.parse(argv); + if (parser.isSet(ParametersHandler.ARG_HELP)) { + Viewer.printUsage(parser.optionsUsage()); + System.exit(0); + } + MyVncClient myClient = new MyVncClient(parser); + new Thread(myClient).start(); + } + + + @Override + public void windowClosing(WindowEvent e) { + if (e != null && e.getComponent() != null) { + e.getWindow().setVisible(false); + } + closeApp(); + } + + @Override + public void windowActivated(WindowEvent e) { /*nop*/ } + + @Override + public void windowClosed(WindowEvent e) { /*nop*/ } + + @Override + public void windowDeactivated(WindowEvent e) { /*nop*/ } + + @Override + public void windowDeiconified(WindowEvent e) { /*nop*/ } + + @Override + public void windowIconified(WindowEvent e) { /*nop*/ } + + @Override + public void windowOpened(WindowEvent e) { /*nop*/ } + + @Override + public void settingsChanged(SettingsChangedEvent event) { + // TODO Auto-generated method stub + + } + + @Override + public void rfbSessionStopped(String reason) { + // TODO Auto-generated method stub + + } + +} diff -r 614db20d8fab -r 2b3eb4a9492f src/main/java/jp/ac/u_ryukyu/treevnc/RequestScreenThread.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/RequestScreenThread.java Wed Jul 02 17:58:55 2014 +0900 @@ -0,0 +1,48 @@ +package jp.ac.u_ryukyu.treevnc; + +import com.glavsoft.rfb.protocol.Protocol; + +public class RequestScreenThread implements Runnable { + public MyRfbProto rfb; + Protocol protocol; + + public RequestScreenThread(MyRfbProto _rfb) { + rfb = _rfb; + } + + + + /* (non-Javadoc) + * @see java.lang.Runnable#run() + * + * request full screen update every 3 second. + * restart send this immediately. + * nobody call this. + * + */ + public void run() { + while (true) { + try { + synchronized(this) { + wait(); + } + Thread.sleep(3000); + rfb.writeFramebufferUpdateRequest(0, 0, protocol.getFbWidth(), + protocol.getFbHeight(), false); + + } catch (Exception e) { + e.printStackTrace(); + break; + } + } + } + + + /** + * do nothing. + */ + public synchronized void reStart() { + notify(); + } + +} diff -r 614db20d8fab -r 2b3eb4a9492f src/main/java/jp/ac/u_ryukyu/treevnc/TreeManagement.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/TreeManagement.java Wed Jul 02 17:58:55 2014 +0900 @@ -0,0 +1,196 @@ +package jp.ac.u_ryukyu.treevnc; + + +import java.io.IOException; +import java.util.LinkedList; + +public class TreeManagement { + + private LinkedList nodeList = new LinkedList(); + boolean runflag = false; + private final int treebranch = 2; + private boolean showTreeNode = 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) ; + } + + 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 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 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); + if (deadParent.getTreeNum() == 0) { + // if dead root, connect me. + connectTo(me, lostParentNode); + return; + } + if (getChildNode(deadParent, 0) == null) return; + moveLastNodeToLostNodePosition(deadParent.getTreeNum()); + lostNodeConnection(deadParent); + // if lostParentNode is the last one, we don't need reconnection + // Thread.sleep(intv_time); + } + + /** + * send reconnect to all children + * @param newparent + */ + private void lostNodeConnection(TreeVNCNode oldParent) { + 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); + if (child != null) { + connectTo(newparent, child); + } + } + + if (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 + } + } + + private int isLeader(TreeVNCNode n) { + return ( n.getTreeNum() % treebranch == 1) ? 1 : 0; + } + + private TreeVNCNode lookup(LinkedList list, String hostname, int port) { + 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) { + 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; + for (int i=0; i getList() { + return nodeList; + } + + public void setList(LinkedList _ls) { + nodeList = _ls; + } + + public int getTreeBranch() { + return treebranch; + } + +} \ No newline at end of file diff -r 614db20d8fab -r 2b3eb4a9492f src/main/java/jp/ac/u_ryukyu/treevnc/TreeRootFinderListener.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/TreeRootFinderListener.java Wed Jul 02 17:58:55 2014 +0900 @@ -0,0 +1,102 @@ +package jp.ac.u_ryukyu.treevnc; + +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.InetAddress; +import java.net.MulticastSocket; +import java.net.UnknownHostException; + +import com.glavsoft.rfb.protocol.ProtocolContext.TreeCommand; +import com.glavsoft.viewer.ViewerInterface; +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 ViewerInterface vps; + private MulticastSocket soc; + + public TreeRootFinderListener(ViewerInterface vncProxyService) { + vps = vncProxyService; + } + + /** + * 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 hostname = recvPacket.getAddress().getHostAddress(); + byte[] reply = recvPacket.getData(); + int len = recvPacket.getLength(); + if (len != 12) { + continue; + } + if ((reply[0]&0xff) != TreeCommand.FIND_ROOT.cmd) { + 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(hostname, port); + try { + t.findRootReply(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(); + } +} diff -r 614db20d8fab -r 2b3eb4a9492f src/main/java/jp/ac/u_ryukyu/treevnc/TreeVNCNetwork.java --- a/src/main/java/jp/ac/u_ryukyu/treevnc/TreeVNCNetwork.java Wed Jul 02 16:31:25 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/TreeVNCNetwork.java Wed Jul 02 17:58:55 2014 +0900 @@ -7,8 +7,6 @@ import java.util.Enumeration; import java.util.HashMap; -import jp.ac.u_ryukyu.treevnc.server.TreeManagement; - import com.glavsoft.viewer.swing.ConnectionParams; public class TreeVNCNetwork { diff -r 614db20d8fab -r 2b3eb4a9492f src/main/java/jp/ac/u_ryukyu/treevnc/TreeVNCNode.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/TreeVNCNode.java Wed Jul 02 17:58:55 2014 +0900 @@ -0,0 +1,40 @@ +package jp.ac.u_ryukyu.treevnc; + +public class TreeVNCNode { + String hostname; + int port; + int treeNum; + String localhostname; + + public TreeVNCNode(String hostname, int port, String localhostname) { + this.hostname = hostname; + this.port = port; + this.localhostname = localhostname; + } + + public int getTreeNum() { + return treeNum; + } + + public void setTreeNum(int treeNum) { + this.treeNum = treeNum; + } + + public String getHostname() { + return hostname; + } + + public int getPort() { + return port; + } + + public void setPort(int acceptPort) { + port = acceptPort; + } + + public void setHostName(String localhostname) { + hostname = localhostname; + } + + +} diff -r 614db20d8fab -r 2b3eb4a9492f src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncProtocol.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncProtocol.java Wed Jul 02 17:58:55 2014 +0900 @@ -0,0 +1,90 @@ +package jp.ac.u_ryukyu.treevnc; + +import java.io.*; +import java.net.*; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import com.glavsoft.rfb.protocol.ProtocolContext.TreeCommand; + +public class TreeVncProtocol { + private BufferedReader is = null; + private DataOutputStream os = null; + private Socket echoSocket = null; + private int rootPort; + private String rootAddress; + + public TreeVncProtocol(String name, int echoPort) { + this.rootPort = echoPort; + this.rootAddress = name; + } + + public void openport() { + try { + echoSocket = new Socket(rootAddress, rootPort); + // echoSocket.setReuseAddress(true); + os = new DataOutputStream(echoSocket.getOutputStream()); + is = new BufferedReader(new InputStreamReader( + echoSocket.getInputStream())); + } catch (UnknownHostException e) { + System.err.println("Don't know about host: "+rootAddress); + } catch (IOException e) { + System.out.println(rootAddress + " Connection Faild"); + System.exit(0); + } + } + + public void findRootReply(int port) throws IOException { + sendWithHostAndPort(TreeCommand.FIND_ROOT_REPLY, null, port, (short) 0); + } + + public void whereToConnect(String hostname, int port) throws IOException { + sendWithHostAndPort(TreeCommand.WHERE_TO_CONNECT, hostname, port, (short) 0); + } + + public void connectTo(String hostname, int port,int leaderFlag, short yourId) throws IOException{ + TreeCommand command = leaderFlag == 1 ? TreeCommand.CONNECT_TO_AS_LEADER : TreeCommand.CONNECT_TO; + sendWithHostAndPort(command , hostname, port, yourId); + } + + public void lostParent(String hostname, int port) throws IOException { + sendWithHostAndPort(TreeCommand.LOST_PARENT, hostname, port, (short) 0); + } + + public void sendWithHostAndPort(TreeCommand command, String hostname, int port, short value) + throws IOException { + openport(); + if (hostname == null) { + // in case of root finder, we can't get localaddress from datagram packet. + // so use local part of TCP socket. + hostname = echoSocket.getLocalAddress().getHostAddress(); + } + int cmdlen = 4+4+4+hostname.length(); + if (cmdlen < 12) cmdlen=12; + ByteBuffer buf = ByteBuffer.allocate(cmdlen); + buf.order(ByteOrder.BIG_ENDIAN); + buf.put((byte) command.cmd); + + buf.put((byte) 0); + buf.putShort(value); + buf.putInt(4+hostname.length()); // length + buf.putInt(port); + buf.put(hostname.getBytes(), 0, hostname.length()); + while (buf.hasRemaining() ) buf.put((byte)0) ; + buf.flip(); + sendCommandToTheRoot(buf); + } + + public void sendCommandToTheRoot(ByteBuffer buf) throws IOException { + char[] charBuf = new char[12]; + is.read(charBuf , 0, 12); // skip root's version header + os.write(buf.array(), 0, buf.limit()); // send our command + streamClose(); + } + + void streamClose() throws IOException { + os.close(); + is.close(); + echoSocket.close(); + } + +} \ No newline at end of file diff -r 614db20d8fab -r 2b3eb4a9492f src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncRootSelectionPanel.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncRootSelectionPanel.java Wed Jul 02 17:58:55 2014 +0900 @@ -0,0 +1,136 @@ +package jp.ac.u_ryukyu.treevnc; + +import javax.swing.*; + +import com.glavsoft.viewer.swing.ConnectionParams; + +import java.awt.*; +import java.awt.event.*; + +public class TreeVncRootSelectionPanel extends JFrame implements ActionListener, + ItemListener { + + /** + * + */ + private static final long serialVersionUID = 1L; + private JPanel panel = new JPanel(); + private JButton button = new JButton("Connect"); + private TextField t1; + private TextField t2; + private double width = 750; + private double height = 500; + private JLabel label; + private boolean flag; + private int counter = 0; + // private JCheckBox[] check = new JCheckBox[20]; + private Checkbox[] check = new Checkbox[20]; + private String port; + private CheckboxGroup ch = new CheckboxGroup(); + private Container contentPane = getContentPane(); + private CreateConnectionParam cp; + + public TreeVncRootSelectionPanel() { + setTitle("TreeVNC Root Address"); + setResizable(false); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + + public void ipRegister() { + setSize(); + setText(); + setButton(); + visible(); + } + + private void setSize() { + Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); + width = (d.getWidth() / 2); + height = (d.getHeight() / 2); + } + + public void visible() { + Point point = new Point(); + point.setLocation(width - 250, height - 80); + setLocation(point.getLocation()); + pack(); + setVisible(true); + } + + public void unVisible() { + setVisible(false); + } + + private void setText() { + t1 = new TextField("Address", 30); + t2 = new TextField(Integer.toString(ConnectionParams.DEFAULT_VNC_ROOT), 5); + panel.add(t1); + panel.add(t2); + //panel.add(button); + //button.addActionListener(this); + label = new JLabel(); + contentPane.add(panel, BorderLayout.CENTER); + contentPane.add(label, BorderLayout.SOUTH); + } + + public void checkBox(String str) { + if (counter == 0) + check[counter] = new Checkbox(str, true, ch); + else + check[counter] = new Checkbox(str, false, ch); + check[counter].addItemListener(this); + panel.add(check[counter]); + panel.setLayout(new GridLayout(counter + 2, 0)); + panel.setLocation((int) width - 250, (int) height - 80); + counter++; + } + + public void setButton() { + panel.add(button); + button.addActionListener(this); + contentPane.add(panel, BorderLayout.CENTER); + } + + public String getAddressOption() { + while (!(flag)) { + try { + Thread.sleep(500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + return t1.getText(); + } + + public String getPortOption() { + return t2.getText(); + } + + public String getPort() { + return port; + } + + public void actionPerformed(ActionEvent e) { + flag = true; + for (int t = 0; t < counter; t++) { + if (check[t].getState()) { + System.out.println(check[t].getLabel()); + String str = check[t].getLabel(); + String[] temp = str.split(":"); + String port = temp[0]; + String hostAddress = temp[1]; + String rootAddress = temp[2]; + cp.setHostName(hostAddress,Integer.parseInt(port),rootAddress); + unVisible(); + } + } + } + + + public void itemStateChanged(ItemEvent e) { + } + + public void setCp(CreateConnectionParam cp) { + this.cp = cp; + } +} diff -r 614db20d8fab -r 2b3eb4a9492f src/main/java/jp/ac/u_ryukyu/treevnc/VncProxyService.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/VncProxyService.java Wed Jul 02 17:58:55 2014 +0900 @@ -0,0 +1,83 @@ +package jp.ac.u_ryukyu.treevnc; + +import java.awt.event.*; +import java.util.logging.Logger; + +import com.glavsoft.core.SettingsChangedEvent; +import com.glavsoft.rfb.IChangeSettingsListener; +import com.glavsoft.rfb.IRfbSessionListener; +import com.glavsoft.rfb.protocol.ProtocolSettings; +import com.glavsoft.viewer.Viewer; +import com.glavsoft.viewer.cli.Parser; +import com.glavsoft.viewer.swing.ParametersHandler; + +public class VncProxyService extends Viewer implements Runnable, + IRfbSessionListener, IChangeSettingsListener { + + /** + * + */ + private static final long serialVersionUID = 1L; + + public static Logger logger = Logger.getLogger("com.glavsoft"); + private boolean forceReconnection; + private String reconnectionReason; + static VncProxyService currentVps; + + public VncProxyService() { + ProtocolSettings.getDefaultSettings(); + uiSettings = super.uiSettings; + } + + public VncProxyService(VncProxyService vps, String hostName) { + this(); + connectionParams.hostName = hostName; + myRfb = vps.myRfb; + forceReconnection = vps.forceReconnection; + reconnectionReason = vps.reconnectionReason; + } + + public static void main(String[] argv) { + String[] mainArgs = argv; + System.out.println(mainArgs.length); + // input into arguments Decision + + Parser parser = new Parser(); + ParametersHandler.completeParserOptions(parser); + + parser.parse(argv); + if (parser.isSet(ParametersHandler.ARG_HELP)) { + printUsage(parser.optionsUsage()); + System.exit(0); + } + VncProxyService vps = new VncProxyService(); + vps.initRoot(vps.myRfb, "localhost"); + } + + + + @Override + public void destroy() { + + } + + @Override + public void windowClosing(WindowEvent e) { + if (e != null && e.getComponent() != null) { + e.getWindow().setVisible(false); + } + closeApp(); + } + + protected void socketClose() { + } + + @Override + public void rfbSessionStopped(final String reason) { + } + + @Override + public void settingsChanged(SettingsChangedEvent event) { + } + +} diff -r 614db20d8fab -r 2b3eb4a9492f src/main/java/jp/ac/u_ryukyu/treevnc/client/FindRoot.java --- a/src/main/java/jp/ac/u_ryukyu/treevnc/client/FindRoot.java Wed Jul 02 16:31:25 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,157 +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.TreeCommand; -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) TreeCommand.FIND_ROOT.cmd); - 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(); - } - - } - - 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; - } -} diff -r 614db20d8fab -r 2b3eb4a9492f src/main/java/jp/ac/u_ryukyu/treevnc/client/MyRfbProtoClient.java --- a/src/main/java/jp/ac/u_ryukyu/treevnc/client/MyRfbProtoClient.java Wed Jul 02 16:31:25 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,85 +0,0 @@ -package jp.ac.u_ryukyu.treevnc.client; - -import java.io.IOException; -import java.io.OutputStream; - -import jp.ac.u_ryukyu.treevnc.CreateConnectionParam; -import jp.ac.u_ryukyu.treevnc.MyRfbProto; - -import com.glavsoft.exceptions.TransportException; -import com.glavsoft.transport.Reader; - -public class MyRfbProtoClient extends MyRfbProto { - private boolean normalTermination; - private TreeVncRootSelectionPanel rootSelectionPanel; - - - public MyRfbProtoClient() { - } - - @Override - public boolean isTreeManager() { - return false; - } - - void sendInitData(OutputStream os) throws IOException { - os.write(context.getInitData()); - } - - - @Override - public void setTerminationType(boolean setType) { - normalTermination = setType; - } - - @Override - 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(); - } - } - - @Override - public void createRootSelectionPanel(CreateConnectionParam cp) { - rootSelectionPanel = new TreeVncRootSelectionPanel(); - rootSelectionPanel.setCp(cp); - } - - - /* - * 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 sendData = new LinkedList(); -// 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); - - } - -} diff -r 614db20d8fab -r 2b3eb4a9492f src/main/java/jp/ac/u_ryukyu/treevnc/client/MyVncClient.java --- a/src/main/java/jp/ac/u_ryukyu/treevnc/client/MyVncClient.java Wed Jul 02 16:31:25 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,113 +0,0 @@ -package jp.ac.u_ryukyu.treevnc.client; - -import java.awt.event.WindowEvent; -import java.awt.event.WindowListener; -import java.util.logging.Logger; - -import com.glavsoft.core.SettingsChangedEvent; -import com.glavsoft.rfb.IChangeSettingsListener; -import com.glavsoft.rfb.IRfbSessionListener; -import com.glavsoft.rfb.protocol.ProtocolSettings; -import com.glavsoft.viewer.Viewer; -import com.glavsoft.viewer.cli.Parser; -import com.glavsoft.viewer.swing.ParametersHandler; - -// "Viewer.java" location is src/viewer_swing/java/com/glavsoft/viewer. -public class MyVncClient extends Viewer implements IRfbSessionListener, - WindowListener, IChangeSettingsListener { - - /** - * - */ - private static final long serialVersionUID = 1L; - public static Logger logger = Logger.getLogger("com.glavsoft"); - private final ProtocolSettings settings; - - - public MyVncClient() { - settings = ProtocolSettings.getDefaultSettings(); - //treeConnectionManager = new TreeConnectionManager(this, isApplet); - } - - public MyVncClient(Parser parser) { - this(); - ParametersHandler.completeSettingsFromCLI(parser, connectionParams, settings, uiSettings); - passwordFromParams = parser.getValueFor(ParametersHandler.ARG_PASSWORD); - logger.info("TightVNC Viewer version " + ver()); - isApplet = false; - } - - public void treeVncClient(String[] argv) { - new MyVncClient(); - } - - public static void main(String[] argv) { - String[] mainArgs = argv; - System.out.println(mainArgs.length); - Parser parser = new Parser(); - ParametersHandler.completeParserOptions(parser); - - parser.parse(argv); - if (parser.isSet(ParametersHandler.ARG_HELP)) { - printUsage(parser.optionsUsage()); - System.exit(0); - } - MyVncClient myClient = new MyVncClient(parser); - new Thread(myClient).start(); - } - - public void startClient(String[] argv) { - String[] mainArgs = argv; - System.out.println(mainArgs.length); - Parser parser = new Parser(); - ParametersHandler.completeParserOptions(parser); - - parser.parse(argv); - if (parser.isSet(ParametersHandler.ARG_HELP)) { - Viewer.printUsage(parser.optionsUsage()); - System.exit(0); - } - MyVncClient myClient = new MyVncClient(parser); - new Thread(myClient).start(); - } - - - @Override - public void windowClosing(WindowEvent e) { - if (e != null && e.getComponent() != null) { - e.getWindow().setVisible(false); - } - closeApp(); - } - - @Override - public void windowActivated(WindowEvent e) { /*nop*/ } - - @Override - public void windowClosed(WindowEvent e) { /*nop*/ } - - @Override - public void windowDeactivated(WindowEvent e) { /*nop*/ } - - @Override - public void windowDeiconified(WindowEvent e) { /*nop*/ } - - @Override - public void windowIconified(WindowEvent e) { /*nop*/ } - - @Override - public void windowOpened(WindowEvent e) { /*nop*/ } - - @Override - public void settingsChanged(SettingsChangedEvent event) { - // TODO Auto-generated method stub - - } - - @Override - public void rfbSessionStopped(String reason) { - // TODO Auto-generated method stub - - } - -} diff -r 614db20d8fab -r 2b3eb4a9492f src/main/java/jp/ac/u_ryukyu/treevnc/client/TreeVncProtocol.java --- a/src/main/java/jp/ac/u_ryukyu/treevnc/client/TreeVncProtocol.java Wed Jul 02 16:31:25 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,90 +0,0 @@ -package jp.ac.u_ryukyu.treevnc.client; - -import java.io.*; -import java.net.*; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import com.glavsoft.rfb.protocol.ProtocolContext.TreeCommand; - -public class TreeVncProtocol { - private BufferedReader is = null; - private DataOutputStream os = null; - private Socket echoSocket = null; - private int rootPort; - private String rootAddress; - - public TreeVncProtocol(String name, int echoPort) { - this.rootPort = echoPort; - this.rootAddress = name; - } - - public void openport() { - try { - echoSocket = new Socket(rootAddress, rootPort); - // echoSocket.setReuseAddress(true); - os = new DataOutputStream(echoSocket.getOutputStream()); - is = new BufferedReader(new InputStreamReader( - echoSocket.getInputStream())); - } catch (UnknownHostException e) { - System.err.println("Don't know about host: "+rootAddress); - } catch (IOException e) { - System.out.println(rootAddress + " Connection Faild"); - System.exit(0); - } - } - - public void findRootReply(int port) throws IOException { - sendWithHostAndPort(TreeCommand.FIND_ROOT_REPLY, null, port, (short) 0); - } - - public void whereToConnect(String hostname, int port) throws IOException { - sendWithHostAndPort(TreeCommand.WHERE_TO_CONNECT, hostname, port, (short) 0); - } - - public void connectTo(String hostname, int port,int leaderFlag, short yourId) throws IOException{ - TreeCommand command = leaderFlag == 1 ? TreeCommand.CONNECT_TO_AS_LEADER : TreeCommand.CONNECT_TO; - sendWithHostAndPort(command , hostname, port, yourId); - } - - public void lostParent(String hostname, int port) throws IOException { - sendWithHostAndPort(TreeCommand.LOST_PARENT, hostname, port, (short) 0); - } - - public void sendWithHostAndPort(TreeCommand command, String hostname, int port, short value) - throws IOException { - openport(); - if (hostname == null) { - // in case of root finder, we can't get localaddress from datagram packet. - // so use local part of TCP socket. - hostname = echoSocket.getLocalAddress().getHostAddress(); - } - int cmdlen = 4+4+4+hostname.length(); - if (cmdlen < 12) cmdlen=12; - ByteBuffer buf = ByteBuffer.allocate(cmdlen); - buf.order(ByteOrder.BIG_ENDIAN); - buf.put((byte) command.cmd); - - buf.put((byte) 0); - buf.putShort(value); - buf.putInt(4+hostname.length()); // length - buf.putInt(port); - buf.put(hostname.getBytes(), 0, hostname.length()); - while (buf.hasRemaining() ) buf.put((byte)0) ; - buf.flip(); - sendCommandToTheRoot(buf); - } - - public void sendCommandToTheRoot(ByteBuffer buf) throws IOException { - char[] charBuf = new char[12]; - is.read(charBuf , 0, 12); // skip root's version header - os.write(buf.array(), 0, buf.limit()); // send our command - streamClose(); - } - - void streamClose() throws IOException { - os.close(); - is.close(); - echoSocket.close(); - } - -} \ No newline at end of file diff -r 614db20d8fab -r 2b3eb4a9492f src/main/java/jp/ac/u_ryukyu/treevnc/client/TreeVncRootSelectionPanel.java --- a/src/main/java/jp/ac/u_ryukyu/treevnc/client/TreeVncRootSelectionPanel.java Wed Jul 02 16:31:25 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,137 +0,0 @@ -package jp.ac.u_ryukyu.treevnc.client; - -import javax.swing.*; - -import com.glavsoft.viewer.swing.ConnectionParams; - -import java.awt.*; -import java.awt.event.*; -import jp.ac.u_ryukyu.treevnc.CreateConnectionParam; - -public class TreeVncRootSelectionPanel extends JFrame implements ActionListener, - ItemListener { - - /** - * - */ - private static final long serialVersionUID = 1L; - private JPanel panel = new JPanel(); - private JButton button = new JButton("Connect"); - private TextField t1; - private TextField t2; - private double width = 750; - private double height = 500; - private JLabel label; - private boolean flag; - private int counter = 0; - // private JCheckBox[] check = new JCheckBox[20]; - private Checkbox[] check = new Checkbox[20]; - private String port; - private CheckboxGroup ch = new CheckboxGroup(); - private Container contentPane = getContentPane(); - private CreateConnectionParam cp; - - public TreeVncRootSelectionPanel() { - setTitle("TreeVNC Root Address"); - setResizable(false); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - } - - public void ipRegister() { - setSize(); - setText(); - setButton(); - visible(); - } - - private void setSize() { - Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); - width = (d.getWidth() / 2); - height = (d.getHeight() / 2); - } - - public void visible() { - Point point = new Point(); - point.setLocation(width - 250, height - 80); - setLocation(point.getLocation()); - pack(); - setVisible(true); - } - - public void unVisible() { - setVisible(false); - } - - private void setText() { - t1 = new TextField("Address", 30); - t2 = new TextField(Integer.toString(ConnectionParams.DEFAULT_VNC_ROOT), 5); - panel.add(t1); - panel.add(t2); - //panel.add(button); - //button.addActionListener(this); - label = new JLabel(); - contentPane.add(panel, BorderLayout.CENTER); - contentPane.add(label, BorderLayout.SOUTH); - } - - public void checkBox(String str) { - if (counter == 0) - check[counter] = new Checkbox(str, true, ch); - else - check[counter] = new Checkbox(str, false, ch); - check[counter].addItemListener(this); - panel.add(check[counter]); - panel.setLayout(new GridLayout(counter + 2, 0)); - panel.setLocation((int) width - 250, (int) height - 80); - counter++; - } - - public void setButton() { - panel.add(button); - button.addActionListener(this); - contentPane.add(panel, BorderLayout.CENTER); - } - - public String getAddressOption() { - while (!(flag)) { - try { - Thread.sleep(500); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - return t1.getText(); - } - - public String getPortOption() { - return t2.getText(); - } - - public String getPort() { - return port; - } - - public void actionPerformed(ActionEvent e) { - flag = true; - for (int t = 0; t < counter; t++) { - if (check[t].getState()) { - System.out.println(check[t].getLabel()); - String str = check[t].getLabel(); - String[] temp = str.split(":"); - String port = temp[0]; - String hostAddress = temp[1]; - String rootAddress = temp[2]; - cp.setHostName(hostAddress,Integer.parseInt(port),rootAddress); - unVisible(); - } - } - } - - - public void itemStateChanged(ItemEvent e) { - } - - public void setCp(CreateConnectionParam cp) { - this.cp = cp; - } -} diff -r 614db20d8fab -r 2b3eb4a9492f src/main/java/jp/ac/u_ryukyu/treevnc/server/MyRfbProtoProxy.java --- a/src/main/java/jp/ac/u_ryukyu/treevnc/server/MyRfbProtoProxy.java Wed Jul 02 16:31:25 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,111 +0,0 @@ -package jp.ac.u_ryukyu.treevnc.server; - -//import static org.junit.Assert.*; -//import org.junit.Test; - -import java.io.IOException; -import java.net.Socket; -import java.nio.ByteBuffer; -import java.util.LinkedList; -import com.glavsoft.transport.Writer; -import jp.ac.u_ryukyu.treevnc.MyRfbProto; - -public class MyRfbProtoProxy extends MyRfbProto { - - /** - * CheckMillis is one of new msgType for RFB 3.855 and 3.856. - */ - final static byte SpeedCheckMillis = 4; - // Secyrity type of OS X - final static int SecTypeReqAccess = 32; - // Supported authentication types - final static int AuthAccess = 32; - boolean printStatusFlag = false; - long startCheckTime; - private LinkedList cliList; - public RequestScreenThread rThread; - private Thread requestThread; - public boolean showTreeNode = false; - - public MyRfbProtoProxy() { - rThread = new RequestScreenThread(this); - requestThread = new Thread(rThread, "full-screen-request-timer"); - } - - @Override - public boolean isTreeManager() { - return true; - } - - public void setStream(Writer _writer) { - // os = _writer; - } - - public int getAcceptPort() { - return acceptPort; - } - - void setSoTimeout(int num) throws IOException { - servSock.setSoTimeout(num); - } - - public Socket accept() throws IOException { - return servSock.accept(); - } - - public void socketClose() throws IOException { - servSock.close(); - } - - void addSock(Socket sock) { - cliList.add(sock); - } - - - void requestThreadStart() { - requestThread.start(); - } - - public synchronized void requestThreadNotify() { - rThread.reStart(); - } - - public void dump32(LinkedList bufs) { - int len = 0; - for (ByteBuffer b : bufs) - len += b.remaining(); - ByteBuffer top = bufs.getFirst(); - ByteBuffer end = bufs.getLast(); - System.err.println("length: " + len); - System.err.print("head 0: "); - for (int i = 0; i < 16 && i < top.remaining(); i++) { - System.err.print(" " + top.get(i)); - } - System.err.print("tail 0: "); - for (int i = 0; i < 16 && i < end.remaining(); i++) { - System.err.print(" " + end.get(i)); - } - System.err.println(); - } - - - - @Override - public boolean getReadyReconnect() { - return readyReconnect; - } - - - - @Override - public void addHostToSelectionPanel(int port, String hostname, - String myHostName) { - } - - public void setShowTree(boolean showTree) { - showTreeNode = showTree; - } - - -} - diff -r 614db20d8fab -r 2b3eb4a9492f src/main/java/jp/ac/u_ryukyu/treevnc/server/RequestScreenThread.java --- a/src/main/java/jp/ac/u_ryukyu/treevnc/server/RequestScreenThread.java Wed Jul 02 16:31:25 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,50 +0,0 @@ -package jp.ac.u_ryukyu.treevnc.server; - -import jp.ac.u_ryukyu.treevnc.MyRfbProto; - -import com.glavsoft.rfb.protocol.Protocol; - -public class RequestScreenThread implements Runnable { - public MyRfbProto rfb; - Protocol protocol; - - public RequestScreenThread(MyRfbProto _rfb) { - rfb = _rfb; - } - - - - /* (non-Javadoc) - * @see java.lang.Runnable#run() - * - * request full screen update every 3 second. - * restart send this immediately. - * nobody call this. - * - */ - public void run() { - while (true) { - try { - synchronized(this) { - wait(); - } - Thread.sleep(3000); - rfb.writeFramebufferUpdateRequest(0, 0, protocol.getFbWidth(), - protocol.getFbHeight(), false); - - } catch (Exception e) { - e.printStackTrace(); - break; - } - } - } - - - /** - * do nothing. - */ - public synchronized void reStart() { - notify(); - } - -} diff -r 614db20d8fab -r 2b3eb4a9492f src/main/java/jp/ac/u_ryukyu/treevnc/server/TreeManagement.java --- a/src/main/java/jp/ac/u_ryukyu/treevnc/server/TreeManagement.java Wed Jul 02 16:31:25 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,198 +0,0 @@ -package jp.ac.u_ryukyu.treevnc.server; - - -import java.io.IOException; -import java.util.LinkedList; - -import jp.ac.u_ryukyu.treevnc.client.TreeVncProtocol; - -public class TreeManagement { - - private LinkedList nodeList = new LinkedList(); - boolean runflag = false; - private final int treebranch = 2; - private boolean showTreeNode = 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) ; - } - - 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 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 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); - if (deadParent.getTreeNum() == 0) { - // if dead root, connect me. - connectTo(me, lostParentNode); - return; - } - if (getChildNode(deadParent, 0) == null) return; - moveLastNodeToLostNodePosition(deadParent.getTreeNum()); - lostNodeConnection(deadParent); - // if lostParentNode is the last one, we don't need reconnection - // Thread.sleep(intv_time); - } - - /** - * send reconnect to all children - * @param newparent - */ - private void lostNodeConnection(TreeVNCNode oldParent) { - 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); - if (child != null) { - connectTo(newparent, child); - } - } - - if (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 - } - } - - private int isLeader(TreeVNCNode n) { - return ( n.getTreeNum() % treebranch == 1) ? 1 : 0; - } - - private TreeVNCNode lookup(LinkedList list, String hostname, int port) { - 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) { - 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; - for (int i=0; i getList() { - return nodeList; - } - - public void setList(LinkedList _ls) { - nodeList = _ls; - } - - public int getTreeBranch() { - return treebranch; - } - -} \ No newline at end of file diff -r 614db20d8fab -r 2b3eb4a9492f src/main/java/jp/ac/u_ryukyu/treevnc/server/TreeRootFinderListener.java --- a/src/main/java/jp/ac/u_ryukyu/treevnc/server/TreeRootFinderListener.java Wed Jul 02 16:31:25 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,104 +0,0 @@ -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.TreeCommand; -import com.glavsoft.viewer.ViewerInterface; -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 ViewerInterface vps; - private MulticastSocket soc; - - public TreeRootFinderListener(ViewerInterface vncProxyService) { - vps = vncProxyService; - } - - /** - * 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 hostname = recvPacket.getAddress().getHostAddress(); - byte[] reply = recvPacket.getData(); - int len = recvPacket.getLength(); - if (len != 12) { - continue; - } - if ((reply[0]&0xff) != TreeCommand.FIND_ROOT.cmd) { - 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(hostname, port); - try { - t.findRootReply(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(); - } -} diff -r 614db20d8fab -r 2b3eb4a9492f src/main/java/jp/ac/u_ryukyu/treevnc/server/TreeVNCNode.java --- a/src/main/java/jp/ac/u_ryukyu/treevnc/server/TreeVNCNode.java Wed Jul 02 16:31:25 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,40 +0,0 @@ -package jp.ac.u_ryukyu.treevnc.server; - -public class TreeVNCNode { - String hostname; - int port; - int treeNum; - String localhostname; - - public TreeVNCNode(String hostname, int port, String localhostname) { - this.hostname = hostname; - this.port = port; - this.localhostname = localhostname; - } - - public int getTreeNum() { - return treeNum; - } - - public void setTreeNum(int treeNum) { - this.treeNum = treeNum; - } - - public String getHostname() { - return hostname; - } - - public int getPort() { - return port; - } - - public void setPort(int acceptPort) { - port = acceptPort; - } - - public void setHostName(String localhostname) { - hostname = localhostname; - } - - -} diff -r 614db20d8fab -r 2b3eb4a9492f src/main/java/jp/ac/u_ryukyu/treevnc/server/VncProxyService.java --- a/src/main/java/jp/ac/u_ryukyu/treevnc/server/VncProxyService.java Wed Jul 02 16:31:25 2014 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,83 +0,0 @@ -package jp.ac.u_ryukyu.treevnc.server; - -import java.awt.event.*; -import java.util.logging.Logger; - -import com.glavsoft.core.SettingsChangedEvent; -import com.glavsoft.rfb.IChangeSettingsListener; -import com.glavsoft.rfb.IRfbSessionListener; -import com.glavsoft.rfb.protocol.ProtocolSettings; -import com.glavsoft.viewer.Viewer; -import com.glavsoft.viewer.cli.Parser; -import com.glavsoft.viewer.swing.ParametersHandler; - -public class VncProxyService extends Viewer implements Runnable, - IRfbSessionListener, IChangeSettingsListener { - - /** - * - */ - private static final long serialVersionUID = 1L; - - public static Logger logger = Logger.getLogger("com.glavsoft"); - private boolean forceReconnection; - private String reconnectionReason; - static VncProxyService currentVps; - - public VncProxyService() { - ProtocolSettings.getDefaultSettings(); - uiSettings = super.uiSettings; - } - - public VncProxyService(VncProxyService vps, String hostName) { - this(); - connectionParams.hostName = hostName; - myRfb = vps.myRfb; - forceReconnection = vps.forceReconnection; - reconnectionReason = vps.reconnectionReason; - } - - public static void main(String[] argv) { - String[] mainArgs = argv; - System.out.println(mainArgs.length); - // input into arguments Decision - - Parser parser = new Parser(); - ParametersHandler.completeParserOptions(parser); - - parser.parse(argv); - if (parser.isSet(ParametersHandler.ARG_HELP)) { - printUsage(parser.optionsUsage()); - System.exit(0); - } - VncProxyService vps = new VncProxyService(); - vps.initRoot(vps.myRfb, "localhost"); - } - - - - @Override - public void destroy() { - - } - - @Override - public void windowClosing(WindowEvent e) { - if (e != null && e.getComponent() != null) { - e.getWindow().setVisible(false); - } - closeApp(); - } - - protected void socketClose() { - } - - @Override - public void rfbSessionStopped(final String reason) { - } - - @Override - public void settingsChanged(SettingsChangedEvent event) { - } - -} diff -r 614db20d8fab -r 2b3eb4a9492f src/main/java/jp/ac/u_ryukyu/treevnc/test/TestTextBox.java --- a/src/main/java/jp/ac/u_ryukyu/treevnc/test/TestTextBox.java Wed Jul 02 16:31:25 2014 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/test/TestTextBox.java Wed Jul 02 17:58:55 2014 +0900 @@ -1,6 +1,6 @@ package jp.ac.u_ryukyu.treevnc.test; -import jp.ac.u_ryukyu.treevnc.client.TreeVncRootSelectionPanel; +import jp.ac.u_ryukyu.treevnc.TreeVncRootSelectionPanel; public class TestTextBox { TreeVncRootSelectionPanel text = new TreeVncRootSelectionPanel(); diff -r 614db20d8fab -r 2b3eb4a9492f src/viewer_swing/java/com/glavsoft/viewer/CuiViewer.java --- a/src/viewer_swing/java/com/glavsoft/viewer/CuiViewer.java Wed Jul 02 16:31:25 2014 +0900 +++ b/src/viewer_swing/java/com/glavsoft/viewer/CuiViewer.java Wed Jul 02 17:58:55 2014 +0900 @@ -20,7 +20,7 @@ import jp.ac.u_ryukyu.treevnc.CreateConnectionParam; import jp.ac.u_ryukyu.treevnc.MyRfbProto; -import jp.ac.u_ryukyu.treevnc.client.MyRfbProtoClient; +import jp.ac.u_ryukyu.treevnc.MyRfbProtoClient; public class CuiViewer implements Runnable, WindowListener, ViewerInterface { @@ -211,7 +211,7 @@ public void startTreeViewer(String hostName, boolean cui) { CuiViewer viewer = new CuiViewer(); viewer.cuiVersion = cui; - MyRfbProtoClient rfb = new MyRfbProtoClient(); + MyRfbProto rfb = new MyRfbProto(false); CreateConnectionParam cp = new CreateConnectionParam(rfb); if (hostName==null) { try { diff -r 614db20d8fab -r 2b3eb4a9492f src/viewer_swing/java/com/glavsoft/viewer/TreeViewer.java --- a/src/viewer_swing/java/com/glavsoft/viewer/TreeViewer.java Wed Jul 02 16:31:25 2014 +0900 +++ b/src/viewer_swing/java/com/glavsoft/viewer/TreeViewer.java Wed Jul 02 17:58:55 2014 +0900 @@ -1,6 +1,6 @@ package com.glavsoft.viewer; -import jp.ac.u_ryukyu.treevnc.server.VncProxyService; +import jp.ac.u_ryukyu.treevnc.VncProxyService; public class TreeViewer { private boolean treeProxy; diff -r 614db20d8fab -r 2b3eb4a9492f src/viewer_swing/java/com/glavsoft/viewer/Viewer.java --- a/src/viewer_swing/java/com/glavsoft/viewer/Viewer.java Wed Jul 02 16:31:25 2014 +0900 +++ b/src/viewer_swing/java/com/glavsoft/viewer/Viewer.java Wed Jul 02 17:58:55 2014 +0900 @@ -48,9 +48,9 @@ import jp.ac.u_ryukyu.treevnc.CreateConnectionParam; import jp.ac.u_ryukyu.treevnc.MyRfbProto; -import jp.ac.u_ryukyu.treevnc.client.MyRfbProtoClient; -import jp.ac.u_ryukyu.treevnc.server.MyRfbProtoProxy; -import jp.ac.u_ryukyu.treevnc.server.TreeManagement; +import jp.ac.u_ryukyu.treevnc.MyRfbProtoClient; +import jp.ac.u_ryukyu.treevnc.MyRfbProtoProxy; +import jp.ac.u_ryukyu.treevnc.TreeManagement; @SuppressWarnings("serial") public class Viewer extends JApplet implements Runnable, WindowListener , ViewerInterface { @@ -292,7 +292,7 @@ * start TreeVNC viewer */ public void startTreeViewer(String hostName,boolean cui) { - MyRfbProtoClient rfb = new MyRfbProtoClient(); + MyRfbProto rfb = new MyRfbProto(false); rfb.setCuiVersion(cui); rfb.setHasViewer(true); rfb.setViewer(this); @@ -380,7 +380,7 @@ System.exit(0); } String hostname = "localhost"; - MyRfbProtoProxy rfb = new MyRfbProtoProxy(); + MyRfbProto rfb = new MyRfbProto(true); myRfb = rfb; rfb.setShowTree(showTree); rfb.setViewer(this);