Mercurial > hg > Members > riono > TreeVNC_ja_comment
annotate src/main/java/jp/ac/u_ryukyu/treevnc/BroadcastRFBListener.java @ 521:3fc0fedbc3fd
try catch in loop
author | mir3636 |
---|---|
date | Fri, 22 Feb 2019 16:07:03 +0900 |
parents | 3a9dadc8821f |
children | dca83cc39438 |
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 | |
520 | 24 static final int BufSize = 1024*64; |
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 } |
521 | 47 /*try { |
434
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."); |
521 | 51 }*/ |
434
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]; |
263 | 61 |
521 | 62 DatagramPacket recvPacket = new DatagramPacket(buf, BufSize); |
63 while (!stopFlag) { | |
64 try { | |
11 | 65 soc.receive(recvPacket); |
155 | 66 String hostname = recvPacket.getAddress().getHostAddress(); |
521 | 67 byte[] reply = recvPacket.getData(); |
82 | 68 int len = recvPacket.getLength(); |
514 | 69 boolean isTreeRoot = rfb.isTreeManager(); |
521 | 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 } |
521 | 76 } catch (Exception e) { |
77 System.out.println("broadcast-rfb-listener :" + e.getMessage()); | |
11 | 78 } |
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 | |
521 | 84 /*try { |
514 | 85 DatagramPacket sendPacket = new DatagramPacket(buf.array |
515 | 86 (), buf.limit(), InetAddress.getByName(TreeRootFinderListener.Ipv6McastAddr), ConnectionParams.DEFAULT_VNC_BROADCAST); |
514 | 87 soc.send(sendPacket); |
88 } catch (IOException e) { | |
89 System.out.println("cannot send UpdateRectangle."); | |
90 e.printStackTrace(); | |
521 | 91 }*/ |
514 | 92 |
93 try { | |
515 | 94 DatagramPacket sendPacket1 = new DatagramPacket(buf.array(), buf.limit(), InetAddress.getByName(TreeRootFinderListener.Ipv4McastAddr), ConnectionParams.DEFAULT_VNC_BROADCAST); |
514 | 95 soc.send(sendPacket1); |
96 } catch (IOException e) { | |
97 System.out.println("cannot send UpdateRectangle."); | |
98 e.printStackTrace(); | |
99 } | |
100 } | |
101 | |
134
128cce60c43c
where to connect command
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
126
diff
changeset
|
102 public void run() { |
514 | 103 rfbBroadcastLoop(); |
11 | 104 } |
105 | |
436
9db8862ef200
remove unsed code in FindRoot
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
435
diff
changeset
|
106 // it looks like that we never stop |
11 | 107 public void setStopFlag(boolean stopFlag) { |
108 this.stopFlag = stopFlag; | |
109 } | |
110 | |
111 public boolean isStopFlag() { | |
112 return stopFlag; | |
113 } | |
510
89f1f7e41838
broadcast FrameBufferUpdate Rectangle
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
506
diff
changeset
|
114 |
89f1f7e41838
broadcast FrameBufferUpdate Rectangle
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
506
diff
changeset
|
115 public MulticastSocket getSocket() { |
89f1f7e41838
broadcast FrameBufferUpdate Rectangle
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
506
diff
changeset
|
116 return soc; |
89f1f7e41838
broadcast FrameBufferUpdate Rectangle
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
506
diff
changeset
|
117 } |
514 | 118 |
119 public void init(TreeRFBProto rfb_, ReceiverTask receiverTask_) { | |
120 rfb = rfb_; | |
121 receiverTask = receiverTask_; | |
122 } | |
11 | 123 } |