Mercurial > hg > Members > riono > TreeVNC_ja_comment
view src/main/java/jp/ac/u_ryukyu/treevnc/client/TreeVncProtocol.java @ 151:43b4281cf27d
do not create new socket in connect parent.
author | oc |
---|---|
date | Fri, 13 Jun 2014 17:00:04 +0900 |
parents | 1291cf1122ca |
children | 4096a8408fdb |
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 javax.swing.SwingUtilities; 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(); 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); 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()); 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(); } 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; client.setTeminationType(true); client.closeApp(); client.setConnectionParam(hostname, port); SwingUtilities.invokeLater(client); // client.run(); } }