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.protocol.transport;
05:
06: import com.tc.bytes.TCByteBuffer;
07: import com.tc.net.core.TCConnection;
08: import com.tc.net.protocol.AbstractTCNetworkMessage;
09: import com.tc.net.protocol.TCNetworkHeader;
10: import com.tc.net.protocol.TCNetworkMessage;
11:
12: /**
13: * Wire protocol message. All network communications in the TC world are conducted with wire protocol messages. Wire
14: * protocol is a lot like TCP/IP in the sense that is meant to carry other "application" level protocols on top/within
15: * it
16: *
17: * @author teck
18: */
19: public class WireProtocolMessageImpl extends AbstractTCNetworkMessage
20: implements WireProtocolMessage {
21: private final TCConnection sourceConnection;
22:
23: /**
24: * Wrap the given network message with a wire protocol message instance. The header for the returned instance will
25: * have it's total length
26: *
27: * @param msgPayload the network message to wrap
28: * @return a new wire protocol message instance that contains the given message as it's payload.
29: */
30: public static WireProtocolMessage wrapMessage(
31: TCNetworkMessage msgPayload, TCConnection source) {
32: WireProtocolHeader header = new WireProtocolHeader();
33: header.setProtocol(WireProtocolHeader
34: .getProtocolForMessageClass(msgPayload));
35:
36: // seal the message if necessary
37: if (!msgPayload.isSealed()) {
38: msgPayload.seal();
39: }
40:
41: WireProtocolMessage rv = new WireProtocolMessageImpl(source,
42: header, msgPayload);
43: return rv;
44: }
45:
46: protected WireProtocolMessageImpl(TCConnection source,
47: TCNetworkHeader header, TCByteBuffer[] data) {
48: super (header, data);
49: recordLength();
50: this .sourceConnection = source;
51: }
52:
53: private WireProtocolMessageImpl(TCConnection source,
54: TCNetworkHeader header, TCNetworkMessage subMessage) {
55: super (header, subMessage);
56: recordLength();
57: this .sourceConnection = source;
58: }
59:
60: public void doRecycleOnWrite() {
61: getWireProtocolHeader().recycle();
62: AbstractTCNetworkMessage messagePayLoad = (AbstractTCNetworkMessage) getMessagePayload();
63: if (messagePayLoad != null) {
64: messagePayLoad.doRecycleOnWrite();
65: }
66: }
67:
68: public short getMessageProtocol() {
69: return ((WireProtocolHeader) getHeader()).getProtocol();
70: }
71:
72: public WireProtocolHeader getWireProtocolHeader() {
73: return ((WireProtocolHeader) getHeader());
74: }
75:
76: public TCConnection getSource() {
77: return sourceConnection;
78: }
79:
80: protected void recordLength() {
81: TCNetworkMessage msgPayload = getMessagePayload();
82: // if the payload is null, then we need to record our own length as the packet length. Otherwise, we need to add the
83: // our header length + the length of the our payload message length.
84: int packetLength = msgPayload == null ? getTotalLength()
85: : getHeader().getHeaderByteLength()
86: + msgPayload.getTotalLength();
87:
88: ((WireProtocolHeader) getHeader())
89: .setTotalPacketLength(packetLength);
90: }
91:
92: }
|