01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tc.net.protocol.delivery;
06:
07: import com.tc.bytes.TCByteBuffer;
08: import com.tc.net.protocol.TCProtocolException;
09: import com.tc.util.Assert;
10:
11: /**
12: * Parses incoming network data into ProtocolMessages
13: */
14: class OOOProtocolMessageParser {
15: private final OOOProtocolMessageFactory messageFactory;
16:
17: public OOOProtocolMessageParser(
18: OOOProtocolMessageFactory messageFactory) {
19: this .messageFactory = messageFactory;
20: }
21:
22: public OOOProtocolMessage parseMessage(TCByteBuffer[] data)
23: throws TCProtocolException {
24: int hdrLength = OOOProtocolMessageHeader.HEADER_LENGTH;
25: if (hdrLength > data[0].limit()) {
26: throw new TCProtocolException(
27: "header not contained in first buffer: "
28: + hdrLength + " > " + data[0].limit());
29: }
30:
31: OOOProtocolMessageHeader header = new OOOProtocolMessageHeader(
32: data[0].duplicate().limit(
33: OOOProtocolMessageHeader.HEADER_LENGTH));
34: header.validate();
35:
36: TCByteBuffer msgData[];
37: if (header.getHeaderByteLength() < data[0].limit()) {
38: msgData = new TCByteBuffer[data.length];
39: System.arraycopy(data, 0, msgData, 0, msgData.length);
40:
41: TCByteBuffer firstPayloadBuffer = msgData[0].duplicate();
42: firstPayloadBuffer.position(header.getHeaderByteLength());
43: msgData[0] = firstPayloadBuffer.slice();
44: } else {
45: Assert.eval(data.length >= 1);
46: msgData = new TCByteBuffer[data.length - 1];
47: System.arraycopy(data, 1, msgData, 0, msgData.length);
48: }
49:
50: return messageFactory.createNewMessage(header, msgData);
51: }
52:
53: }
|