KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > protocol > tcm > TCMessageHeaderImpl


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.tcm;
5
6 import com.tc.bytes.TCByteBuffer;
7 import com.tc.net.protocol.AbstractTCNetworkHeader;
8 import com.tc.net.protocol.TCProtocolException;
9 import com.tc.util.Assert;
10
11 /**
12  * TODO: document me
13  *
14  * @author teck
15  */

16 public class TCMessageHeaderImpl extends AbstractTCNetworkHeader implements TCMessageHeader {
17   // TODO: This class (and other network headers) should be auto-generated from some form of definition file
18

19   protected TCMessageHeaderImpl(TCByteBuffer hdrData) {
20     super(hdrData, MIN_LENGTH, MAX_LENGTH);
21   }
22
23   TCMessageHeaderImpl(TCMessageType type) {
24     super(MIN_LENGTH, MAX_LENGTH);
25
26     setMessageType(type.getType());
27     setVersion(VERSION_1);
28     setHeaderLength((short) (MIN_LENGTH / 4));
29   }
30
31   public int getHeaderByteLength() {
32     return getHeaderLength() * 4;
33   }
34
35   public short getVersion() {
36     return data.getUbyte(0);
37   }
38
39   public int getHeaderLength() {
40     return data.getUbyte(1);
41   }
42
43   public int getMessageType() {
44     return data.getUshort(2);
45   }
46
47   public int getMessageTypeVersion() {
48     return data.getUshort(4);
49   }
50
51   void setVersion(short version) {
52     data.putUbyte(0, version);
53   }
54
55   protected void setHeaderLength(short length) {
56     Assert.eval(length <= MAX_LENGTH);
57     data.putUbyte(1, length);
58   }
59
60   public void setMessageType(int type) {
61     data.putUshort(2, type);
62   }
63
64   public void setMessageTypeVersion(int version) {
65     data.putUshort(4, version);
66   }
67
68   public String JavaDoc toString() {
69     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
70
71     TCMessageType type = TCMessageType.getInstance(getMessageType());
72
73     buf.append("msgType: ");
74     if (type != null) {
75       buf.append(type.toString());
76     } else {
77       buf.append("UNKNOWN").append('(').append(getMessageType()).append(')');
78     }
79
80     buf.append(", msgVer=").append(getMessageTypeVersion());
81     buf.append('\n');
82
83     return buf.toString();
84   }
85
86   public void validate() throws TCProtocolException {
87     final short version = getVersion();
88     final short expect = VERSION_1;
89     if (version != expect) { throw new TCProtocolException("Version " + version + " does not match expected version "
90                                                            + expect); }
91         
92     // XXX: validate other fields
93
}
94
95   protected boolean isHeaderLengthAvail() {
96     return data.position() > 1;
97   }
98
99 }
Popular Tags