1 |
package appl.parallel.server; |
2 |
|
3 |
import java.io.IOException; |
4 |
import java.net.DatagramPacket; |
5 |
import java.net.InetAddress; |
6 |
import java.net.MulticastSocket; |
7 |
|
8 |
import org.apache.log4j.LogManager; |
9 |
import org.apache.log4j.Logger; |
10 |
|
11 |
import appl.ext.XuluConfig; |
12 |
import appl.parallel.services.MulticastDiscoveryService; |
13 |
|
14 |
/** |
15 |
* Waits for multicast messages from clients and responds properly. |
16 |
* |
17 |
* @see MulticastDiscoveryService |
18 |
* @author Dominik Appl |
19 |
*/ |
20 |
public class ServerMulticastReceiver extends Thread { |
21 |
|
22 |
private final Logger LOG = LogManager.getLogger(this.getClass().getName()); |
23 |
|
24 |
private static final String helloFromXuluMessage = "Hello Servers"; |
25 |
|
26 |
private static final String helloFromServerMessage = "Hello Xulu from XuluServer"; |
27 |
|
28 |
boolean exit = false; |
29 |
|
30 |
MulticastSocket socket; |
31 |
|
32 |
int multicastPort = XuluConfig.getXuluConfig().getIntProperty( |
33 |
"XuluServer.multicastport"); |
34 |
|
35 |
/** |
36 |
* @param socket |
37 |
* Socket which is used for listening. Set a timeout for the |
38 |
* socket via {@link MulticastSocket#setSoTimeout(int)} so that |
39 |
* the thread can be stopped (because of the blocked reading) |
40 |
*/ |
41 |
public ServerMulticastReceiver(MulticastSocket socket) { |
42 |
this.socket = socket; |
43 |
// try { |
44 |
// socket.setSoTimeout(5000); |
45 |
// } catch (SocketException e) { |
46 |
// //do not printout the timeout |
47 |
// } |
48 |
} |
49 |
|
50 |
/** |
51 |
* Safe method to stop the Thread. Use this method instead of {@link Thread} |
52 |
*/ |
53 |
public void stopThread() { |
54 |
exit = true; |
55 |
} |
56 |
|
57 |
public void run() { |
58 |
System.out.println("Listening on port " + socket.getLocalPort() |
59 |
+ " for Xulu hello messages!"); |
60 |
while (!exit) { |
61 |
try { |
62 |
// receive buffer |
63 |
byte[] buffer = new byte[256]; |
64 |
DatagramPacket datagram = new DatagramPacket(buffer, |
65 |
buffer.length); |
66 |
socket.receive(datagram); |
67 |
|
68 |
// get right message format (cut out null bytes) |
69 |
byte[] result = new byte[datagram.getLength()]; |
70 |
System.arraycopy(datagram.getData(), 0, result, 0, datagram |
71 |
.getLength()); |
72 |
|
73 |
// check message: |
74 |
if (new String(result).equals(helloFromXuluMessage)) { |
75 |
// LOG.info("received hello from Client " + |
76 |
// datagram.getAddress()); |
77 |
LOG.info("received hello from Client " |
78 |
+ datagram.getAddress()); |
79 |
|
80 |
// get the IP again and send the answer |
81 |
String IP = XuluConfig.getXuluConfig().getProperty( |
82 |
"XuluServer.multicastgroup"); |
83 |
DatagramPacket answer = new DatagramPacket( |
84 |
helloFromServerMessage.getBytes(), |
85 |
helloFromServerMessage.length(), InetAddress |
86 |
.getByName(IP), multicastPort); |
87 |
socket.send(answer); |
88 |
} |
89 |
} catch (IOException e) { |
90 |
// TODO Auto-generated catch block |
91 |
e.printStackTrace(); |
92 |
} catch (Exception e) { |
93 |
if (!(e instanceof InterruptedException)) |
94 |
e.printStackTrace(); |
95 |
} |
96 |
} |
97 |
} |
98 |
} |