KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > protocol > utobcast > ProcessMembershipImpl


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

24
25 package org.objectweb.dream.protocol.utobcast;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.TreeSet JavaDoc;
29
30 import org.objectweb.dream.AbstractComponent;
31 import org.objectweb.dream.protocol.Process;
32 import org.objectweb.fractal.api.NoSuchInterfaceException;
33 import org.objectweb.fractal.api.control.IllegalBindingException;
34 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
35 import org.objectweb.util.monolog.api.BasicLevel;
36
37 /**
38  * Implementation of the {@link ProcessMembership}interface.
39  */

40 public class ProcessMembershipImpl extends AbstractComponent
41     implements
42       ProcessMembership,
43       ProcessMembershipAttributeController
44 {
45
46   boolean blocking = false;
47
48   // ---------------------------------------------------------------------------
49
// Fields of this class
50
// ---------------------------------------------------------------------------
51

52   /**
53    * The collection client interface bound to components that need be notified
54    * about updates.
55    */

56   HashMap JavaDoc processMembershipUpdateNotificationsItf = new HashMap JavaDoc();
57
58   /** The leader process. */
59   Process JavaDoc leader;
60   Object JavaDoc leaderLock = new Object JavaDoc();
61
62   /** The backup process. */
63   Process JavaDoc backup;
64   Object JavaDoc backupLock = new Object JavaDoc();
65
66   /** The process that contains this component. */
67   Process JavaDoc myself;
68   Object JavaDoc myselfLock = new Object JavaDoc();
69
70   /**
71    * A TreeMapBuffer containing all the processes except leader and backup
72    * processes.
73    */

74   TreeSet JavaDoc otherProcesses = new TreeSet JavaDoc();
75   Object JavaDoc otherProcessesLock = new Object JavaDoc();
76
77   /** An array containing all the processes except leader and backup processes. */
78   Process JavaDoc[] otherProcessesArray = new Process JavaDoc[0];
79
80   // ---------------------------------------------------------------------------
81
// Constructor
82
// ---------------------------------------------------------------------------
83

84   /**
85    * Constructor.
86    */

87   public ProcessMembershipImpl()
88   {
89   }
90
91   // ---------------------------------------------------------------------------
92
// Implementation of the ProcessMembership interface
93
// ---------------------------------------------------------------------------
94

95   /**
96    * @throws InterruptedException
97    * @see org.objectweb.dream.protocol.utobcast.ProcessMembership#getLeader()
98    */

99   public Process JavaDoc getLeader() throws InterruptedException JavaDoc
100   {
101     if (leader == null)
102     {
103       if (blocking)
104       {
105         synchronized (leaderLock)
106         {
107           // To avoid taking the lock each time the method is called
108
if (leader == null)
109           {
110             leaderLock.wait();
111           }
112         }
113       }
114     }
115     return leader;
116   }
117
118   /**
119    * @see ProcessMembership#setLeader(Process)
120    */

121   public void setLeader(Process JavaDoc leader)
122   {
123     synchronized (leaderLock)
124     {
125       this.leader = leader;
126       leaderLock.notifyAll();
127     }
128   }
129
130   /**
131    * @see ProcessMembership#getBackup()
132    */

133   public Process JavaDoc getBackup() throws InterruptedException JavaDoc
134   {
135     if (backup == null)
136     {
137       if (blocking)
138       {
139         synchronized (backupLock)
140         {
141           // To avoid taking the lock each time the method is called
142
if (backup == null)
143           {
144             backupLock.wait();
145           }
146         }
147       }
148     }
149     return backup;
150   }
151
152   /**
153    * @see ProcessMembership#setBackup(Process)
154    */

155   public void setBackup(Process JavaDoc backup)
156   {
157     synchronized (backupLock)
158     {
159       this.backup = backup;
160       backupLock.notifyAll();
161     }
162   }
163
164   /**
165    * @throws InterruptedException
166    * @see ProcessMembership#getMyself()
167    */

168   public Process JavaDoc getMyself() throws InterruptedException JavaDoc
169   {
170     if (myself == null)
171     {
172       if (blocking)
173       {
174         synchronized (myselfLock)
175         {
176           // To avoid taking the lock each time the method is called
177
if (myself == null)
178           {
179             myselfLock.wait();
180           }
181         }
182       }
183     }
184     return myself;
185   }
186
187   /**
188    * @see ProcessMembership#setMyself(Process)
189    */

190   public void setMyself(Process JavaDoc myself)
191   {
192     synchronized (myselfLock)
193     {
194       this.myself = myself;
195       myselfLock.notifyAll();
196     }
197   }
198
199   /**
200    * @see org.objectweb.dream.protocol.utobcast.ProcessMembership#setOtherProcesses(org.objectweb.dream.protocol.Process[])
201    */

202   public void setOtherProcesses(Process JavaDoc[] processes)
203   {
204     synchronized (otherProcessesLock)
205     {
206       otherProcesses = new TreeSet JavaDoc();
207       for (int i = 0; i < processes.length; i++)
208       {
209         otherProcesses.add(processes[i]);
210       }
211       this.otherProcessesArray = processes;
212       otherProcessesLock.notifyAll();
213     }
214   }
215
216   /**
217    * @see ProcessMembership#getOtherProcesses()
218    */

219   public Process JavaDoc[] getOtherProcesses()
220   {
221     synchronized (otherProcessesLock)
222     {
223       return otherProcessesArray;
224     }
225   }
226
227   /**
228    * @see ProcessMembership#addProcess(Process)
229    */

230   public void addProcess(Process JavaDoc process)
231   {
232     synchronized (otherProcessesLock)
233     {
234       otherProcesses.add(process);
235       createOtherProcessesArray();
236       otherProcessesLock.notifyAll();
237     }
238   }
239
240   /**
241    * @see ProcessMembership#removeProcess(org.objectweb.dream.protocol.Process)
242    */

243   public void removeProcess(Process JavaDoc process)
244   {
245     synchronized (otherProcessesLock)
246     {
247       otherProcesses.remove(process);
248       createOtherProcessesArray();
249     }
250   }
251
252   /**
253    * @see org.objectweb.dream.protocol.utobcast.ProcessMembership#electBackup()
254    */

255   public Process JavaDoc electBackup() throws InterruptedException JavaDoc
256   {
257     synchronized (otherProcessesLock)
258     {
259       while (otherProcesses.size() <= 0)
260       {
261         if (blocking)
262         {
263           otherProcessesLock.wait();
264         }
265         else
266         {
267           return null;
268         }
269       }
270       backup = (Process JavaDoc) otherProcesses.first();
271       otherProcesses.remove(backup);
272       createOtherProcessesArray();
273       return backup;
274     }
275   }
276
277   /**
278    * @see org.objectweb.dream.protocol.utobcast.ProcessMembership#electBackupAsLeader(org.objectweb.dream.protocol.Process)
279    */

280   public Process JavaDoc electBackupAsLeader(Process JavaDoc oldLeader)
281   {
282     synchronized (leaderLock)
283     {
284       if (oldLeader.equals(leader))
285       {
286         logger.log(BasicLevel.INFO, "elect the backup as leader");
287         leader = backup;
288         // Not necessary (nobody should be waiting)
289
leaderLock.notifyAll();
290       }
291     }
292     return leader;
293   }
294
295   // ---------------------------------------------------------------------------
296
// Private methods
297
// ---------------------------------------------------------------------------
298

299   /**
300    * Creates the <code>otherProcessesArray</code> array. Calls to this method
301    * must be synchronized with <code>this</code>.
302    */

303   private void createOtherProcessesArray()
304   {
305     otherProcessesArray = new Process JavaDoc[otherProcesses.size()];
306     otherProcesses.toArray(otherProcessesArray);
307   }
308
309   // ---------------------------------------------------------------------------
310
// Implementation of the ProcessMembershipAttributeController interface
311
// ---------------------------------------------------------------------------
312

313   /**
314    * @see org.objectweb.dream.protocol.utobcast.ProcessMembershipAttributeController#setBlocking(boolean)
315    */

316   public void setBlocking(boolean blocking)
317   {
318     this.blocking = blocking;
319   }
320
321   /**
322    * @see org.objectweb.dream.protocol.utobcast.ProcessMembershipAttributeController#getBlocking()
323    */

324   public boolean getBlocking()
325   {
326     return blocking;
327   }
328
329   // ---------------------------------------------------------------------------
330
// Implementation of the BindingController interface
331
// ---------------------------------------------------------------------------
332

333   /**
334    * @see org.objectweb.fractal.api.control.BindingController#bindFc(java.lang.String,
335    * java.lang.Object)
336    */

337   public synchronized void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
338       throws NoSuchInterfaceException, IllegalBindingException,
339       IllegalLifeCycleException
340   {
341     super.bindFc(clientItfName, serverItf);
342     processMembershipUpdateNotificationsItf.put(clientItfName, serverItf);
343   }
344
345   /**
346    * @see org.objectweb.fractal.api.control.BindingController#unbindFc(java.lang.String)
347    */

348   public synchronized void unbindFc(String JavaDoc clientItfName)
349       throws NoSuchInterfaceException, IllegalBindingException,
350       IllegalLifeCycleException
351   {
352     super.unbindFc(clientItfName);
353     processMembershipUpdateNotificationsItf.remove(clientItfName);
354   }
355
356   /**
357    * @see org.objectweb.fractal.api.control.BindingController#listFc()
358    */

359   public synchronized String JavaDoc[] listFc()
360   {
361     String JavaDoc[] toBeReturned = new String JavaDoc[processMembershipUpdateNotificationsItf
362         .size()];
363     processMembershipUpdateNotificationsItf.keySet().toArray(toBeReturned);
364     return toBeReturned;
365   }
366
367 }
Popular Tags