1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.eclipse.jetty.spdy;
18
19 import java.io.IOException;
20 import java.nio.channels.SocketChannel;
21 import java.util.concurrent.Executor;
22 import java.util.concurrent.ScheduledExecutorService;
23
24 import org.eclipse.jetty.io.AsyncEndPoint;
25 import org.eclipse.jetty.io.Connection;
26 import org.eclipse.jetty.io.nio.AsyncConnection;
27 import org.eclipse.jetty.spdy.api.server.ServerSessionFrameListener;
28 import org.eclipse.jetty.spdy.generator.Generator;
29 import org.eclipse.jetty.spdy.parser.Parser;
30
31 public class ServerSPDYAsyncConnectionFactory implements AsyncConnectionFactory
32 {
33 private final ByteBufferPool bufferPool;
34 private final Executor threadPool;
35 private final ScheduledExecutorService scheduler;
36 private final short version;
37 private final ServerSessionFrameListener listener;
38
39 public ServerSPDYAsyncConnectionFactory(short version, ByteBufferPool bufferPool, Executor threadPool, ScheduledExecutorService scheduler)
40 {
41 this(version, bufferPool, threadPool, scheduler, null);
42 }
43
44 public ServerSPDYAsyncConnectionFactory(short version, ByteBufferPool bufferPool, Executor threadPool, ScheduledExecutorService scheduler, ServerSessionFrameListener listener)
45 {
46 this.version = version;
47 this.bufferPool = bufferPool;
48 this.threadPool = threadPool;
49 this.scheduler = scheduler;
50 this.listener = listener;
51 }
52
53 @Override
54 public AsyncConnection newAsyncConnection(SocketChannel channel, AsyncEndPoint endPoint, Object attachment)
55 {
56 CompressionFactory compressionFactory = new StandardCompressionFactory();
57 Parser parser = new Parser(compressionFactory.newDecompressor());
58 Generator generator = new Generator(bufferPool, compressionFactory.newCompressor());
59
60 ServerSessionFrameListener listener = this.listener;
61 if (listener == null)
62 listener = newServerSessionFrameListener(endPoint, attachment);
63
64 SPDYServerConnector connector = (SPDYServerConnector)attachment;
65
66 SPDYAsyncConnection connection = new ServerSPDYAsyncConnection(endPoint, bufferPool, parser, listener, connector);
67 endPoint.setConnection(connection);
68
69 final StandardSession session = new StandardSession(version, bufferPool, threadPool, scheduler, connection, connection, 2, listener, generator);
70 parser.addListener(session);
71 connection.setSession(session);
72
73 connector.sessionOpened(session);
74
75 return connection;
76 }
77
78 protected ServerSessionFrameListener newServerSessionFrameListener(AsyncEndPoint endPoint, Object attachment)
79 {
80 return listener;
81 }
82
83 private static class ServerSPDYAsyncConnection extends SPDYAsyncConnection
84 {
85 private final ServerSessionFrameListener listener;
86 private final SPDYServerConnector connector;
87 private volatile boolean connected;
88
89 private ServerSPDYAsyncConnection(AsyncEndPoint endPoint, ByteBufferPool bufferPool, Parser parser, ServerSessionFrameListener listener, SPDYServerConnector connector)
90 {
91 super(endPoint, bufferPool, parser);
92 this.listener = listener;
93 this.connector = connector;
94 }
95
96 @Override
97 public Connection handle() throws IOException
98 {
99 if (!connected)
100 {
101
102 if (listener != null)
103 listener.onConnect(getSession());
104 connected = true;
105 }
106 return super.handle();
107 }
108
109 @Override
110 public void onClose()
111 {
112 super.onClose();
113 connector.sessionClosed(getSession());
114 }
115 }
116 }