1 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 { 42 43 private boolean onItinerary,initialized; 44 private transient PenguinFrame myFrame; 45 private PenguinMessageReceiver controler; 46 private Penguin otherPenguin; 47 private javax.swing.ImageIcon 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 name; 52 private String [] itinerary; 53 54 55 58 public Penguin() { 59 } 60 61 62 public Penguin(Integer 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 sendMessageToControler("I just got in node "+ProActive.getBodyOnThis().getNodeURL()); 78 try { 79 Thread.sleep(2000); 80 } catch (InterruptedException 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 toString() { 94 return this.name; 95 } 96 97 98 public void initialize(String [] s) { 99 try { 100 ClassLoader cl = this.getClass().getClassLoader(); 103 java.net.URL u = cl.getResource("org/objectweb/proactive/examples/penguin/PenguinSmall.jpg"); 104 face = new javax.swing.ImageIcon (u); 105 } catch (Exception 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 while (b.isActive()) { 148 while (service.hasRequestToServe()) { 149 service.serveOldest(); 150 } 151 if (onItinerary) { 152 r = nextHop(); 153 try { 154 if (r != null && !NodeFactory.isNodeLocal(NodeFactory.getNode(r.getDestination()))) 156 ProActive.migrateTo(r.getDestination()); 157 } catch (Exception e) { 158 e.printStackTrace(); 159 } 160 } else { 161 service.blockingServeOldest(); 162 } 163 } 164 } 165 166 167 public String [] getItinerary() { 168 return itinerary; 169 } 170 171 172 public void setItinerary(String [] 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 (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 (0, 150, 0)); 203 onItinerary = true; 204 } 205 } 206 207 208 public String 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 message) { 224 if (controler != null) 225 controler.receiveMessage("["+name+"] : "+message); 226 } 227 228 229 private void sendMessageToControler(String message, java.awt.Color color) { 230 if (controler != null) 231 controler.receiveMessage("[" + name + "] : " + message, color); 232 } 233 234 235 public static void main(String [] 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 n.initialize(args); 245 Object [] param = new Object [1]; 248 param[0] = n; 249 org.objectweb.proactive.ProActive.newActive(PenguinMessageReceiver.class.getName(), param, (org.objectweb.proactive.core.node.Node) null); 250 } catch (Exception e) { 254 e.printStackTrace(); 255 } 256 } 257 } 258 | Popular Tags |