001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.memorydatastore.message;
005:
006: import com.tc.bytes.TCByteBuffer;
007: import com.tc.io.TCByteBufferOutput;
008: import com.tc.net.protocol.tcm.MessageChannel;
009: import com.tc.net.protocol.tcm.MessageMonitor;
010: import com.tc.net.protocol.tcm.TCMessageHeader;
011: import com.tc.net.protocol.tcm.TCMessageType;
012: import com.tc.object.lockmanager.api.ThreadID;
013: import com.tc.object.msg.DSOMessageBase;
014: import com.tc.object.session.SessionID;
015:
016: import java.io.IOException;
017: import java.util.Collection;
018:
019: public class MemoryDataStoreResponseMessage extends DSOMessageBase {
020: private static final byte TYPE = 1;
021: private static final byte THREAD_ID = 2;
022: private static final byte REQUEST_COMPLETED_FLAG = 3;
023: private static final byte VALUE = 4;
024: private static final byte NUM_OF_REMOVE = 5;
025:
026: public static final int PUT_RESPONSE = 4;
027: public static final int GET_RESPONSE = 5;
028: public static final int GET_ALL_RESPONSE = 6;
029: public static final int REMOVE_RESPONSE = 7;
030: public static final int REMOVE_ALL_RESPONSE = 8;
031:
032: private int type;
033: private int numOfRemove;
034: private boolean requestCompletedFlag;
035: private ThreadID threadID;
036: private TCMemoryDataStoreMessageData value;
037:
038: public MemoryDataStoreResponseMessage(SessionID sessionID,
039: MessageMonitor monitor, TCByteBufferOutput out,
040: MessageChannel channel, TCMessageType type) {
041: super (sessionID, monitor, out, channel, type);
042: }
043:
044: public MemoryDataStoreResponseMessage(SessionID sessionID,
045: MessageMonitor monitor, MessageChannel channel,
046: TCMessageHeader header, TCByteBuffer[] data) {
047: super (sessionID, monitor, channel, header, data);
048: }
049:
050: protected void dehydrateValues() {
051: putNVPair(TYPE, this .type);
052: putNVPair(THREAD_ID, threadID.toLong());
053: putNVPair(REQUEST_COMPLETED_FLAG, this .requestCompletedFlag);
054: if (isGetResponse() || isGetAllResponse() || isRemoveResponse()) {
055: putNVPair(VALUE, value);
056: }
057: if (isRemoveAllResponse()) {
058: putNVPair(NUM_OF_REMOVE, this .numOfRemove);
059: }
060: }
061:
062: protected String describePayload() {
063: StringBuffer rv = new StringBuffer();
064: rv.append("Type : ");
065:
066: if (isPutResponse()) {
067: rv.append("PUT RESPONSE \n");
068: } else if (isGetResponse()) {
069: rv.append("GET RESPONSE \n");
070: } else if (isRemoveResponse()) {
071: rv.append("REMOVE RESPONSE \n");
072: } else {
073: rv.append("UNKNOWN \n");
074: }
075: rv.append("Request Completed Flag: ");
076: rv.append(this .requestCompletedFlag);
077: rv.append(" \n");
078:
079: rv.append(value).append('\n');
080:
081: return rv.toString();
082: }
083:
084: protected boolean hydrateValue(byte name) throws IOException {
085: switch (name) {
086: case TYPE:
087: this .type = getIntValue();
088: return true;
089: case THREAD_ID:
090: this .threadID = new ThreadID(getLongValue());
091: return true;
092: case REQUEST_COMPLETED_FLAG:
093: this .requestCompletedFlag = getBooleanValue();
094: return true;
095: case VALUE:
096: value = new TCMemoryDataStoreMessageData(type);
097: getObject(value);
098: return true;
099: case NUM_OF_REMOVE:
100: this .numOfRemove = getIntValue();
101: return true;
102: default:
103: return false;
104: }
105: }
106:
107: public void initializePutResponse(ThreadID threadID,
108: boolean requestCompletedFlag) {
109: this .type = PUT_RESPONSE;
110: this .threadID = threadID;
111: this .requestCompletedFlag = requestCompletedFlag;
112: }
113:
114: public void initializeGetResponse(ThreadID threadID, byte[] value,
115: boolean requestCompletedFlag) {
116: this .type = GET_RESPONSE;
117: this .threadID = threadID;
118: this .requestCompletedFlag = requestCompletedFlag;
119: this .value = new TCMemoryDataStoreMessageData(type, null, value);
120: }
121:
122: public void initializeGetAllResponse(ThreadID threadID,
123: Collection values, boolean requestCompletedFlag) {
124: this .type = GET_ALL_RESPONSE;
125: this .threadID = threadID;
126: this .requestCompletedFlag = requestCompletedFlag;
127: this .value = new TCMemoryDataStoreMessageData(type, null,
128: values);
129: }
130:
131: public void initializeRemoveResponse(ThreadID threadID,
132: byte[] value, boolean requestCompletedFlag) {
133: this .type = REMOVE_RESPONSE;
134: this .threadID = threadID;
135: this .requestCompletedFlag = requestCompletedFlag;
136: this .value = new TCMemoryDataStoreMessageData(type, null, value);
137: }
138:
139: public void initializeRemoveAllResponse(ThreadID threadID,
140: int numOfRemove, boolean requestCompletedFlag) {
141: this .type = REMOVE_ALL_RESPONSE;
142: this .threadID = threadID;
143: this .numOfRemove = numOfRemove;
144: this .requestCompletedFlag = requestCompletedFlag;
145: }
146:
147: public boolean isRequestCompletedFlag() {
148: return requestCompletedFlag;
149: }
150:
151: public byte[] getValue() {
152: return this .value.getValue();
153: }
154:
155: public Collection getValues() {
156: return this .value.getValues();
157: }
158:
159: public ThreadID getThreadID() {
160: return this .threadID;
161: }
162:
163: public int getType() {
164: return this .type;
165: }
166:
167: public int getNumOfRemove() {
168: return numOfRemove;
169: }
170:
171: public boolean isGetResponse() {
172: return this .type == GET_RESPONSE
173: || this .type == GET_ALL_RESPONSE;
174: }
175:
176: private boolean isPutResponse() {
177: return this .type == PUT_RESPONSE;
178: }
179:
180: private boolean isGetAllResponse() {
181: return this .type == GET_ALL_RESPONSE;
182: }
183:
184: private boolean isRemoveResponse() {
185: return this .type == REMOVE_RESPONSE;
186: }
187:
188: private boolean isRemoveAllResponse() {
189: return this.type == REMOVE_ALL_RESPONSE;
190: }
191: }
|