1 24 package fr.dyade.aaa.agent; 25 26 import java.io.*; 27 import fr.dyade.aaa.util.*; 28 29 40 final class AgentIdStamp implements Serializable { 41 42 static AgentIdStamp stamp = null; 43 44 45 private int local; 46 47 private int remote; 48 49 55 static void init() 56 throws IOException, ClassNotFoundException { 57 stamp = load(); 58 if (stamp == null) { 59 stamp = new AgentIdStamp(); 60 stamp.save(); 61 } 62 } 63 64 AgentIdStamp() { 65 local = AgentId.MaxIdStamp; 66 remote = AgentId.MaxIdStamp; 67 } 68 69 72 void save() throws IOException { 73 AgentServer.getTransaction().save(this, "AgentIdStamp"); 74 } 75 76 79 static AgentIdStamp 80 load() throws IOException, ClassNotFoundException { 81 return (AgentIdStamp) AgentServer.getTransaction().load("AgentIdStamp"); 82 } 83 84 91 private void writeObject(java.io.ObjectOutputStream out) 92 throws IOException { 93 out.writeInt(local); 94 out.writeInt(remote); 95 } 96 97 103 private void readObject(java.io.ObjectInputStream in) 104 throws IOException, ClassNotFoundException { 105 local = in.readInt(); 106 remote = in.readInt(); 107 } 108 109 114 synchronized int newStamp(short to) throws IOException { 115 int current = (to == AgentServer.getServerId())?(++local):(++remote); 116 save(); 117 return current; 118 } 119 } 120 121 155 public final class AgentId implements Serializable { 156 static final long serialVersionUID = -5096976989176739863L; 157 158 161 162 transient short from; 163 164 transient short to; 165 166 transient int stamp; 167 168 172 transient String str = null; 173 174 181 private void writeObject(java.io.ObjectOutputStream out) 182 throws IOException { 183 out.writeShort(from); 184 out.writeShort(to); 185 out.writeInt(stamp); 186 } 187 188 194 private void readObject(java.io.ObjectInputStream in) 195 throws IOException, ClassNotFoundException { 196 from = in.readShort(); 197 to = in.readShort(); 198 stamp = in.readInt(); 199 } 200 201 205 206 public static final int NullIdStamp = 0; 207 208 public static final int FactoryIdStamp = 1; 209 210 public static final int AdminIdStamp = 2; 211 212 public static final int MaxSystemIdStamp = 2; 213 214 218 219 public static int MinWKSIdStamp = MaxSystemIdStamp + 1; 220 221 public static int NameServiceStamp = 4; 222 223 public static int SchedulerServiceStamp = 5; 224 225 public static int FileTransfertStamp = 6; 226 227 public static int JndiServiceStamp = 7; 228 229 public static int LocalJndiServiceStamp = 8; 230 231 public static int SCAdminProxyStamp = 9; 232 233 public static int JoramAdminStamp = 10; 234 235 public static int JoramAdminPxStamp = 11; 236 237 public static int ControlTopicStamp = 12; 238 239 public static int ServerConfigStamp = 13; 240 241 public static int MaxWKSIdStamp = 1024; 242 243 public static int MaxIdStamp = MaxWKSIdStamp; 244 245 249 250 public final static AgentId nullId = new AgentId((short) 0, 251 (short) 0, 252 NullIdStamp); 253 261 static AgentId localId; 262 266 static AgentId factoryId; 267 271 public static AgentId adminId; 272 273 279 public final static AgentId factoryId(short sid) { 280 return new AgentId(sid, sid, FactoryIdStamp); 281 } 282 283 public final static AgentId localId(short sid) { 284 return new AgentId(sid, sid, NullIdStamp); 285 } 286 287 290 static void init() 291 throws IOException, ClassNotFoundException { 292 localId = new AgentId(AgentServer.getServerId(), 294 AgentServer.getServerId(), 295 NullIdStamp); 296 factoryId = new AgentId(AgentServer.getServerId(), 297 AgentServer.getServerId(), 298 FactoryIdStamp); 299 adminId = new AgentId(AgentServer.getServerId(), 300 AgentServer.getServerId(), 301 AdminIdStamp); 302 AgentIdStamp.init(); 304 } 305 306 310 AgentId() throws IOException { 311 this(AgentServer.getServerId()); 312 } 313 314 320 AgentId(short to) throws IOException { 321 this(AgentServer.getServerId(), to, AgentIdStamp.stamp.newStamp(to)); 322 } 323 324 332 public AgentId(short from, short to, int stamp) { 333 this.from = from; 334 this.to = to; 335 this.stamp = stamp; 336 } 337 338 341 public final short getFrom() { 342 return from; 343 } 344 345 348 public final short getTo() { 349 return to; 350 } 351 352 355 public final int getStamp() { 356 return stamp; 357 } 358 359 371 public static final int parseInt(String str, 372 int idx, 373 int end) throws NumberFormatException { 374 int result = 0; 375 int digit; 376 int limit = Integer.MAX_VALUE / 10; 377 int digitzero = '0'; 378 379 while (idx < end) { 380 digit = str.charAt(idx++) - digitzero; 381 if ((digit < 0) || (digit > 9)) 382 throw new NumberFormatException ("bad digit"); 383 384 if (result >= limit) 385 throw new NumberFormatException ("limit"); 386 387 result *= 10; 388 result += digit; 389 } 390 391 return result; 392 } 393 394 399 public static final AgentId fromString(String str) { 400 if (str == null) return null; 401 if (str.charAt(0) != '#') 402 throw new IllegalArgumentException (str + ": bad id"); 403 404 try { 405 int start = 1; 406 int end = str.indexOf('.', start); 407 short from = (short) parseInt(str, start, end); 408 start = end +1; 409 end = str.indexOf('.', start); 410 short to = (short) parseInt(str, start, end); 411 start = end +1; 412 end = str.length(); 413 int stamp = parseInt(str, start, end); 414 415 return new AgentId(from, to, stamp); 416 } catch (Exception exc) { 417 throw new IllegalArgumentException (str + ": " + exc); 418 } 419 } 420 421 426 public final String toString() { 427 if (str == null) 428 str = StringId.toStringId('#', '.', from, to, stamp); 429 return str; 430 } 431 432 438 public int hashCode() { 439 return stamp; 440 } 441 442 447 public final boolean isNullId() { 448 return (stamp == NullIdStamp); 449 } 450 451 460 public boolean equals(Object obj) { 461 if ((obj instanceof AgentId) && 462 (((AgentId) obj).from == from) && 463 (((AgentId) obj).to == to) && 464 (((AgentId) obj).stamp == stamp)) { 465 return true; 466 } else { 467 return false; 468 } 469 } 470 } 471 | Popular Tags |