001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.net.groups;
006:
007: import com.tc.bytes.TCByteBuffer;
008: import com.tc.bytes.TCByteBufferFactory;
009: import com.tc.io.TCByteBufferInputStream;
010: import com.tc.io.TCByteBufferOutputStream;
011: import com.tc.object.ObjectID;
012: import com.tc.object.dna.impl.ObjectStringSerializer;
013:
014: import java.io.IOException;
015: import java.io.ObjectInput;
016: import java.io.ObjectOutput;
017: import java.util.Iterator;
018: import java.util.Set;
019:
020: public abstract class AbstractGroupMessage implements GroupMessage {
021:
022: private static long nextID = 0;
023:
024: private int type;
025: private MessageID id;
026: private MessageID requestID;
027:
028: private transient NodeID messageOrginator = NodeIDImpl.NULL_ID;
029:
030: protected AbstractGroupMessage(int type) {
031: this .type = type;
032: id = getNextID();
033: requestID = MessageID.NULL_ID;
034: }
035:
036: protected AbstractGroupMessage(int type, MessageID requestID) {
037: this .type = type;
038: id = getNextID();
039: this .requestID = requestID;
040: }
041:
042: private static final synchronized MessageID getNextID() {
043: return new MessageID(nextID++);
044: }
045:
046: public int getType() {
047: return type;
048: }
049:
050: public MessageID getMessageID() {
051: return id;
052: }
053:
054: public MessageID inResponseTo() {
055: return requestID;
056: }
057:
058: public void setMessageOrginator(NodeID n) {
059: this .messageOrginator = n;
060: }
061:
062: public NodeID messageFrom() {
063: return messageOrginator;
064: }
065:
066: public final void readExternal(ObjectInput in) throws IOException,
067: ClassNotFoundException {
068: type = in.readInt();
069: id = new MessageID(in.readLong());
070: requestID = new MessageID(in.readLong());
071: basicReadExternal(type, in);
072:
073: }
074:
075: public final void writeExternal(ObjectOutput out)
076: throws IOException {
077: out.writeInt(type);
078: out.writeLong(id.toLong());
079: out.writeLong(requestID.toLong());
080: basicWriteExternal(type, out);
081: }
082:
083: protected abstract void basicWriteExternal(int msgType,
084: ObjectOutput out) throws IOException;
085:
086: protected abstract void basicReadExternal(int msgType,
087: ObjectInput in) throws IOException, ClassNotFoundException;
088:
089: protected void writeObjectStringSerializer(ObjectOutput out,
090: ObjectStringSerializer lserializer) throws IOException {
091: TCByteBufferOutputStream tcbo = new TCByteBufferOutputStream();
092: lserializer.serializeTo(tcbo);
093: writeByteBuffers(out, tcbo.toArray());
094: tcbo.recycle();
095: }
096:
097: protected void writeByteBuffers(ObjectOutput out,
098: TCByteBuffer[] buffers) throws IOException {
099: out.writeInt(buffers.length);
100: for (int i = 0; i < buffers.length; i++) {
101: TCByteBuffer buffer = buffers[i];
102: int length = buffer.limit();
103: out.writeInt(length);
104: out.write(buffer.array(), buffer.arrayOffset(), length);
105: }
106: }
107:
108: protected ObjectStringSerializer readObjectStringSerializer(
109: ObjectInput in) throws IOException {
110: TCByteBuffer buffers[] = readByteBuffers(in);
111: ObjectStringSerializer lserializer = new ObjectStringSerializer();
112: lserializer
113: .deserializeFrom(new TCByteBufferInputStream(buffers));
114: return lserializer;
115: }
116:
117: protected TCByteBuffer[] readByteBuffers(ObjectInput in)
118: throws IOException {
119: int size = in.readInt();
120: TCByteBuffer buffers[] = new TCByteBuffer[size];
121: for (int i = 0; i < buffers.length; i++) {
122: int length = in.readInt();
123: byte bytes[] = new byte[length];
124: int start = 0;
125: while (length > 0) {
126: int read = in.read(bytes, start, length);
127: start += read;
128: length -= read;
129: }
130: buffers[i] = TCByteBufferFactory.wrap(bytes);
131: }
132: return buffers;
133: }
134:
135: protected void writeObjectIDS(ObjectOutput out, Set oids)
136: throws IOException {
137: out.writeInt(oids.size());
138: for (Iterator i = oids.iterator(); i.hasNext();) {
139: ObjectID oid = (ObjectID) i.next();
140: out.writeLong(oid.toLong());
141: }
142: }
143:
144: protected Set readObjectIDS(ObjectInput in, Set oids)
145: throws IOException {
146: int size = in.readInt();
147: for (int i = 0; i < size; i++) {
148: oids.add(new ObjectID(in.readLong()));
149: }
150: return oids;
151: }
152: }
|