1 5 package com.tc.net.groups; 6 7 import com.tc.bytes.TCByteBuffer; 8 import com.tc.bytes.TCByteBufferFactory; 9 import com.tc.io.TCByteBufferInputStream; 10 import com.tc.io.TCByteBufferOutputStream; 11 import com.tc.object.dna.impl.ObjectStringSerializer; 12 13 import java.io.IOException ; 14 import java.io.ObjectInput ; 15 import java.io.ObjectOutput ; 16 17 public abstract class AbstractGroupMessage implements GroupMessage { 18 19 private static long nextID = 0; 20 21 private int type; 22 private MessageID id; 23 private MessageID requestID; 24 25 private transient NodeID messageOrginator = NodeID.NULL_ID; 26 27 protected AbstractGroupMessage(int type) { 28 this.type = type; 29 id = getNextID(); 30 requestID = MessageID.NULL_ID; 31 } 32 33 protected AbstractGroupMessage(int type, MessageID requestID) { 34 this.type = type; 35 id = getNextID(); 36 this.requestID = requestID; 37 } 38 39 private static final synchronized MessageID getNextID() { 40 return new MessageID(nextID++); 41 } 42 43 public int getType() { 44 return type; 45 } 46 47 public MessageID getMessageID() { 48 return id; 49 } 50 51 public MessageID inResponseTo() { 52 return requestID; 53 } 54 55 public void setMessageOrginator(NodeID n) { 56 this.messageOrginator = n; 57 } 58 59 public NodeID messageFrom() { 60 return messageOrginator; 61 } 62 63 public final void readExternal(ObjectInput in) throws IOException , ClassNotFoundException { 64 type = in.readInt(); 65 id = new MessageID(in.readLong()); 66 requestID = new MessageID(in.readLong()); 67 basicReadExternal(type, in); 68 69 } 70 71 public final void writeExternal(ObjectOutput out) throws IOException { 72 out.writeInt(type); 73 out.writeLong(id.toLong()); 74 out.writeLong(requestID.toLong()); 75 basicWriteExternal(type, out); 76 } 77 78 protected abstract void basicWriteExternal(int msgType, ObjectOutput out) throws IOException ; 79 80 protected abstract void basicReadExternal(int msgType, ObjectInput in) throws IOException , ClassNotFoundException ; 81 82 protected void writeObjectStringSerializer(ObjectOutput out, ObjectStringSerializer lserializer) throws IOException { 83 TCByteBufferOutputStream tcbo = new TCByteBufferOutputStream(); 84 lserializer.serializeTo(tcbo); 85 writeByteBuffers(out, tcbo.toArray()); 86 tcbo.recycle(); 87 } 88 89 protected void writeByteBuffers(ObjectOutput out, TCByteBuffer[] buffers) throws IOException { 90 out.writeInt(buffers.length); 91 for (int i = 0; i < buffers.length; i++) { 92 TCByteBuffer buffer = buffers[i]; 93 int length = buffer.limit(); 94 out.writeInt(length); 95 out.write(buffer.array(), buffer.arrayOffset(), length); 96 } 97 } 98 99 100 protected ObjectStringSerializer readObjectStringSerializer(ObjectInput in) throws IOException { 101 TCByteBuffer buffers[] = readByteBuffers(in); 102 ObjectStringSerializer lserializer = new ObjectStringSerializer(); 103 lserializer.deserializeFrom(new TCByteBufferInputStream(buffers)); 104 return lserializer; 105 } 106 107 protected TCByteBuffer[] readByteBuffers(ObjectInput in) throws IOException { 108 int size = in.readInt(); 109 TCByteBuffer buffers[] = new TCByteBuffer[size]; 110 for (int i = 0; i < buffers.length; i++) { 111 int length = in.readInt(); 112 byte bytes[] = new byte[length]; 113 int start = 0; 114 while (length > 0) { 115 int read = in.read(bytes, start, length); 116 start += read; 117 length -= read; 118 } 119 buffers[i] = TCByteBufferFactory.wrap(bytes); 120 } 121 return buffers; 122 } 123 124 } 125 | Popular Tags |