KickJava   Java API By Example, From Geeks To Geeks.

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


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
34
35 import org.apache.log4j.Logger;
36 import org.objectweb.proactive.Body;
37 import org.objectweb.proactive.EndActive;
38 import org.objectweb.proactive.InitActive;
39 import org.objectweb.proactive.ProActive;
40 import org.objectweb.proactive.RunActive;
41
42 /**
43  * This class represents a migratable Agent
44  */

45 public class Agent implements InitActive, RunActive, EndActive, java.io.Serializable JavaDoc {
46     
47     static Logger logger = Logger.getLogger(Agent.class.getName());
48   private String JavaDoc name;
49   private String JavaDoc nodename;
50   private String JavaDoc hostname;
51
52   public Agent() {
53   }
54
55   public Agent(String JavaDoc name) {
56     this.name = name;
57   }
58
59   public String JavaDoc getName() {
60     try {
61       //System.out.println("getName called");
62
//return the name of the Host
63
return java.net.InetAddress.getLocalHost().getCanonicalHostName().toUpperCase();
64     } catch (Exception JavaDoc e) {
65       e.printStackTrace();
66       return "getName failed";
67     }
68
69   }
70
71   public String JavaDoc getNodeName() {
72     try {
73       //System.out.println("getNodeName called");
74
//return the name of the Node
75
return ProActive.getBodyOnThis().getNodeURL().toUpperCase();
76     } catch (Exception JavaDoc e) {
77       e.printStackTrace();
78       return "getNodeName failed";
79     }
80   }
81
82   public void moveTo(String JavaDoc nodeURL) throws Exception JavaDoc{
83     //try {
84
logger.info(" I am going to migate");
85       ProActive.migrateTo(nodeURL);
86      // System.out.println("migration done");
87
// } catch (Exception e) {
88
// e.printStackTrace();
89
// }
90
}
91
92   public void endBodyActivity() {
93     ProActive.getBodyOnThis().terminate();
94   }
95
96   public void initActivity(Body body) {
97     logger.info("Initialization of the Activity");
98   }
99
100   public void runActivity(Body body) {
101     org.objectweb.proactive.Service service = new org.objectweb.proactive.Service(body);
102     while (body.isActive()) {
103       // The synchro policy is FIFO
104
service.blockingServeOldest();
105       // The synchro policy is LIFO uncomment the following lines
106
//service.waitForRequest();
107
//System.out.println(" I am going to serve " + service.getYoungest().getMethodName());
108
//service.serveYoungest();
109
//System.out.println("served");
110
}
111   }
112
113   public void endActivity(Body body) {
114     logger.info("End of the activity of this Active Object");
115   }
116 }
117
Popular Tags