View Javadoc

1   /*
2    * Copyright (c) 2012 the original author or authors.
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  
17  package org.eclipse.jetty.spdy.generator;
18  
19  import java.nio.ByteBuffer;
20  
21  import org.eclipse.jetty.spdy.ByteBufferPool;
22  import org.eclipse.jetty.spdy.api.SPDY;
23  import org.eclipse.jetty.spdy.frames.ControlFrame;
24  import org.eclipse.jetty.spdy.frames.GoAwayFrame;
25  
26  public class GoAwayGenerator extends ControlFrameGenerator
27  {
28      public GoAwayGenerator(ByteBufferPool bufferPool)
29      {
30          super(bufferPool);
31      }
32  
33      @Override
34      public ByteBuffer generate(ControlFrame frame)
35      {
36          GoAwayFrame goAway = (GoAwayFrame)frame;
37  
38          int frameBodyLength = 8;
39          int totalLength = ControlFrame.HEADER_LENGTH + frameBodyLength;
40          ByteBuffer buffer = getByteBufferPool().acquire(totalLength, true);
41          generateControlFrameHeader(goAway, frameBodyLength, buffer);
42  
43          buffer.putInt(goAway.getLastStreamId() & 0x7F_FF_FF_FF);
44          writeStatusCode(goAway, buffer);
45  
46          buffer.flip();
47          return buffer;
48      }
49  
50      private void writeStatusCode(GoAwayFrame goAway, ByteBuffer buffer)
51      {
52          switch (goAway.getVersion())
53          {
54              case SPDY.V2:
55                  break;
56              case SPDY.V3:
57                  buffer.putInt(goAway.getStatusCode());
58                  break;
59              default:
60                  throw new IllegalStateException();
61          }
62      }
63  }