Mercurial > hg > Applications > TreeVNC
annotate src/main/java/jp/ac/u_ryukyu/treevnc/FindRoot.java @ 288:0b61b7214ece
add button images
author | oc |
---|---|
date | Mon, 12 Jan 2015 21:29:41 +0900 |
parents | 61938de1dd06 |
children | 293c35aa902b |
rev | line source |
---|---|
206 | 1 package jp.ac.u_ryukyu.treevnc; |
1 | 2 |
82 | 3 import java.io.BufferedReader; |
1 | 4 import java.io.IOException; |
82 | 5 import java.io.InputStreamReader; |
1 | 6 import java.net.DatagramPacket; |
7 import java.net.InetAddress; | |
8 import java.net.MulticastSocket; | |
82 | 9 import java.net.ServerSocket; |
10 import java.net.Socket; | |
125 | 11 import java.nio.ByteBuffer; |
12 import java.nio.ByteOrder; | |
206 | 13 |
159 | 14 import com.glavsoft.rfb.protocol.ProtocolContext.TreeCommand; |
82 | 15 import com.glavsoft.viewer.swing.ConnectionParams; |
16 | |
134
128cce60c43c
where to connect command
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
131
diff
changeset
|
17 public class FindRoot implements Runnable { |
1 | 18 final int BUFSIZE = 1024; |
19 private InetAddress mAddr; | |
20 private MulticastSocket soc; | |
21 | |
82 | 22 private ServerSocket server = null; |
23 private BufferedReader is; | |
126 | 24 private int port; |
82 | 25 private boolean stopFlag; |
129 | 26 private TreeVncRootSelectionPanel rootSelectionPanel = new TreeVncRootSelectionPanel(); |
82 | 27 private String proxyAddr; |
28 | |
125 | 29 /** |
287 | 30 * To find vnc root, a client sends a multicast packet. |
31 * @param _port | |
130 | 32 * @param createConnectionParam |
125 | 33 */ |
134
128cce60c43c
where to connect command
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
131
diff
changeset
|
34 public FindRoot(int _port, CreateConnectionParam createConnectionParam) { |
130 | 35 CreateConnectionParam cp = createConnectionParam; |
126 | 36 port = _port; |
130 | 37 rootSelectionPanel.setCp(cp); |
134
128cce60c43c
where to connect command
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
131
diff
changeset
|
38 createSocket(); |
87 | 39 } |
82 | 40 |
127 | 41 public void createSocket() { |
1 | 42 try { |
134
128cce60c43c
where to connect command
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
131
diff
changeset
|
43 mAddr = InetAddress.getByName(TreeRootFinderListener.McastAddr); |
1 | 44 soc = new MulticastSocket(); |
288 | 45 soc.joinGroup(mAddr); |
1 | 46 soc.setTimeToLive(1); |
47 } catch (IOException e) { | |
48 e.printStackTrace(); | |
49 } | |
50 } | |
51 | |
130 | 52 /** |
53 * send find root message. | |
54 * | |
55 */ | |
125 | 56 public void findRoot() { |
57 ByteBuffer buf = ByteBuffer.allocate(12); | |
58 buf.order(ByteOrder.BIG_ENDIAN); | |
159 | 59 buf.put((byte) TreeCommand.FIND_ROOT.cmd); |
125 | 60 buf.put((byte) 0); |
61 buf.put((byte) 0); | |
62 buf.put((byte) 0); | |
63 buf.putInt(4); // length | |
64 buf.putInt(port); | |
65 buf.flip(); | |
66 | |
67 DatagramPacket sendPacket = new DatagramPacket(buf.array(), buf.limit(), mAddr, ConnectionParams.DEFAULT_VNC_ROOT_FINDER); | |
68 try { | |
69 soc.send(sendPacket); | |
70 } catch (IOException e) { | |
71 e.printStackTrace(); | |
72 } | |
73 | |
74 } | |
82 | 75 |
76 public String textPort() { | |
129 | 77 return rootSelectionPanel.getPort(); |
82 | 78 } |
79 | |
80 void socketClose() { | |
81 try { | |
129 | 82 rootSelectionPanel.unVisible(); |
82 | 83 is.close(); |
84 } catch (IOException e) { | |
85 e.printStackTrace(); | |
86 } | |
87 } | |
88 | |
89 /** | |
90 * TREEVNC_ROOT_FINDER is sent, wait for reply | |
91 */ | |
92 @Override | |
93 public void run() { | |
94 try { | |
95 while(!stopFlag) { | |
139 | 96 Socket socket = server.accept(); |
82 | 97 is = new BufferedReader(new InputStreamReader( |
98 socket.getInputStream())); | |
99 proxyAddr = is.readLine(); | |
100 // proxyAddr format | |
101 // 5999:localhost:localhost:133.13.59.210: | |
288 | 102 // port:hostname:hostname:ip address: |
82 | 103 // GetBroadCastProxy create this string on VNC root |
104 InetAddress adr = socket.getInetAddress(); | |
105 String proxyAddr1 = adr.getHostName(); | |
129 | 106 String proxyAddr2 = null; |
82 | 107 if (proxyAddr1 != null) { |
108 int portIndex = proxyAddr.indexOf(":"); | |
109 int restIndex = proxyAddr.indexOf(":", portIndex+1); | |
110 if (portIndex>0) { | |
129 | 111 proxyAddr2 = proxyAddr.substring(0, portIndex)+":"+proxyAddr1 + ":" + proxyAddr.substring(restIndex+1); |
82 | 112 } |
113 } | |
129 | 114 if(proxyAddr2!=null) |
115 rootSelectionPanel.checkBox(proxyAddr2); | |
116 rootSelectionPanel.setButton(); | |
117 rootSelectionPanel.visible(); | |
82 | 118 } |
87 | 119 System.err.println("stop"); |
82 | 120 } catch (IOException e) { |
121 } | |
122 } | |
123 | |
124 public void setStopFlag(boolean stopFlag) { | |
125 this.stopFlag = stopFlag; | |
126 | |
127 } | |
88 | 128 |
129 /** | |
130 * the thread is waiting accept system call, | |
131 * close the socket causes exception, | |
132 * which will terminated the thread. | |
133 * | |
134 */ | |
82 | 135 public void interrupt() { |
87 | 136 stopFlag = true; |
137 try { | |
138 server.close(); | |
139 soc.close(); | |
140 } catch (IOException e) { | |
141 } | |
82 | 142 Thread.currentThread().interrupt(); |
143 } | |
144 | |
145 public boolean isStopFlag() { | |
146 return stopFlag; | |
147 } | |
148 | |
149 public void ipRegister() { | |
129 | 150 rootSelectionPanel.ipRegister(); |
82 | 151 } |
152 | |
153 public String getProxyAddr() { | |
154 return proxyAddr; | |
155 } | |
1 | 156 } |