Mercurial > hg > Applications > TreeVNC
view src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncProtocol.java @ 630:7071b01621c2 default tip
Determine ipv4 or ipv6 and fix scond display
author | riono <e165729@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 28 Apr 2020 19:49:38 +0900 |
parents | 432e2967eaab |
children |
line wrap: on
line source
package jp.ac.u_ryukyu.treevnc; import com.glavsoft.rfb.protocol.ProtocolContext.TreeCommand; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; import java.nio.ByteBuffer; import java.nio.ByteOrder; 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() 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) { try { sendWithHostAndPort(TreeCommand.FIND_ROOT_REPLY, null, port, (short) 0); } catch (IOException e) { e.printStackTrace(); System.out.println("cannot send findRootReplay"); } } public void whereToConnect(String hostname, int port) { try { sendWithHostAndPort(TreeCommand.WHERE_TO_CONNECT, hostname, port, (short) 0); } catch (IOException e) { e.printStackTrace(); System.out.println("cannot send whereToConnect"); } } 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) { try { sendWithHostAndPort(TreeCommand.LOST_PARENT, hostname, port, (short) 0); } catch (IOException e) { e.printStackTrace(); System.out.println("cannot send lostParent"); } } public void lostChild(String myHostName, int myHostPort, int myId) { try { sendWithHostAndPort(TreeCommand.LOST_CHILD, myHostName, myHostPort, (short) myId); } catch (IOException e) { e.printStackTrace(); System.out.println("cannot send lostChild"); } } 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 } void streamClose() throws IOException { os.close(); is.close(); echoSocket.close(); } }