Mercurial > hg > Members > riono > TreeVNC_ja_comment
view src/main/java/jp/ac/u_ryukyu/treevnc/client/TreeVncProtocol.java @ 145:649794dfb9d5
add my hostname to handle multiple network
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 12 Jun 2014 22:01:05 +0900 |
parents | 4547543ca73c |
children | 703db66138b5 |
line wrap: on
line source
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; import com.glavsoft.viewer.ViewerImpl; public class TreeVncProtocol { private BufferedReader is = null; private DataOutputStream os = null; private Socket echoSocket = null; private int echoPort; public ViewerImpl client; private String parentAddress; public boolean leaderFlag; private String myAddress; public TreeVncProtocol(String name, int echoPort) { this.echoPort = echoPort; this.parentAddress = name; } public void setLeader(boolean f) { leaderFlag = f; } public void openport() { try { echoSocket = new Socket(parentAddress, echoPort); 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: "+parentAddress); } catch (IOException e) { System.out.println(parentAddress + " Connection Faild"); System.exit(0); } } public void findRootReply(String hostname, int port) throws IOException { sendWithHostAndPort(ProtocolContext.FIND_ROOT_REPLY, hostname, port); } public void whereToConnect(String hostname, int port) throws IOException { sendWithHostAndPort(ProtocolContext.WHERE_TO_CONNECT, hostname, port); } public void connectTo(String hostname, int port,int leaderFlag) throws IOException{ int command = leaderFlag == 1 ? ProtocolContext.CONNECT_TO_AS_LEADER : ProtocolContext.CONNECT_TO; sendWithHostAndPort(command , hostname, port); } public void lostParent(String hostname, int port) throws IOException { sendWithHostAndPort(ProtocolContext.LOST_PARENT, hostname, port); } public void sendWithHostAndPort(int command, String hostname, int port) throws IOException { openport(); ByteBuffer buf = ByteBuffer.allocate(4+4+4+hostname.length()); buf.order(ByteOrder.BIG_ENDIAN); buf.put((byte) command); buf.put((byte) 0); buf.put((byte) 0); buf.put((byte) 0); buf.putInt(4+hostname.length()); // length buf.putInt(port); buf.put(hostname.getBytes(), 0, hostname.length()); sendCommandToTheRoot(buf); } public void sendCommandToTheRoot(ByteBuffer buf) throws IOException { buf.flip(); 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(); } public void setViewer(ViewerImpl v) { client = v; } public ViewerImpl getViewer() { return client; } public String getMyAddress() { return myAddress; } public String getParentAddress() { return parentAddress; } public int getParentPort() { return echoPort; } /** * Start client with new parent (including reconnection) * @param port * @param hostname * @param leader * @throws IOException */ public void connectToParenet(int port, String hostname, boolean leader) throws IOException { this.leaderFlag = leader; Socket socket = new Socket(hostname,port); socket.setReuseAddress(true); client.setTeminationType(true); client.closeApp(); client.setSocket(socket); client.run(); } }