KickJava   Java API By Example, From Geeks To Geeks.

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


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.ActiveObjectCreationException;
34 import org.objectweb.proactive.core.node.Node;
35 import org.objectweb.proactive.core.node.NodeException;
36 import org.objectweb.proactive.core.util.UrlBuilder;
37 import org.objectweb.proactive.ic2d.event.HostObjectListener;
38 import org.objectweb.proactive.ic2d.util.HostNodeFinder;
39 import org.objectweb.proactive.ic2d.util.IbisHostNodeFinder;
40 import org.objectweb.proactive.ic2d.util.RMIHostNodeFinder;
41 import org.objectweb.proactive.ic2d.util.RunnableProcessor;
42
43
44 /**
45  * Holder class for the host data representation
46  */

47 public class HostObject extends AbstractDataObject {
48
49     /** Name of this Host */
50     protected String JavaDoc hostname;
51
52     /** OS */
53     protected String JavaDoc os;
54     protected HostNodeFinder nodeFinder;
55     protected HostObjectListener listener;
56
57     //
58
// -- CONSTRUCTORS -----------------------------------------------
59
//
60
public HostObject(WorldObject parent, String JavaDoc hostname, String JavaDoc protocol) {
61         super(parent);
62         // Test if there is port defined, then remove it to see if hostname exists
63
try {
64             String JavaDoc shortHostname = java.net.InetAddress.getByName(UrlBuilder.removePortFromHost(hostname))
65                                                        .getHostName();
66
67             this.hostname = hostname;
68
69             //controller.log("HostObject "+this.hostname+ " created");
70
} catch (java.net.UnknownHostException JavaDoc e) {
71             this.hostname = hostname;
72             controller.warn("Hostname " + hostname + " failed reverse lookup.");
73         }
74         if ("ibis".equals(protocol)) {
75             this.nodeFinder = new IbisHostNodeFinder(controller);
76         } else {
77             this.nodeFinder = new RMIHostNodeFinder(controller);
78         }
79     }
80
81     //
82
// -- PUBLIC METHODS -----------------------------------------------
83
//
84
public String JavaDoc toString() {
85         return "Host: " + hostname + "\n" + super.toString();
86     }
87
88     public void createAllNodes() {
89         RunnableProcessor.getInstance().processRunnable("Create nodes for " +
90             hostname, new CreateNodeTask(), controller);
91     }
92
93     public void createOneNode(String JavaDoc nodeName) {
94         RunnableProcessor.getInstance().processRunnable("Create one node for " +
95             hostname, new CreateNodeTask(nodeName), controller);
96     }
97
98     //
99
// accessor methods
100
//
101
public String JavaDoc getHostName() {
102         return hostname;
103     }
104
105     public String JavaDoc getOperatingSystem() {
106         return os;
107     }
108
109     // public boolean isGlobusEnabled() {
110
// if (this.hostname.indexOf("globus") != -1) return true;
111
// return false;
112
// }
113
//
114
// Event Listener
115
//
116
public void registerListener(HostObjectListener listener) {
117         this.messageMonitoringListener = listener;
118         this.listener = listener;
119     }
120
121     //
122
// VM related methods
123
//
124

125     /**
126      * Register the node
127      */

128     public VMObject addVMObject(Node node) {
129         java.rmi.dgc.VMID JavaDoc vmid = node.getNodeInformation().getVMID();
130         String JavaDoc protocolId = node.getNodeInformation().getCreationProtocolID();
131         VMObject vmObject = getVMObject(vmid);
132         if (vmObject != null) {
133             controller.log("The node " + node.getNodeInformation().getURL() +
134                 " belongs to an already existing vm id=" + vmid);
135             // add the node to the existing vm in case it doesn't exist
136
vmObject.addNodeObject(node);
137             // refresh ActiveObject for this vm
138
vmObject.sendEventsForAllActiveObjects();
139             return vmObject;
140         }
141         try {
142             vmObject = new VMObject(this, vmid, node, protocolId);
143             putChild(vmid, vmObject);
144             controller.log("The node " + node.getNodeInformation().getURL() +
145                 " has been found on vm id=" + vmid);
146             if (listener != null) {
147                 listener.vmObjectAdded(vmObject);
148             }
149             if (os == null) {
150                 os = vmObject.getSystemProperty("os.name");
151                 if (listener != null) {
152                     listener.operatingSystemFound(os);
153                 }
154             }
155             return vmObject;
156         } catch (ActiveObjectCreationException e) {
157             controller.log("Cannot create the spy on host " + hostname +
158                 " on node " + node.getNodeInformation().getURL(), e);
159             return null;
160         } catch (NodeException e) {
161             controller.log("Problem with the node " +
162                 node.getNodeInformation().getURL(), e);
163             return null;
164         }
165     }
166
167     public void removeVMObject(java.rmi.dgc.VMID JavaDoc id) {
168         VMObject vmObject = (VMObject) removeChild(id);
169         if ((vmObject != null) && (listener != null)) {
170             listener.vmObjectRemoved(vmObject);
171         }
172     }
173
174     public VMObject getVMObject(java.rmi.dgc.VMID JavaDoc id) {
175         return (VMObject) getChild(id);
176     }
177
178     public void destroyObject() {
179         getTypedParent().removeHostObject(hostname);
180     }
181
182     public synchronized VMObject findVMObjectHavingExistingNode(String JavaDoc nodeName) {
183         if (getChildObjectsCount() == 0) {
184             return null;
185         }
186         java.util.Iterator JavaDoc iterator = childsIterator();
187         while (iterator.hasNext()) {
188             VMObject vmObject = (VMObject) iterator.next();
189             if (vmObject.getNodeObject(nodeName) != null) {
190                 controller.log("Found that vm id=" + vmObject.getID() +
191                     " own the node " + nodeName);
192                 return vmObject;
193             }
194         }
195         return null;
196     }
197
198     //
199
// -- PROTECTED METHOD -----------------------------------------------
200
//
201
protected WorldObject getTypedParent() {
202         return (WorldObject) parent;
203     }
204
205     protected synchronized boolean destroy() {
206         // destroy all childs
207
if (super.destroy()) {
208             // remove ref on other object
209
listener = null;
210             nodeFinder = null;
211             return true;
212         } else {
213             return false;
214         }
215     }
216
217     //
218
// -- INNER CLASSES -----------------------------------------------
219
//
220
private class CreateNodeTask implements Runnable JavaDoc {
221         private String JavaDoc targetNodeName;
222
223         public CreateNodeTask() {
224         }
225
226         public CreateNodeTask(String JavaDoc targetNodeName) {
227             this.targetNodeName = targetNodeName;
228         }
229
230         public void run() {
231             Node[] nodes;
232             try {
233                 nodes = nodeFinder.findNodes(hostname);
234                 // System.out.println("XXXXXXX");
235
} catch (java.io.IOException JavaDoc e) {
236                 controller.log("There is no RMI Registry on host " + hostname, e);
237                 return;
238             }
239             if (nodes.length == 0) {
240                 controller.warn("A RMIRegistry has been found on host " +
241                     hostname + " but no Node object are bound !");
242             }
243             for (int i = 0; i < nodes.length; i++) {
244                 Node node = nodes[i];
245
246                 //System.out.println("nodeURL "+node.getNodeInformation().getURL());
247
String JavaDoc nodeName = node.getNodeInformation().getName();
248                 if ((targetNodeName == null) ||
249                         targetNodeName.equals(nodeName)) {
250                     VMObject vmObject = findVMObjectHavingExistingNode(nodeName);
251                     if (vmObject == null) {
252                         // new NodeObject
253
addVMObject(node);
254                     } else {
255                         controller.log("The node " + nodeName +
256                             " is already known by host " + hostname +
257                             " look for new objects");
258                         vmObject.sendEventsForAllActiveObjects();
259                     }
260                 }
261             }
262             if ((targetNodeName != null) &&
263                     (findVMObjectHavingExistingNode(targetNodeName) == null)) {
264                 controller.warn("The node " + targetNodeName +
265                     " was not found on host " + hostname +
266                     ". Check the name of the node");
267             }
268         }
269     }
270      // end inner class CreateNodeTask
271
}
272
Popular Tags