annotate src/main/java/jp/ac/u_ryukyu/treevnc/TreeRootFinderListener.java @ 435:4ac83ab4be81

find our screen on fitScreen remove requestSingleScreenSize
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 12 Jun 2016 09:06:53 +0900
parents aad75cd6d9e2
children 9db8862ef200
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
206
2b3eb4a9492f MyRfbProto reorganization
oc
parents: 174
diff changeset
1 package jp.ac.u_ryukyu.treevnc;
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
159
1c9f6acdfeb2 TreeCommand enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 155
diff changeset
3 import com.glavsoft.rfb.protocol.ProtocolContext.TreeCommand;
174
2e1530139169 reorganization
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 159
diff changeset
4 import com.glavsoft.viewer.ViewerInterface;
82
cc72fdbb7c72 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 81
diff changeset
5 import com.glavsoft.viewer.swing.ConnectionParams;
cc72fdbb7c72 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 81
diff changeset
6
434
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
7 import java.io.IOException;
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
8 import java.net.*;
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
9
134
128cce60c43c where to connect command
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 126
diff changeset
10 public class TreeRootFinderListener implements Runnable {
263
4075f68f1c91 add joinGroup IPv6 and IPv4 adderss version.
oc
parents: 262
diff changeset
11 public static final String Ipv4McastAddr = "224.0.0.1";
434
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
12 public static final String Ipv6McastAddr = "ff02::1";
263
4075f68f1c91 add joinGroup IPv6 and IPv4 adderss version.
oc
parents: 262
diff changeset
13 public static String McastAddr = Ipv4McastAddr;
4075f68f1c91 add joinGroup IPv6 and IPv4 adderss version.
oc
parents: 262
diff changeset
14
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 static final int BufSize = 1024;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 private boolean stopFlag = false;
174
2e1530139169 reorganization
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 159
diff changeset
17 private ViewerInterface vps;
69
21108e863b8a update client list.
one
parents: 68
diff changeset
18 private MulticastSocket soc;
259
606953a43198 fix how to join Multicast for the IPv6 address.
oc
parents: 258
diff changeset
19 private SecurityManager securityManager;
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20
174
2e1530139169 reorganization
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 159
diff changeset
21 public TreeRootFinderListener(ViewerInterface vncProxyService) {
2e1530139169 reorganization
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 159
diff changeset
22 vps = vncProxyService;
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 }
434
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
24
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
25 public static MulticastSocket createMulticastSocket() throws IOException {
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
26 MulticastSocket soc = new MulticastSocket(ConnectionParams.DEFAULT_VNC_ROOT_FINDER);
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
27 try {
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
28 soc.joinGroup(InetAddress.getByName(McastAddr));
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
29 } catch (SocketException e) {
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
30 System.out.println("join to " + Ipv4McastAddr + " failed.");
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
31 }
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
32 try {
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
33 soc.joinGroup(InetAddress.getByName(Ipv6McastAddr));
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
34 } catch (SocketException e) {
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
35 System.out.println("join to " + Ipv6McastAddr + " failed.");
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
36 }
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
37 return soc;
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
38 }
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
39
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
40 /**
81
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
41 * To find TreeVNC root, a client sends me a multicast, reply our address to him.
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
42 * It contains a port to receive, so multiple TREEVNC clients can run on a PC.
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
43 */
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
44 private void replyToRootSearchMulticast() {
55
9fdb8ff88497 change parameter.
one
parents: 38
diff changeset
45 byte[] buf = new byte[BufSize];
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 try {
434
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
47 soc = createMulticastSocket();
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
48 System.out.println("FindRoot listening on "+ InetAddress.getByName(McastAddr));
263
4075f68f1c91 add joinGroup IPv6 and IPv4 adderss version.
oc
parents: 262
diff changeset
49
259
606953a43198 fix how to join Multicast for the IPv6 address.
oc
parents: 258
diff changeset
50 DatagramPacket recvPacket = new DatagramPacket(buf, BufSize);
606953a43198 fix how to join Multicast for the IPv6 address.
oc
parents: 258
diff changeset
51 while (!stopFlag) {
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 soc.receive(recvPacket);
155
1c95bd5bebcf root finder for multiple network.
oc
parents: 134
diff changeset
53 String hostname = recvPacket.getAddress().getHostAddress();
82
cc72fdbb7c72 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 81
diff changeset
54 byte[] reply = recvPacket.getData();
cc72fdbb7c72 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 81
diff changeset
55 int len = recvPacket.getLength();
126
775ce6a14f89 find root.
oc
parents: 83
diff changeset
56 if (len != 12) {
775ce6a14f89 find root.
oc
parents: 83
diff changeset
57 continue;
775ce6a14f89 find root.
oc
parents: 83
diff changeset
58 }
159
1c9f6acdfeb2 TreeCommand enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 155
diff changeset
59 if ((reply[0]&0xff) != TreeCommand.FIND_ROOT.cmd) {
126
775ce6a14f89 find root.
oc
parents: 83
diff changeset
60 continue;
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 }
126
775ce6a14f89 find root.
oc
parents: 83
diff changeset
62 int port = 0;
775ce6a14f89 find root.
oc
parents: 83
diff changeset
63 port = reply[8];
775ce6a14f89 find root.
oc
parents: 83
diff changeset
64 port = port * 256 + reply[9];
775ce6a14f89 find root.
oc
parents: 83
diff changeset
65 port = port * 256 + reply[10];
775ce6a14f89 find root.
oc
parents: 83
diff changeset
66 port = port * 256 + reply[11];
775ce6a14f89 find root.
oc
parents: 83
diff changeset
67
155
1c95bd5bebcf root finder for multiple network.
oc
parents: 134
diff changeset
68 TreeVncProtocol t = new TreeVncProtocol(hostname, port);
306
5e55cc34f789 change public to private
oc
parents: 301
diff changeset
69 int singleWidth = vps.getRfb().getSingleWidth();
5e55cc34f789 change public to private
oc
parents: 301
diff changeset
70 int singleHeight = vps.getRfb().getSingleHeight();
5e55cc34f789 change public to private
oc
parents: 301
diff changeset
71 t.setSingleSize(singleWidth, singleHeight);
331
42fcc9419498 add error message, add lostParent case by fail to connectTo.
oc
parents: 306
diff changeset
72 t.findRootReply(vps.getRfb().getAcceptPort());
38
c2f0b6907448 add doc directory
one
parents: 13
diff changeset
73 if(stopFlag) break;
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 }
81
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
75 } catch (Exception e) {
257
11b59b223222 add function of fixing display size
oc
parents: 215
diff changeset
76 System.out.println("tree-root-find-listener :" + e.getMessage());
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 }
434
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
79
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
80 public int parse_code( byte[] bs,int offset,int len )
81
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
81 {
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
82 int intval = 0;
82
cc72fdbb7c72 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 81
diff changeset
83 for( int i = offset; i < len ; i++ ) {
cc72fdbb7c72 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 81
diff changeset
84 if (bs[i]==0 ) return intval;
81
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
85 intval = intval * 10 + ( bs[ i ] - '0' );
82
cc72fdbb7c72 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 81
diff changeset
86 }
81
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
87 return intval;
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
88 }
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
89
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
90
134
128cce60c43c where to connect command
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 126
diff changeset
91 public void run() {
81
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
92 replyToRootSearchMulticast();
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
94
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 public void setStopFlag(boolean stopFlag) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 this.stopFlag = stopFlag;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
98
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 public boolean isStopFlag() {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 return stopFlag;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 }