KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > protocol > transport > WireProtocolMessageImpl


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.net.protocol.transport;
5
6 import com.tc.bytes.TCByteBuffer;
7 import com.tc.net.core.TCConnection;
8 import com.tc.net.protocol.AbstractTCNetworkMessage;
9 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 implements WireProtocolMessage {
20   private final TCConnection sourceConnection;
21
22   /**
23    * Wrap the given network message with a wire protocol message instance. The header for the returned instance will
24    * have it's total length
25    *
26    * @param msgPayload the network message to wrap
27    * @return a new wire protocol message instance that contains the given message as it's payload.
28    */

29   static WireProtocolMessage wrapMessage(TCNetworkMessage msgPayload, TCConnection source) {
30     WireProtocolHeader header = new WireProtocolHeader();
31     header.setProtocol(WireProtocolHeader.getProtocolForMessageClass(msgPayload));
32
33     // seal the message if necessary
34
if (!msgPayload.isSealed()) {
35       msgPayload.seal();
36     }
37
38     WireProtocolMessage rv = new WireProtocolMessageImpl(source, header, msgPayload);
39     return rv;
40   }
41
42   protected WireProtocolMessageImpl(TCConnection source, TCNetworkHeader header, TCByteBuffer[] data) {
43     super(header, data);
44     recordLength();
45     this.sourceConnection = source;
46   }
47
48   private WireProtocolMessageImpl(TCConnection source, TCNetworkHeader header, TCNetworkMessage subMessage) {
49     super(header, subMessage);
50     recordLength();
51     this.sourceConnection = source;
52   }
53   
54   public void doRecycleOnWrite() {
55     getWireProtocolHeader().recycle();
56     AbstractTCNetworkMessage messagePayLoad = (AbstractTCNetworkMessage) getMessagePayload();
57     if(messagePayLoad != null) {
58       messagePayLoad.doRecycleOnWrite();
59     }
60   }
61
62   public short getMessageProtocol() {
63     return ((WireProtocolHeader) getHeader()).getProtocol();
64   }
65
66   public WireProtocolHeader getWireProtocolHeader() {
67     return ((WireProtocolHeader) getHeader());
68   }
69
70   public TCConnection getSource() {
71     return sourceConnection;
72   }
73
74   protected void recordLength() {
75     TCNetworkMessage msgPayload = getMessagePayload();
76     // if the payload is null, then we need to record our own length as the packet length. Otherwise, we need to add the
77
// our header length + the length of the our payload message length.
78
int packetLength = msgPayload == null ? getTotalLength() : getHeader().getHeaderByteLength()
79                                                                + msgPayload.getTotalLength();
80
81     ((WireProtocolHeader) getHeader()).setTotalPacketLength(packetLength);
82   }
83
84 }
Popular Tags