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  import java.util.EnumMap;
23  
24  import org.eclipse.jetty.io.ByteBufferPool;
25  import org.eclipse.jetty.spdy.CompressionFactory;
26  import org.eclipse.jetty.spdy.api.DataInfo;
27  import org.eclipse.jetty.spdy.frames.ControlFrame;
28  import org.eclipse.jetty.spdy.frames.ControlFrameType;
29  
30  public class Generator
31  {
32      final static boolean useDirectBuffers=false;
33      private final EnumMap<ControlFrameType, ControlFrameGenerator> generators = new EnumMap<>(ControlFrameType.class);
34      private final DataFrameGenerator dataFrameGenerator;
35  
36      public Generator(ByteBufferPool bufferPool, CompressionFactory.Compressor compressor)
37      {
38          HeadersBlockGenerator headersBlockGenerator = new HeadersBlockGenerator(compressor);
39          generators.put(ControlFrameType.SYN_STREAM, new SynStreamGenerator(bufferPool, headersBlockGenerator));
40          generators.put(ControlFrameType.SYN_REPLY, new SynReplyGenerator(bufferPool, headersBlockGenerator));
41          generators.put(ControlFrameType.RST_STREAM, new RstStreamGenerator(bufferPool));
42          generators.put(ControlFrameType.SETTINGS, new SettingsGenerator(bufferPool));
43          generators.put(ControlFrameType.NOOP, new NoOpGenerator(bufferPool));
44          generators.put(ControlFrameType.PING, new PingGenerator(bufferPool));
45          generators.put(ControlFrameType.GO_AWAY, new GoAwayGenerator(bufferPool));
46          generators.put(ControlFrameType.HEADERS, new HeadersGenerator(bufferPool, headersBlockGenerator));
47          generators.put(ControlFrameType.WINDOW_UPDATE, new WindowUpdateGenerator(bufferPool));
48          generators.put(ControlFrameType.CREDENTIAL, new CredentialGenerator(bufferPool));
49  
50          dataFrameGenerator = new DataFrameGenerator(bufferPool);
51      }
52  
53      public ByteBuffer control(ControlFrame frame)
54      {
55          ControlFrameGenerator generator = generators.get(frame.getType());
56          return generator.generate(frame);
57      }
58  
59      public ByteBuffer data(int streamId, int length, DataInfo dataInfo)
60      {
61          return dataFrameGenerator.generate(streamId, length, dataInfo);
62      }
63  }