1 26 27 28 import org.objectweb.mobilitools.smi.api.Agency; 29 import org.objectweb.mobilitools.smi.api.*; 30 import org.objectweb.mobilitools.smi.goodies.RegionManager; 31 32 33 53 public class ThreadAgent implements MobileObject, Runnable 54 { 55 56 protected Name my_name; 57 58 protected transient Agency my_agency; 59 60 protected volatile Location[] itinerary; 61 62 protected int pause = 0; 63 64 protected int step; 65 66 protected boolean dead = false; 67 68 protected boolean suspended = false; 69 70 71 public ThreadAgent() 72 { 73 } 74 75 76 80 protected synchronized void resetItinerary() 81 { 82 try 83 { 84 itinerary = (new RegionManager(my_agency.getORB())).listAgencies(my_agency.getRegion(), null); 85 step = 0; 86 } 87 catch (BadOperation ex) 88 { 89 System.out.println(my_name + " could not update itinerary - suspending activity"); 90 try 91 { 92 my_agency.suspendLocalAgent(this); 93 } 94 catch (BadOperation ex2) 95 { 96 System.err.println("Warning: could not suspend agent " + my_name + ":\n" + ex2); 97 } 98 } 99 } 100 101 102 105 protected void runActivity() 106 { 107 (new Thread (this, my_name.toString())).start(); 108 } 109 110 111 115 116 120 public void run() 121 { 122 try 123 { 124 Thread.sleep(pause); 125 } 126 catch (InterruptedException e) 127 { 128 System.err.println("Warning: interrupted sleep for " + my_name); 129 } 130 synchronized (this) 131 { 132 if (! (dead || suspended)) 133 { 134 if (itinerary.length == 1) 135 { 136 resetItinerary(); 137 runActivity(); 138 } 139 else 140 { 141 if (step == itinerary.length) 142 { 143 step = 0; 144 } 145 try 146 { 147 my_agency.moveLocalAgent(this, itinerary[step++], "a_place"); 148 } 149 catch (BadOperation e) 150 { 151 System.err.println(my_name + ": " + e.getMessage()); 152 resetItinerary(); 153 runActivity(); 154 } 155 } 156 } 157 } 158 } 159 160 161 165 166 172 public void afterBirth(AgentSystem agency, AgentInfo entry, Object arg) 173 throws BadOperation 174 { 175 my_agency = (Agency)agency; 176 my_name = entry.getName(); 177 if (arg instanceof String []) 178 { 179 String [] argStr = (String [])arg; 180 if (argStr.length != 0) 181 { 182 try 183 { 184 pause = Integer.parseInt(argStr[0]); 185 } 186 catch (NumberFormatException e) 187 { 188 throw new BadOperation(BadOperation.REJECTED, "argument should be an String-formated integer", e); 189 } 190 } 191 } 192 resetItinerary(); 193 runActivity(); 194 } 195 196 197 200 public synchronized void beforeSuspend() 201 { 202 System.out.println(my_name + " is suspended"); 203 suspended = true; 204 } 205 206 207 211 public synchronized void beforeResume() 212 throws BadOperation 213 { 214 suspended = false; 215 resetItinerary(); 216 runActivity(); 217 } 218 219 220 223 public void beforeMove(Location dummy1, String dummy2) 224 { 225 } 226 227 228 231 public void afterMove(AgentSystem agency, Location dummy1, String dummy2) 232 { 233 my_agency = (Agency)agency; 234 runActivity(); 235 } 236 237 238 241 public void afterMoveFailed( 242 Location dummy1, 243 String dummy2, 244 int dummy3, 245 String dummy4) 246 { 247 } 248 249 250 253 public synchronized void beforeDeath() 254 { 255 dead = true; 256 System.out.println(my_name + " is dead."); 257 } 258 259 260 263 public void beforeShutdown() 264 { 265 } 266 } 267 | Popular Tags |