View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 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.http2.generator;
20  
21  import org.eclipse.jetty.http2.frames.DataFrame;
22  import org.eclipse.jetty.http2.frames.Frame;
23  import org.eclipse.jetty.http2.frames.FrameType;
24  import org.eclipse.jetty.http2.hpack.HpackEncoder;
25  import org.eclipse.jetty.io.ByteBufferPool;
26  
27  public class Generator
28  {
29      private final ByteBufferPool byteBufferPool;
30      private final HeaderGenerator headerGenerator;
31      private final HpackEncoder hpackEncoder;
32      private final FrameGenerator[] generators;
33      private final DataGenerator dataGenerator;
34  
35      public Generator(ByteBufferPool byteBufferPool)
36      {
37          this(byteBufferPool, 4096, 0);
38      }
39  
40      public Generator(ByteBufferPool byteBufferPool, int maxDynamicTableSize, int maxHeaderBlockFragment)
41      {
42          this.byteBufferPool = byteBufferPool;
43  
44          headerGenerator = new HeaderGenerator();
45          hpackEncoder = new HpackEncoder(maxDynamicTableSize);
46  
47          this.generators = new FrameGenerator[FrameType.values().length];
48          this.generators[FrameType.HEADERS.getType()] = new HeadersGenerator(headerGenerator, hpackEncoder, maxHeaderBlockFragment);
49          this.generators[FrameType.PRIORITY.getType()] = new PriorityGenerator(headerGenerator);
50          this.generators[FrameType.RST_STREAM.getType()] = new ResetGenerator(headerGenerator);
51          this.generators[FrameType.SETTINGS.getType()] = new SettingsGenerator(headerGenerator);
52          this.generators[FrameType.PUSH_PROMISE.getType()] = new PushPromiseGenerator(headerGenerator, hpackEncoder);
53          this.generators[FrameType.PING.getType()] = new PingGenerator(headerGenerator);
54          this.generators[FrameType.GO_AWAY.getType()] = new GoAwayGenerator(headerGenerator);
55          this.generators[FrameType.WINDOW_UPDATE.getType()] = new WindowUpdateGenerator(headerGenerator);
56          this.generators[FrameType.CONTINUATION.getType()] = null; // Never generated explicitly.
57          this.generators[FrameType.PREFACE.getType()] = new PrefaceGenerator();
58          this.generators[FrameType.DISCONNECT.getType()] = new DisconnectGenerator();
59  
60          this.dataGenerator = new DataGenerator(headerGenerator);
61      }
62  
63      public ByteBufferPool getByteBufferPool()
64      {
65          return byteBufferPool;
66      }
67  
68      public void setHeaderTableSize(int headerTableSize)
69      {
70          hpackEncoder.setRemoteMaxDynamicTableSize(headerTableSize);
71      }
72  
73      public void setMaxFrameSize(int maxFrameSize)
74      {
75          headerGenerator.setMaxFrameSize(maxFrameSize);
76      }
77  
78      public void control(ByteBufferPool.Lease lease, Frame frame)
79      {
80          generators[frame.getType().getType()].generate(lease, frame);
81      }
82  
83      public void data(ByteBufferPool.Lease lease, DataFrame frame, int maxLength)
84      {
85          dataGenerator.generate(lease, frame, maxLength);
86      }
87  }