KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > memorydatastore > message > MemoryDataStoreResponseMessage


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.memorydatastore.message;
5
6 import com.tc.bytes.TCByteBuffer;
7 import com.tc.io.TCByteBufferOutput;
8 import com.tc.net.protocol.tcm.MessageChannel;
9 import com.tc.net.protocol.tcm.MessageMonitor;
10 import com.tc.net.protocol.tcm.TCMessageHeader;
11 import com.tc.net.protocol.tcm.TCMessageType;
12 import com.tc.object.lockmanager.api.ThreadID;
13 import com.tc.object.msg.DSOMessageBase;
14 import com.tc.object.session.SessionID;
15
16 import java.io.IOException JavaDoc;
17 import java.util.Collection JavaDoc;
18
19 public class MemoryDataStoreResponseMessage extends DSOMessageBase {
20   private static final byte TYPE = 1;
21   private static final byte THREAD_ID = 2;
22   private static final byte REQUEST_COMPLETED_FLAG = 3;
23   private static final byte VALUE = 4;
24   private static final byte NUM_OF_REMOVE = 5;
25
26   public static final int PUT_RESPONSE = 4;
27   public static final int GET_RESPONSE = 5;
28   public static final int GET_ALL_RESPONSE = 6;
29   public static final int REMOVE_RESPONSE = 7;
30   public static final int REMOVE_ALL_RESPONSE = 8;
31
32   private int type;
33   private int numOfRemove;
34   private boolean requestCompletedFlag;
35   private ThreadID threadID;
36   private TCMemoryDataStoreMessageData value;
37
38   public MemoryDataStoreResponseMessage(MessageMonitor monitor, TCByteBufferOutput out, MessageChannel channel,
39       TCMessageType type) {
40     super(monitor, out, channel, type);
41   }
42
43   public MemoryDataStoreResponseMessage(SessionID sessionID, MessageMonitor monitor, MessageChannel channel,
44       TCMessageHeader header, TCByteBuffer[] data) {
45     super(sessionID, monitor, channel, header, data);
46   }
47
48   protected void dehydrateValues() {
49     putNVPair(TYPE, this.type);
50     putNVPair(THREAD_ID, threadID.toLong());
51     putNVPair(REQUEST_COMPLETED_FLAG, this.requestCompletedFlag);
52     if (isGetResponse() || isGetAllResponse() || isRemoveResponse()) {
53       putNVPair(VALUE, value);
54     }
55     if (isRemoveAllResponse()) {
56       putNVPair(NUM_OF_REMOVE, this.numOfRemove);
57     }
58   }
59
60   protected String JavaDoc describePayload() {
61     StringBuffer JavaDoc rv = new StringBuffer JavaDoc();
62     rv.append("Type : ");
63
64     if (isPutResponse()) {
65       rv.append("PUT RESPONSE \n");
66     } else if (isGetResponse()) {
67       rv.append("GET RESPONSE \n");
68     } else if (isRemoveResponse()) {
69       rv.append("REMOVE RESPONSE \n");
70     } else {
71       rv.append("UNKNOWN \n");
72     }
73     rv.append("Request Completed Flag: ");
74     rv.append(this.requestCompletedFlag);
75     rv.append(" \n");
76
77     rv.append(value).append('\n');
78
79     return rv.toString();
80   }
81
82   protected boolean hydrateValue(byte name) throws IOException JavaDoc {
83     switch (name) {
84     case TYPE:
85       this.type = getIntValue();
86       return true;
87     case THREAD_ID:
88       this.threadID = new ThreadID(getLongValue());
89       return true;
90     case REQUEST_COMPLETED_FLAG:
91       this.requestCompletedFlag = getBooleanValue();
92       return true;
93     case VALUE:
94       value = new TCMemoryDataStoreMessageData(type);
95       getObject(value);
96       return true;
97     case NUM_OF_REMOVE:
98       this.numOfRemove = getIntValue();
99       return true;
100     default:
101       return false;
102     }
103   }
104
105   public void initializePutResponse(ThreadID threadID, boolean requestCompletedFlag) {
106     this.type = PUT_RESPONSE;
107     this.threadID = threadID;
108     this.requestCompletedFlag = requestCompletedFlag;
109   }
110
111   public void initializeGetResponse(ThreadID threadID, byte[] value, boolean requestCompletedFlag) {
112     this.type = GET_RESPONSE;
113     this.threadID = threadID;
114     this.requestCompletedFlag = requestCompletedFlag;
115     this.value = new TCMemoryDataStoreMessageData(type, null, value);
116   }
117   
118   public void initializeGetAllResponse(ThreadID threadID, Collection JavaDoc values, boolean requestCompletedFlag) {
119     this.type = GET_ALL_RESPONSE;
120     this.threadID = threadID;
121     this.requestCompletedFlag = requestCompletedFlag;
122     this.value = new TCMemoryDataStoreMessageData(type, null, values);
123   }
124   
125   public void initializeRemoveResponse(ThreadID threadID, byte[] value, boolean requestCompletedFlag) {
126     this.type = REMOVE_RESPONSE;
127     this.threadID = threadID;
128     this.requestCompletedFlag = requestCompletedFlag;
129     this.value = new TCMemoryDataStoreMessageData(type, null, value);
130   }
131   
132   public void initializeRemoveAllResponse(ThreadID threadID, int numOfRemove, boolean requestCompletedFlag) {
133     this.type = REMOVE_ALL_RESPONSE;
134     this.threadID = threadID;
135     this.numOfRemove = numOfRemove;
136     this.requestCompletedFlag = requestCompletedFlag;
137   }
138
139   public boolean isRequestCompletedFlag() {
140     return requestCompletedFlag;
141   }
142
143   public byte[] getValue() {
144     return this.value.getValue();
145   }
146   
147   public Collection JavaDoc getValues() {
148     return this.value.getValues();
149   }
150
151   public ThreadID getThreadID() {
152     return this.threadID;
153   }
154
155   public int getType() {
156     return this.type;
157   }
158
159   public int getNumOfRemove() {
160     return numOfRemove;
161   }
162   
163   public boolean isGetResponse() {
164     return this.type == GET_RESPONSE || this.type == GET_ALL_RESPONSE;
165   }
166   
167   private boolean isPutResponse() {
168     return this.type == PUT_RESPONSE;
169   }
170
171   private boolean isGetAllResponse() {
172     return this.type == GET_ALL_RESPONSE;
173   }
174   
175   private boolean isRemoveResponse() {
176     return this.type == REMOVE_RESPONSE;
177   }
178   
179   private boolean isRemoveAllResponse() {
180     return this.type == REMOVE_ALL_RESPONSE;
181   }
182 }
Popular Tags