View Javadoc

1   /*
2    * Copyright (c) 2005 Regents of the University of California (Regents). Created
3    * by TELS, Graduate School of Education, University of California at Berkeley.
4    *
5    * This software is distributed under the GNU Lesser General Public License, v2.
6    *
7    * Permission is hereby granted, without written agreement and without license
8    * or royalty fees, to use, copy, modify, and distribute this software and its
9    * documentation for any purpose, provided that the above copyright notice and
10   * the following two paragraphs appear in all copies of this software.
11   *
12   * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
13   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
14   * PURPOSE. THE SOFTWAREAND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
15   * HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
16   * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17   *
18   * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
19   * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
20   * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
21   * REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22   */
23  package net.sf.sail.common.messaging.multicast;
24  
25  import java.util.logging.Logger;
26  
27  import java.io.IOException;
28  import java.net.DatagramPacket;
29  import java.net.InetAddress;
30  import java.net.MulticastSocket;
31  import java.util.HashSet;
32  import java.util.Set;
33  
34  /**
35   * @author turadg
36   */
37  public class MulticastReceiverThread extends Thread {
38  	/**
39  	 * Logger for this class
40  	 */
41  	private static final Logger logger = Logger
42  			.getLogger(MulticastReceiverThread.class.getName());
43  
44  	InetAddress address;
45  
46  	Set<MessageListener> listeners = new HashSet<MessageListener>();
47  
48  	DatagramPacket packet;
49  
50  	MulticastSocket socket;
51  
52  	public MulticastReceiverThread() throws IOException {
53  		socket = new MulticastSocket(Multicaster.SAIL_PORT);
54  		address = InetAddress.getByName("230.0.0.1");
55  		socket.joinGroup(address);
56  	}
57  
58  	/**
59  	 * @param listener
60  	 */
61  	public void addMessageListener(MessageListener listener) {
62  		listeners.add(listener);
63  	}
64  
65  	@Override
66  	public void run() {
67  		while (true) {
68  			byte[] buf = new byte[256];
69  			packet = new DatagramPacket(buf, buf.length);
70  			try {
71  				socket.receive(packet);
72  			} catch (IOException e) {
73  				// TODO Auto-generated catch block
74  				logger.severe("exception: " + e); //$NON-NLS-1$
75  				break;
76  			}
77  
78  			String received = new String(packet.getData(), 0, packet.getLength());
79  			for (Object element : listeners) {
80  				MessageListener listener = (MessageListener) element;
81  				listener.newMessage(received);
82  			}
83  		}
84  
85  		try {
86  			socket.leaveGroup(address);
87  		} catch (IOException e) {
88  			// TODO Auto-generated catch block
89  			logger.severe("exception: " + e); //$NON-NLS-1$
90  		}
91  		socket.close();
92  	}
93  }