annotate src/main/java/jp/ac/u_ryukyu/treevnc/TreeRootFinderListener.java @ 259:606953a43198

fix how to join Multicast for the IPv6 address.
author oc
date Mon, 01 Dec 2014 22:29:59 +0900
parents fbd792d91055
children 7d8f45cadd89
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
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 import java.io.IOException;
259
606953a43198 fix how to join Multicast for the IPv6 address.
oc
parents: 258
diff changeset
4 import java.net.*;
606953a43198 fix how to join Multicast for the IPv6 address.
oc
parents: 258
diff changeset
5 import java.lang.SecurityManager;
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6
159
1c9f6acdfeb2 TreeCommand enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 155
diff changeset
7 import com.glavsoft.rfb.protocol.ProtocolContext.TreeCommand;
174
2e1530139169 reorganization
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 159
diff changeset
8 import com.glavsoft.viewer.ViewerInterface;
82
cc72fdbb7c72 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 81
diff changeset
9 import com.glavsoft.viewer.swing.ConnectionParams;
cc72fdbb7c72 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 81
diff changeset
10
134
128cce60c43c where to connect command
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 126
diff changeset
11 public class TreeRootFinderListener implements Runnable {
81
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
12 public static final String McastAddr = "224.0.0.1";
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 static final int BufSize = 1024;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 private boolean stopFlag = false;
174
2e1530139169 reorganization
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 159
diff changeset
15 private ViewerInterface vps;
69
21108e863b8a update client list.
one
parents: 68
diff changeset
16 private MulticastSocket soc;
259
606953a43198 fix how to join Multicast for the IPv6 address.
oc
parents: 258
diff changeset
17 private SecurityManager securityManager;
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18
174
2e1530139169 reorganization
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 159
diff changeset
19 public TreeRootFinderListener(ViewerInterface vncProxyService) {
2e1530139169 reorganization
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 159
diff changeset
20 vps = vncProxyService;
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22
81
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
23 /**
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
24 * 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
25 * 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
26 */
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
27 private void replyToRootSearchMulticast() {
55
9fdb8ff88497 change parameter.
one
parents: 38
diff changeset
28 byte[] buf = new byte[BufSize];
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 try {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 InetAddress mAddr = InetAddress.getByName(McastAddr);
82
cc72fdbb7c72 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 81
diff changeset
31 soc = new MulticastSocket(ConnectionParams.DEFAULT_VNC_ROOT_FINDER);
259
606953a43198 fix how to join Multicast for the IPv6 address.
oc
parents: 258
diff changeset
32 SocketAddress socAdd = new InetSocketAddress(mAddr, 5900);
606953a43198 fix how to join Multicast for the IPv6 address.
oc
parents: 258
diff changeset
33 soc.joinGroup(socAdd, null);
606953a43198 fix how to join Multicast for the IPv6 address.
oc
parents: 258
diff changeset
34 DatagramPacket recvPacket = new DatagramPacket(buf, BufSize);
606953a43198 fix how to join Multicast for the IPv6 address.
oc
parents: 258
diff changeset
35 while (!stopFlag) {
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 soc.receive(recvPacket);
155
1c95bd5bebcf root finder for multiple network.
oc
parents: 134
diff changeset
37 String hostname = recvPacket.getAddress().getHostAddress();
82
cc72fdbb7c72 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 81
diff changeset
38 byte[] reply = recvPacket.getData();
cc72fdbb7c72 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 81
diff changeset
39 int len = recvPacket.getLength();
126
775ce6a14f89 find root.
oc
parents: 83
diff changeset
40 if (len != 12) {
775ce6a14f89 find root.
oc
parents: 83
diff changeset
41 continue;
775ce6a14f89 find root.
oc
parents: 83
diff changeset
42 }
159
1c9f6acdfeb2 TreeCommand enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 155
diff changeset
43 if ((reply[0]&0xff) != TreeCommand.FIND_ROOT.cmd) {
126
775ce6a14f89 find root.
oc
parents: 83
diff changeset
44 continue;
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 }
126
775ce6a14f89 find root.
oc
parents: 83
diff changeset
46 int port = 0;
775ce6a14f89 find root.
oc
parents: 83
diff changeset
47 port = reply[8];
775ce6a14f89 find root.
oc
parents: 83
diff changeset
48 port = port * 256 + reply[9];
775ce6a14f89 find root.
oc
parents: 83
diff changeset
49 port = port * 256 + reply[10];
775ce6a14f89 find root.
oc
parents: 83
diff changeset
50 port = port * 256 + reply[11];
775ce6a14f89 find root.
oc
parents: 83
diff changeset
51
155
1c95bd5bebcf root finder for multiple network.
oc
parents: 134
diff changeset
52 TreeVncProtocol t = new TreeVncProtocol(hostname, port);
126
775ce6a14f89 find root.
oc
parents: 83
diff changeset
53 try {
155
1c95bd5bebcf root finder for multiple network.
oc
parents: 134
diff changeset
54 t.findRootReply(vps.getRfb().getAcceptPort());
126
775ce6a14f89 find root.
oc
parents: 83
diff changeset
55 } catch (IOException e) {
775ce6a14f89 find root.
oc
parents: 83
diff changeset
56 e.printStackTrace();
775ce6a14f89 find root.
oc
parents: 83
diff changeset
57 }
38
c2f0b6907448 add doc directory
one
parents: 13
diff changeset
58 if(stopFlag) break;
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 }
81
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
60 } catch (Exception e) {
257
11b59b223222 add function of fixing display size
oc
parents: 215
diff changeset
61 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
62 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
64
82
cc72fdbb7c72 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 81
diff changeset
65 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
66 {
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
67 int intval = 0;
82
cc72fdbb7c72 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 81
diff changeset
68 for( int i = offset; i < len ; i++ ) {
cc72fdbb7c72 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 81
diff changeset
69 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
70 intval = intval * 10 + ( bs[ i ] - '0' );
82
cc72fdbb7c72 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 81
diff changeset
71 }
81
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
72 return intval;
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
73 }
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
74
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
75
134
128cce60c43c where to connect command
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 126
diff changeset
76 public void run() {
81
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
77 replyToRootSearchMulticast();
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
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 public void setStopFlag(boolean stopFlag) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 this.stopFlag = stopFlag;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 }
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 public boolean isStopFlag() {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 return stopFlag;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 }
13
406fa09ae645 modify VncProxyService.java
one
parents: 12
diff changeset
87
81
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 * getLocalHost() returns hostname's address. It may not connectable, but
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
90 * it gives readable hostname. Do not use it to connect.
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
91 * @return
f93d0286c2ab root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 80
diff changeset
92 */
13
406fa09ae645 modify VncProxyService.java
one
parents: 12
diff changeset
93 String getMyAddress () {
406fa09ae645 modify VncProxyService.java
one
parents: 12
diff changeset
94 InetAddress addr = null;
406fa09ae645 modify VncProxyService.java
one
parents: 12
diff changeset
95 try {
406fa09ae645 modify VncProxyService.java
one
parents: 12
diff changeset
96 addr = InetAddress.getLocalHost();
406fa09ae645 modify VncProxyService.java
one
parents: 12
diff changeset
97 } catch (UnknownHostException e) {
80
2951dd85e9fc connect to real connection address instead of bad getLocalAddress().
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
98 return "localhost";
13
406fa09ae645 modify VncProxyService.java
one
parents: 12
diff changeset
99 }
80
2951dd85e9fc connect to real connection address instead of bad getLocalAddress().
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
100 return addr.getHostAddress();
13
406fa09ae645 modify VncProxyService.java
one
parents: 12
diff changeset
101 }
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 }