Mercurial > hg > Members > riono > TreeVNC_ja_comment
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 |
rev | line source |
---|---|
206 | 1 package jp.ac.u_ryukyu.treevnc; |
11 | 2 |
159 | 3 import com.glavsoft.rfb.protocol.ProtocolContext.TreeCommand; |
505 | 4 import com.glavsoft.rfb.protocol.ReceiverTask; |
5 import com.glavsoft.transport.Reader; | |
174 | 6 import com.glavsoft.viewer.ViewerInterface; |
82 | 7 import com.glavsoft.viewer.swing.ConnectionParams; |
8 | |
505 | 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 | 13 import static com.glavsoft.rfb.protocol.ReceiverTask.FRAMEBUFFER_UPDATE; |
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 | 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 | 18 public static String McastAddr = Ipv4McastAddr; |
19 | |
11 | 20 static final int BufSize = 1024; |
21 private boolean stopFlag = false; | |
174 | 22 private ViewerInterface vps; |
69 | 23 private MulticastSocket soc; |
259 | 24 private SecurityManager securityManager; |
11 | 25 |
174 | 26 public TreeRootFinderListener(ViewerInterface vncProxyService) { |
27 vps = vncProxyService; | |
513 | 28 try { |
29 soc = createMulticastSocket(); | |
30 System.out.println("FindRoot listening on "+ InetAddress.getByName(McastAddr)); | |
31 } catch (IOException e) { | |
32 e.printStackTrace(); | |
33 } | |
11 | 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 | 56 byte[] buf = new byte[BufSize]; |
11 | 57 try { |
263 | 58 |
259 | 59 DatagramPacket recvPacket = new DatagramPacket(buf, BufSize); |
60 while (!stopFlag) { | |
11 | 61 soc.receive(recvPacket); |
155 | 62 String hostname = recvPacket.getAddress().getHostAddress(); |
82 | 63 byte[] reply = recvPacket.getData(); |
64 int len = recvPacket.getLength(); | |
512 | 65 boolean isTreeRoot = vps.getRfb().isTreeManager(); |
506 | 66 if (isTreeRoot && len == 12 && (reply[0] & 0xff) == TreeCommand.FIND_ROOT.cmd) { |
505 | 67 int port = reply[8]; |
68 port = port * 256 + reply[9]; | |
69 port = port * 256 + reply[10]; | |
70 port = port * 256 + reply[11]; | |
71 | |
72 TreeVncProtocol t = new TreeVncProtocol(hostname, port); | |
73 t.findRootReply(vps.getRfb().getAcceptPort()); | |
11 | 74 } |
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 | 77 System.out.println("tree-root-find-listener :" + e.getMessage()); |
11 | 78 } |
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 | 83 } |
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 | 86 public void setStopFlag(boolean stopFlag) { |
87 this.stopFlag = stopFlag; | |
88 } | |
89 | |
90 public boolean isStopFlag() { | |
91 return stopFlag; | |
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 | 97 } |