KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > migration > SimpleObjectMigration


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.examples.migration;
32
33 import java.io.Serializable JavaDoc;
34 import java.net.InetAddress JavaDoc;
35 import java.net.UnknownHostException JavaDoc;
36
37 import org.apache.log4j.Logger;
38 import org.objectweb.proactive.ActiveObjectCreationException;
39 import org.objectweb.proactive.ProActive;
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
44 /**
45  * Our 'hello' server without IHM
46  * This Active Object can migrate
47  * There is a lot of "trace" to locate the object
48  *
49  * @author ProActive trainee Team
50  * @version ProActive 1.0 (March 2002)
51  */

52 public class SimpleObjectMigration implements Serializable JavaDoc {
53     
54     static Logger logger = Logger.getLogger(SimpleObjectMigration.class.getName());
55
56   private static final int SLEEP_TIME = 9000;
57
58   private String JavaDoc name; // The name of the instance
59
private String JavaDoc hi = " say hello from "; // The 'hello' sentence
60

61   /**
62    * Creates a new <code>SimpleObjectMigration</code> instance.
63    *
64    */

65   public SimpleObjectMigration() {
66     logger.info("SimpleObjectMigration> Empty constructor");
67   }
68
69   /**
70    * Creates a new <code>SimpleObjectMigration</code> instance.
71    *
72    * @param name a <code>String</code> value who represents the name
73    of the instance
74   */

75   public SimpleObjectMigration(String JavaDoc name) {
76     logger.info("SimpleObjectMigration> Constructor with a parameter : " + name);
77     this.name = name;
78   }
79
80   /**
81    * Describe <code>sayHello</code> method here.
82    *
83    * @return a <code>String</code> value who is the 'hello' sentence
84    */

85   public String JavaDoc sayHello() {
86     logger.info("SimpleObjectMigration> sayHello()");
87     String JavaDoc localhost = null;
88     try {
89       localhost = InetAddress.getLocalHost().toString();
90     } catch (Exception JavaDoc e) {
91       e.printStackTrace();
92     }
93     String JavaDoc sentence = name + hi + localhost;
94     logger.info("SimpleObjectMigration> sayHello() --> " + sentence);
95     return sentence;
96   }
97
98   /**
99    * Describe <code>moveTo</code> method here.
100    * This methods is used to migrate the instance
101    *
102    * @param t The url of the destination node
103    */

104   public void moveTo(String JavaDoc t) {
105     try {
106
107       logger.info("SimpleObjectMigration> moveTo(" + t + ") " + "% start migration");
108       ProActive.migrateTo(t);
109       logger.info("SimpleObjectMigration> moveTo(" + t + ") " + "% stop migration");
110     } catch (Exception JavaDoc e) {
111       e.printStackTrace();
112     }
113   }
114
115   /**
116    * Describe <code>main</code> method here.
117    * It migrate a simple object from node1 to node2
118    *
119    *<ul>
120    * <li>param1 : The source node url
121    (like <code>jini://host1/JiniNode1</code>)<li>
122    * <li>param2 : The destination node url
123    (like <code>rmi://host2/RmiNode2</code>)<li>
124    *</ul>
125    * @param args the 2 parameters in an array
126    */

127   public static void main(String JavaDoc[] args) {
128     // The source node
129
String JavaDoc urlSourceNode = "";
130     Node sourceNode = null;
131
132     // The destination node
133
String JavaDoc urlDestinationNode = "";
134     Node destinationNode = null;
135
136     // we need 2 args to migrate
137
if (args.length == 2) {
138       urlSourceNode = args[0];
139       urlDestinationNode = args[1];
140     } else {
141       logger.info("USAGE : java SimpleObjectMigration " + "urlSourceNode urlDestinationNode ");
142       logger.info("Example : java SimpleObjectMigration " + "rmi://host1/Mynode1 jini://host2/MyNode2 ");
143       System.exit(1);
144     }
145
146     System.out.println(
147       "SimpleObjectMigration> main() > "
148         + "We are going to try to migrate a simple object from "
149         + urlSourceNode
150         + " to "
151         + urlDestinationNode);
152
153     try {
154
155       logger.info("SimpleObjectMigration> main() > " + "we try to get the source node : " + urlSourceNode);
156
157       sourceNode = NodeFactory.getNode(urlSourceNode);
158
159       logger.info("SimpleObjectMigration> main() > " + "we obtain the source node : " + urlSourceNode);
160
161     } catch (NodeException e) {
162      logger.info(
163         "SimpleObjectMigration> main() > "
164           + "Exception during the getting of "
165           + " the source node "
166           + urlSourceNode
167           + " ("
168           + e.getMessage()
169           + ")");
170       e.printStackTrace();
171     }
172
173     try {
174
175       logger.info(
176         "SimpleObjectMigration> main() > " + "we try to get the destination node : " + urlDestinationNode);
177
178       destinationNode = NodeFactory.getNode(urlDestinationNode);
179
180       logger.info("SimpleObjectMigration> main() > " + "we obtain the destination node : " + urlDestinationNode);
181
182     } catch (NodeException e) {
183       System.out.println(
184         "SimpleObjectMigration> main() > "
185           + "Exception during the getting of "
186           + " the destination node "
187           + urlDestinationNode
188           + " ("
189           + e.getMessage()
190           + ")");
191       e.printStackTrace();
192     }
193
194     logger.info(
195       "SimpleObjectMigration> main() > " + "We shows the state before" + " to create the Active Object");
196     // We show the two nodes states
197
showIds(urlSourceNode, sourceNode, urlDestinationNode, destinationNode);
198
199     logger.info("SimpleObjectMigration> main() > " + "We try to create an simple active object");
200
201     // The active obect
202
SimpleObjectMigration activeHello = null;
203     try {
204       String JavaDoc className = SimpleObjectMigration.class.getName();
205       Object JavaDoc[] params = new Object JavaDoc[] { "Created by " + InetAddress.getLocalHost().toString()};
206
207       activeHello = (SimpleObjectMigration) ProActive.newActive(className, params, sourceNode);
208       logger.info("SimpleObjectMigration> main() > " + "We created an simple active object");
209
210       logger.info("SimpleObjectMigration> main() > " + "The simple active object want to say hello ;)");
211
212       String JavaDoc helloAnswer = activeHello.sayHello();
213
214       logger.info("SimpleObjectMigration> main() > " + "The simple active object said '" + helloAnswer + "'");
215
216     } catch (UnknownHostException JavaDoc e) {
217       logger.info(
218         "SimpleObjectMigration> main() > "
219           + "Exception during the creation of the active object"
220           + " ("
221           + e.getMessage()
222           + ")");
223       e.printStackTrace();
224     } catch (ActiveObjectCreationException e) {
225      logger.info(
226         "SimpleObjectMigration> main() > "
227           + "Exception during the creation of the active object"
228           + " ("
229           + e.getMessage()
230           + ")");
231       e.printStackTrace();
232     } catch (NodeException e) {
233       logger.info(
234         "SimpleObjectMigration> main() > "
235           + "Exception during the creation of the active object"
236           + " ("
237           + e.getMessage()
238           + ")");
239       e.printStackTrace();
240     }
241
242     logger.info(
243       "SimpleObjectMigration> main() > " + "We show the state before" + " the migration of the Active Object");
244     // We show the two nodes states
245
showIds(urlSourceNode, sourceNode, urlDestinationNode, destinationNode);
246
247     try {
248      logger.info("SimpleObjectMigration> main() > " + "begin sleep " + SLEEP_TIME + " ...");
249       Thread.sleep(SLEEP_TIME);
250       logger.info("SimpleObjectMigration> main() > " + "... end of sleep " + SLEEP_TIME);
251
252     } catch (InterruptedException JavaDoc e2) {
253     }
254
255     logger.info("SimpleObjectMigration> main() > " + "migrate active object to " + urlDestinationNode);
256
257     logger.info(
258       "SimpleObjectMigration> main() > " + "We show the state after" + " the migration of the Active Object");
259
260     logger.info("");
261
262     // We migrate the Active Object
263
activeHello.moveTo(urlDestinationNode);
264
265     logger.info("");
266
267     try {
268       logger.info("SimpleObjectMigration> main() > " + "begin sleep " + SLEEP_TIME + " ...");
269       Thread.sleep(SLEEP_TIME);
270       logger.info("SimpleObjectMigration> main() > " + "... end of sleep " + SLEEP_TIME);
271
272     } catch (InterruptedException JavaDoc e2) {
273     }
274
275     // We show the two nodes states
276
showIds(urlSourceNode, sourceNode, urlDestinationNode, destinationNode);
277
278     logger.info("SimpleObjectMigration> main() > " + "The simple active object want to say hello ;)");
279
280     String JavaDoc helloAnswer = activeHello.sayHello();
281
282     logger.info("SimpleObjectMigration> main() > " + "The simple active object said '" + helloAnswer + "'");
283
284     logger.info("SimpleObjectMigration> main() > end of test");
285
286   }
287
288   protected static void showIds(
289     String JavaDoc urlSourceNode,
290     Node sourceNode,
291     String JavaDoc urlDestinationNode,
292     Node destinationNode) {
293     try {
294       logger.info("");
295       logger.info("SimpleObjectMigration> showIds() > ");
296       logger.info("-------- Ids on " + urlSourceNode + " ------");
297       /*
298       UniqueID[] ids = sourceNode.getActiveObjectIDs();
299       for (int j = 0; j < ids.length; j++) {
300         System.out.println("\t id" + j + " = " + ids[j]);
301       }
302       System.out.println("-----------------------------------------------");
303
304       System.out.println("");
305
306       System.out.println("-------- Ids on " + urlDestinationNode + " ------");
307       ids = destinationNode.getActiveObjectIDs();
308       for (int j = 0; j < ids.length; j++) {
309         System.out.println("\t id" + j + " = " + ids[j]);
310       }
311       System.out.println("-----------------------------------------------");
312       */

313     } catch (Exception JavaDoc e) {
314       logger.info(
315         "SimpleObjectMigration> showIds() > "
316           + "Exception during the of the node's state"
317           + " ("
318           + e.getMessage()
319           + ")");
320       e.printStackTrace();
321     }
322   }
323 }
324
Popular Tags