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