View Javadoc

1   /*
2    *  Copyright 2006 Simon Raess
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package net.sf.beep4j.internal.tcp;
17  
18  import net.sf.beep4j.ProtocolException;
19  import net.sf.beep4j.internal.util.ByteUtil;
20  
21  /**
22   * Object representation of a SEQ header as used by the TCP transport
23   * mapping defined in RFC 3081.
24   * 
25   * @author Simon Raess
26   */
27  public class SEQHeader {
28  	
29  	public static final String TYPE = "SEQ";
30  	
31  	private int channel;
32  	
33  	private long acknowledgeNumber;
34  	
35  	private int windowSize;
36  	
37  	public SEQHeader(int channel, long acknowledgeNumber, int windowSize) {
38  		this.channel = channel;
39  		this.acknowledgeNumber = acknowledgeNumber;
40  		this.windowSize = windowSize;
41  	}
42  	
43  	public SEQHeader(String[] tokens) {
44  		if (tokens.length != 4) {
45  			throw new ProtocolException("header must consist of 4 tokens");
46  		}
47  		if (!TYPE.equals(tokens[0])) {
48  			throw new ProtocolException("unkown header type: " + tokens[0]);
49  		}
50  		this.channel = ByteUtil.parseUnsignedInt("channel", tokens[1]);
51  		this.acknowledgeNumber = ByteUtil.parseUnsignedLong("acknowledge number", tokens[2]);
52  		this.windowSize = ByteUtil.parseUnsignedInt("window size", tokens[3]);
53  	}
54  	
55  	public final String getType() {
56  		return TYPE;
57  	}
58  	
59  	public int getPayloadSize() {
60  		return 0;
61  	}
62  
63  	public long getAcknowledgeNumber() {
64  		return acknowledgeNumber;
65  	}
66  
67  	public int getChannel() {
68  		return channel;
69  	}
70  
71  	public int getWindowSize() {
72  		return windowSize;
73  	}
74  	
75  	public String[] getTokens() {
76  		return new String[] {
77  			"SEQ", "" + getChannel(), "" + getAcknowledgeNumber(), "" + getWindowSize()
78  		};
79  	}
80  	
81  	@Override
82  	public String toString() {
83  		return "SEQ " + channel + " " + acknowledgeNumber + " " + windowSize;
84  	}
85  	
86  }