1 21 package fr.dyade.aaa.agent; 22 23 import java.io.*; 24 import java.util.Date ; 25 26 import org.objectweb.util.monolog.api.BasicLevel; 27 import org.objectweb.util.monolog.api.Logger; 28 29 import fr.dyade.aaa.util.*; 30 31 40 final class Message implements Serializable { 41 static final long serialVersionUID = -2179939607085028300L; 42 43 44 transient AgentId from; 45 46 transient AgentId to; 47 48 transient Notification not; 49 50 51 transient short source; 52 53 transient short dest; 54 55 transient int stamp; 56 57 58 short getSource() { 59 return source; 60 } 61 62 63 short getDest() { 64 return dest; 65 } 66 67 68 int getStamp() { 69 return stamp; 70 } 71 72 77 public String toString() { 78 return appendToString(new StringBuffer ()).toString(); 79 } 80 81 87 public StringBuffer appendToString(StringBuffer strbuf) { 88 strbuf.append('(').append(super.toString()); 89 strbuf.append(",from=").append(from); 90 strbuf.append(",to=").append(to); 91 strbuf.append(",not=").append(not); 92 strbuf.append(",source=").append(source); 93 strbuf.append(",dest=").append(dest); 94 strbuf.append(",stamp=").append(stamp); 95 strbuf.append(')'); 96 97 return strbuf; 98 } 99 100 final static int LENGTH = 25; 101 102 final static int PERSISTENT = 0x01; 103 final static int DETACHABLE = 0x10; 104 105 transient private byte iobuf[] = new byte [LENGTH]; 106 107 114 private void writeObject(java.io.ObjectOutputStream out) 115 throws IOException { 116 int idx = writeToBuf(iobuf, 0); 117 118 iobuf[idx++] = (byte) ((not.persistent?PERSISTENT:0) | 120 (not.detachable?DETACHABLE:0)); 121 122 out.write(iobuf, 0, LENGTH); 124 125 if (! not.detachable) { 126 out.writeObject(not); 128 } 129 } 130 131 134 int writeToBuf(byte[] buf, int idx) { 135 buf[idx++] = (byte) (from.from >>> 8); 137 buf[idx++] = (byte) (from.from >>> 0); 138 buf[idx++] = (byte) (from.to >>> 8); 139 buf[idx++] = (byte) (from.to >>> 0); 140 buf[idx++] = (byte) (from.stamp >>> 24); 141 buf[idx++] = (byte) (from.stamp >>> 16); 142 buf[idx++] = (byte) (from.stamp >>> 8); 143 buf[idx++] = (byte) (from.stamp >>> 0); 144 buf[idx++] = (byte) (to.from >>> 8); 146 buf[idx++] = (byte) (to.from >>> 0); 147 buf[idx++] = (byte) (to.to >>> 8); 148 buf[idx++] = (byte) (to.to >>> 0); 149 buf[idx++] = (byte) (to.stamp >>> 24); 150 buf[idx++] = (byte) (to.stamp >>> 16); 151 buf[idx++] = (byte) (to.stamp >>> 8); 152 buf[idx++] = (byte) (to.stamp >>> 0); 153 buf[idx++] = (byte) (source >>> 8); 155 buf[idx++] = (byte) (source >>> 0); 156 buf[idx++] = (byte) (dest >>> 8); 158 buf[idx++] = (byte) (dest >>> 0); 159 buf[idx++] = (byte) (stamp >>> 24); 161 buf[idx++] = (byte) (stamp >>> 16); 162 buf[idx++] = (byte) (stamp >>> 8); 163 buf[idx++] = (byte) (stamp >>> 0); 164 165 return idx; 166 } 167 168 174 private void readObject(java.io.ObjectInputStream in) 175 throws IOException, ClassNotFoundException { 176 iobuf = new byte[25]; 177 in.readFully(iobuf, 0, 25); 178 179 int idx = readFromBuf(iobuf, 0); 180 181 boolean persistent = ((iobuf[idx] & PERSISTENT) == 0)?false:true; 183 boolean detachable = ((iobuf[idx] & DETACHABLE) == 0)?false:true; 184 185 if (! detachable) { 186 not = (Notification) in.readObject(); 188 not.detachable = false; 189 not.detached = false; 190 } 191 } 192 193 int readFromBuf(byte[] buf, int idx) { 194 from = new AgentId( 196 (short) (((buf[idx++] & 0xFF) << 8) + (buf[idx++] & 0xFF)), 197 (short) (((buf[idx++] & 0xFF) << 8) + (buf[idx++] & 0xFF)), 198 ((buf[idx++] & 0xFF) << 24) + ((buf[idx++] & 0xFF) << 16) + 199 ((buf[idx++] & 0xFF) << 8) + ((buf[idx++] & 0xFF) << 0)); 200 to = new AgentId( 202 (short) (((buf[idx++] & 0xFF) << 8) + (buf[idx++] & 0xFF)), 203 (short) (((buf[idx++] & 0xFF) << 8) + (buf[idx++] & 0xFF)), 204 ((buf[idx++] & 0xFF) << 24) + ((buf[idx++] & 0xFF) << 16) + 205 ((buf[idx++] & 0xFF) << 8) + ((buf[idx++] & 0xFF) << 0)); 206 source = (short) (((buf[idx++] & 0xFF) << 8) + 208 ((buf[idx++] & 0xFF) << 0)); 209 dest = (short) (((buf[idx++] & 0xFF) << 8) + 211 ((buf[idx++] & 0xFF) << 0)); 212 stamp = ((buf[idx++] & 0xFF) << 24) + ((buf[idx++] & 0xFF) << 16) + 214 ((buf[idx++] & 0xFF) << 8) + ((buf[idx++] & 0xFF) << 0); 215 216 return idx; 217 } 218 219 transient private String stringId = null; 220 221 final String toStringId() { 222 if (stringId == null) { 223 stringId = StringId.toStringId('@', '_', dest, stamp, -1); 224 } 225 return stringId; 226 } 227 228 229 234 boolean isPersistent() { 235 return ((not != null) && not.persistent); 236 } 237 238 241 void save() throws IOException { 242 if (isPersistent()) { 243 AgentServer.getTransaction().save(this, toStringId()); 244 if (not.detachable) { 245 not.messageId = StringId.toStringId('N', '_', dest, stamp, -1); 246 AgentServer.getTransaction().save(not, not.messageId); 247 } 248 } 249 } 250 251 259 static Message 260 load(String name) throws IOException, ClassNotFoundException { 261 Message msg = (Message) AgentServer.getTransaction().load(name); 262 if (msg.not == null) { 263 String messageId = StringId.toStringId('N', '_', msg.dest, msg.stamp, -1); 264 msg.not = (Notification) AgentServer.getTransaction().load(messageId); 265 msg.not.messageId = messageId; 266 msg.not.detachable = true; 267 msg.not.detached = false; 268 } 269 msg.not.persistent = true; 270 271 return msg; 272 } 273 274 277 void delete() throws IOException { 278 if (isPersistent()) { 279 AgentServer.getTransaction().delete(toStringId()); 280 if (not.detachable && ! not.detached) { 281 AgentServer.getTransaction().delete(not.getMessageId()); 284 } 285 } 286 } 287 288 291 private Message() {} 292 293 private static Pool pool = null; 294 295 static { 296 int size = Integer.getInteger("fr.dyade.aaa.agent.Message$Pool.size", 150).intValue(); 297 pool = new Pool("Message", size); 298 } 299 300 303 static Message alloc() { 304 Message msg = null; 305 306 try { 307 msg = (Message) pool.allocElement(); 308 } catch (Exception exc) { 309 return new Message(); 310 } 311 return msg; 312 } 313 314 321 static Message alloc(AgentId from, AgentId to, Notification not) { 322 Message msg = alloc(); 323 msg.set(from, to, not); 324 return msg; 325 } 326 327 330 void free() { 331 not = null; 332 stringId = null; 333 pool.freeElement(this); 334 } 335 336 private void set(AgentId from, AgentId to, Notification not) { 337 this.from = (AgentId) from; 338 this.to = (AgentId) to; 339 if (not != null) { 340 this.not = (Notification) not.clone(); 341 this.not.detached = not.detached; 342 this.not.messageId = not.messageId; 343 } 344 } 345 } 346 | Popular Tags |