12
|
1 package jp.ac.u_ryukyu.treevnc.server;
|
11
|
2
|
|
3 import java.io.ByteArrayInputStream;
|
|
4 import java.io.IOException;
|
|
5 import java.net.DatagramPacket;
|
|
6 import java.net.InetAddress;
|
|
7 import java.net.MulticastSocket;
|
|
8 import java.net.SocketAddress;
|
13
|
9 import java.net.UnknownHostException;
|
11
|
10
|
|
11 //import TextBoxProxy;
|
|
12
|
|
13 public class GetBroadCastProxy implements Runnable {
|
|
14 static final String McastAddr = "224.0.0.1";
|
|
15 static final int Port = 8183;
|
|
16 static final int BufSize = 1024;
|
|
17 private byte[] buf = new byte[BufSize];
|
|
18 private byte[] resorve = new byte[BufSize];
|
|
19 private ByteArrayInputStream inputStream;
|
|
20 private boolean stopFlag = false;
|
|
21 private VncProxyService vps;
|
|
22 private BroadCastProxy bCast;
|
|
23 private String address;
|
|
24
|
13
|
25 public GetBroadCastProxy(VncProxyService _vps,String desktopName,String host){
|
11
|
26 vps = _vps;
|
13
|
27 bCast = new BroadCastProxy(vps.rfb.acceptPort+":"+host+":"
|
|
28 +desktopName+":"+getMyAddress()+":");
|
11
|
29 }
|
|
30
|
|
31 private synchronized void getData() {
|
|
32 try {
|
|
33 InetAddress mAddr = InetAddress.getByName(McastAddr);
|
|
34 MulticastSocket soc = new MulticastSocket(Port);
|
|
35 DatagramPacket recvPacket = new DatagramPacket(buf, BufSize);
|
|
36 soc.joinGroup(mAddr);
|
|
37 while (true) {
|
|
38 soc.receive(recvPacket);
|
|
39 address = getAddress(recvPacket.getSocketAddress());
|
|
40 inputStream = new ByteArrayInputStream(recvPacket.getData());
|
|
41 inputStream.read(resorve);
|
|
42 if("who".equals(castString(resorve))){
|
|
43 replyBroadCast();
|
|
44 }
|
38
|
45 if(stopFlag) break;
|
11
|
46 }
|
|
47 } catch (IOException e) {
|
|
48 e.printStackTrace();
|
|
49 }
|
|
50 }
|
|
51
|
|
52 private void replyBroadCast() {
|
|
53 Runnable sender = new Runnable() {
|
|
54 public void run() {
|
|
55 bCast.createSocket(address);
|
|
56 }
|
|
57 };
|
|
58 new Thread(sender).start();
|
|
59 }
|
|
60
|
|
61 private String getAddress(SocketAddress addr) {
|
|
62 String str = addr.toString();
|
|
63 str = str.substring(1,str.indexOf(":"));
|
|
64 return str;
|
|
65 }
|
|
66 /*
|
|
67 private int castBytoToInt(byte[] a) {
|
|
68 int value = 0;
|
|
69 ByteArrayInputStream bais = new ByteArrayInputStream(a);
|
|
70 DataInputStream dis = new DataInputStream(bais);
|
|
71 try {
|
|
72 value = dis.readInt();
|
|
73 } catch (IOException e) {
|
|
74 }
|
|
75 System.out.println(value);
|
|
76 return value;
|
|
77 }
|
|
78 */
|
|
79 private String castString(byte[] a) {
|
|
80 String recover = new String(a);
|
|
81 recover = recover.replace("¥n", "");
|
|
82 recover = recover.trim();
|
|
83 // System.out.println(recover);
|
|
84 return recover;
|
|
85 }
|
|
86
|
|
87
|
|
88 public void run() {
|
|
89 getData();
|
|
90 }
|
|
91
|
|
92 public void setStopFlag(boolean stopFlag) {
|
|
93 this.stopFlag = stopFlag;
|
|
94 }
|
|
95
|
|
96 public boolean isStopFlag() {
|
|
97 return stopFlag;
|
|
98 }
|
13
|
99
|
|
100 String getMyAddress () {
|
|
101 InetAddress addr = null;
|
|
102 try {
|
|
103 addr = InetAddress.getLocalHost();
|
|
104 } catch (UnknownHostException e) {
|
|
105 e.printStackTrace();
|
|
106 }
|
|
107 return new String(addr.getHostAddress());
|
|
108 }
|
|
109
|
11
|
110
|
|
111 }
|