Mercurial > hg > Members > riono > TreeVNC_ja_comment
annotate src/main/java/jp/ac/u_ryukyu/treevnc/BroadcastRFBListener.java @ 514:887ebd993b3d
separate rfb broadcast
author | mir3636 |
---|---|
date | Thu, 21 Feb 2019 16:52:21 +0900 |
parents | src/main/java/jp/ac/u_ryukyu/treevnc/TreeRootFinderListener.java@b97fdcd337fe |
children | 91f5c9dc91c8 |
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; |
514 | 11 import java.net.DatagramPacket; |
12 import java.net.InetAddress; | |
13 import java.net.MulticastSocket; | |
14 import java.net.SocketException; | |
15 import java.nio.ByteBuffer; | |
434
aad75cd6d9e2
Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
331
diff
changeset
|
16 |
505 | 17 import static com.glavsoft.rfb.protocol.ReceiverTask.FRAMEBUFFER_UPDATE; |
18 | |
514 | 19 public class BroadcastRFBListener implements Runnable { |
263 | 20 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
|
21 public static final String Ipv6McastAddr = "ff02::1"; |
263 | 22 public static String McastAddr = Ipv4McastAddr; |
23 | |
11 | 24 static final int BufSize = 1024; |
505 | 25 private ReceiverTask receiverTask; |
11 | 26 private boolean stopFlag = false; |
514 | 27 private TreeRFBProto rfb; |
69 | 28 private MulticastSocket soc; |
259 | 29 private SecurityManager securityManager; |
11 | 30 |
514 | 31 public BroadcastRFBListener() { |
513 | 32 try { |
33 soc = createMulticastSocket(); | |
34 System.out.println("FindRoot listening on "+ InetAddress.getByName(McastAddr)); | |
35 } catch (IOException e) { | |
36 e.printStackTrace(); | |
37 } | |
11 | 38 } |
434
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 public static MulticastSocket createMulticastSocket() throws IOException { |
514 | 41 MulticastSocket soc = new MulticastSocket(ConnectionParams.DEFAULT_VNC_BROADCAST); |
434
aad75cd6d9e2
Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
331
diff
changeset
|
42 try { |
aad75cd6d9e2
Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
331
diff
changeset
|
43 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
|
44 } catch (SocketException e) { |
aad75cd6d9e2
Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
331
diff
changeset
|
45 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
|
46 } |
aad75cd6d9e2
Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
331
diff
changeset
|
47 try { |
aad75cd6d9e2
Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
331
diff
changeset
|
48 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
|
49 } catch (SocketException e) { |
aad75cd6d9e2
Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
331
diff
changeset
|
50 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
|
51 } |
aad75cd6d9e2
Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
331
diff
changeset
|
52 return soc; |
aad75cd6d9e2
Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
331
diff
changeset
|
53 } |
aad75cd6d9e2
Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
331
diff
changeset
|
54 |
aad75cd6d9e2
Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
331
diff
changeset
|
55 /** |
81
f93d0286c2ab
root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
56 * 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
|
57 * 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
|
58 */ |
514 | 59 private void rfbBroadcastLoop() { |
55 | 60 byte[] buf = new byte[BufSize]; |
11 | 61 try { |
263 | 62 |
259 | 63 DatagramPacket recvPacket = new DatagramPacket(buf, BufSize); |
64 while (!stopFlag) { | |
11 | 65 soc.receive(recvPacket); |
155 | 66 String hostname = recvPacket.getAddress().getHostAddress(); |
82 | 67 byte[] reply = recvPacket.getData(); |
68 int len = recvPacket.getLength(); | |
514 | 69 boolean isTreeRoot = rfb.isTreeManager(); |
70 if (isTreeRoot && receiverTask != null ) { | |
506 | 71 receiverTask.setReader(new Reader(new ByteArrayInputStream(reply))); |
72 if (receiverTask.getMessageId() == FRAMEBUFFER_UPDATE) { | |
73 receiverTask.framebufferUpdateMessage(); | |
505 | 74 } |
11 | 75 } |
76 } | |
81
f93d0286c2ab
root find multicast send port number now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
77 } catch (Exception e) { |
257 | 78 System.out.println("tree-root-find-listener :" + e.getMessage()); |
11 | 79 } |
80 } | |
434
aad75cd6d9e2
Multicast joins both ipv4 and ipv6 now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
331
diff
changeset
|
81 |
514 | 82 public void multicastUpdateRectangle(ByteBuffer buf) { |
83 | |
84 try { | |
85 DatagramPacket sendPacket = new DatagramPacket(buf.array | |
86 (), buf.limit(), InetAddress.getByName(TreeRootFinderListener.Ipv6McastAddr), ConnectionParams.DEFAULT_VNC_ROOT_FINDER); | |
87 soc.send(sendPacket); | |
88 } catch (IOException e) { | |
89 System.out.println("cannot send UpdateRectangle."); | |
90 e.printStackTrace(); | |
91 } | |
92 | |
93 try { | |
94 DatagramPacket sendPacket1 = new DatagramPacket(buf.array(), buf.limit(), InetAddress.getByName(TreeRootFinderListener.Ipv4McastAddr), ConnectionParams.DEFAULT_VNC_ROOT_FINDER); | |
95 soc.send(sendPacket1); | |
96 } catch (IOException e) { | |
97 System.out.println("cannot send UpdateRectangle."); | |
98 e.printStackTrace(); | |
99 } | |
100 soc.close(); | |
101 } | |
102 | |
134
128cce60c43c
where to connect command
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
126
diff
changeset
|
103 public void run() { |
514 | 104 rfbBroadcastLoop(); |
11 | 105 } |
106 | |
436
9db8862ef200
remove unsed code in FindRoot
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
435
diff
changeset
|
107 // it looks like that we never stop |
11 | 108 public void setStopFlag(boolean stopFlag) { |
109 this.stopFlag = stopFlag; | |
110 } | |
111 | |
112 public boolean isStopFlag() { | |
113 return stopFlag; | |
114 } | |
510
89f1f7e41838
broadcast FrameBufferUpdate Rectangle
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
506
diff
changeset
|
115 |
89f1f7e41838
broadcast FrameBufferUpdate Rectangle
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
506
diff
changeset
|
116 public MulticastSocket getSocket() { |
89f1f7e41838
broadcast FrameBufferUpdate Rectangle
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
506
diff
changeset
|
117 return soc; |
89f1f7e41838
broadcast FrameBufferUpdate Rectangle
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
506
diff
changeset
|
118 } |
514 | 119 |
120 public void init(TreeRFBProto rfb_, ReceiverTask receiverTask_) { | |
121 rfb = rfb_; | |
122 receiverTask = receiverTask_; | |
123 } | |
11 | 124 } |