Mercurial > hg > Members > riono > TreeVNC_ja_comment
annotate src/main/java/jp/ac/u_ryukyu/treevnc/client/GetHostClient.java @ 127:cc1fcdc7657c
find root continue.
author | oc |
---|---|
date | Fri, 06 Jun 2014 19:49:47 +0900 |
parents | 775ce6a14f89 |
children | a6dc674546f2 |
rev | line source |
---|---|
12 | 1 package jp.ac.u_ryukyu.treevnc.client; |
1 | 2 |
82 | 3 import java.io.BufferedReader; |
1 | 4 import java.io.IOException; |
82 | 5 import java.io.InputStreamReader; |
6 import java.net.BindException; | |
1 | 7 import java.net.DatagramPacket; |
8 import java.net.InetAddress; | |
9 import java.net.MulticastSocket; | |
82 | 10 import java.net.ServerSocket; |
11 import java.net.Socket; | |
125 | 12 import java.nio.ByteBuffer; |
13 import java.nio.ByteOrder; | |
1 | 14 |
125 | 15 import com.glavsoft.rfb.protocol.ProtocolContext; |
82 | 16 import com.glavsoft.viewer.swing.ConnectionParams; |
17 | |
18 import jp.ac.u_ryukyu.treevnc.server.GetBroadCastProxy; | |
19 | |
20 public class GetHostClient implements Runnable { | |
1 | 21 final int BUFSIZE = 1024; |
22 private byte[] buf = new byte[BUFSIZE]; | |
23 private InetAddress mAddr; | |
24 private MulticastSocket soc; | |
25 private String str; | |
26 | |
82 | 27 private ServerSocket server = null; |
28 private BufferedReader is; | |
126 | 29 private int port; |
82 | 30 private boolean stopFlag; |
31 private TextBoxClient text = new TextBoxClient(); | |
32 private String proxyAddr; | |
33 | |
125 | 34 /** |
35 * To find vnc root, a client sends a multicast packet. | |
36 * @param _str | |
37 */ | |
126 | 38 public GetHostClient(int _port) { |
39 port = _port; | |
87 | 40 } |
82 | 41 |
127 | 42 public void createSocket() { |
1 | 43 try { |
82 | 44 mAddr = InetAddress.getByName(GetBroadCastProxy.McastAddr); |
1 | 45 soc = new MulticastSocket(); |
46 soc.setTimeToLive(1); | |
47 } catch (IOException e) { | |
48 e.printStackTrace(); | |
49 } | |
50 } | |
51 | |
87 | 52 /** |
53 * send find root message. | |
54 * | |
55 */ | |
2
dca3bd61b830
before change MyVncClient.java
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
1
diff
changeset
|
56 public void sendData() { |
82 | 57 String s = str + Integer.toString(port); |
58 buf = s.getBytes(); | |
59 DatagramPacket sendPacket = new DatagramPacket(buf, s.length(), mAddr, ConnectionParams.DEFAULT_VNC_ROOT_FINDER); | |
1 | 60 try { |
32 | 61 soc.send(sendPacket); |
1 | 62 } catch (IOException e) { |
63 e.printStackTrace(); | |
64 } | |
65 } | |
34 | 66 |
67 public void getHost() { | |
68 createSocket(); | |
126 | 69 findRoot(); |
70 // sendData(); | |
34 | 71 } |
82 | 72 |
125 | 73 public void findRoot() { |
74 ByteBuffer buf = ByteBuffer.allocate(12); | |
75 buf.order(ByteOrder.BIG_ENDIAN); | |
76 buf.put((byte) ProtocolContext.FIND_ROOT); | |
77 buf.put((byte) 0); | |
78 buf.put((byte) 0); | |
79 buf.put((byte) 0); | |
80 buf.putInt(4); // length | |
81 buf.putInt(port); | |
82 buf.flip(); | |
83 | |
84 DatagramPacket sendPacket = new DatagramPacket(buf.array(), buf.limit(), mAddr, ConnectionParams.DEFAULT_VNC_ROOT_FINDER); | |
85 try { | |
86 soc.send(sendPacket); | |
87 } catch (IOException e) { | |
88 e.printStackTrace(); | |
89 } | |
90 | |
91 } | |
87 | 92 |
82 | 93 |
87 | 94 /** |
95 * @return | |
96 * reply to findRootMassage | |
97 * | |
98 */ | |
82 | 99 public String textAddress() { |
100 return text.getAddress(); | |
101 } | |
102 | |
103 public String textPort() { | |
104 return text.getPort(); | |
105 } | |
106 | |
107 void socketClose() { | |
108 try { | |
109 text.unVisible(); | |
110 is.close(); | |
111 } catch (IOException e) { | |
112 e.printStackTrace(); | |
113 } | |
114 } | |
115 | |
116 /** | |
117 * TREEVNC_ROOT_FINDER is sent, wait for reply | |
118 */ | |
119 @Override | |
120 public void run() { | |
121 try { | |
122 while(!stopFlag) { | |
123 Socket socket = server.accept(); | |
124 is = new BufferedReader(new InputStreamReader( | |
125 socket.getInputStream())); | |
126 proxyAddr = is.readLine(); | |
127 // proxyAddr format | |
128 // 5999:localhost:localhost:133.13.59.210: | |
129 // port:hostnae:hostname:ip address: | |
130 // GetBroadCastProxy create this string on VNC root | |
131 InetAddress adr = socket.getInetAddress(); | |
132 String proxyAddr1 = adr.getHostName(); | |
133 if (proxyAddr1 != null) { | |
134 int portIndex = proxyAddr.indexOf(":"); | |
135 int restIndex = proxyAddr.indexOf(":", portIndex+1); | |
136 if (portIndex>0) { | |
137 proxyAddr = proxyAddr.substring(0, portIndex)+":"+proxyAddr1 + ":" + proxyAddr.substring(restIndex+1); | |
138 } | |
139 } | |
140 if(proxyAddr!=null) | |
141 text.checkBox(proxyAddr); | |
142 text.setButton(); | |
143 text.visible(); | |
144 } | |
87 | 145 System.err.println("stop"); |
82 | 146 } catch (IOException e) { |
147 } | |
148 } | |
149 | |
150 public void setStopFlag(boolean stopFlag) { | |
151 this.stopFlag = stopFlag; | |
152 | |
153 } | |
88 | 154 |
155 /** | |
156 * the thread is waiting accept system call, | |
157 * close the socket causes exception, | |
158 * which will terminated the thread. | |
159 * | |
160 */ | |
82 | 161 public void interrupt() { |
87 | 162 stopFlag = true; |
163 try { | |
164 server.close(); | |
165 soc.close(); | |
166 } catch (IOException e) { | |
167 } | |
82 | 168 Thread.currentThread().interrupt(); |
169 } | |
170 | |
171 public boolean isStopFlag() { | |
172 return stopFlag; | |
173 } | |
174 | |
175 public void ipRegister() { | |
176 text.ipRegister(); | |
177 } | |
178 | |
179 public String getProxyAddr() { | |
180 return proxyAddr; | |
181 } | |
1 | 182 } |