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.client;
20  
21  import java.nio.channels.SocketChannel;
22  
23  import org.eclipse.jetty.io.ByteBufferPool;
24  import org.eclipse.jetty.io.Connection;
25  import org.eclipse.jetty.io.EndPoint;
26  import org.eclipse.jetty.spdy.CompressionFactory;
27  import org.eclipse.jetty.spdy.FlowControlStrategy;
28  import org.eclipse.jetty.spdy.StandardCompressionFactory;
29  import org.eclipse.jetty.spdy.StandardSession;
30  import org.eclipse.jetty.spdy.client.SPDYClient.Factory;
31  import org.eclipse.jetty.spdy.client.SPDYClient.SessionPromise;
32  import org.eclipse.jetty.spdy.generator.Generator;
33  import org.eclipse.jetty.spdy.parser.Parser;
34  
35  public class SPDYClientConnectionFactory
36  {
37      public Connection newConnection(SocketChannel channel, EndPoint endPoint, Object attachment)
38      {
39          SessionPromise sessionPromise = (SessionPromise)attachment;
40          SPDYClient client = sessionPromise.client;
41          Factory factory = client.factory;
42          ByteBufferPool bufferPool = factory.getByteBufferPool();
43  
44          CompressionFactory compressionFactory = new StandardCompressionFactory();
45          Parser parser = new Parser(compressionFactory.newDecompressor());
46          Generator generator = new Generator(bufferPool, compressionFactory.newCompressor());
47  
48          SPDYConnection connection = new ClientSPDYConnection(endPoint, bufferPool, parser, factory, client.isExecuteOnFillable());
49  
50          FlowControlStrategy flowControlStrategy = client.newFlowControlStrategy();
51  
52          StandardSession session = new StandardSession(client.version, bufferPool, factory.getExecutor(),
53                  factory.getScheduler(), connection, endPoint, connection, 1, sessionPromise.listener, generator,
54                  flowControlStrategy);
55          session.setWindowSize(client.getInitialWindowSize());
56          parser.addListener(session);
57          sessionPromise.succeeded(session);
58          connection.setSession(session);
59  
60          factory.sessionOpened(session);
61  
62          return connection;
63      }
64  
65      private class ClientSPDYConnection extends SPDYConnection
66      {
67          private final Factory factory;
68  
69          public ClientSPDYConnection(EndPoint endPoint, ByteBufferPool bufferPool, Parser parser, Factory factory,
70                                      boolean executeOnFillable)
71          {
72              super(endPoint, bufferPool, parser, factory.getExecutor(), executeOnFillable);
73              this.factory = factory;
74          }
75  
76          @Override
77          public void onClose()
78          {
79              super.onClose();
80              factory.sessionClosed(getSession());
81          }
82      }
83  }