KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > tribe > adapters > MulticastResponse


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.adapters;
26
27 import java.io.Serializable JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashMap JavaDoc;
30
31 import org.objectweb.tribe.common.Member;
32 import org.objectweb.tribe.exceptions.TimeoutException;
33
34 /**
35  * This class defines a MulticastResponse
36  *
37  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
38  * @version 1.0
39  */

40 public class MulticastResponse
41 {
42   private ArrayList JavaDoc recipient;
43   private ArrayList JavaDoc succeedMembers;
44   private ArrayList JavaDoc failedMembers;
45   private HashMap JavaDoc results;
46   private int waitMode;
47   private int nbOfRecipients;
48
49   /**
50    * Creates a new <code>MulticastResponse</code> object
51    *
52    * @param recipient <code>ArrayList</code> of <code>Member</code> to which
53    * the request is sent
54    * @param waitMode one of MulticastRequestAdapter.WAIT_NONE, WAIT_FIRST,
55    * WAIT_MAJORITY or WAIT_ALL
56    */

57   public MulticastResponse(ArrayList JavaDoc recipient, int waitMode)
58   {
59     this.recipient = recipient;
60     this.waitMode = waitMode;
61     nbOfRecipients = recipient.size();
62     succeedMembers = new ArrayList JavaDoc(nbOfRecipients);
63     results = new HashMap JavaDoc(nbOfRecipients);
64   }
65
66   /**
67    * Returns the list of failed Members.
68    *
69    * @return Returns the failed Members.
70    */

71   public ArrayList JavaDoc getFailedMembers()
72   {
73     return failedMembers;
74   }
75
76   /**
77    * Sets the list of failed Members. If the list of failed members is not null
78    * then we check if the wait mode policy has been satisfied and unblock
79    * waitForCompletion if needed.
80    *
81    * @param failedMembers The failed Members to set.
82    */

83   public void setFailedMembers(ArrayList JavaDoc failedMembers)
84   {
85     synchronized (results)
86     {
87       this.failedMembers = failedMembers;
88       if (failedMembers != null)
89         if (shouldNotify())
90           results.notify();
91     }
92   }
93
94   /**
95    * Returns the member results (for all Members that succeeded). The Hashmap
96    * uses the Member as a key and the result as a value.
97    *
98    * @return Returns the member results.
99    */

100   public HashMap JavaDoc getResults()
101   {
102     return results;
103   }
104
105   /**
106    * Get the result returned by the specified member.
107    *
108    * @param m the member for which we need the result
109    * @return the result or null if no result has been found (member failure or
110    * member not part of this request).
111    */

112   public Serializable JavaDoc getResult(Member m)
113   {
114     return (Serializable JavaDoc) results.get(m);
115   }
116
117   /**
118    * Add the given result for the specified member.
119    * <p>
120    * The member is then added in the succeedMembers list and the
121    * waitForCompletion() method might be unblocked according to the waiting mode
122    * policy specified in the constructor.
123    *
124    * @param m member sending this result
125    * @param result result to add.
126    */

127   public void addResult(Member m, Serializable JavaDoc result)
128   {
129     synchronized (results)
130     {
131       results.put(m, result);
132       succeedMembers.add(m);
133       if (shouldNotify())
134         results.notify();
135     }
136   }
137
138   /**
139    * Returns the list of members who sucessfully returned a response.
140    *
141    * @return Returns the succeed Members.
142    * @see #addResult(Member, Serializable)
143    */

144   public ArrayList JavaDoc getSucceedMembers()
145   {
146     return succeedMembers;
147   }
148
149   /**
150    * Returns the original list of the recipients (the ones we sent the message
151    * to).
152    *
153    * @return Returns an <code>ArrayList</code> of <code>Member</code>.
154    */

155   public ArrayList JavaDoc getRecipient()
156   {
157     return recipient;
158   }
159
160   /**
161    * Wait until the needed number of responses has been received (according to
162    * the waiting mode policy).
163    *
164    * @param timeout maximum time in ms to wait for (0 means no timeout)
165    */

166   public void waitForCompletion(long timeout) throws TimeoutException
167   {
168     long start = 0;
169     long end = 0;
170     synchronized (results)
171     {
172       if (shouldNotify())
173         return;
174       try
175       {
176         start = System.currentTimeMillis();
177         results.wait(timeout);
178         end = System.currentTimeMillis();
179       }
180       catch (InterruptedException JavaDoc ignore)
181       {
182       }
183     }
184     if ((timeout != 0) && (end - start >= timeout))
185       throw new TimeoutException();
186   }
187
188   /**
189    * Check if waitForCompletion should be notified of completion. This method
190    * should be calls while synchronized(results).
191    *
192    * @return true if the wait mode policy has been satisfied and the expected
193    * responses have been received.
194    */

195   private boolean shouldNotify()
196   {
197     int succeedSize = succeedMembers.size();
198     switch (waitMode)
199     {
200       case MulticastRequestAdapter.WAIT_NONE :
201         return true;
202       case MulticastRequestAdapter.WAIT_FIRST :
203         return (succeedSize > 0)
204             || ((failedMembers != null) && (failedMembers.size() == nbOfRecipients));
205       case MulticastRequestAdapter.WAIT_MAJORITY :
206         return (succeedSize > (nbOfRecipients / 2))
207             || ((failedMembers != null) && (succeedSize + failedMembers.size() == nbOfRecipients));
208       case MulticastRequestAdapter.WAIT_ALL :
209         return (succeedSize == nbOfRecipients)
210             || ((failedMembers != null) && (succeedSize + failedMembers.size() == nbOfRecipients));
211       default :
212         throw new RuntimeException JavaDoc(
213             "Unexpected wait mode policy in MulticastReponse: " + waitMode);
214     }
215   }
216
217 }
Popular Tags