KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.rmi.dgc.VMID JavaDoc;
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 /**
52  * Holder class for the host data representation
53  */

54 public class VMObject extends AbstractDataObject {
55     
56     static Logger log4jlogger = Logger.getLogger(VMObject.class.getName());
57
58   private static String JavaDoc SPY_LISTENER_NODE_NAME = "SpyListenerNode";
59   private static Node SPY_LISTENER_NODE;
60   static {
61     String JavaDoc currentHost;
62     try {
63       currentHost = java.net.InetAddress.getLocalHost().getCanonicalHostName();
64     } catch (java.net.UnknownHostException JavaDoc e) {
65       currentHost = "localhost";
66     }
67     //System.out.println("current host: "+currentHost);
68
try {
69         // TODO add security here
70
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 JavaDoc vmid;
78   protected String JavaDoc protocolId;
79   protected java.util.HashMap JavaDoc objectNodeMap;
80
81   protected SpyListenerImpl activeSpyListener;
82
83   protected VMObjectListener listener;
84   
85   //
86
// -- CONSTRUCTORS -----------------------------------------------
87
//
88

89   public VMObject(HostObject host, VMID JavaDoc vmid, Node node, String JavaDoc protocolId) throws ActiveObjectCreationException, NodeException {
90     super(host);
91     //System.out.println("nodeURL : "+node.getNodeInformation().getURL());
92
if (log4jlogger.isDebugEnabled()){
93     log4jlogger.debug ("VMObject.<init>");
94     }
95     this.vmid = vmid;
96     this.protocolId = protocolId;
97     this.objectNodeMap = new java.util.HashMap JavaDoc();
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 JavaDoc[] {activeSpyListener} , node);
107     addNodeObject(node);
108     controller.log("VMObject id="+vmid+" created based on node "+node.getNodeInformation().getURL());
109   }
110
111
112   //
113
// -- PUBLIC METHOD -----------------------------------------------
114
//
115

116
117   public String JavaDoc toString() {
118     return "VM id=" + vmid + "\n" + super.toString();
119   }
120   
121   
122
123   //
124
// Event Listener
125
//
126

127   public void registerListener(VMObjectListener listener) {
128     this.messageMonitoringListener = listener;
129     this.listener = listener;
130     // notify existing childs
131
notifyListenerOfExistingChilds();
132     sendEventsForAllActiveObjects();
133   }
134   
135   
136   //
137
// Accessor methods
138
//
139

140   public void migrateTo(UniqueID objectID, String JavaDoc nodeTargetURL) throws MigrationException {
141     try {
142       spy.migrateTo(objectID, nodeTargetURL);
143     } catch (MigrationException e) {
144       throw e;
145     } catch (Exception JavaDoc e) {
146       recoverExceptionInSpy(e);
147       throw new MigrationException("Problem contacting the Spy", e);
148     }
149   }
150
151   
152   public VMID JavaDoc getID() {
153     return vmid;
154   }
155   
156   public String JavaDoc getProtocolId(){
157     return this.protocolId;
158   }
159   
160   public int getActiveObjectsCount() {
161     return objectNodeMap.size();
162   }
163
164   
165   public String JavaDoc getSystemProperty(String JavaDoc key) {
166     try {
167       return spy.getSystemProperty(key);
168     } catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc e) {
201       recoverExceptionInSpy(e);
202     }
203   }
204
205
206
207   //
208
// Node related methods
209
//
210

211   public NodeObject addNodeObject(Node node) {
212     if (log4jlogger.isDebugEnabled()){
213     log4jlogger.debug("VMObject: addNodeObject()");
214     }
215     String JavaDoc 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 JavaDoc 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 JavaDoc nodeName) {
238     // remove the node
239
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   //
254
// -- PROTECTED METHOD -----------------------------------------------
255
//
256

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 JavaDoc 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 JavaDoc e) {
292       recoverExceptionInSpy(e);
293     }
294   }
295   
296   
297   protected HostObject getTypedParent() {
298     return (HostObject)parent;
299   }
300   
301   
302
303   //
304
// -- PRIVATE METHOD -----------------------------------------------
305
//
306

307   private void recoverExceptionInSpy(Exception JavaDoc 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; // unknown node
318
}
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 JavaDoc iterator = childsIterator();
330     while (iterator.hasNext()) {
331       NodeObject nodeObject = (NodeObject) iterator.next();
332       listener.nodeObjectAdded(nodeObject);
333     }
334   }
335   
336
337 // private String getNodeNameFromURL(String nodeURL) {
338
// int n = nodeURL.indexOf('/', 2); // looking for the end of the host
339
// if (n < 3) return nodeURL;
340
// return nodeURL.substring(n+1);
341
// }
342

343   
344   //
345
// -- INNER CLASSES -----------------------------------------------
346
//
347

348
349   private class MySpyEventListener implements SpyEventListener {
350
351     private CommunicationEventListener communicationEventListener;
352     
353     public MySpyEventListener() {
354       communicationEventListener = ((IC2DObject)getTopLevelParent()).getCommunicationEventListener();
355     }
356
357     //
358
// -- Implement SpyEventListener -----------------------------------------------
359
//
360

361     public void activeObjectAdded(UniqueID id, String JavaDoc nodeURL, String JavaDoc classname, boolean isActive) {
362       //String nodeName = getNodeNameFromURL(nodeURL);
363
String JavaDoc nodeName = UrlBuilder.getNameFromUrl(nodeURL);
364       //System.out.println("NodeName "+nodeName+" AO id "+id);
365
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       //System.out.println("activeObjectChanged object="+object.getName()+" isActive="+isActive);
374
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   } // end inner class MySpyEventListener
472

473
474
475 }
476
Popular Tags