00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 package org.objectweb.proactive.core.body;
00032
00033 import java.util.Hashtable;
00034
00035 import org.objectweb.proactive.Body;
00036 import org.objectweb.proactive.core.UniqueID;
00037 import org.objectweb.proactive.core.event.AbstractEventProducer;
00038 import org.objectweb.proactive.core.event.BodyEvent;
00039 import org.objectweb.proactive.core.event.BodyEventListener;
00040 import org.objectweb.proactive.core.event.ProActiveEvent;
00041 import org.objectweb.proactive.core.event.ProActiveListener;
00042
00043
00061 public class BodyMap extends AbstractEventProducer implements Cloneable,
00062 java.io.Externalizable {
00063
00064
00065
00066 private Hashtable<UniqueID, UniversalBody> idToBodyMap;
00067
00068
00069
00070
00071 public BodyMap() {
00072 idToBodyMap = new Hashtable<UniqueID, UniversalBody>();
00073 }
00074
00075
00076
00077
00078
00083 public synchronized void putBody(UniqueID id, UniversalBody b) {
00084 while (idToBodyMap.get(id) != null) {
00085 try {
00086 wait();
00087 } catch (InterruptedException e) {
00088 e.printStackTrace();
00089 }
00090 }
00091 idToBodyMap.put(id, b);
00092
00093 if (hasListeners()) {
00094 notifyAllListeners(new BodyEvent(b, BodyEvent.BODY_CREATED));
00095 }
00096 }
00097
00102 public synchronized void updateBody(UniqueID id, UniversalBody b) {
00103
00104 if (idToBodyMap.get(id) != null) {
00105 idToBodyMap.remove(id);
00106 }
00107
00108
00109 idToBodyMap.put(id, b);
00110
00111 if (hasListeners()) {
00112 notifyAllListeners(new BodyEvent(b, BodyEvent.BODY_CREATED));
00113 }
00114 }
00115
00116 public synchronized void removeBody(UniqueID id) {
00117 UniversalBody b = idToBodyMap.remove(id);
00118 notifyAll();
00119
00120 if ((b != null) && hasListeners()) {
00121 notifyAllListeners(new BodyEvent(b, BodyEvent.BODY_DESTROYED));
00122 }
00123 }
00124
00125 public synchronized int size() {
00126 int val = idToBodyMap.size();
00127
00128 return val;
00129 }
00130
00131 public synchronized UniversalBody getBody(UniqueID id) {
00132 Object o = null;
00133 if (id != null) {
00134 o = idToBodyMap.get(id);
00135 }
00136
00137 return (UniversalBody) o;
00138 }
00139
00140 public synchronized boolean containsBody(UniqueID id) {
00141 return idToBodyMap.containsKey(id);
00142 }
00143
00144 public java.util.Iterator<UniversalBody> bodiesIterator() {
00145 return idToBodyMap.values().iterator();
00146 }
00147
00148 public synchronized String toString() {
00149 StringBuffer sb = new StringBuffer();
00150
00151 sb.append(" -- BodyMap ------- \n");
00152
00153 java.util.Set entrySet = idToBodyMap.entrySet();
00154 java.util.Iterator iterator = entrySet.iterator();
00155
00156 while (iterator.hasNext()) {
00157 java.util.Map.Entry entry = (java.util.Map.Entry) iterator.next();
00158 sb.append(entry.getKey()).append(" body = ")
00159 .append(entry.getValue()).append("\n");
00160 }
00161
00162 return sb.toString();
00163 }
00164
00165
00166
00167
00168 public Object clone() {
00169 BodyMap newLocationTable = new BodyMap();
00170 newLocationTable.idToBodyMap = (Hashtable<UniqueID, UniversalBody>)idToBodyMap.clone();
00171
00172 return newLocationTable;
00173 }
00174
00175
00176
00177
00178 public void addBodyEventListener(BodyEventListener listener) {
00179 addListener(listener);
00180 }
00181
00182 public void removeBodyEventListener(BodyEventListener listener) {
00183 removeListener(listener);
00184 }
00185
00186
00187
00188
00189
00194 public synchronized void readExternal(java.io.ObjectInput in)
00195 throws java.io.IOException, ClassNotFoundException {
00196 int size = in.readInt();
00197
00198 for (int i = 0; i < size; i++) {
00199 UniqueID id = (UniqueID) in.readObject();
00200 UniversalBody remoteBody = (UniversalBody) in.readObject();
00201 idToBodyMap.put(id, remoteBody);
00202 }
00203 }
00204
00210 public synchronized void writeExternal(java.io.ObjectOutput out)
00211 throws java.io.IOException {
00212 int size = idToBodyMap.size();
00213 out.writeInt(size);
00214
00215 java.util.Set entrySet = idToBodyMap.entrySet();
00216 java.util.Iterator iterator = entrySet.iterator();
00217
00218 while (iterator.hasNext()) {
00219 java.util.Map.Entry entry = (java.util.Map.Entry) iterator.next();
00220 out.writeObject(entry.getKey());
00221
00222 Object value = entry.getValue();
00223
00224 if (value instanceof Body) {
00225 out.writeObject(((Body) value).getRemoteAdapter());
00226 } else {
00227 out.writeObject(value);
00228 }
00229 }
00230 }
00231
00232
00233
00234
00235 protected void notifyOneListener(ProActiveListener listener,
00236 ProActiveEvent event) {
00237 BodyEvent bodyEvent = (BodyEvent) event;
00238
00239 switch (bodyEvent.getType()) {
00240 case BodyEvent.BODY_CREATED:
00241 ((BodyEventListener) listener).bodyCreated(bodyEvent);
00242 break;
00243 case BodyEvent.BODY_DESTROYED:
00244 ((BodyEventListener) listener).bodyDestroyed(bodyEvent);
00245 break;
00246 }
00247 }
00248 }