Mercurial > hg > Members > riono > TreeVNC_ja_comment
annotate src/main/java/jp/ac/u_ryukyu/treevnc/TreeVncProtocol.java @ 206:2b3eb4a9492f
MyRfbProto reorganization
author | oc |
---|---|
date | Wed, 02 Jul 2014 17:58:55 +0900 |
parents | src/main/java/jp/ac/u_ryukyu/treevnc/client/TreeVncProtocol.java@2e1530139169 |
children | dd154ffe1a53 |
rev | line source |
---|---|
206 | 1 package jp.ac.u_ryukyu.treevnc; |
1 | 2 |
3 import java.io.*; | |
4 import java.net.*; | |
125 | 5 import java.nio.ByteBuffer; |
6 import java.nio.ByteOrder; | |
159 | 7 import com.glavsoft.rfb.protocol.ProtocolContext.TreeCommand; |
1 | 8 |
125 | 9 public class TreeVncProtocol { |
1 | 10 private BufferedReader is = null; |
11 private DataOutputStream os = null; | |
12 private Socket echoSocket = null; | |
174 | 13 private int rootPort; |
14 private String rootAddress; | |
125 | 15 |
16 public TreeVncProtocol(String name, int echoPort) { | |
174 | 17 this.rootPort = echoPort; |
18 this.rootAddress = name; | |
3 | 19 } |
1 | 20 |
32 | 21 public void openport() { |
1 | 22 try { |
174 | 23 echoSocket = new Socket(rootAddress, rootPort); |
24 // echoSocket.setReuseAddress(true); | |
1 | 25 os = new DataOutputStream(echoSocket.getOutputStream()); |
26 is = new BufferedReader(new InputStreamReader( | |
27 echoSocket.getInputStream())); | |
28 } catch (UnknownHostException e) { | |
174 | 29 System.err.println("Don't know about host: "+rootAddress); |
1 | 30 } catch (IOException e) { |
174 | 31 System.out.println(rootAddress + " Connection Faild"); |
1 | 32 System.exit(0); |
33 } | |
34 } | |
35 | |
155 | 36 public void findRootReply(int port) throws IOException { |
169 | 37 sendWithHostAndPort(TreeCommand.FIND_ROOT_REPLY, null, port, (short) 0); |
125 | 38 } |
39 | |
40 public void whereToConnect(String hostname, int port) throws IOException { | |
169 | 41 sendWithHostAndPort(TreeCommand.WHERE_TO_CONNECT, hostname, port, (short) 0); |
125 | 42 } |
133 | 43 |
169 | 44 public void connectTo(String hostname, int port,int leaderFlag, short yourId) throws IOException{ |
159 | 45 TreeCommand command = leaderFlag == 1 ? TreeCommand.CONNECT_TO_AS_LEADER : TreeCommand.CONNECT_TO; |
169 | 46 sendWithHostAndPort(command , hostname, port, yourId); |
145
649794dfb9d5
add my hostname to handle multiple network
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
143
diff
changeset
|
47 } |
133 | 48 |
125 | 49 public void lostParent(String hostname, int port) throws IOException { |
169 | 50 sendWithHostAndPort(TreeCommand.LOST_PARENT, hostname, port, (short) 0); |
125 | 51 } |
135
ada4d850a820
lostParent and notFoundParenet
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
134
diff
changeset
|
52 |
169 | 53 public void sendWithHostAndPort(TreeCommand command, String hostname, int port, short value) |
125 | 54 throws IOException { |
55 openport(); | |
155 | 56 if (hostname == null) { |
57 // in case of root finder, we can't get localaddress from datagram packet. | |
58 // so use local part of TCP socket. | |
59 hostname = echoSocket.getLocalAddress().getHostAddress(); | |
60 } | |
147 | 61 int cmdlen = 4+4+4+hostname.length(); |
62 if (cmdlen < 12) cmdlen=12; | |
63 ByteBuffer buf = ByteBuffer.allocate(cmdlen); | |
125 | 64 buf.order(ByteOrder.BIG_ENDIAN); |
159 | 65 buf.put((byte) command.cmd); |
125 | 66 |
67 buf.put((byte) 0); | |
169 | 68 buf.putShort(value); |
125 | 69 buf.putInt(4+hostname.length()); // length |
70 buf.putInt(port); | |
71 buf.put(hostname.getBytes(), 0, hostname.length()); | |
147 | 72 while (buf.hasRemaining() ) buf.put((byte)0) ; |
73 buf.flip(); | |
133 | 74 sendCommandToTheRoot(buf); |
125 | 75 } |
135
ada4d850a820
lostParent and notFoundParenet
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
134
diff
changeset
|
76 |
ada4d850a820
lostParent and notFoundParenet
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
134
diff
changeset
|
77 public void sendCommandToTheRoot(ByteBuffer buf) throws IOException { |
ada4d850a820
lostParent and notFoundParenet
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
134
diff
changeset
|
78 char[] charBuf = new char[12]; |
ada4d850a820
lostParent and notFoundParenet
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
134
diff
changeset
|
79 is.read(charBuf , 0, 12); // skip root's version header |
ada4d850a820
lostParent and notFoundParenet
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
134
diff
changeset
|
80 os.write(buf.array(), 0, buf.limit()); // send our command |
ada4d850a820
lostParent and notFoundParenet
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
134
diff
changeset
|
81 streamClose(); |
ada4d850a820
lostParent and notFoundParenet
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
134
diff
changeset
|
82 } |
ada4d850a820
lostParent and notFoundParenet
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
134
diff
changeset
|
83 |
1 | 84 void streamClose() throws IOException { |
85 os.close(); | |
86 is.close(); | |
87 echoSocket.close(); | |
88 } | |
89 | |
90 } |