01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.net.core;
05:
06: import com.tc.bytes.TCByteBuffer;
07: import com.tc.net.protocol.GenericNetworkMessage;
08: import com.tc.net.protocol.GenericNetworkMessageSink;
09: import com.tc.net.protocol.TCNetworkHeader;
10: import com.tc.util.HexDump;
11:
12: public class Verifier implements GenericNetworkMessageSink {
13: private final int clientNum;
14: private int counter = 0;
15: private int sequence = 0;
16:
17: public Verifier(int clientNum) {
18: this .clientNum = clientNum;
19: }
20:
21: public int getClientNum() {
22: return this .clientNum;
23: }
24:
25: public void putMessage(GenericNetworkMessage msg) {
26: final int cn = msg.getClientNum();
27: if (cn != clientNum) {
28: headerError("unexpected client number " + cn
29: + ", expecting " + clientNum, msg);
30: }
31:
32: final int seq = msg.getSequence();
33: if (seq != sequence) {
34: headerError("unexpected sequence number " + seq
35: + ", expecting sequence " + sequence, msg);
36: }
37: sequence++;
38:
39: verifyMessage(msg);
40: }
41:
42: private void verifyMessage(GenericNetworkMessage msg) {
43: TCByteBuffer data[] = msg.getPayload();
44: for (int i = 0; i < data.length; i++) {
45: TCByteBuffer buf = data[i].duplicate();
46:
47: while (buf.hasRemaining()) {
48: final int num = buf.getInt();
49: if (num != clientNum) {
50: dataError("unexpected client number " + num
51: + ", expecting " + clientNum, buf, buf
52: .position() - 4, i, data.length);
53: }
54:
55: final int cnt = buf.getInt();
56: if (cnt != counter) {
57: dataError("unexpected counter value " + cnt
58: + ", expecting " + counter, buf, buf
59: .position() - 4, i, data.length);
60: }
61: counter++;
62: }
63: }
64: }
65:
66: private void dataError(String error, TCByteBuffer buf,
67: int position, int numBuf, int numBufs) {
68: error += "\n";
69: error += "Message " + sequence + ", Buffer " + (numBuf + 1)
70: + " of " + numBufs + " at position 0x"
71: + Integer.toHexString(position).toUpperCase();
72: error += " "
73: + HexDump.dump(buf.array(), buf.arrayOffset(), buf
74: .limit());
75: throw new RuntimeException(error);
76: }
77:
78: private void headerError(String errorMsg, GenericNetworkMessage msg) {
79: TCNetworkHeader hdr = msg.getHeader();
80: TCByteBuffer hdrData = hdr.getDataBuffer();
81: throw new RuntimeException(errorMsg
82: + "\n"
83: + HexDump.dump(hdrData.array(), hdrData.arrayOffset(),
84: hdr.getHeaderByteLength()));
85: }
86: }
|