KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > ic2d > data > ActiveObject


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.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 /**
38  * Holder class for the host data representation
39  */

40 public class ActiveObject extends AbstractDataObject {
41
42   private static final int ACTIVE_OBJECT_CACHE_CLEANUP_INTERVAL = 300000; // 300 seconds
43

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  /**
52   * Every so often we cleanup the cache
53   */

54   private static long lastActiveObjectCacheCleanupTime = System.currentTimeMillis();
55
56   protected UniqueID id;
57   protected String JavaDoc className;
58   protected String JavaDoc name;
59   protected int requestQueueLength = -1; // -1 = not known
60

61   // current status
62
protected int servingStatus;
63   
64   protected ActiveObjectListener listener;
65
66
67   //
68
// -- CONSTRUCTORS -----------------------------------------------
69
//
70

71   public ActiveObject(NodeObject parent, UniqueID id, String JavaDoc className, boolean isActive) {
72     super(parent);
73     this.className = className;
74     name = shortClassName(className)+"#"+counter(id);
75     //System.out.println("AO "+name+" created with Id "+id);
76
this.id = id;
77     //controller.log("ActiveObject "+className+" id="+id+" created.");
78
if (isMonitoring()) monitoringMessageEventChanged(this, true);
79   }
80                            
81                            
82   //
83
// -- PUBLIC METHODS -----------------------------------------------
84
//
85

86   public String JavaDoc 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 JavaDoc 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   //
147
// Event Listener
148
//
149

150   public void registerListener(ActiveObjectListener listener) {
151     this.messageMonitoringListener = listener;
152     this.listener = listener;
153   }
154   
155   
156   //
157
// Accessor methods
158
//
159

160   public String JavaDoc getName() {
161     return name;
162   }
163
164   public String JavaDoc 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   //
182
// -- implements MessageMonitoringController -----------------------------------------------
183
//
184

185   public void monitorRequestReceiver(boolean shouldMonitor) {
186     if (isDestroyed) return;
187     if (monitoringRequestReceiver == shouldMonitor) return; // already doing the right thing
188
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; // already doing the right thing
204
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; // already doing the right thing
220
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; // already doing the right thing
236
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   //
250
// -- PROTECTED METHODS -----------------------------------------------
251
//
252

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   //
265
// -- PRIVATE METHODS -----------------------------------------------
266
//
267

268   
269   private static String JavaDoc shortClassName(String JavaDoc 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   //
277
// -- PRIVATE STATIC METHODS -----------------------------------------------
278
//
279

280   private static int counter = 1;
281   private static java.util.HashMap JavaDoc knownActiveObjectCache = new java.util.HashMap JavaDoc();
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       // not cached
291
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; // 5mn
303
java.util.Collection JavaDoc values = knownActiveObjectCache.values();
304     java.util.Iterator JavaDoc iterator = values.iterator();
305     while (iterator.hasNext()) {
306       CachedActiveObject cachedObject = (CachedActiveObject) iterator.next();
307       if (cachedObject.destroyed && cachedObject.timestamp < oldestAcceptableTime) {
308         // remove from the cache
309
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