1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.spdy.client;
20
21 import java.io.IOException;
22 import java.util.Map;
23
24 import org.eclipse.jetty.io.ByteBufferPool;
25 import org.eclipse.jetty.io.ClientConnectionFactory;
26 import org.eclipse.jetty.io.Connection;
27 import org.eclipse.jetty.io.EndPoint;
28 import org.eclipse.jetty.spdy.CompressionFactory;
29 import org.eclipse.jetty.spdy.FlowControlStrategy;
30 import org.eclipse.jetty.spdy.StandardCompressionFactory;
31 import org.eclipse.jetty.spdy.StandardSession;
32 import org.eclipse.jetty.spdy.api.Session;
33 import org.eclipse.jetty.spdy.api.SessionFrameListener;
34 import org.eclipse.jetty.spdy.client.SPDYClient.Factory;
35 import org.eclipse.jetty.spdy.generator.Generator;
36 import org.eclipse.jetty.spdy.parser.Parser;
37 import org.eclipse.jetty.util.Promise;
38
39 public class SPDYClientConnectionFactory implements ClientConnectionFactory
40 {
41 public static final String SPDY_CLIENT_CONTEXT_KEY = "spdy.client";
42 public static final String SPDY_SESSION_LISTENER_CONTEXT_KEY = "spdy.session.listener";
43 public static final String SPDY_SESSION_PROMISE_CONTEXT_KEY = "spdy.session.promise";
44
45 @Override
46 public Connection newConnection(EndPoint endPoint, Map<String, Object> context) throws IOException
47 {
48 SPDYClient client = (SPDYClient)context.get(SPDY_CLIENT_CONTEXT_KEY);
49 SPDYClient.Factory factory = client.getFactory();
50 ByteBufferPool byteBufferPool = factory.getByteBufferPool();
51 CompressionFactory compressionFactory = new StandardCompressionFactory();
52 Parser parser = new Parser(compressionFactory.newDecompressor());
53 Generator generator = new Generator(byteBufferPool, compressionFactory.newCompressor());
54
55 SPDYConnection connection = new ClientSPDYConnection(endPoint, byteBufferPool, parser, factory, client.isDispatchIO());
56
57 FlowControlStrategy flowControlStrategy = client.newFlowControlStrategy();
58
59 SessionFrameListener listener = (SessionFrameListener)context.get(SPDY_SESSION_LISTENER_CONTEXT_KEY);
60 StandardSession session = new StandardSession(client.getVersion(), byteBufferPool,
61 factory.getScheduler(), connection, endPoint, connection, 1, listener, generator, flowControlStrategy);
62
63 session.setWindowSize(client.getInitialWindowSize());
64 parser.addListener(session);
65 connection.setSession(session);
66
67 @SuppressWarnings("unchecked")
68 Promise<Session> promise = (Promise<Session>)context.get(SPDY_SESSION_PROMISE_CONTEXT_KEY);
69 promise.succeeded(session);
70
71 return connection;
72 }
73
74 private class ClientSPDYConnection extends SPDYConnection
75 {
76 private final Factory factory;
77
78 public ClientSPDYConnection(EndPoint endPoint, ByteBufferPool bufferPool, Parser parser, Factory factory, boolean dispatchIO)
79 {
80 super(endPoint, bufferPool, parser, factory.getExecutor(), dispatchIO);
81 this.factory = factory;
82 }
83
84 @Override
85 public void onOpen()
86 {
87 super.onOpen();
88 factory.sessionOpened(getSession());
89 }
90
91 @Override
92 public void onClose()
93 {
94 super.onClose();
95 factory.sessionClosed(getSession());
96 }
97 }
98 }