Mercurial > hg > Members > riono > TreeVNC_ja_comment
diff src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncProtocol.java @ 302:7ef19658eb41
add createOriginalInitData().
author | oc |
---|---|
date | Mon, 19 Jan 2015 22:39:02 +0900 |
parents | ae7deb002b5f |
children | 1d4d5055a288 |
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncProtocol.java Sun Jan 18 08:36:06 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncProtocol.java Mon Jan 19 22:39:02 2015 +0900 @@ -7,72 +7,71 @@ 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; + private BufferedReader is = null; + private DataOutputStream os = null; + private Socket echoSocket = null; + private int rootPort; + private String rootAddress; private int singleWidth; private int singleHeight; - - public TreeVncProtocol(String name, int echoPort) { - this.rootPort = echoPort; - this.rootAddress = name; - } + + public TreeVncProtocol(String name, int echoPort) { + this.rootPort = echoPort; + this.rootAddress = name; + } public void setSingleSize(int singleWidth, int singleHeight) { this.singleWidth = singleWidth; this.singleHeight = singleHeight; } - public void openport() throws IOException { - echoSocket = new Socket(rootAddress, rootPort); - // echoSocket.setReuseAddress(true); - os = new DataOutputStream(echoSocket.getOutputStream()); - is = new BufferedReader(new InputStreamReader( - echoSocket.getInputStream())); - } + public void openport() throws IOException { + echoSocket = new Socket(rootAddress, rootPort); + // echoSocket.setReuseAddress(true); + os = new DataOutputStream(echoSocket.getOutputStream()); + is = new BufferedReader(new InputStreamReader( + echoSocket.getInputStream())); + } - public void findRootReply(int port) throws IOException { - sendWithHostAndPort(TreeCommand.FIND_ROOT_REPLY, null, port, (short) 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 { + 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; + 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 { + 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) ; + } + + 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); - } + sendCommandToTheRoot(buf); + } public void sendCommandToTheRoot(ByteBuffer buf) throws IOException { char[] charBuf = new char[12]; @@ -81,10 +80,10 @@ streamClose(); } - void streamClose() throws IOException { - os.close(); - is.close(); - echoSocket.close(); - } + void streamClose() throws IOException { + os.close(); + is.close(); + echoSocket.close(); + } } \ No newline at end of file