KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > tribe > gms > GroupMembershipListenerThread


1 /**
2  * Tribe: Group communication library.
3  * Copyright (C) 2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: tribe@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.tribe.gms;
26
27 import org.objectweb.tribe.channel.ReceiveBuffer;
28 import org.objectweb.tribe.common.log.Trace;
29 import org.objectweb.tribe.exceptions.EmptyBufferException;
30 import org.objectweb.tribe.gms.protocol.GroupMembershipMessage;
31 import org.objectweb.tribe.messages.ChannelMessage;
32
33 /**
34  * This class defines a GroupMembershipListenerThread.
35  * <p>
36  * This threads listen for group membership messages and call the appropriate
37  * callbacks.
38  *
39  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
40  * @version 1.0
41  */

42 public class GroupMembershipListenerThread extends Thread JavaDoc
43 {
44   private GroupMembershipService listener;
45   private ReceiveBuffer receiveBuffer; // where GMS messages are
46
// delivered
47
private boolean isKilled = false;
48   private Trace logger;
49
50   /**
51    * Creates a new <code>GroupMembershipListenerThread</code> object
52    */

53   public GroupMembershipListenerThread(GroupMembershipService gms)
54   {
55     super("GroupMembershipListenerThread");
56     this.listener = gms;
57     logger = GroupMembershipService.getLogger();
58   }
59
60   /**
61    * Terminates the GroupMembershipListenerThread.
62    */

63   public void kill()
64   {
65     isKilled = true;
66     // Wake up thread with a null message
67
receiveBuffer.addMessage(null);
68   }
69
70   /**
71    * @see java.lang.Runnable#run()
72    */

73   public void run()
74   {
75     if (logger.isDebugEnabled())
76       logger.debug("GroupMembershipListenerThread started");
77     receiveBuffer = listener.getReceiveBuffer();
78     while (!isKilled)
79     {
80       try
81       {
82         ChannelMessage msg = receiveBuffer.getMessage();
83         if (msg instanceof GroupMembershipMessage)
84         {
85           if (logger.isDebugEnabled())
86             logger.debug("Delivering GMS message: " + msg);
87           GroupMembershipMessage gmsMsg = (GroupMembershipMessage) msg;
88           gmsMsg.deliver(listener);
89         }
90         else
91           logger
92               .debug("Unexpected message type in GroupMembershipListenerThread:"
93                   + msg);
94       }
95       catch (EmptyBufferException e)
96       {
97         logger.error("Error while getting message for GMS service", e);
98       }
99     }
100   }
101 }
Popular Tags