1 31 package org.objectweb.proactive.ic2d.data; 32 33 import org.objectweb.proactive.ic2d.event.ActiveObjectListener; 34 import org.objectweb.proactive.core.UniqueID; 35 import org.objectweb.proactive.core.body.migration.MigrationException; 36 37 40 public class ActiveObject extends AbstractDataObject { 41 42 private static final int ACTIVE_OBJECT_CACHE_CLEANUP_INTERVAL = 300000; 44 public static final int STATUS_UNKNOWN = 0; 45 public static final int STATUS_WAITING_FOR_REQUEST = 1; 46 public static final int STATUS_SERVING_REQUEST = 2; 47 public static final int STATUS_WAITING_BY_NECESSITY_WHILE_ACTIVE = 3; 48 public static final int STATUS_WAITING_BY_NECESSITY_WHILE_SERVING = 4; 49 public static final int STATUS_ACTIVE = 5; 50 51 54 private static long lastActiveObjectCacheCleanupTime = System.currentTimeMillis(); 55 56 protected UniqueID id; 57 protected String className; 58 protected String name; 59 protected int requestQueueLength = -1; 61 protected int servingStatus; 63 64 protected ActiveObjectListener listener; 65 66 67 71 public ActiveObject(NodeObject parent, UniqueID id, String className, boolean isActive) { 72 super(parent); 73 this.className = className; 74 name = shortClassName(className)+"#"+counter(id); 75 this.id = id; 77 if (isMonitoring()) monitoringMessageEventChanged(this, true); 79 } 80 81 82 86 public String toString() { 87 return "Object "+name+" ID#" + id + "\n" + 88 " class : " + className + "\n" + 89 " monitoring : RequestReceiver(" + monitoringRequestReceiver + 90 "), RequestSender(" + monitoringRequestSender + 91 "), ReplyReceiver(" + monitoringReplyReceiver + 92 "), ReplyReceiver(" + monitoringReplySender + ")\n"; 93 } 94 95 96 public boolean migrateTo(String nodeTargetURL) { 97 if (isDestroyed) return false; 98 try { 99 getTypedParent().getTypedParent().migrateTo(id, nodeTargetURL); 100 if (controller != null) controller.log("Successfully migrated " + className + " to "+nodeTargetURL); 101 return true; 102 } catch (MigrationException e) { 103 if (controller != null) controller.log("Couldn't migrate "+className+" to "+nodeTargetURL, e); 104 return false; 105 } 106 } 107 108 109 public boolean isInsideSameVM(ActiveObject o) { 110 if (isDestroyed || o.isDestroyed) return false; 111 return getTypedParent().isInsideSameVM(o.getTypedParent()); 112 } 113 114 public boolean isInsideSameNode(ActiveObject o) { 115 if (isDestroyed || o.isDestroyed) return false; 116 return getTypedParent().isInsideSameNode(o.getTypedParent()); 117 } 118 119 public void setServingStatus(int value) { 120 if (isDestroyed) return; 121 if (value != servingStatus) { 122 servingStatus = value; 123 if (listener != null) listener.servingStatusChanged(value); 124 } 125 } 126 127 public ActiveObject findActiveObjectById(UniqueID id) { 128 if (id == this.id) 129 return this; 130 else return null; 131 } 132 133 public void setRequestQueueLength(int value) { 134 if (isDestroyed) return; 135 if (requestQueueLength != value) { 136 requestQueueLength = value; 137 if (listener != null) listener.requestQueueLengthChanged(value); 138 } 139 } 140 141 public void destroyObject() { 142 getTypedParent().removeActiveObject(id); 143 } 144 145 146 150 public void registerListener(ActiveObjectListener listener) { 151 this.messageMonitoringListener = listener; 152 this.listener = listener; 153 } 154 155 156 160 public String getName() { 161 return name; 162 } 163 164 public String getClassName() { 165 return className; 166 } 167 168 public UniqueID getID() { 169 return id; 170 } 171 172 public int getRequestQueueLength() { 173 return requestQueueLength; 174 } 175 176 public int getServingStatus() { 177 return servingStatus; 178 } 179 180 181 185 public void monitorRequestReceiver(boolean shouldMonitor) { 186 if (isDestroyed) return; 187 if (monitoringRequestReceiver == shouldMonitor) return; if (shouldMonitor) { 189 controller.log("Starting monitoring of RequestReceiver for " + className); 190 } else { 191 controller.log("Stopping monitoring of RequestReceiver for " + className); 192 } 193 boolean isMonitoringBefore = isMonitoring(); 194 monitoringRequestReceiver = shouldMonitor; 195 boolean isMonitoringAfter = isMonitoring(); 196 if (isMonitoringBefore != isMonitoringAfter) monitoringMessageEventChanged(this, isMonitoringAfter); 197 if (listener != null) listener.monitoringRequestReceiverChanged(shouldMonitor); 198 } 199 200 201 public void monitorRequestSender(boolean shouldMonitor) { 202 if (isDestroyed) return; 203 if (monitoringRequestSender == shouldMonitor) return; if (shouldMonitor) { 205 controller.log("Starting monitoring RequestSender for " + className); 206 } else { 207 controller.log("Stopping monitoring RequestSender for " + className); 208 } 209 boolean isMonitoringBefore = isMonitoring(); 210 monitoringRequestSender = shouldMonitor; 211 boolean isMonitoringAfter = isMonitoring(); 212 if (isMonitoringBefore != isMonitoringAfter) monitoringMessageEventChanged(this, isMonitoringAfter); 213 if (listener != null) listener.monitoringRequestSenderChanged(shouldMonitor); 214 } 215 216 217 public void monitorReplyReceiver(boolean shouldMonitor) { 218 if (isDestroyed) return; 219 if (monitoringReplyReceiver == shouldMonitor) return; if (shouldMonitor) { 221 controller.log("Starting monitoring ReplyReceiver for " + className); 222 } else { 223 controller.log("Stopping monitoring ReplyReceiver for " + className); 224 } 225 boolean isMonitoringBefore = isMonitoring(); 226 monitoringReplyReceiver = shouldMonitor; 227 boolean isMonitoringAfter = isMonitoring(); 228 if (isMonitoringBefore != isMonitoringAfter) monitoringMessageEventChanged(this, isMonitoringAfter); 229 if (listener != null) listener.monitoringReplyReceiverChanged(shouldMonitor); 230 } 231 232 233 public void monitorReplySender(boolean shouldMonitor) { 234 if (isDestroyed) return; 235 if (monitoringReplySender == shouldMonitor) return; if (shouldMonitor) { 237 controller.log("Starting monitoring ReplySender for " + className); 238 } else { 239 controller.log("Stopping monitoring ReplySender for " + className); 240 } 241 boolean isMonitoringBefore = isMonitoring(); 242 monitoringReplySender = shouldMonitor; 243 boolean isMonitoringAfter = isMonitoring(); 244 if (isMonitoringBefore != isMonitoringAfter) monitoringMessageEventChanged(this, isMonitoringAfter); 245 if (listener != null) listener.monitoringReplySenderChanged(shouldMonitor); 246 } 247 248 249 253 protected NodeObject getTypedParent() { 254 return (NodeObject)parent; 255 } 256 257 258 protected boolean destroy() { 259 destroyCachedObject(id); 260 return super.destroy(); 261 } 262 263 264 268 269 private static String shortClassName(String fqn) { 270 int n = fqn.lastIndexOf('.'); 271 if (n == -1 || n == fqn.length()-1) return fqn; 272 return fqn.substring(n+1); 273 } 274 275 276 280 private static int counter = 1; 281 private static java.util.HashMap knownActiveObjectCache = new java.util.HashMap (); 282 283 private static synchronized int counter(UniqueID id) { 284 CachedActiveObject cachedObject = (CachedActiveObject) knownActiveObjectCache.get(id); 285 if (lastActiveObjectCacheCleanupTime + ACTIVE_OBJECT_CACHE_CLEANUP_INTERVAL > System.currentTimeMillis()) { 286 lastActiveObjectCacheCleanupTime = System.currentTimeMillis(); 287 clearOldCachedObject(); 288 } 289 if (cachedObject == null) { 290 int count = counter++; 292 cachedObject = new CachedActiveObject(count); 293 knownActiveObjectCache.put(id,cachedObject); 294 return count; 295 } else { 296 cachedObject.resurrect(); 297 return cachedObject.count; 298 } 299 } 300 301 private static void clearOldCachedObject() { 302 long oldestAcceptableTime = System.currentTimeMillis() - 300000; java.util.Collection values = knownActiveObjectCache.values(); 304 java.util.Iterator iterator = values.iterator(); 305 while (iterator.hasNext()) { 306 CachedActiveObject cachedObject = (CachedActiveObject) iterator.next(); 307 if (cachedObject.destroyed && cachedObject.timestamp < oldestAcceptableTime) { 308 iterator.remove(); 310 } 311 } 312 } 313 314 private static void destroyCachedObject(UniqueID id) { 315 CachedActiveObject cachedObject = (CachedActiveObject) knownActiveObjectCache.get(id); 316 if (cachedObject == null) return; 317 cachedObject.destroy(); 318 } 319 320 private static class CachedActiveObject { 321 long timestamp; 322 int count; 323 boolean destroyed; 324 CachedActiveObject(int count) { 325 this.count = count; 326 } 327 void destroy() { 328 this.timestamp = System.currentTimeMillis(); 329 this.destroyed = true; 330 } 331 void resurrect() { 332 this.timestamp = 0; 333 this.destroyed = false; 334 } 335 } 336 337 } | Popular Tags |