Mercurial > hg > Members > riono > TreeVNC_ja_comment
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 |
rev | line source |
---|---|
206 | 1 package jp.ac.u_ryukyu.treevnc; |
11 | 2 |
159 | 3 import com.glavsoft.rfb.protocol.ProtocolContext.TreeCommand; |
174 | 4 import com.glavsoft.viewer.ViewerInterface; |
82 | 5 import com.glavsoft.viewer.swing.ConnectionParams; |
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 | 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 | 13 public static String McastAddr = Ipv4McastAddr; |
14 | |
11 | 15 static final int BufSize = 1024; |
16 private boolean stopFlag = false; | |
174 | 17 private ViewerInterface vps; |
69 | 18 private MulticastSocket soc; |
259 | 19 private SecurityManager securityManager; |
11 | 20 |
174 | 21 public TreeRootFinderListener(ViewerInterface vncProxyService) { |
22 vps = vncProxyService; | |
11 | 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 | 45 byte[] buf = new byte[BufSize]; |
11 | 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 | 49 |
259 | 50 DatagramPacket recvPacket = new DatagramPacket(buf, BufSize); |
51 while (!stopFlag) { | |
11 | 52 soc.receive(recvPacket); |
155 | 53 String hostname = recvPacket.getAddress().getHostAddress(); |
82 | 54 byte[] reply = recvPacket.getData(); |
55 int len = recvPacket.getLength(); | |
126 | 56 if (len != 12) { |
57 continue; | |
58 } | |
159 | 59 if ((reply[0]&0xff) != TreeCommand.FIND_ROOT.cmd) { |
126 | 60 continue; |
11 | 61 } |
126 | 62 int port = 0; |
63 port = reply[8]; | |
64 port = port * 256 + reply[9]; | |
65 port = port * 256 + reply[10]; | |
66 port = port * 256 + reply[11]; | |
67 | |
155 | 68 TreeVncProtocol t = new TreeVncProtocol(hostname, port); |
306 | 69 int singleWidth = vps.getRfb().getSingleWidth(); |
70 int singleHeight = vps.getRfb().getSingleHeight(); | |
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 | 73 if(stopFlag) break; |
11 | 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 | 76 System.out.println("tree-root-find-listener :" + e.getMessage()); |
11 | 77 } |
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 | 83 for( int i = offset; i < len ; i++ ) { |
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 | 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 | 93 } |
94 | |
95 public void setStopFlag(boolean stopFlag) { | |
96 this.stopFlag = stopFlag; | |
97 } | |
98 | |
99 public boolean isStopFlag() { | |
100 return stopFlag; | |
101 } | |
102 } |