1 46 package org.mr.core.net; 47 48 import java.net.SocketAddress ; 49 import java.nio.ByteBuffer ; 50 import java.security.MessageDigest ; 51 import java.security.NoSuchAlgorithmException ; 52 53 import org.mr.core.protocol.MantaBusMessage; 54 import org.mr.core.util.byteable.NetOutByteBufferPool; 55 56 72 73 public class CNLMessage { 74 public static final byte CNL_NETWORK = 0; 75 public static final byte CNL_MANTA = 1; 76 public static final byte CNL_MD5_MASK = 0x10; 77 public static final byte CNL_TYPE_MASK = 0x0F; 78 public static final int CNL_HEADERLEN = 25; 79 80 private byte type; 81 private int id; 82 private int length; 83 private byte[] md5; 84 private ByteBuffer [] buffers; 85 private ByteBuffer headerBuffer; 86 private int nUsers; 87 private MantaBusMessage busMessage; 88 private boolean isTCP; 89 private SocketAddress source; 90 private SocketAddress dest; 91 private String password; 92 private boolean sent; 93 94 101 public CNLMessage(byte type, ByteBuffer [] buffers) { 102 this.type = type; 103 this.id = 0; 104 this.length = 0; 105 this.md5 = new byte[16]; 106 for (int i = 0; i < buffers.length; i++) { 107 buffers[i].rewind(); 108 this.length += buffers[i].remaining(); 109 } 110 this.buffers = buffers; 111 this.nUsers = 0; 112 this.sent = false; 113 } 114 115 118 public CNLMessage(boolean isTCP) { 119 this.type = -1; 120 this.id = 0; 121 this.length = 0; 122 this.md5 = new byte[16]; 123 this.buffers = null; 124 this.nUsers = 0; 125 this.isTCP = isTCP; 126 this.sent = false; 127 } 128 129 133 public void readHeader(ByteBuffer buffer) { 134 this.type = buffer.get(); 135 this.id = buffer.getInt(); 136 this.length = buffer.getInt(); 137 buffer.get(md5); 138 } 139 140 public void setBuffer(ByteBuffer buffer) { 141 this.buffers = new ByteBuffer [1]; 142 this.buffers[0] = buffer; 143 this.busMessage = null; 144 } 145 146 public void setBusMessage(MantaBusMessage busMessage) { 147 this.busMessage = busMessage; 148 } 149 150 154 public byte getType() { 155 return (byte) (type & CNL_TYPE_MASK); 156 } 157 158 162 public int getLength() { 163 return length; 164 } 165 166 170 public int getTotalLength() { 171 return this.length + CNL_HEADERLEN; 172 } 173 174 178 public int getID() { 179 return this.id; 180 } 181 182 public boolean isSigned() { 183 return (this.type & CNL_MD5_MASK) != 0; 184 } 185 186 190 public byte[] getMessageMD5() { 191 if (isSigned()) { 192 return this.md5; 193 } else { 194 return null; 195 } 196 } 197 198 202 public MessageDigest getPartialMD5() { 203 return partialDigest(); 204 } 205 206 210 public ByteBuffer headerAsBuffer(int id) { 211 if (this.headerBuffer == null) { 212 if (isSigned()) { 213 this.id = id; 214 this.md5 = realSignMessage(this.password); 215 } 216 this.headerBuffer = 217 NetOutByteBufferPool.getInstance().getBuffer(CNL_HEADERLEN); 218 this.headerBuffer.limit(CNL_HEADERLEN); 219 this.headerBuffer.put(this.type); 220 this.headerBuffer.putInt(id); 221 this.headerBuffer.putInt(this.length); 222 this.headerBuffer.put(this.md5); 223 this.headerBuffer.flip(); 224 } 225 226 return this.headerBuffer.duplicate(); 227 } 228 229 public void headerToBuffer(ByteBuffer buf, int id) { 230 this.id = id; 231 if (isSigned()) { 232 this.md5 = realSignMessage(this.password); 233 } 234 buf.put(this.type); 235 buf.putInt(id); 236 buf.putInt(this.length); 237 buf.put(this.md5); 238 buf.flip(); 239 } 240 241 245 public ByteBuffer [] valueAsBuffers() { 246 ByteBuffer [] dups = new ByteBuffer [this.buffers.length]; 247 for (int i = 0; i < dups.length; i++) { 248 dups[i] = this.buffers[i].duplicate(); 249 } 250 return dups; 251 } 252 253 261 public ByteBuffer [] buffers() { 262 return this.buffers; 263 } 264 265 public synchronized void use() { 266 this.nUsers++; 267 } 268 269 public synchronized void unuse() { 270 this.nUsers--; 271 if (this.nUsers == 0) { 272 if (this.headerBuffer != null) { 275 NetOutByteBufferPool.getInstance().release(this.headerBuffer); 276 } 277 if (this.busMessage != null) { 278 this.busMessage.release(wasSent()); 279 } 280 } else if (this.nUsers < 0) { 281 } 284 } 285 286 291 public void setSourceAddress(SocketAddress source) { 292 this.source = source; 293 } 294 295 300 public void setDestAddress(SocketAddress dest) { 301 this.dest = dest; 302 } 303 304 307 public SocketAddress getSourceAddress() { 308 return this.source; 309 } 310 311 314 public SocketAddress getDestAddress() { 315 return this.dest; 316 } 317 318 321 public boolean isTCP() { 322 return this.isTCP; 323 } 324 325 public String toString() { 326 StringBuffer buf = new StringBuffer (); 327 if (type == CNL_NETWORK) { 328 buf.append("NET: "); 329 } else if (type == CNL_MANTA) { 330 buf.append("APP: "); 331 } else { 332 buf.append((int)type).append(": "); 333 } 334 buf.append(id).append("/").append(length).append("b"); 335 if (this.busMessage != null) { 336 buf.append(" ").append(this.busMessage.getMessageId()); 337 } 338 return buf.toString(); 339 } 340 341 public void signMessage(String password) { 342 this.password = password; 343 this.type = (byte) (this.type | CNL_MD5_MASK); 344 } 345 346 public void setSent() { 347 this.sent = true; 348 } 349 350 public boolean wasSent() { 351 return this.sent; 352 } 353 354 356 private MessageDigest partialDigest() { 357 MessageDigest digest; 358 try { 359 digest = MessageDigest.getInstance("MD5"); 360 } catch (NoSuchAlgorithmException e) { 361 return null; 363 } 364 digest.update(type); 365 digest.update((byte)((id >>> 24) & 0xFF)); 366 digest.update((byte)((id >>> 16) & 0xFF)); 367 digest.update((byte)((id >>> 8) & 0xFF)); 368 digest.update((byte)((id >>> 0) & 0xFF)); 369 digest.update((byte)((length >>> 24) & 0xFF)); 370 digest.update((byte)((length >>> 16) & 0xFF)); 371 digest.update((byte)((length >>> 8) & 0xFF)); 372 digest.update((byte)((length >>> 0) & 0xFF)); 373 for (int i = 0; i < this.buffers.length; ++i) { 374 digest.update(this.buffers[i].array(), 0, buffers[i].limit()); 375 } 376 return digest; 377 } 378 379 private byte[] realSignMessage(String password) { 380 MessageDigest digest = partialDigest(); 381 digest.update(password.getBytes()); 382 return digest.digest(); 383 } 384 } | Popular Tags |