View Javadoc

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