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