View Javadoc

1   //========================================================================
2   //Copyright 2011-2012 Mort Bay Consulting Pty. Ltd.
3   //------------------------------------------------------------------------
4   //All rights reserved. This program and the accompanying materials
5   //are made available under the terms of the Eclipse Public License v1.0
6   //and Apache License v2.0 which accompanies this distribution.
7   //The Eclipse Public License is available at
8   //http://www.eclipse.org/legal/epl-v10.html
9   //The Apache License v2.0 is available at
10  //http://www.opensource.org/licenses/apache2.0.php
11  //You may elect to redistribute this code under either of these licenses.
12  //========================================================================
13  
14  package org.eclipse.jetty.spdy.generator;
15  
16  import java.nio.ByteBuffer;
17  
18  import org.eclipse.jetty.spdy.ByteBufferPool;
19  import org.eclipse.jetty.spdy.api.SPDY;
20  import org.eclipse.jetty.spdy.frames.ControlFrame;
21  import org.eclipse.jetty.spdy.frames.GoAwayFrame;
22  
23  public class GoAwayGenerator extends ControlFrameGenerator
24  {
25      public GoAwayGenerator(ByteBufferPool bufferPool)
26      {
27          super(bufferPool);
28      }
29  
30      @Override
31      public ByteBuffer generate(ControlFrame frame)
32      {
33          GoAwayFrame goAway = (GoAwayFrame)frame;
34  
35          int frameBodyLength = 8;
36          int totalLength = ControlFrame.HEADER_LENGTH + frameBodyLength;
37          ByteBuffer buffer = getByteBufferPool().acquire(totalLength, true);
38          generateControlFrameHeader(goAway, frameBodyLength, buffer);
39  
40          buffer.putInt(goAway.getLastStreamId() & 0x7F_FF_FF_FF);
41          writeStatusCode(goAway, buffer);
42  
43          buffer.flip();
44          return buffer;
45      }
46  
47      private void writeStatusCode(GoAwayFrame goAway, ByteBuffer buffer)
48      {
49          switch (goAway.getVersion())
50          {
51              case SPDY.V2:
52                  break;
53              case SPDY.V3:
54                  buffer.putInt(goAway.getStatusCode());
55                  break;
56              default:
57                  throw new IllegalStateException();
58          }
59      }
60  }