1 31 package org.objectweb.proactive.ic2d.data; 32 33 import java.rmi.dgc.VMID ; 34 35 import org.apache.log4j.Logger; 36 import org.objectweb.proactive.ActiveObjectCreationException; 37 import org.objectweb.proactive.ProActive; 38 import org.objectweb.proactive.core.UniqueID; 39 import org.objectweb.proactive.core.body.migration.MigrationException; 40 import org.objectweb.proactive.core.node.Node; 41 import org.objectweb.proactive.core.node.NodeException; 42 import org.objectweb.proactive.core.node.NodeFactory; 43 import org.objectweb.proactive.core.util.UrlBuilder; 44 import org.objectweb.proactive.ic2d.event.CommunicationEventListener; 45 import org.objectweb.proactive.ic2d.event.SpyEventListener; 46 import org.objectweb.proactive.ic2d.event.VMObjectListener; 47 import org.objectweb.proactive.ic2d.spy.Spy; 48 import org.objectweb.proactive.ic2d.spy.SpyEvent; 49 import org.objectweb.proactive.ic2d.spy.SpyMessageEvent; 50 51 54 public class VMObject extends AbstractDataObject { 55 56 static Logger log4jlogger = Logger.getLogger(VMObject.class.getName()); 57 58 private static String SPY_LISTENER_NODE_NAME = "SpyListenerNode"; 59 private static Node SPY_LISTENER_NODE; 60 static { 61 String currentHost; 62 try { 63 currentHost = java.net.InetAddress.getLocalHost().getCanonicalHostName(); 64 } catch (java.net.UnknownHostException e) { 65 currentHost = "localhost"; 66 } 67 try { 69 SPY_LISTENER_NODE = NodeFactory.createNode("//"+currentHost+"/"+SPY_LISTENER_NODE_NAME, true,null,null); 71 } catch (NodeException e) { 72 SPY_LISTENER_NODE = null; 73 } 74 } 75 76 protected Spy spy; 77 protected VMID vmid; 78 protected String protocolId; 79 protected java.util.HashMap objectNodeMap; 80 81 protected SpyListenerImpl activeSpyListener; 82 83 protected VMObjectListener listener; 84 85 89 public VMObject(HostObject host, VMID vmid, Node node, String protocolId) throws ActiveObjectCreationException, NodeException { 90 super(host); 91 if (log4jlogger.isDebugEnabled()){ 93 log4jlogger.debug ("VMObject.<init>"); 94 } 95 this.vmid = vmid; 96 this.protocolId = protocolId; 97 this.objectNodeMap = new java.util.HashMap (); 98 SpyListenerImpl spyListener = new SpyListenerImpl(new MySpyEventListener()); 99 if (log4jlogger.isDebugEnabled()){ 100 log4jlogger.debug("VMObject.<init> creating activeSpyListener"); 101 } 102 this.activeSpyListener = (SpyListenerImpl) ProActive.turnActive(spyListener, SPY_LISTENER_NODE); 103 if (log4jlogger.isDebugEnabled()){ 104 log4jlogger.debug("VMObject.<init> creating spy"); 105 } 106 this.spy = (Spy) ProActive.newActive(Spy.class.getName(), new Object [] {activeSpyListener} , node); 107 addNodeObject(node); 108 controller.log("VMObject id="+vmid+" created based on node "+node.getNodeInformation().getURL()); 109 } 110 111 112 116 117 public String toString() { 118 return "VM id=" + vmid + "\n" + super.toString(); 119 } 120 121 122 123 127 public void registerListener(VMObjectListener listener) { 128 this.messageMonitoringListener = listener; 129 this.listener = listener; 130 notifyListenerOfExistingChilds(); 132 sendEventsForAllActiveObjects(); 133 } 134 135 136 140 public void migrateTo(UniqueID objectID, String nodeTargetURL) throws MigrationException { 141 try { 142 spy.migrateTo(objectID, nodeTargetURL); 143 } catch (MigrationException e) { 144 throw e; 145 } catch (Exception e) { 146 recoverExceptionInSpy(e); 147 throw new MigrationException("Problem contacting the Spy", e); 148 } 149 } 150 151 152 public VMID getID() { 153 return vmid; 154 } 155 156 public String getProtocolId(){ 157 return this.protocolId; 158 } 159 160 public int getActiveObjectsCount() { 161 return objectNodeMap.size(); 162 } 163 164 165 public String getSystemProperty(String key) { 166 try { 167 return spy.getSystemProperty(key); 168 } catch (Exception e) { 169 recoverExceptionInSpy(e); 170 return "! Error occured"; 171 } 172 } 173 174 175 public long getUpdateFrequence() { 176 try { 177 return spy.getUpdateFrequence(); 178 } catch (Exception e) { 179 recoverExceptionInSpy(e); 180 return 0; 181 } 182 } 183 184 185 public void setUpdateFrequence(long updateFrequence) { 186 try { 187 spy.setUpdateFrequence(updateFrequence); 188 } catch (Exception e) { 189 recoverExceptionInSpy(e); 190 } 191 } 192 193 194 public void sendEventsForAllActiveObjects() { 195 if (log4jlogger.isDebugEnabled()){ 196 log4jlogger.debug("VMObject.sendEventForAllActiveObjects()"); 197 } 198 try { 199 spy.sendEventsForAllActiveObjects(); 200 } catch (Exception e) { 201 recoverExceptionInSpy(e); 202 } 203 } 204 205 206 207 211 public NodeObject addNodeObject(Node node) { 212 if (log4jlogger.isDebugEnabled()){ 213 log4jlogger.debug("VMObject: addNodeObject()"); 214 } 215 String nodeName = node.getNodeInformation().getName(); 216 NodeObject nodeObject = (NodeObject) getChild(nodeName); 217 if (nodeObject == null) { 218 nodeObject = new NodeObject(this, node); 219 putChild(nodeName, nodeObject); 220 if (listener != null) listener.nodeObjectAdded(nodeObject); 221 sendEventsForAllActiveObjects(); 222 } 223 return nodeObject; 224 } 225 226 227 public NodeObject getNodeObject(String nodeName) { 228 return (NodeObject) getChild(nodeName); 229 } 230 231 232 public NodeObject getNodeObject(UniqueID bodyID) { 233 return (NodeObject) objectNodeMap.get(bodyID); 234 } 235 236 237 public void removeNodeObject(String nodeName) { 238 NodeObject nodeObject = (NodeObject) removeChild(nodeName); 240 if (nodeObject == null) { 241 controller.log("The node "+nodeName+" does not exist. Cannot remove it"); 242 } else { 243 if (listener != null) listener.nodeObjectRemoved(nodeObject); 244 } 245 } 246 247 248 public void destroyObject() { 249 getTypedParent().removeVMObject(vmid); 250 } 251 252 253 257 protected void registerActiveObject(UniqueID id, NodeObject nodeObject) { 258 objectNodeMap.put(id, nodeObject); 259 } 260 261 protected void unregisterActiveObject(UniqueID id) { 262 objectNodeMap.remove(id); 263 } 264 265 266 protected synchronized boolean destroy() { 267 if (super.destroy()) { 268 try { 269 spy.terminate(); 270 } catch (Exception e) {} 271 activeSpyListener.terminate(); 272 objectNodeMap.clear(); 273 spy = null; 274 activeSpyListener = null; 275 listener = null; 276 return true; 277 } else { 278 return false; 279 } 280 } 281 282 283 protected void monitoringMessageEventChanged(ActiveObject object, boolean value) { 284 try { 285 if (value) { 286 spy.addMessageEventListener(object.getID()); 287 } else { 288 spy.removeMessageEventListener(object.getID()); 289 } 290 super.monitoringMessageEventChanged(object, value); 291 } catch (Exception e) { 292 recoverExceptionInSpy(e); 293 } 294 } 295 296 297 protected HostObject getTypedParent() { 298 return (HostObject)parent; 299 } 300 301 302 303 307 private void recoverExceptionInSpy(Exception e) { 308 controller.log("Exception occured while contacting Spy for VM "+vmid+". Now removing the VM from IC2D.",e); 309 destroyObject(); 310 } 311 312 313 private ActiveObject findActiveObject(UniqueID id) { 314 NodeObject nodeObject = getNodeObject(id); 315 if (nodeObject == null) { 316 controller.log("!! Event received for an unknown node, id="+id); 317 return null; } 319 ActiveObject ao = nodeObject.getActiveObject(id); 320 if (ao == null) { 321 controller.log("!! Event received for an unknown active object, id="+id); 322 } 323 return ao; 324 } 325 326 327 private synchronized void notifyListenerOfExistingChilds() { 328 if (getChildObjectsCount() == 0) return; 329 java.util.Iterator iterator = childsIterator(); 330 while (iterator.hasNext()) { 331 NodeObject nodeObject = (NodeObject) iterator.next(); 332 listener.nodeObjectAdded(nodeObject); 333 } 334 } 335 336 337 343 344 348 349 private class MySpyEventListener implements SpyEventListener { 350 351 private CommunicationEventListener communicationEventListener; 352 353 public MySpyEventListener() { 354 communicationEventListener = ((IC2DObject)getTopLevelParent()).getCommunicationEventListener(); 355 } 356 357 361 public void activeObjectAdded(UniqueID id, String nodeURL, String classname, boolean isActive) { 362 String nodeName = UrlBuilder.getNameFromUrl(nodeURL); 364 NodeObject nodeObject = getNodeObject(nodeName); 366 if (nodeObject != null) { 367 nodeObject.addActiveObject(classname, id, isActive); 368 } 369 } 370 371 public void activeObjectChanged(UniqueID id, boolean isActive, boolean isAlive) { 372 ActiveObject object = findActiveObject(id); 373 if (object == null) return; 375 if (! isAlive) { 376 object.destroyObject(); 377 } 378 } 379 380 public void objectWaitingForRequest(UniqueID id, SpyEvent spyEvent) { 381 if (! controller.isMonitoring()) return; 382 ActiveObject object = findActiveObject(id); 383 if (object == null) { return; } 384 object.setServingStatus(ActiveObject.STATUS_WAITING_FOR_REQUEST); 385 object.setRequestQueueLength(0); 386 communicationEventListener.objectWaitingForRequest(object, spyEvent); 387 } 388 389 public void objectWaitingByNecessity(UniqueID id, SpyEvent spyEvent) { 390 if (! controller.isMonitoring()) return; 391 ActiveObject object = findActiveObject(id); 392 if (object == null) { return; } 393 object.setServingStatus(object.getServingStatus() == ActiveObject.STATUS_SERVING_REQUEST ? 394 ActiveObject.STATUS_WAITING_BY_NECESSITY_WHILE_SERVING : 395 ActiveObject.STATUS_WAITING_BY_NECESSITY_WHILE_ACTIVE); 396 communicationEventListener.objectWaitingByNecessity(object, spyEvent); 397 } 398 399 public void objectReceivedFutureResult(UniqueID id, SpyEvent spyEvent) { 400 if (! controller.isMonitoring()) return; 401 ActiveObject object = findActiveObject(id); 402 if (object == null) { return; } 403 switch (object.getServingStatus()) { 404 case ActiveObject.STATUS_WAITING_BY_NECESSITY_WHILE_SERVING : 405 object.setServingStatus(ActiveObject.STATUS_SERVING_REQUEST); 406 break; 407 case ActiveObject.STATUS_WAITING_BY_NECESSITY_WHILE_ACTIVE : 408 object.setServingStatus(ActiveObject.STATUS_ACTIVE); 409 break; 410 } 411 } 412 413 public void requestMessageSent(UniqueID id, SpyEvent spyEvent) { 414 if (! controller.isMonitoring()) return; 415 ActiveObject object = findActiveObject(id); 416 if (object == null) { return; } 417 if (! object.isMonitoringRequestSender()) return; 418 communicationEventListener.requestMessageSent(object, spyEvent); 419 } 420 421 public void replyMessageSent(UniqueID id, SpyEvent spyEvent) { 422 if (! controller.isMonitoring()) return; 423 ActiveObject object = findActiveObject(id); 424 if (object == null) { return; } 425 object.setRequestQueueLength(((SpyMessageEvent)spyEvent).getRequestQueueLength()); 426 object.setServingStatus(ActiveObject.STATUS_ACTIVE); 427 if (! object.isMonitoringReplySender()) return; 428 communicationEventListener.replyMessageSent(object, spyEvent); 429 } 430 431 public void requestMessageReceived(UniqueID id, SpyEvent spyEvent) { 432 if (! controller.isMonitoring()) return; 433 ActiveObject object = findActiveObject(id); 434 if (object == null) { return; } 435 object.setRequestQueueLength(((SpyMessageEvent)spyEvent).getRequestQueueLength()); 436 if (! object.isMonitoringRequestReceiver()) return; 437 communicationEventListener.requestMessageReceived(object, spyEvent); 438 } 439 440 public void replyMessageReceived(UniqueID id, SpyEvent spyEvent) { 441 if (! controller.isMonitoring()) return; 442 ActiveObject object = findActiveObject(id); 443 if (object == null) { return; } 444 if (! object.isMonitoringReplySender()) return; 445 communicationEventListener.replyMessageReceived(object, spyEvent); 446 } 447 448 public void voidRequestServed(UniqueID id, SpyEvent spyEvent) { 449 if (! controller.isMonitoring()) return; 450 ActiveObject object = findActiveObject(id); 451 if (object == null) { return; } 452 object.setRequestQueueLength(((SpyMessageEvent)spyEvent).getRequestQueueLength()); 453 object.setServingStatus(ActiveObject.STATUS_ACTIVE); 454 if (! object.isMonitoringReplySender()) return; 455 communicationEventListener.voidRequestServed(object, spyEvent); 456 } 457 458 public void servingStarted(UniqueID id, SpyEvent spyEvent) { 459 if (! controller.isMonitoring()) return; 460 ActiveObject object = findActiveObject(id); 461 if (object == null) { return; } 462 object.setRequestQueueLength(((SpyMessageEvent)spyEvent).getRequestQueueLength()); 463 object.setServingStatus(ActiveObject.STATUS_SERVING_REQUEST); 464 } 465 466 public void allEventsProcessed() { 467 if (! controller.isMonitoring()) return; 468 communicationEventListener.allEventsProcessed(); 469 } 470 471 } 473 474 475 } 476 | Popular Tags |