comparison 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
comparison
equal deleted inserted replaced
144:1e004d9c599d 145:649794dfb9d5
10 10
11 public class TreeVncProtocol { 11 public class TreeVncProtocol {
12 private BufferedReader is = null; 12 private BufferedReader is = null;
13 private DataOutputStream os = null; 13 private DataOutputStream os = null;
14 private Socket echoSocket = null; 14 private Socket echoSocket = null;
15 private boolean runflag = false;
16 private Socket clientSocket = null;
17 private int echoPort; 15 private int echoPort;
18 public ViewerImpl client; 16 public ViewerImpl client;
19 private String parentAddress; 17 private String parentAddress;
20 public String parentNum;
21 public String treeNum;
22 public boolean leaderFlag; 18 public boolean leaderFlag;
23 private String myAddress; 19 private String myAddress;
24 20
25 public TreeVncProtocol(String name, int echoPort) { 21 public TreeVncProtocol(String name, int echoPort) {
26 this.echoPort = echoPort; 22 this.echoPort = echoPort;
50 public void findRootReply(String hostname, int port) throws IOException { 46 public void findRootReply(String hostname, int port) throws IOException {
51 sendWithHostAndPort(ProtocolContext.FIND_ROOT_REPLY, hostname, port); 47 sendWithHostAndPort(ProtocolContext.FIND_ROOT_REPLY, hostname, port);
52 } 48 }
53 49
54 public void whereToConnect(String hostname, int port) throws IOException { 50 public void whereToConnect(String hostname, int port) throws IOException {
55 sendWithHostAndPort(ProtocolContext.WHERE_TO_CONNECT, hostname, port); 51 sendWithHostAndPort(ProtocolContext.WHERE_TO_CONNECT, hostname, port);
56 } 52 }
57 53
58 public void connectTo(String hostname, int port,int leaderFlag) throws IOException{ 54 public void connectTo(String hostname, int port,int leaderFlag) throws IOException{
59 int command = ProtocolContext.CONNECT_TO; 55 int command = leaderFlag == 1 ? ProtocolContext.CONNECT_TO_AS_LEADER : ProtocolContext.CONNECT_TO;
60 openport(); 56 sendWithHostAndPort(command , hostname, port);
61 ByteBuffer buf = ByteBuffer.allocate(4+4+4+4+hostname.length()); 57 }
62 buf.order(ByteOrder.BIG_ENDIAN);
63 buf.put((byte) command);
64 buf.put((byte) leaderFlag);
65 buf.put((byte) 0);
66 buf.put((byte) 0);
67 buf.putInt(4+hostname.length()); // length
68 buf.putInt(port);
69 buf.put(hostname.getBytes(), 0, hostname.length());
70 sendCommandToTheRoot(buf);
71 }
72 58
73 public void lostParent(String hostname, int port) throws IOException { 59 public void lostParent(String hostname, int port) throws IOException {
74 sendWithHostAndPort(ProtocolContext.LOST_PARENT, hostname, port); 60 sendWithHostAndPort(ProtocolContext.LOST_PARENT, hostname, port);
75 } 61 }
76 62
97 is.read(charBuf , 0, 12); // skip root's version header 83 is.read(charBuf , 0, 12); // skip root's version header
98 os.write(buf.array(), 0, buf.limit()); // send our command 84 os.write(buf.array(), 0, buf.limit()); // send our command
99 streamClose(); 85 streamClose();
100 } 86 }
101 87
102
103 /**
104 * Call at lost host
105 * @throws IOException
106 * @throws NumberFormatException
107 */
108 public boolean lostHost() throws NumberFormatException, IOException {
109 openport();
110 if (runflag) return true;
111 if ("1".equals(leaderFlag)) {
112 openport();
113 lostParent(treeNum,Integer.parseInt(parentNum));
114 streamClose();
115 }
116 return true;
117 }
118
119 public boolean notfoundParent() {
120 openport();
121 runflag = true;
122 try {
123 lostParent(treeNum, Integer.parseInt(parentNum)); // 2
124 } catch (Exception e) {
125 }
126 return true;
127 }
128
129 public void notfoundParent(String hostname, int port) {
130
131 }
132
133
134 void streamClose() throws IOException { 88 void streamClose() throws IOException {
135 os.close(); 89 os.close();
136 is.close(); 90 is.close();
137 echoSocket.close(); 91 echoSocket.close();
138 } 92 }
145 return client; 99 return client;
146 } 100 }
147 101
148 public String getMyAddress() { 102 public String getMyAddress() {
149 return myAddress; 103 return myAddress;
150 }
151
152 // create socket for ReConnection.
153 public Socket createSocketForClient(Socket soc, boolean flag) throws IOException {
154 String parentAddress;
155 int count = 0;
156 System.out.println("########################PATH************************");
157 BufferedReader is = new BufferedReader(new InputStreamReader(soc.getInputStream()));
158 parentAddress = is.readLine();
159 String port = is.readLine();
160 // It is called when the screen changes.
161 if ("reconnection".equals(parentAddress)) {
162 while (true) {
163 try {
164 client.setOpenPort(Integer.parseInt(port));
165 System.out.println("----------:"+this.parentAddress+"----------:"+port);
166 return new Socket(this.parentAddress,
167 Integer.parseInt(port));
168 } catch (IOException e) {
169 try {
170 Thread.sleep(1000);
171 } catch (InterruptedException e1) {
172 e1.printStackTrace();
173 }
174 if (count++ > 5)
175 break;
176 continue;
177 }
178 }
179 } else {
180 System.out.println("###########################faild" + parentAddress+"port::"+port);
181 if (parentAddress == null)
182 return null;
183 is.readLine();// parentNum
184 }
185 Socket socket = new Socket(parentAddress, Integer.parseInt(port));
186 socket.setReuseAddress(true);
187 System.out.println("Return Soket");
188 return socket;
189 } 104 }
190 105
191 public String getParentAddress() { 106 public String getParentAddress() {
192 return parentAddress; 107 return parentAddress;
193 } 108 }