1 31 package org.objectweb.proactive.ic2d.spy; 32 33 import org.objectweb.proactive.Body; 34 import org.objectweb.proactive.core.UniqueID; 35 import org.objectweb.proactive.core.body.LocalBodyStore; 36 import org.objectweb.proactive.core.body.migration.Migratable; 37 import org.objectweb.proactive.core.body.migration.MigrationException; 38 import org.objectweb.proactive.core.node.Node; 39 40 43 public class Spy implements org.objectweb.proactive.RunActive { 44 45 46 47 48 protected long updateFrequence = 3000; 49 50 51 protected transient SpyEventManager spyEventManager; 52 53 54 protected SpyListener spyListener; 55 56 protected boolean isActive = true; 57 58 62 public Spy() { 63 } 64 65 66 public Spy(SpyListener spyListener) { 67 this.spyListener = spyListener; 68 } 69 70 71 75 public long getUpdateFrequence() { 76 return updateFrequence; 77 } 78 79 80 public void sendEventsForAllActiveObjects() { 81 SpyEvent[] spyEvents = spyEventManager.createSpyEventForExistingBodies(LocalBodyStore.getInstance().getCurrentThreadBody()); 82 notifyListener(spyEvents); 83 } 84 85 86 public void setUpdateFrequence(long updateFrequence) { 87 this.updateFrequence = updateFrequence; 88 } 89 90 91 public void migrateTo(UniqueID bodyId, String nodeDestination) throws MigrationException { 92 Body body = LocalBodyStore.getInstance().getLocalBody(bodyId); 93 if (body == null) { 94 throw new MigrationException("Cannot find object id="+bodyId); 95 } 96 if (! (body instanceof Migratable)) { 97 throw new MigrationException("Object cannot Migrate"); 98 } 99 Node node = null; 100 try { 101 102 node = org.objectweb.proactive.core.node.NodeFactory.getNode(nodeDestination); 103 } catch (org.objectweb.proactive.core.node.NodeException e) { 104 throw new MigrationException("Cannot find node "+nodeDestination, e); 105 } 106 ((Migratable)body).migrateTo(node); 107 } 108 109 110 public String getSystemProperty(String key) { 111 return System.getProperty(key); 112 } 113 114 115 public void terminate() { 116 isActive = false; 117 } 118 119 123 public void addMessageEventListener(UniqueID bodyId) { 124 Body body = LocalBodyStore.getInstance().getLocalBody(bodyId); 125 if (body != null) spyEventManager.addMessageEventListener(body); 126 } 127 128 129 public void removeMessageEventListener(UniqueID bodyId) { 130 Body body = LocalBodyStore.getInstance().getLocalBody(bodyId); 131 if (body != null) spyEventManager.removeMessageEventListener(body); 132 } 133 134 135 public void runActivity(org.objectweb.proactive.Body body) { 136 spyEventManager = new SpyEventManager(body.getID()); 137 spyEventManager.addBodyEventListener(); 138 spyEventManager.addFutureEventListener(); 139 org.objectweb.proactive.Service service = new org.objectweb.proactive.Service(body); 140 while (isActive) { 141 long nextUpdate = System.currentTimeMillis() + updateFrequence; 142 service.blockingServeOldest(updateFrequence); 144 while (service.hasRequestToServe()) { 146 service.serveOldest(); 147 } 148 if (System.currentTimeMillis() >= nextUpdate) { 150 SpyEvent[] spyEvents = spyEventManager.collectPendingSpyEvents(); 151 if (spyEvents.length > 0) 152 notifyListener(spyEvents); 155 } 156 } 157 spyEventManager.removeBodyEventListener(); 158 spyEventManager.removeFutureEventListener(); 159 body.terminate(); 160 } 161 162 163 164 168 protected void notifyListener(SpyEvent[] events) { 169 spyListener.observationsPerformed(events); 170 } 171 172 173 174 178 private void log(String str) { 179 } 181 182 } 183 | Popular Tags |