1 24 25 package org.objectweb.tribe.adapters; 26 27 import java.io.Serializable ; 28 import java.util.ArrayList ; 29 import java.util.HashMap ; 30 31 import org.objectweb.tribe.common.Member; 32 import org.objectweb.tribe.exceptions.TimeoutException; 33 34 40 public class MulticastResponse 41 { 42 private ArrayList recipient; 43 private ArrayList succeedMembers; 44 private ArrayList failedMembers; 45 private HashMap results; 46 private int waitMode; 47 private int nbOfRecipients; 48 49 57 public MulticastResponse(ArrayList recipient, int waitMode) 58 { 59 this.recipient = recipient; 60 this.waitMode = waitMode; 61 nbOfRecipients = recipient.size(); 62 succeedMembers = new ArrayList (nbOfRecipients); 63 results = new HashMap (nbOfRecipients); 64 } 65 66 71 public ArrayList getFailedMembers() 72 { 73 return failedMembers; 74 } 75 76 83 public void setFailedMembers(ArrayList failedMembers) 84 { 85 synchronized (results) 86 { 87 this.failedMembers = failedMembers; 88 if (failedMembers != null) 89 if (shouldNotify()) 90 results.notify(); 91 } 92 } 93 94 100 public HashMap getResults() 101 { 102 return results; 103 } 104 105 112 public Serializable getResult(Member m) 113 { 114 return (Serializable ) results.get(m); 115 } 116 117 127 public void addResult(Member m, Serializable 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 144 public ArrayList getSucceedMembers() 145 { 146 return succeedMembers; 147 } 148 149 155 public ArrayList getRecipient() 156 { 157 return recipient; 158 } 159 160 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 ignore) 181 { 182 } 183 } 184 if ((timeout != 0) && (end - start >= timeout)) 185 throw new TimeoutException(); 186 } 187 188 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 ( 213 "Unexpected wait mode policy in MulticastReponse: " + waitMode); 214 } 215 } 216 217 } | Popular Tags |