/[xulu]/branches/1.8-gt2-2.6/src/appl/parallel/server/ServerMulticastReceiver.java
ViewVC logotype

Annotation of /branches/1.8-gt2-2.6/src/appl/parallel/server/ServerMulticastReceiver.java

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26