Reorganize the SPDY example
- Move the server example to spdy.server - Move the client example to spdy.client - Fix inspection warnings
This commit is contained in:
parent
20d2fb8c2e
commit
cadaeb658d
@ -13,7 +13,7 @@
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.example.spdyclient;
|
||||
package io.netty.example.spdy.client;
|
||||
|
||||
import io.netty.channel.ChannelFuture;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
@ -69,20 +69,20 @@ public class HttpResponseClientHandler extends SimpleChannelInboundHandler<HttpO
|
||||
|
||||
if (content instanceof LastHttpContent) {
|
||||
System.out.println("} END OF CONTENT");
|
||||
this.queue.add(ctx.channel().newSucceededFuture());
|
||||
queue.add(ctx.channel().newSucceededFuture());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
this.queue.add(ctx.channel().newFailedFuture(cause));
|
||||
queue.add(ctx.channel().newFailedFuture(cause));
|
||||
cause.printStackTrace();
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
public BlockingQueue<ChannelFuture> queue() {
|
||||
return this.queue;
|
||||
return queue;
|
||||
}
|
||||
|
||||
}
|
@ -13,9 +13,8 @@
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.example.spdyclient;
|
||||
package io.netty.example.spdy.client;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelFuture;
|
||||
@ -32,13 +31,15 @@ import io.netty.handler.codec.http.HttpVersion;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.*;
|
||||
|
||||
/**
|
||||
* An SPDY client that allows you to send HTTP GET to a SPDY server.
|
||||
* <p>
|
||||
* This class must be run with the JVM parameter: {@code java -Xbootclasspath/p:<path_to_npn_boot_jar> ...}. The
|
||||
* "path_to_npn_boot_jar" is the path on the file system for the NPN Boot Jar file which can be downloaded from Maven at
|
||||
* coordinates org.mortbay.jetty.npn:npn-boot. Different versions applies to different OpenJDK versions. See
|
||||
* {@link http://www.eclipse.org/jetty/documentation/current/npn-chapter.html Jetty docs} for more information.
|
||||
* <a href="http://www.eclipse.org/jetty/documentation/current/npn-chapter.html">Jetty docs</a> for more information.
|
||||
* <p>
|
||||
*/
|
||||
public class SpdyClient {
|
||||
@ -52,54 +53,54 @@ public class SpdyClient {
|
||||
public SpdyClient(String host, int port) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.httpResponseHandler = new HttpResponseClientHandler();
|
||||
httpResponseHandler = new HttpResponseClientHandler();
|
||||
}
|
||||
|
||||
public void start() {
|
||||
if (this.channel != null) {
|
||||
if (channel != null) {
|
||||
System.out.println("Already running!");
|
||||
return;
|
||||
}
|
||||
|
||||
this.workerGroup = new NioEventLoopGroup();
|
||||
workerGroup = new NioEventLoopGroup();
|
||||
|
||||
Bootstrap b = new Bootstrap();
|
||||
b.group(workerGroup);
|
||||
b.channel(NioSocketChannel.class);
|
||||
b.option(ChannelOption.SO_KEEPALIVE, true);
|
||||
b.remoteAddress(new InetSocketAddress(this.host, this.port));
|
||||
b.handler(new SpdyClientInitializer(this.httpResponseHandler));
|
||||
b.remoteAddress(new InetSocketAddress(host, port));
|
||||
b.handler(new SpdyClientInitializer(httpResponseHandler));
|
||||
|
||||
// Start the client.
|
||||
this.channel = b.connect().syncUninterruptibly().channel();
|
||||
System.out.println("Connected to [" + this.host + ":" + this.port + "]");
|
||||
channel = b.connect().syncUninterruptibly().channel();
|
||||
System.out.println("Connected to [" + host + ':' + port + ']');
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
try {
|
||||
// Wait until the connection is closed.
|
||||
this.channel.close().syncUninterruptibly();
|
||||
channel.close().syncUninterruptibly();
|
||||
} finally {
|
||||
if (this.workerGroup != null) {
|
||||
this.workerGroup.shutdownGracefully();
|
||||
if (workerGroup != null) {
|
||||
workerGroup.shutdownGracefully();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ChannelFuture send(HttpRequest request) {
|
||||
// Sends the HTTP request.
|
||||
return this.channel.writeAndFlush(request);
|
||||
return channel.writeAndFlush(request);
|
||||
}
|
||||
|
||||
public HttpRequest get() {
|
||||
HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "");
|
||||
request.headers().set(HttpHeaders.Names.HOST, this.host);
|
||||
request.headers().set(HttpHeaders.Names.HOST, host);
|
||||
request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
|
||||
return request;
|
||||
}
|
||||
|
||||
public BlockingQueue<ChannelFuture> httpResponseQueue() {
|
||||
return this.httpResponseHandler.queue();
|
||||
return httpResponseHandler.queue();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
@ -13,10 +13,8 @@
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.example.spdyclient;
|
||||
package io.netty.example.spdy.client;
|
||||
|
||||
import static io.netty.handler.codec.spdy.SpdyVersion.SPDY_3_1;
|
||||
import static io.netty.util.internal.logging.InternalLogLevel.INFO;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
@ -27,10 +25,12 @@ import io.netty.handler.codec.spdy.SpdyHttpDecoder;
|
||||
import io.netty.handler.codec.spdy.SpdyHttpEncoder;
|
||||
import io.netty.handler.codec.spdy.SpdySessionHandler;
|
||||
import io.netty.handler.ssl.SslHandler;
|
||||
import org.eclipse.jetty.npn.NextProtoNego;
|
||||
|
||||
import javax.net.ssl.SSLEngine;
|
||||
|
||||
import org.eclipse.jetty.npn.NextProtoNego;
|
||||
import static io.netty.handler.codec.spdy.SpdyVersion.*;
|
||||
import static io.netty.util.internal.logging.InternalLogLevel.*;
|
||||
|
||||
public class SpdyClientInitializer extends ChannelInitializer<SocketChannel> {
|
||||
|
@ -13,14 +13,13 @@
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.example.spdyclient;
|
||||
package io.netty.example.spdy.client;
|
||||
|
||||
import static io.netty.handler.codec.spdy.SpdyOrHttpChooser.SelectedProtocol.HTTP_1_1;
|
||||
import static io.netty.handler.codec.spdy.SpdyOrHttpChooser.SelectedProtocol.SPDY_3_1;
|
||||
import org.eclipse.jetty.npn.NextProtoNego.ClientProvider;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.jetty.npn.NextProtoNego.ClientProvider;
|
||||
import static io.netty.handler.codec.spdy.SpdyOrHttpChooser.SelectedProtocol.*;
|
||||
|
||||
/**
|
||||
* The Jetty project provides an implementation of the Transport Layer Security (TLS) extension for Next Protocol
|
||||
@ -33,7 +32,7 @@ import org.eclipse.jetty.npn.NextProtoNego.ClientProvider;
|
||||
* "path_to_npn_boot_jar" is the path on the file system for the NPN Boot Jar file which can be downloaded from Maven
|
||||
* at coordinates org.mortbay.jetty.npn:npn-boot. Different versions applies to different OpenJDK versions.
|
||||
*
|
||||
* @see http://www.eclipse.org/jetty/documentation/current/npn-chapter.html
|
||||
* @see <a href="http://www.eclipse.org/jetty/documentation/current/npn-chapter.html">Jetty documentation</a>
|
||||
*/
|
||||
public class SpdyClientProvider implements ClientProvider {
|
||||
|
||||
@ -54,6 +53,6 @@ public class SpdyClientProvider implements ClientProvider {
|
||||
|
||||
@Override
|
||||
public void unsupported() {
|
||||
this.selectedProtocol = HTTP_1_1.protocolName();
|
||||
selectedProtocol = HTTP_1_1.protocolName();
|
||||
}
|
||||
}
|
@ -13,7 +13,7 @@
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.example.spdyclient;
|
||||
package io.netty.example.spdy.client;
|
||||
|
||||
import io.netty.channel.ChannelHandlerAdapter;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
@ -37,7 +37,7 @@ public class SpdyClientStreamIdHandler extends ChannelHandlerAdapter {
|
||||
if (acceptOutboundMessage(msg)) {
|
||||
HttpMessage httpMsg = (HttpMessage) msg;
|
||||
if (!httpMsg.headers().contains(SpdyHttpHeaders.Names.STREAM_ID)) {
|
||||
SpdyHttpHeaders.setStreamId(httpMsg, this.currentStreamId);
|
||||
SpdyHttpHeaders.setStreamId(httpMsg, currentStreamId);
|
||||
// Client stream IDs are always odd
|
||||
currentStreamId += 2;
|
||||
}
|
@ -13,7 +13,7 @@
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.example.spdyclient;
|
||||
package io.netty.example.spdy.client;
|
||||
|
||||
import io.netty.channel.ChannelHandlerAdapter;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
@ -40,7 +40,7 @@ public class SpdyFrameLogger extends ChannelHandlerAdapter {
|
||||
throw new NullPointerException("level");
|
||||
}
|
||||
|
||||
this.logger = InternalLoggerFactory.getInstance(getClass());
|
||||
logger = InternalLoggerFactory.getInstance(getClass());
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
@ -60,16 +60,16 @@ public class SpdyFrameLogger extends ChannelHandlerAdapter {
|
||||
super.write(ctx, msg, promise);
|
||||
}
|
||||
|
||||
private boolean acceptMessage(Object msg) throws Exception {
|
||||
private static boolean acceptMessage(Object msg) throws Exception {
|
||||
return msg instanceof SpdyFrame;
|
||||
}
|
||||
|
||||
private void log(SpdyFrame msg, Direction d) {
|
||||
if (logger.isEnabled(this.level)) {
|
||||
if (logger.isEnabled(level)) {
|
||||
StringBuilder b = new StringBuilder("\n----------------").append(d.name()).append("--------------------\n");
|
||||
b.append(msg.toString());
|
||||
b.append(msg);
|
||||
b.append("\n------------------------------------");
|
||||
logger.log(this.level, b.toString());
|
||||
logger.log(level, b.toString());
|
||||
}
|
||||
}
|
||||
}
|
@ -16,21 +16,22 @@
|
||||
|
||||
/**
|
||||
* This package contains an example SPDY HTTP client. It will behave like a SPDY-enabled browser and you can see the
|
||||
* SPDY frames flowing in and out using the {@link io.netty.example.spdyclient.SpdyFrameLogger}.
|
||||
* SPDY frames flowing in and out using the {@link io.netty.example.spdy.client.SpdyFrameLogger}.
|
||||
*
|
||||
* <p>
|
||||
* This package relies on the Jetty project's implementation of the Transport Layer Security (TLS) extension for Next
|
||||
* Protocol Negotiation (NPN) for OpenJDK 7 is required. NPN allows the application layer to negotiate which
|
||||
* protocol, SPDY or HTTP, to use.
|
||||
* <p>
|
||||
* To start, run {@link io.netty.example.spdy.SpdyServer} with the JVM parameter:
|
||||
* To start, run {@link io.netty.example.spdy.server.SpdyServer} with the JVM parameter:
|
||||
* {@code java -Xbootclasspath/p:<path_to_npn_boot_jar> ...}.
|
||||
* The "path_to_npn_boot_jar" is the path on the file system for the NPN Boot Jar file which can be downloaded from
|
||||
* Maven at coordinates org.mortbay.jetty.npn:npn-boot. Different versions applies to different OpenJDK versions.
|
||||
* See {@link http://www.eclipse.org/jetty/documentation/current/npn-chapter.html Jetty docs} for more information.
|
||||
* See <a href="http://www.eclipse.org/jetty/documentation/current/npn-chapter.html">Jetty docs</a> for more
|
||||
* information.
|
||||
* <p>
|
||||
* After that, you can run {@link io.netty.example.spdyclient.SpdyClient}, also settings the JVM parameter
|
||||
* After that, you can run {@link io.netty.example.spdy.client.SpdyClient}, also settings the JVM parameter
|
||||
* mentioned above.
|
||||
*/
|
||||
package io.netty.example.spdyclient;
|
||||
package io.netty.example.spdy.client;
|
||||
|
@ -13,15 +13,13 @@
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.example.spdy;
|
||||
|
||||
import javax.net.ssl.SSLEngine;
|
||||
|
||||
import org.eclipse.jetty.npn.NextProtoNego;
|
||||
package io.netty.example.spdy.server;
|
||||
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.handler.codec.spdy.SpdyOrHttpChooser;
|
||||
import org.eclipse.jetty.npn.NextProtoNego;
|
||||
|
||||
import javax.net.ssl.SSLEngine;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
@ -54,5 +52,4 @@ public class SpdyOrHttpHandler extends SpdyOrHttpChooser {
|
||||
protected ChannelHandler createHttpRequestHandlerForHttp() {
|
||||
return new SpdyServerHandler();
|
||||
}
|
||||
|
||||
}
|
@ -13,7 +13,7 @@
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.example.spdy;
|
||||
package io.netty.example.spdy.server;
|
||||
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.channel.Channel;
|
||||
@ -28,10 +28,11 @@ import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||
* This class must be run with the JVM parameter: {@code java -Xbootclasspath/p:<path_to_npn_boot_jar> ...}.
|
||||
* The "path_to_npn_boot_jar" is the path on the file system for the NPN Boot Jar file which can be downloaded from
|
||||
* Maven at coordinates org.mortbay.jetty.npn:npn-boot. Different versions applies to different OpenJDK versions.
|
||||
* See {@link http://www.eclipse.org/jetty/documentation/current/npn-chapter.html Jetty docs} for more information.
|
||||
* See <a href="http://www.eclipse.org/jetty/documentation/current/npn-chapter.html">Jetty docs</a> for more
|
||||
* information.
|
||||
* <p>
|
||||
* Once started, you can test the server with your
|
||||
* {@link http://en.wikipedia.org/wiki/SPDY#Browser_support_and_usage SPDY enabled web browser} by navigating
|
||||
* <a href="http://en.wikipedia.org/wiki/SPDY#Browser_support_and_usage">SPDY enabled web browser</a> by navigating
|
||||
* to https://localhost:8443/.
|
||||
*/
|
||||
public class SpdyServer {
|
@ -13,9 +13,7 @@
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.example.spdy;
|
||||
|
||||
import java.util.Date;
|
||||
package io.netty.example.spdy.server;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
@ -27,6 +25,8 @@ import io.netty.handler.codec.http.FullHttpResponse;
|
||||
import io.netty.handler.codec.http.HttpRequest;
|
||||
import io.netty.util.CharsetUtil;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import static io.netty.handler.codec.http.HttpHeaders.Names.*;
|
||||
import static io.netty.handler.codec.http.HttpHeaders.*;
|
||||
import static io.netty.handler.codec.http.HttpResponseStatus.*;
|
||||
@ -51,7 +51,7 @@ public class SpdyServerHandler extends SimpleChannelInboundHandler<Object> {
|
||||
}
|
||||
boolean keepAlive = isKeepAlive(req);
|
||||
|
||||
ByteBuf content = Unpooled.copiedBuffer("Hello World " + (new Date()).toString(), CharsetUtil.UTF_8);
|
||||
ByteBuf content = Unpooled.copiedBuffer("Hello World " + new Date(), CharsetUtil.UTF_8);
|
||||
|
||||
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content);
|
||||
response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
|
@ -13,17 +13,16 @@
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.example.spdy;
|
||||
|
||||
import javax.net.ssl.SSLEngine;
|
||||
|
||||
import org.eclipse.jetty.npn.NextProtoNego;
|
||||
package io.netty.example.spdy.server;
|
||||
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.example.securechat.SecureChatSslContextFactory;
|
||||
import io.netty.handler.ssl.SslHandler;
|
||||
import org.eclipse.jetty.npn.NextProtoNego;
|
||||
|
||||
import javax.net.ssl.SSLEngine;
|
||||
|
||||
/**
|
||||
* Sets up the Netty pipeline
|
@ -13,14 +13,14 @@
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.example.spdy;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
package io.netty.example.spdy.server;
|
||||
|
||||
import io.netty.handler.codec.spdy.SpdyOrHttpChooser;
|
||||
import org.eclipse.jetty.npn.NextProtoNego.ServerProvider;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The Jetty project provides an implementation of the Transport Layer Security (TLS) extension for Next
|
||||
* Protocol Negotiation (NPN) for OpenJDK 7 or greater. NPN allows the application layer to negotiate which
|
||||
@ -32,7 +32,7 @@ import org.eclipse.jetty.npn.NextProtoNego.ServerProvider;
|
||||
* "path_to_npn_boot_jar" is the path on the file system for the NPN Boot Jar file which can be downloaded from
|
||||
* Maven at coordinates org.mortbay.jetty.npn:npn-boot. Different versions applies to different OpenJDK versions.
|
||||
*
|
||||
* @see http://www.eclipse.org/jetty/documentation/current/npn-chapter.html
|
||||
* @see <a href="http://www.eclipse.org/jetty/documentation/current/npn-chapter.html">Jetty documentation</a>
|
||||
*/
|
||||
public class SpdyServerProvider implements ServerProvider {
|
||||
|
@ -21,13 +21,15 @@
|
||||
* Protocol Negotiation (NPN) for OpenJDK 7 is required. NPN allows the application layer to negotiate which
|
||||
* protocol, SPDY or HTTP, to use.
|
||||
* <p>
|
||||
* To start, run {@link SpdyServer} with the JVM parameter: {@code java -Xbootclasspath/p:<path_to_npn_boot_jar> ...}.
|
||||
* To start, run {@link io.netty.example.spdy.server.SpdyServer} with the JVM parameter:
|
||||
* {@code java -Xbootclasspath/p:<path_to_npn_boot_jar> ...}.
|
||||
* The "path_to_npn_boot_jar" is the path on the file system for the NPN Boot Jar file which can be downloaded from
|
||||
* Maven at coordinates org.mortbay.jetty.npn:npn-boot. Different versions applies to different OpenJDK versions.
|
||||
* See {@link http://www.eclipse.org/jetty/documentation/current/npn-chapter.html Jetty docs} for more information.
|
||||
* See <a href="http://www.eclipse.org/jetty/documentation/current/npn-chapter.html">Jetty docs</a> for more
|
||||
* information.
|
||||
* <p>
|
||||
* Once started, you can test the server with your
|
||||
* {@link http://en.wikipedia.org/wiki/SPDY#Browser_support_and_usage SPDY enabled web browser} by navigating
|
||||
* to https://localhost:8443/.
|
||||
* <a href="http://en.wikipedia.org/wiki/SPDY#Browser_support_and_usage">SPDY enabled web browser</a> by navigating
|
||||
* to <a href="https://localhost:8443/">https://localhost:8443/</a>
|
||||
*/
|
||||
package io.netty.example.spdy;
|
||||
package io.netty.example.spdy.server;
|
Loading…
Reference in New Issue
Block a user