KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > penguin > Penguin


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.penguin;
32
33 import org.objectweb.proactive.Body;
34 import org.objectweb.proactive.ProActive;
35 import org.objectweb.proactive.Service;
36 import org.objectweb.proactive.core.config.ProActiveConfiguration;
37 import org.objectweb.proactive.core.node.NodeFactory;
38 import org.objectweb.proactive.ext.migration.Destination;
39 import org.objectweb.proactive.ext.migration.MigrationStrategyImpl;
40
41 public class Penguin implements org.objectweb.proactive.RunActive, java.io.Serializable JavaDoc {
42
43   private boolean onItinerary,initialized;
44   private transient PenguinFrame myFrame;
45   private PenguinMessageReceiver controler;
46   private Penguin otherPenguin;
47   private javax.swing.ImageIcon JavaDoc face;
48   private org.objectweb.proactive.ext.migration.MigrationStrategy myStrategy;
49   private org.objectweb.proactive.ext.migration.MigrationStrategyManager myStrategyManager;
50   private int index;
51   private String JavaDoc name;
52   private String JavaDoc[] itinerary;
53
54
55   /**
56    * Empty constructor for ProActive
57    */

58   public Penguin() {
59   }
60
61
62   public Penguin(Integer JavaDoc ind) {
63     this.index = ind.intValue();
64     this.name = "Agent " + index;
65   }
66
67
68   public void loop() {
69     rebuild();
70   }
71
72
73   public void rebuild() {
74     Body body = ProActive.getBodyOnThis();
75     myFrame = new PenguinFrame(face, body.getNodeURL(), index);
76     //System.out.println("Penguin is here");
77
sendMessageToControler("I just got in node "+ProActive.getBodyOnThis().getNodeURL());
78     try {
79       Thread.sleep(2000);
80     } catch (InterruptedException JavaDoc e) {
81     }
82   }
83
84
85   public void clean() {
86     if (myFrame != null) {
87       myFrame.dispose();
88       myFrame = null;
89     }
90   }
91
92
93   public String JavaDoc toString() {
94     return this.name;
95   }
96
97
98   public void initialize(String JavaDoc[] s) {
99     try {
100       //With this we can load the image from the same location than
101
//the classes
102
ClassLoader JavaDoc cl = this.getClass().getClassLoader();
103       java.net.URL JavaDoc u = cl.getResource("org/objectweb/proactive/examples/penguin/PenguinSmall.jpg");
104       face = new javax.swing.ImageIcon JavaDoc(u);
105     } catch (Exception JavaDoc e) {
106       e.printStackTrace();
107     }
108     myStrategyManager = new org.objectweb.proactive.ext.migration.MigrationStrategyManagerImpl(
109             (org.objectweb.proactive.core.body.migration.Migratable) org.objectweb.proactive.ProActive.getBodyOnThis());
110     myStrategyManager.onDeparture("clean");
111     myStrategy = new MigrationStrategyImpl();
112     itinerary = s;
113     for (int i = 0; i < s.length; i++)
114       myStrategy.add(s[i], null);
115   }
116
117
118   public void setControler(PenguinMessageReceiver c) {
119     this.controler = c;
120     this.initialized = true;
121   }
122
123
124   public void setOther(Penguin penguin) {
125     this.otherPenguin = penguin;
126   }
127
128
129   public Destination nextHop() {
130     Destination r = myStrategy.next();
131     if (r == null) {
132       myStrategy.reset();
133       r = myStrategy.next();
134     }
135     return r;
136   }
137
138
139   public void runActivity(Body b) {
140     Service service = new Service(b);
141     if (!initialized) {
142       service.blockingServeOldest();
143     }
144     rebuild();
145     Destination r = null;
146     //first we empty the RequestQueue
147
while (b.isActive()) {
148       while (service.hasRequestToServe()) {
149         service.serveOldest();
150       }
151       if (onItinerary) {
152         r = nextHop();
153         try {
154         //Non migration to the same node or a null node
155
if (r != null && !NodeFactory.isNodeLocal(NodeFactory.getNode(r.getDestination())))
156         ProActive.migrateTo(r.getDestination());
157         } catch (Exception JavaDoc e) {
158           e.printStackTrace();
159         }
160       } else {
161         service.blockingServeOldest();
162       }
163     }
164   }
165
166
167   public String JavaDoc[] getItinerary() {
168     return itinerary;
169   }
170
171
172   public void setItinerary(String JavaDoc[] newItinerary) {
173     sendMessageToControler("I changed my itinerary", java.awt.Color.blue);
174     myStrategy = new MigrationStrategyImpl();
175     itinerary = newItinerary;
176     for (int i = 0; i < newItinerary.length; i++)
177       myStrategy.add(newItinerary[i], null);
178   }
179
180
181   public void start() {
182     sendMessageToControler("I am starting my itinerary", new java.awt.Color JavaDoc(0, 150, 0));
183     myStrategy.reset();
184     onItinerary = true;
185   }
186
187
188   public void suspend() {
189     if (! onItinerary) {
190       sendMessageToControler("I'm already suspended");
191     } else {
192       sendMessageToControler("I suspended my itinerary", java.awt.Color.red);
193       onItinerary = false;
194     }
195   }
196
197
198   public void resume() {
199     if (onItinerary) {
200       sendMessageToControler("I'm already on my itinerary");
201     } else {
202       sendMessageToControler("I'm resuming my itinerary", new java.awt.Color JavaDoc(0, 150, 0));
203       onItinerary = true;
204     }
205   }
206
207
208   public String JavaDoc call() {
209     return "["+name+"] : I am working on node " + ProActive.getBodyOnThis().getNodeURL();
210   }
211
212
213   public void chainedCall() {
214     if (otherPenguin != null) {
215       sendMessageToControler("I'm calling my peer agent");
216       otherPenguin.chainedCall();
217     } else {
218       sendMessageToControler("I don't have a peer agent to call");
219     }
220   }
221
222
223   private void sendMessageToControler(String JavaDoc message) {
224     if (controler != null)
225       controler.receiveMessage("["+name+"] : "+message);
226   }
227
228
229   private void sendMessageToControler(String JavaDoc message, java.awt.Color JavaDoc color) {
230     if (controler != null)
231       controler.receiveMessage("[" + name + "] : " + message, color);
232   }
233
234
235   public static void main(String JavaDoc[] args) {
236     ProActiveConfiguration.load();
237     if (!(args.length > 1)) {
238      System.out.println("Usage: java org.objectweb.proactive.examples.penguin.Penguin hostname1/NodeName1 hostname2/NodeName2 ");
239       System.exit(-1);
240     }
241     try {
242       Penguin n = (Penguin) org.objectweb.proactive.ProActive.newActive(Penguin.class.getName(), null);
243       // Penguin n2 = (Penguin)org.objectweb.proactive.ProActive.newActive(Penguin.class.getName(), null);
244
n.initialize(args);
245       // n2.initialize(args);
246
// n.startItinerary();
247
Object JavaDoc[] param = new Object JavaDoc[1];
248       param[0] = n;
249       org.objectweb.proactive.ProActive.newActive(PenguinMessageReceiver.class.getName(), param, (org.objectweb.proactive.core.node.Node) null);
250       // n.setOther(n2);
251
// n2.setOther(null);
252
// n2.startItinerary();
253
} catch (Exception JavaDoc e) {
254       e.printStackTrace();
255     }
256   }
257 }
258
Popular Tags