View Javadoc

1   /*
2    * Copyright 2013 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.spdy;
17  
18  import io.netty.util.internal.StringUtil;
19  
20  /**
21   * The default {@link SpdySynStreamFrame} implementation.
22   */
23  public class DefaultSpdySynStreamFrame extends DefaultSpdyHeadersFrame
24          implements SpdySynStreamFrame {
25  
26      private int associatedStreamId;
27      private byte priority;
28      private boolean unidirectional;
29  
30      /**
31       * Creates a new instance.
32       *
33       * @param streamId           the Stream-ID of this frame
34       * @param associatedStreamId the Associated-To-Stream-ID of this frame
35       * @param priority           the priority of the stream
36       */
37      public DefaultSpdySynStreamFrame(int streamId, int associatedStreamId, byte priority) {
38          super(streamId);
39          setAssociatedStreamId(associatedStreamId);
40          setPriority(priority);
41      }
42  
43      @Override
44      public SpdySynStreamFrame setStreamId(int streamId) {
45          super.setStreamId(streamId);
46          return this;
47      }
48  
49      @Override
50      public SpdySynStreamFrame setLast(boolean last) {
51          super.setLast(last);
52          return this;
53      }
54  
55      @Override
56      public SpdySynStreamFrame setInvalid() {
57          super.setInvalid();
58          return this;
59      }
60  
61      @Override
62      public int associatedStreamId() {
63          return associatedStreamId;
64      }
65  
66      @Override
67      public SpdySynStreamFrame setAssociatedStreamId(int associatedStreamId) {
68          if (associatedStreamId < 0) {
69              throw new IllegalArgumentException(
70                      "Associated-To-Stream-ID cannot be negative: " +
71                      associatedStreamId);
72          }
73          this.associatedStreamId = associatedStreamId;
74          return this;
75      }
76  
77      @Override
78      public byte priority() {
79          return priority;
80      }
81  
82      @Override
83      public SpdySynStreamFrame setPriority(byte priority) {
84          if (priority < 0 || priority > 7) {
85              throw new IllegalArgumentException(
86                      "Priority must be between 0 and 7 inclusive: " + priority);
87          }
88          this.priority = priority;
89          return this;
90      }
91  
92      @Override
93      public boolean isUnidirectional() {
94          return unidirectional;
95      }
96  
97      @Override
98      public SpdySynStreamFrame setUnidirectional(boolean unidirectional) {
99          this.unidirectional = unidirectional;
100         return this;
101     }
102 
103     @Override
104     public String toString() {
105         StringBuilder buf = new StringBuilder()
106             .append(StringUtil.simpleClassName(this))
107             .append("(last: ")
108             .append(isLast())
109             .append("; unidirectional: ")
110             .append(isUnidirectional())
111             .append(')')
112             .append(StringUtil.NEWLINE)
113             .append("--> Stream-ID = ")
114             .append(streamId())
115             .append(StringUtil.NEWLINE);
116         if (associatedStreamId != 0) {
117             buf.append("--> Associated-To-Stream-ID = ")
118                .append(associatedStreamId())
119                .append(StringUtil.NEWLINE);
120         }
121         buf.append("--> Priority = ")
122            .append(priority())
123            .append(StringUtil.NEWLINE)
124            .append("--> Headers:")
125            .append(StringUtil.NEWLINE);
126         appendHeaders(buf);
127 
128         // Remove the last newline.
129         buf.setLength(buf.length() - StringUtil.NEWLINE.length());
130         return buf.toString();
131     }
132 }