1 24 25 package org.objectweb.dream.protocol.utobcast; 26 27 import java.util.HashMap ; 28 import java.util.TreeSet ; 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 40 public class ProcessMembershipImpl extends AbstractComponent 41 implements 42 ProcessMembership, 43 ProcessMembershipAttributeController 44 { 45 46 boolean blocking = false; 47 48 52 56 HashMap processMembershipUpdateNotificationsItf = new HashMap (); 57 58 59 Process leader; 60 Object leaderLock = new Object (); 61 62 63 Process backup; 64 Object backupLock = new Object (); 65 66 67 Process myself; 68 Object myselfLock = new Object (); 69 70 74 TreeSet otherProcesses = new TreeSet (); 75 Object otherProcessesLock = new Object (); 76 77 78 Process [] otherProcessesArray = new Process [0]; 79 80 84 87 public ProcessMembershipImpl() 88 { 89 } 90 91 95 99 public Process getLeader() throws InterruptedException 100 { 101 if (leader == null) 102 { 103 if (blocking) 104 { 105 synchronized (leaderLock) 106 { 107 if (leader == null) 109 { 110 leaderLock.wait(); 111 } 112 } 113 } 114 } 115 return leader; 116 } 117 118 121 public void setLeader(Process leader) 122 { 123 synchronized (leaderLock) 124 { 125 this.leader = leader; 126 leaderLock.notifyAll(); 127 } 128 } 129 130 133 public Process getBackup() throws InterruptedException 134 { 135 if (backup == null) 136 { 137 if (blocking) 138 { 139 synchronized (backupLock) 140 { 141 if (backup == null) 143 { 144 backupLock.wait(); 145 } 146 } 147 } 148 } 149 return backup; 150 } 151 152 155 public void setBackup(Process backup) 156 { 157 synchronized (backupLock) 158 { 159 this.backup = backup; 160 backupLock.notifyAll(); 161 } 162 } 163 164 168 public Process getMyself() throws InterruptedException 169 { 170 if (myself == null) 171 { 172 if (blocking) 173 { 174 synchronized (myselfLock) 175 { 176 if (myself == null) 178 { 179 myselfLock.wait(); 180 } 181 } 182 } 183 } 184 return myself; 185 } 186 187 190 public void setMyself(Process myself) 191 { 192 synchronized (myselfLock) 193 { 194 this.myself = myself; 195 myselfLock.notifyAll(); 196 } 197 } 198 199 202 public void setOtherProcesses(Process [] processes) 203 { 204 synchronized (otherProcessesLock) 205 { 206 otherProcesses = new TreeSet (); 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 219 public Process [] getOtherProcesses() 220 { 221 synchronized (otherProcessesLock) 222 { 223 return otherProcessesArray; 224 } 225 } 226 227 230 public void addProcess(Process process) 231 { 232 synchronized (otherProcessesLock) 233 { 234 otherProcesses.add(process); 235 createOtherProcessesArray(); 236 otherProcessesLock.notifyAll(); 237 } 238 } 239 240 243 public void removeProcess(Process process) 244 { 245 synchronized (otherProcessesLock) 246 { 247 otherProcesses.remove(process); 248 createOtherProcessesArray(); 249 } 250 } 251 252 255 public Process electBackup() throws InterruptedException 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 ) otherProcesses.first(); 271 otherProcesses.remove(backup); 272 createOtherProcessesArray(); 273 return backup; 274 } 275 } 276 277 280 public Process electBackupAsLeader(Process 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 leaderLock.notifyAll(); 290 } 291 } 292 return leader; 293 } 294 295 299 303 private void createOtherProcessesArray() 304 { 305 otherProcessesArray = new Process [otherProcesses.size()]; 306 otherProcesses.toArray(otherProcessesArray); 307 } 308 309 313 316 public void setBlocking(boolean blocking) 317 { 318 this.blocking = blocking; 319 } 320 321 324 public boolean getBlocking() 325 { 326 return blocking; 327 } 328 329 333 337 public synchronized void bindFc(String clientItfName, Object serverItf) 338 throws NoSuchInterfaceException, IllegalBindingException, 339 IllegalLifeCycleException 340 { 341 super.bindFc(clientItfName, serverItf); 342 processMembershipUpdateNotificationsItf.put(clientItfName, serverItf); 343 } 344 345 348 public synchronized void unbindFc(String clientItfName) 349 throws NoSuchInterfaceException, IllegalBindingException, 350 IllegalLifeCycleException 351 { 352 super.unbindFc(clientItfName); 353 processMembershipUpdateNotificationsItf.remove(clientItfName); 354 } 355 356 359 public synchronized String [] listFc() 360 { 361 String [] toBeReturned = new String [processMembershipUpdateNotificationsItf 362 .size()]; 363 processMembershipUpdateNotificationsItf.keySet().toArray(toBeReturned); 364 return toBeReturned; 365 } 366 367 } | Popular Tags |