KickJava   Java API By Example, From Geeks To Geeks.

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


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.core.config.ProActiveConfiguration;
36 import org.objectweb.proactive.core.descriptor.data.ProActiveDescriptor;
37 import org.objectweb.proactive.core.descriptor.data.VirtualNode;
38 import org.objectweb.proactive.core.util.CircularArrayList;
39 import org.objectweb.proactive.ext.migration.MigrationStrategyManagerImpl;
40
41
42 public class PenguinControler implements org.objectweb.proactive.RunActive, PenguinMessageReceiver, java.io.Serializable JavaDoc {
43
44   //The image panel
45
private transient PenguinApplet display;
46   protected CircularArrayList penguinList;
47   String JavaDoc[] args;
48   private MigrationStrategyManagerImpl myStrategyManager;
49
50
51   public PenguinControler() {
52   }
53
54
55   public PenguinControler(String JavaDoc[] args) {
56     this.penguinList = new CircularArrayList(20);
57     this.args = args;
58     try {
59       ProActive.turnActive(this);
60     } catch (Exception JavaDoc e) {
61       e.printStackTrace();
62     }
63   }
64
65
66   public void rebuild() {
67     this.display = new PenguinApplet((PenguinControler) ProActive.getStubOnThis(), penguinList);
68   }
69
70
71   public void clean() {
72     if (display != null) {
73       display.dispose();
74       display = null;
75     }
76   }
77
78
79   public void receiveMessage(String JavaDoc s) {
80     display.receiveMessage(s);
81   }
82
83
84   public void receiveMessage(String JavaDoc s, java.awt.Color JavaDoc c) {
85     display.receiveMessage(s, c);
86   }
87
88   public Penguin createPenguin(int n) {
89     try {
90       Penguin newPenguin = (Penguin) org.objectweb.proactive.ProActive.newActive(Penguin.class.getName(), new Object JavaDoc[] { new Integer JavaDoc(n) });
91       newPenguin.initialize(args);
92       newPenguin.setControler((PenguinControler) ProActive.getStubOnThis());
93       return newPenguin;
94     } catch (Exception JavaDoc e) {
95       e.printStackTrace();
96       return null;
97     }
98   }
99
100   public void runActivity(Body b) {
101     org.objectweb.proactive.Service service = new org.objectweb.proactive.Service(b);
102     myStrategyManager = new MigrationStrategyManagerImpl((org.objectweb.proactive.core.body.migration.Migratable) b);
103     myStrategyManager.onDeparture("clean");
104     rebuild();
105     service.fifoServing();
106     clean();
107   }
108
109
110 //<<<<<<< PenguinControler.java
111
public static void main(String JavaDoc args[]) {
112     ProActiveConfiguration.load();
113     // Version without descriptor
114
// try {
115
// // ProActive.newActive(AdvancedPenguinControler.class.getName(),null,(Node) null);
116
// new PenguinControler(args);
117
// } catch (Exception e) {
118
// e.printStackTrace();
119
// }
120

121         // Version with descriptor
122
ProActiveDescriptor proActiveDescriptor=null;
123         try{
124         proActiveDescriptor=ProActive.getProactiveDescriptor("file:"+args[0]);
125         proActiveDescriptor.activateMappings();
126         VirtualNode vn1 = proActiveDescriptor.getVirtualNode("penguinNode");
127         //Thread.sleep(15000);
128
String JavaDoc[] nodes = vn1.getNodesURL();
129             new PenguinControler(nodes);
130     }catch(Exception JavaDoc e){
131     e.printStackTrace();
132     }
133     
134   }
135
136 // public static void main(String args[]) {
137
// //args modification to replace relative name of nodes to them absolute name
138
// try {
139
// java.net.InetAddress localhost = java.net.InetAddress.getLocalHost();
140
// for (int i=0; i<args.length; i++) {
141
// if (args[i].startsWith("//localhost")) {
142
// String nodeName;
143
// int index = args[i].lastIndexOf('/');
144
// if (index > 0 && index < args[i].length() - 1) {
145
// nodeName = args[i].substring(index + 1);
146
// args[i] = "//" + localhost.getHostName() + "/" + nodeName;
147
// }
148
// }
149
// }
150
// } catch (java.net.UnknownHostException e) {
151
// e.printStackTrace();
152
// }
153
// try {
154
// // ProActive.newActive(AdvancedPenguinControler.class.getName(),null,(Node) null);
155
// new PenguinControler(args);
156
// } catch (Exception e) {
157
// e.printStackTrace();
158
// }
159
// }
160
//>>>>>>> 1.6
161
}
162
Popular Tags