KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > tribe > channel > ReceiveBuffer


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.channel;
26
27 import java.util.ArrayList JavaDoc;
28
29 import org.objectweb.tribe.exceptions.EmptyBufferException;
30 import org.objectweb.tribe.messages.ChannelMessage;
31
32 /**
33  * This class defines a ReceiveBuffer which behaves as a FIFO buffer.
34  *
35  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
36  * @version 1.0
37  */

38 public class ReceiveBuffer
39 {
40   /** The queue holding messages */
41   private ArrayList JavaDoc queue;
42   private Object JavaDoc bufferKey;
43
44   /**
45    * Creates a new <code>ReceiveBuffer</code> that collects messages sent to
46    * the specified destination key.
47    *
48    * @param bufferKey message destination key.
49    */

50   public ReceiveBuffer(Object JavaDoc bufferKey)
51   {
52     queue = new ArrayList JavaDoc();
53     this.bufferKey = bufferKey;
54   }
55
56   /**
57    * Returns the destination key this buffer is interested in.
58    *
59    * @return Returns the buffer destination key.
60    */

61   public Object JavaDoc getBufferKey()
62   {
63     return bufferKey;
64   }
65
66   /**
67    * Adds a new message to the buffer.
68    *
69    * @param message Message to add
70    */

71   public void addMessage(Object JavaDoc message)
72   {
73     synchronized (queue)
74     {
75       // Note that it is safe to notify first since the awakened thread cannot
76
// access the queue before we release the lock by exiting this method.
77
if (queue.isEmpty())
78         queue.notifyAll();
79       queue.add(message);
80     }
81   }
82
83   /**
84    * Get the first message from the buffer. This method blocks until a message
85    * is available from the buffer.
86    *
87    * @return the first ChannelMessage
88    * @throws EmptyBufferException if an error occurs
89    */

90   public ChannelMessage getMessage() throws EmptyBufferException
91   {
92     synchronized (queue)
93     {
94       while (queue.isEmpty())
95       {
96         try
97         {
98           queue.wait();
99         }
100         catch (InterruptedException JavaDoc ignore)
101         {
102         }
103       }
104       return (ChannelMessage) removeMessage();
105     }
106   }
107
108   /**
109    * Removes the first message from the buffer.
110    *
111    * @return the first message from the queue
112    * @throws EmptyBufferException if the buffer is empty
113    */

114   public Object JavaDoc removeMessage() throws EmptyBufferException
115   {
116     synchronized (queue)
117     {
118       if (queue.isEmpty())
119         throw new EmptyBufferException();
120       return queue.remove(0);
121     }
122   }
123
124   /**
125    * Returns true if the buffer is empty.
126    *
127    * @return true if the buffer is empty.
128    */

129   public synchronized boolean isEmpty()
130   {
131     return queue.isEmpty();
132   }
133
134 }
Popular Tags