1 4 package com.tc.net.protocol.tcm; 5 6 import com.tc.bytes.TCByteBuffer; 7 import com.tc.logging.TCLogger; 8 import com.tc.logging.TCLogging; 9 import com.tc.util.Assert; 10 11 14 class TCMessageParser { 15 private static final TCLogger logger = TCLogging.getLogger(TCMessageParser.class); 16 private final TCMessageFactory factory; 17 18 TCMessageParser(TCMessageFactory factory) { 19 this.factory = factory; 20 } 21 22 TCMessage parseMessage(MessageChannel source, TCByteBuffer[] data) { 23 TCMessageHeader hdr = new TCMessageHeaderImpl(data[0].duplicate().limit(TCMessageHeader.HEADER_LENGTH)); 24 final int headerLength = hdr.getHeaderByteLength(); 25 26 if (headerLength != TCMessageHeader.HEADER_LENGTH) { 27 logger.error("Invalid header length ! length = " + headerLength); 28 logger.error("error header = " + hdr); 29 logger.error(" buffer data is " + toString(data)); 30 throw new RuntimeException ("Invalid header length: " + headerLength); 31 } 32 33 final TCByteBuffer msgData[]; 34 35 if (data[0].limit() > headerLength) { 36 msgData = new TCByteBuffer[data.length]; 37 System.arraycopy(data, 0, msgData, 0, msgData.length); 38 msgData[0] = msgData[0].position(headerLength).slice(); 39 } else { 40 Assert.eval(data.length > 1); 41 msgData = new TCByteBuffer[data.length - 1]; 42 System.arraycopy(data, 1, msgData, 0, msgData.length); 43 } 44 45 final int msgType = hdr.getMessageType(); 46 final TCMessageType type = TCMessageType.getInstance(hdr.getMessageType()); 47 48 if (type == null) { 49 throw new RuntimeException ("Can't find message type for type: " + msgType); 50 } 51 52 return factory.createMessage(source, type, hdr, msgData); 53 } 54 55 private String toString(TCByteBuffer[] data) { 56 if(data == null || data.length == 0) { return "null or size 0"; } 57 StringBuffer sb = new StringBuffer (); 58 for (int i = 0; i < data.length; i++) { 59 sb.append(data[i]); 60 sb.append(" { "); 61 byte b[] = data[i].array(); 62 for (int j = 0; j < b.length; j++) { 63 sb.append(b[j]).append(" "); 64 } 65 sb.append(" } "); 66 } 67 return sb.toString(); 68 } 69 } | Popular Tags |