annotate src/main/java/jp/ac/u_ryukyu/treevnc/TreeRootFinderListener.java @ 514:887ebd993b3d

separate rfb broadcast
author mir3636
date Thu, 21 Feb 2019 16:52:21 +0900
parents b97fdcd337fe
children
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;
505
af958194248b change replyToRootSearchMulticast
oshiro
parents: 454
diff changeset
4 import com.glavsoft.rfb.protocol.ReceiverTask;
af958194248b change replyToRootSearchMulticast
oshiro
parents: 454
diff changeset
5 import com.glavsoft.transport.Reader;
174
2e1530139169 reorganization
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 159
diff changeset
6 import com.glavsoft.viewer.ViewerInterface;
82
cc72fdbb7c72 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 81
diff changeset
7 import com.glavsoft.viewer.swing.ConnectionParams;
cc72fdbb7c72 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 81
diff changeset
8
505
af958194248b change replyToRootSearchMulticast
oshiro
parents: 454
diff changeset
9 import java.io.ByteArrayInputStream;
434
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
10 import java.io.IOException;
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
11 import java.net.*;
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
12
505
af958194248b change replyToRootSearchMulticast
oshiro
parents: 454
diff changeset
13 import static com.glavsoft.rfb.protocol.ReceiverTask.FRAMEBUFFER_UPDATE;
af958194248b change replyToRootSearchMulticast
oshiro
parents: 454
diff changeset
14
134
128cce60c43c where to connect command
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 126
diff changeset
15 public class TreeRootFinderListener implements Runnable {
263
4075f68f1c91 add joinGroup IPv6 and IPv4 adderss version.
oc
parents: 262
diff changeset
16 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
17 public static final String Ipv6McastAddr = "ff02::1";
263
4075f68f1c91 add joinGroup IPv6 and IPv4 adderss version.
oc
parents: 262
diff changeset
18 public static String McastAddr = Ipv4McastAddr;
4075f68f1c91 add joinGroup IPv6 and IPv4 adderss version.
oc
parents: 262
diff changeset
19
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 static final int BufSize = 1024;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 private boolean stopFlag = false;
174
2e1530139169 reorganization
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 159
diff changeset
22 private ViewerInterface vps;
69
21108e863b8a update client list.
one
parents: 68
diff changeset
23 private MulticastSocket soc;
259
606953a43198 fix how to join Multicast for the IPv6 address.
oc
parents: 258
diff changeset
24 private SecurityManager securityManager;
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
25
174
2e1530139169 reorganization
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 159
diff changeset
26 public TreeRootFinderListener(ViewerInterface vncProxyService) {
2e1530139169 reorganization
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 159
diff changeset
27 vps = vncProxyService;
513
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 512
diff changeset
28 try {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 512
diff changeset
29 soc = createMulticastSocket();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 512
diff changeset
30 System.out.println("FindRoot listening on "+ InetAddress.getByName(McastAddr));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 512
diff changeset
31 } catch (IOException e) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 512
diff changeset
32 e.printStackTrace();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 512
diff changeset
33 }
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 }
434
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
35
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
36 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
37 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
38 try {
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
39 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
40 } catch (SocketException e) {
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
41 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
42 }
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
43 try {
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
44 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
45 } catch (SocketException e) {
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
46 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
47 }
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
48 return soc;
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
49 }
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
50
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
51 /**
81
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
52 * 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
53 * 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
54 */
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
55 private void replyToRootSearchMulticast() {
55
9fdb8ff88497 change parameter.
one
parents: 38
diff changeset
56 byte[] buf = new byte[BufSize];
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 try {
263
4075f68f1c91 add joinGroup IPv6 and IPv4 adderss version.
oc
parents: 262
diff changeset
58
259
606953a43198 fix how to join Multicast for the IPv6 address.
oc
parents: 258
diff changeset
59 DatagramPacket recvPacket = new DatagramPacket(buf, BufSize);
606953a43198 fix how to join Multicast for the IPv6 address.
oc
parents: 258
diff changeset
60 while (!stopFlag) {
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 soc.receive(recvPacket);
155
1c95bd5bebcf root finder for multiple network.
oc
parents: 134
diff changeset
62 String hostname = recvPacket.getAddress().getHostAddress();
82
cc72fdbb7c72 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 81
diff changeset
63 byte[] reply = recvPacket.getData();
cc72fdbb7c72 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 81
diff changeset
64 int len = recvPacket.getLength();
512
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 510
diff changeset
65 boolean isTreeRoot = vps.getRfb().isTreeManager();
506
3fe7e1a372df Multicast CUI
toubaru
parents: 505
diff changeset
66 if (isTreeRoot && len == 12 && (reply[0] & 0xff) == TreeCommand.FIND_ROOT.cmd) {
505
af958194248b change replyToRootSearchMulticast
oshiro
parents: 454
diff changeset
67 int port = reply[8];
af958194248b change replyToRootSearchMulticast
oshiro
parents: 454
diff changeset
68 port = port * 256 + reply[9];
af958194248b change replyToRootSearchMulticast
oshiro
parents: 454
diff changeset
69 port = port * 256 + reply[10];
af958194248b change replyToRootSearchMulticast
oshiro
parents: 454
diff changeset
70 port = port * 256 + reply[11];
af958194248b change replyToRootSearchMulticast
oshiro
parents: 454
diff changeset
71
af958194248b change replyToRootSearchMulticast
oshiro
parents: 454
diff changeset
72 TreeVncProtocol t = new TreeVncProtocol(hostname, port);
af958194248b change replyToRootSearchMulticast
oshiro
parents: 454
diff changeset
73 t.findRootReply(vps.getRfb().getAcceptPort());
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 }
81
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
76 } catch (Exception e) {
257
11b59b223222 add function of fixing display size
oc
parents: 215
diff changeset
77 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
78 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 }
434
aad75cd6d9e2 Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 331
diff changeset
80
134
128cce60c43c where to connect command
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 126
diff changeset
81 public void run() {
81
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
82 replyToRootSearchMulticast();
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
84
436
9db8862ef200 remove unsed code in FindRoot
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 435
diff changeset
85 // it looks like that we never stop
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 public void setStopFlag(boolean stopFlag) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 this.stopFlag = stopFlag;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
89
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 public boolean isStopFlag() {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 return stopFlag;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 }
510
89f1f7e41838 broadcast FrameBufferUpdate Rectangle
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 506
diff changeset
93
89f1f7e41838 broadcast FrameBufferUpdate Rectangle
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 506
diff changeset
94 public MulticastSocket getSocket() {
89f1f7e41838 broadcast FrameBufferUpdate Rectangle
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 506
diff changeset
95 return soc;
89f1f7e41838 broadcast FrameBufferUpdate Rectangle
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 506
diff changeset
96 }
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 }