KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > ic2d > spy > Spy


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

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 /**
41  * The master Spy class
42  */

43 public class Spy implements org.objectweb.proactive.RunActive {
44
45
46   
47   /** Timeout between updates */
48   protected long updateFrequence = 3000;
49   
50   /** event manager */
51   protected transient SpyEventManager spyEventManager;
52   
53   /** the listener of our events */
54   protected SpyListener spyListener;
55   
56   protected boolean isActive = true;
57
58   //
59
// -- CONSTRUCTORS -----------------------------------------------
60
//
61

62   public Spy() {
63   }
64
65
66   public Spy(SpyListener spyListener) {
67     this.spyListener = spyListener;
68   }
69
70
71   //
72
// -- PUBLIC METHOD -----------------------------------------------
73
//
74

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 JavaDoc 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 JavaDoc getSystemProperty(String JavaDoc key) {
111     return System.getProperty(key);
112   }
113
114
115   public void terminate() {
116     isActive = false;
117   }
118
119   //
120
// -- ADD / REMOVE LISTENERS -----------------------------------------------
121
//
122

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       // Sleep till the next update, or be awaken by incoming requests!
143
service.blockingServeOldest(updateFrequence);
144       // Serve any pending request
145
while (service.hasRequestToServe()) {
146         service.serveOldest();
147       }
148       // Check if we have been awaken earlier or not
149
if (System.currentTimeMillis() >= nextUpdate) {
150         SpyEvent[] spyEvents = spyEventManager.collectPendingSpyEvents();
151         if (spyEvents.length > 0)
152         //System.out.println("sending spyEvents n="+spyEvents.length);
153
// we send event if size == 0 just has a check that the listener is still there
154
notifyListener(spyEvents);
155       }
156     }
157     spyEventManager.removeBodyEventListener();
158     spyEventManager.removeFutureEventListener();
159     body.terminate();
160   }
161
162
163
164   //
165
// -- PROTECTED METHODS -----------------------------------------------
166
//
167

168   protected void notifyListener(SpyEvent[] events) {
169     spyListener.observationsPerformed(events);
170   }
171
172
173
174   //
175
// -- PRIVATE METHODS -----------------------------------------------
176
//
177

178   private void log(String JavaDoc str) {
179     //System.out.println(str);
180
}
181   
182 }
183
Popular Tags