1 4 package com.tc.net.core; 5 6 import com.tc.bytes.TCByteBuffer; 7 import com.tc.net.protocol.GenericNetworkMessage; 8 import com.tc.net.protocol.GenericNetworkMessageSink; 9 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 + ", expecting " + clientNum, msg); 29 } 30 31 final int seq = msg.getSequence(); 32 if (seq != sequence) { 33 headerError("unexpected sequence number " + seq + ", expecting sequence " + sequence, msg); 34 } 35 sequence++; 36 37 verifyMessage(msg); 38 } 39 40 private void verifyMessage(GenericNetworkMessage msg) { 41 TCByteBuffer data[] = msg.getPayload(); 42 for (int i = 0; i < data.length; i++) { 43 TCByteBuffer buf = data[i].duplicate(); 44 45 while (buf.hasRemaining()) { 46 final int num = buf.getInt(); 47 if (num != clientNum) { 48 dataError("unexpected client number " + num + ", expecting " + clientNum, buf, buf.position() - 4, i, 49 data.length); 50 } 51 52 final int cnt = buf.getInt(); 53 if (cnt != counter) { 54 dataError("unexpected counter value " + cnt + ", expecting " + counter, buf, buf.position() - 4, i, 55 data.length); 56 } 57 counter++; 58 } 59 } 60 } 61 62 private void dataError(String error, TCByteBuffer buf, int position, int numBuf, int numBufs) { 63 error += "\n"; 64 error += "Message " + sequence + ", Buffer " + (numBuf + 1) + " of " + numBufs + " at position 0x" 65 + Integer.toHexString(position).toUpperCase(); 66 error += " " + HexDump.dump(buf.array(), buf.arrayOffset(), buf.limit()); 67 throw new RuntimeException (error); 68 } 69 70 private void headerError(String errorMsg, GenericNetworkMessage msg) { 71 TCNetworkHeader hdr = msg.getHeader(); 72 TCByteBuffer hdrData = hdr.getDataBuffer(); 73 throw new RuntimeException (errorMsg + "\n" 74 + HexDump.dump(hdrData.array(), hdrData.arrayOffset(), hdr.getHeaderByteLength())); 75 } 76 } | Popular Tags |