001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.object.msg;
006:
007: import com.tc.bytes.TCByteBuffer;
008: import com.tc.io.TCByteBufferOutput;
009: import com.tc.io.TCSerializable;
010: import com.tc.net.protocol.tcm.MessageChannel;
011: import com.tc.net.protocol.tcm.MessageMonitor;
012: import com.tc.net.protocol.tcm.TCMessageHeader;
013: import com.tc.net.protocol.tcm.TCMessageType;
014: import com.tc.object.ObjectID;
015: import com.tc.object.lockmanager.api.LockContext;
016: import com.tc.object.lockmanager.api.TryLockContext;
017: import com.tc.object.lockmanager.api.WaitContext;
018: import com.tc.object.session.SessionID;
019: import com.tc.object.tx.TransactionID;
020: import com.tc.util.Assert;
021: import com.tc.util.SequenceID;
022:
023: import java.io.IOException;
024: import java.util.Collection;
025: import java.util.HashSet;
026: import java.util.Iterator;
027: import java.util.Set;
028:
029: public class ClientHandshakeMessageImpl extends DSOMessageBase
030: implements ClientHandshakeMessage {
031:
032: private static final byte MANAGED_OBJECT_ID = 1;
033: private static final byte WAIT_CONTEXT = 2;
034: private static final byte LOCK_CONTEXT = 3;
035: private static final byte TRANSACTION_SEQUENCE_IDS = 4;
036: private static final byte PENDING_LOCK_CONTEXT = 5;
037: private static final byte RESENT_TRANSACTION_IDS = 6;
038: private static final byte REQUEST_OBJECT_IDS = 7;
039: private static final byte PENDING_TRY_LOCK_CONTEXT = 8;
040: private static final byte CLIENT_VERSION = 9;
041:
042: private final Set objectIDs = new HashSet();
043: private final Set lockContexts = new HashSet();
044: private final Set waitContexts = new HashSet();
045: private final Set pendingLockContexts = new HashSet();
046: private final Set pendingTryLockContexts = new HashSet();
047: private final Set sequenceIDs = new HashSet();
048: private final Set txnIDs = new HashSet();
049: private boolean requestObjectIDs;
050: private String clientVersion = "UNKNOW";
051:
052: public ClientHandshakeMessageImpl(SessionID sessionID,
053: MessageMonitor monitor, TCByteBufferOutput out,
054: MessageChannel channel, TCMessageType messageType) {
055: super (sessionID, monitor, out, channel, messageType);
056: }
057:
058: public ClientHandshakeMessageImpl(SessionID sessionID,
059: MessageMonitor monitor, MessageChannel channel,
060: TCMessageHeader header, TCByteBuffer[] data) {
061: super (sessionID, monitor, channel, header, data);
062: }
063:
064: public void addObjectID(ObjectID objectID) {
065: synchronized (objectIDs) {
066: objectIDs.add(objectID);
067: }
068: }
069:
070: public void addLockContext(LockContext ctxt) {
071: synchronized (lockContexts) {
072: lockContexts.add(ctxt);
073: }
074: }
075:
076: public Collection getLockContexts() {
077: synchronized (lockContexts) {
078: return new HashSet(lockContexts);
079: }
080: }
081:
082: public void addPendingLockContext(LockContext ctxt) {
083: synchronized (pendingLockContexts) {
084: pendingLockContexts.add(ctxt);
085: }
086: }
087:
088: public void addPendingTryLockContext(LockContext ctxt) {
089: Assert.eval(ctxt instanceof TryLockContext);
090: synchronized (pendingTryLockContexts) {
091: pendingTryLockContexts.add(ctxt);
092: }
093: }
094:
095: public Collection getPendingLockContexts() {
096: synchronized (pendingLockContexts) {
097: return new HashSet(pendingLockContexts);
098: }
099: }
100:
101: public Collection getPendingTryLockContexts() {
102: synchronized (pendingTryLockContexts) {
103: return new HashSet(pendingTryLockContexts);
104: }
105: }
106:
107: public void addWaitContext(WaitContext ctxt) {
108: synchronized (waitContexts) {
109: waitContexts.add(ctxt);
110: }
111: }
112:
113: public Collection getWaitContexts() {
114: synchronized (waitContexts) {
115: return new HashSet(waitContexts);
116: }
117: }
118:
119: public Set getObjectIDs() {
120: synchronized (objectIDs) {
121: return new HashSet(objectIDs);
122: }
123: }
124:
125: protected void dehydrateValues() {
126: for (Iterator i = objectIDs.iterator(); i.hasNext();) {
127: putNVPair(MANAGED_OBJECT_ID, ((ObjectID) i.next()).toLong());
128: }
129: for (Iterator i = lockContexts.iterator(); i.hasNext();) {
130: putNVPair(LOCK_CONTEXT, (TCSerializable) i.next());
131: }
132: for (Iterator i = waitContexts.iterator(); i.hasNext();) {
133: putNVPair(WAIT_CONTEXT, (TCSerializable) i.next());
134: }
135: for (Iterator i = pendingLockContexts.iterator(); i.hasNext();) {
136: putNVPair(PENDING_LOCK_CONTEXT, (TCSerializable) i.next());
137: }
138: for (Iterator i = pendingTryLockContexts.iterator(); i
139: .hasNext();) {
140: putNVPair(PENDING_TRY_LOCK_CONTEXT, (TCSerializable) i
141: .next());
142: }
143: for (Iterator i = sequenceIDs.iterator(); i.hasNext();) {
144: putNVPair(TRANSACTION_SEQUENCE_IDS, ((SequenceID) i.next())
145: .toLong());
146: }
147: for (Iterator i = txnIDs.iterator(); i.hasNext();) {
148: putNVPair(RESENT_TRANSACTION_IDS,
149: ((TransactionID) i.next()).toLong());
150: }
151: putNVPair(REQUEST_OBJECT_IDS, this .requestObjectIDs);
152: putNVPair(CLIENT_VERSION, this .clientVersion);
153: }
154:
155: protected boolean hydrateValue(byte name) throws IOException {
156: switch (name) {
157: case MANAGED_OBJECT_ID:
158: objectIDs.add(new ObjectID(getLongValue()));
159: return true;
160: case LOCK_CONTEXT:
161: lockContexts.add(getObject(new LockContext()));
162: return true;
163: case WAIT_CONTEXT:
164: waitContexts.add(getObject(new WaitContext()));
165: return true;
166: case PENDING_LOCK_CONTEXT:
167: pendingLockContexts.add(getObject(new LockContext()));
168: return true;
169: case PENDING_TRY_LOCK_CONTEXT:
170: pendingTryLockContexts.add(getObject(new TryLockContext()));
171: return true;
172: case TRANSACTION_SEQUENCE_IDS:
173: sequenceIDs.add(new SequenceID(getLongValue()));
174: return true;
175: case RESENT_TRANSACTION_IDS:
176: txnIDs.add(new TransactionID(getLongValue()));
177: return true;
178: case REQUEST_OBJECT_IDS:
179: this .requestObjectIDs = getBooleanValue();
180: return true;
181: case CLIENT_VERSION:
182: this .clientVersion = getStringValue();
183: return true;
184: default:
185: return false;
186: }
187: }
188:
189: public Collection getTransactionSequenceIDs() {
190: return sequenceIDs;
191: }
192:
193: public Collection getResentTransactionIDs() {
194: return txnIDs;
195: }
196:
197: public void setTransactionSequenceIDs(Collection seqIDs) {
198: this .sequenceIDs.addAll(seqIDs);
199: }
200:
201: public void setResentTransactionIDs(Collection resentTransactionIDs) {
202: this .txnIDs.addAll(resentTransactionIDs);
203: }
204:
205: public void setIsObjectIDsRequested(boolean request) {
206: this .requestObjectIDs = request;
207: }
208:
209: public boolean isObjectIDsRequested() {
210: return this .requestObjectIDs;
211: }
212:
213: public String getClientVersion() {
214: return clientVersion;
215: }
216:
217: public void setClientVersion(String v) {
218: clientVersion = v;
219: }
220: }
|