Overall code cleanup
* Fixes based on checkstyle feed back * Simplify WebSocket client example (needs more work) * Rename Autobahn test server classes
This commit is contained in:
parent
831a13694c
commit
c850bd9231
@ -76,8 +76,7 @@ final class LocalClientChannelSink extends AbstractChannelSink {
|
|||||||
future.setSuccess();
|
future.setSuccess();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
} else if (e instanceof MessageEvent) {
|
||||||
else if (e instanceof MessageEvent) {
|
|
||||||
MessageEvent event = (MessageEvent) e;
|
MessageEvent event = (MessageEvent) e;
|
||||||
DefaultLocalChannel channel = (DefaultLocalChannel) event.getChannel();
|
DefaultLocalChannel channel = (DefaultLocalChannel) event.getChannel();
|
||||||
boolean offered = channel.writeBuffer.offer(event);
|
boolean offered = channel.writeBuffer.offer(event);
|
||||||
|
@ -194,10 +194,6 @@ class NioSocketChannel extends AbstractChannel
|
|||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* (non-Javadoc)
|
|
||||||
* @see java.util.concurrent.BlockingQueue#offer(java.lang.Object)
|
|
||||||
*/
|
|
||||||
public boolean offer(MessageEvent e) {
|
public boolean offer(MessageEvent e) {
|
||||||
boolean success = queue.offer(e);
|
boolean success = queue.offer(e);
|
||||||
assert success;
|
assert success;
|
||||||
@ -219,10 +215,6 @@ class NioSocketChannel extends AbstractChannel
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* (non-Javadoc)
|
|
||||||
* @see java.util.Queue#poll()
|
|
||||||
*/
|
|
||||||
public MessageEvent poll() {
|
public MessageEvent poll() {
|
||||||
MessageEvent e = queue.poll();
|
MessageEvent e = queue.poll();
|
||||||
if (e != null) {
|
if (e != null) {
|
||||||
|
@ -30,28 +30,6 @@ import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
|
|||||||
*/
|
*/
|
||||||
public class DiscardClient {
|
public class DiscardClient {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
// Print usage if no argument is specified.
|
|
||||||
if (args.length < 2 || args.length > 3) {
|
|
||||||
System.err.println(
|
|
||||||
"Usage: " + DiscardClient.class.getSimpleName() +
|
|
||||||
" <host> <port> [<first message size>]");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse options.
|
|
||||||
final String host = args[0];
|
|
||||||
final int port = Integer.parseInt(args[1]);
|
|
||||||
final int firstMessageSize;
|
|
||||||
if (args.length == 3) {
|
|
||||||
firstMessageSize = Integer.parseInt(args[2]);
|
|
||||||
} else {
|
|
||||||
firstMessageSize = 256;
|
|
||||||
}
|
|
||||||
|
|
||||||
new DiscardClient(host, port, firstMessageSize).run();
|
|
||||||
}
|
|
||||||
|
|
||||||
private final String host;
|
private final String host;
|
||||||
private final int port;
|
private final int port;
|
||||||
private final int firstMessageSize;
|
private final int firstMessageSize;
|
||||||
@ -86,4 +64,26 @@ public class DiscardClient {
|
|||||||
// Shut down thread pools to exit.
|
// Shut down thread pools to exit.
|
||||||
bootstrap.releaseExternalResources();
|
bootstrap.releaseExternalResources();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
// Print usage if no argument is specified.
|
||||||
|
if (args.length < 2 || args.length > 3) {
|
||||||
|
System.err.println(
|
||||||
|
"Usage: " + DiscardClient.class.getSimpleName() +
|
||||||
|
" <host> <port> [<first message size>]");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse options.
|
||||||
|
final String host = args[0];
|
||||||
|
final int port = Integer.parseInt(args[1]);
|
||||||
|
final int firstMessageSize;
|
||||||
|
if (args.length == 3) {
|
||||||
|
firstMessageSize = Integer.parseInt(args[2]);
|
||||||
|
} else {
|
||||||
|
firstMessageSize = 256;
|
||||||
|
}
|
||||||
|
|
||||||
|
new DiscardClient(host, port, firstMessageSize).run();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,8 +29,10 @@ import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
|
|||||||
*/
|
*/
|
||||||
public class DiscardServer {
|
public class DiscardServer {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
private final int port;
|
||||||
new DiscardServer().run();
|
|
||||||
|
public DiscardServer(int port) {
|
||||||
|
this.port = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
@ -48,6 +50,16 @@ public class DiscardServer {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Bind and start to accept incoming connections.
|
// Bind and start to accept incoming connections.
|
||||||
bootstrap.bind(new InetSocketAddress(8080));
|
bootstrap.bind(new InetSocketAddress(port));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
int port;
|
||||||
|
if (args.length > 0) {
|
||||||
|
port = Integer.parseInt(args[0]);
|
||||||
|
} else {
|
||||||
|
port = 8080;
|
||||||
|
}
|
||||||
|
new DiscardServer(port).run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,28 +33,6 @@ import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
|
|||||||
*/
|
*/
|
||||||
public class EchoClient {
|
public class EchoClient {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
// Print usage if no argument is specified.
|
|
||||||
if (args.length < 2 || args.length > 3) {
|
|
||||||
System.err.println(
|
|
||||||
"Usage: " + EchoClient.class.getSimpleName() +
|
|
||||||
" <host> <port> [<first message size>]");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse options.
|
|
||||||
final String host = args[0];
|
|
||||||
final int port = Integer.parseInt(args[1]);
|
|
||||||
final int firstMessageSize;
|
|
||||||
if (args.length == 3) {
|
|
||||||
firstMessageSize = Integer.parseInt(args[2]);
|
|
||||||
} else {
|
|
||||||
firstMessageSize = 256;
|
|
||||||
}
|
|
||||||
|
|
||||||
new EchoClient(host, port, firstMessageSize).run();
|
|
||||||
}
|
|
||||||
|
|
||||||
private final String host;
|
private final String host;
|
||||||
private final int port;
|
private final int port;
|
||||||
private final int firstMessageSize;
|
private final int firstMessageSize;
|
||||||
@ -89,4 +67,26 @@ public class EchoClient {
|
|||||||
// Shut down thread pools to exit.
|
// Shut down thread pools to exit.
|
||||||
bootstrap.releaseExternalResources();
|
bootstrap.releaseExternalResources();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
// Print usage if no argument is specified.
|
||||||
|
if (args.length < 2 || args.length > 3) {
|
||||||
|
System.err.println(
|
||||||
|
"Usage: " + EchoClient.class.getSimpleName() +
|
||||||
|
" <host> <port> [<first message size>]");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse options.
|
||||||
|
final String host = args[0];
|
||||||
|
final int port = Integer.parseInt(args[1]);
|
||||||
|
final int firstMessageSize;
|
||||||
|
if (args.length == 3) {
|
||||||
|
firstMessageSize = Integer.parseInt(args[2]);
|
||||||
|
} else {
|
||||||
|
firstMessageSize = 256;
|
||||||
|
}
|
||||||
|
|
||||||
|
new EchoClient(host, port, firstMessageSize).run();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,8 +29,10 @@ import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
|
|||||||
*/
|
*/
|
||||||
public class EchoServer {
|
public class EchoServer {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
private final int port;
|
||||||
new EchoServer().run();
|
|
||||||
|
public EchoServer(int port) {
|
||||||
|
this.port = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
@ -48,6 +50,16 @@ public class EchoServer {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Bind and start to accept incoming connections.
|
// Bind and start to accept incoming connections.
|
||||||
bootstrap.bind(new InetSocketAddress(8080));
|
bootstrap.bind(new InetSocketAddress(port));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
int port;
|
||||||
|
if (args.length > 0) {
|
||||||
|
port = Integer.parseInt(args[0]);
|
||||||
|
} else {
|
||||||
|
port = 8080;
|
||||||
|
}
|
||||||
|
new EchoServer(port).run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,26 +29,6 @@ import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
|
|||||||
*/
|
*/
|
||||||
public class FactorialClient {
|
public class FactorialClient {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
// Print usage if no argument is specified.
|
|
||||||
if (args.length != 3) {
|
|
||||||
System.err.println(
|
|
||||||
"Usage: " + FactorialClient.class.getSimpleName() +
|
|
||||||
" <host> <port> <count>");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse options.
|
|
||||||
String host = args[0];
|
|
||||||
int port = Integer.parseInt(args[1]);
|
|
||||||
int count = Integer.parseInt(args[2]);
|
|
||||||
if (count <= 0) {
|
|
||||||
throw new IllegalArgumentException("count must be a positive integer.");
|
|
||||||
}
|
|
||||||
|
|
||||||
new FactorialClient(host, port, count).run();
|
|
||||||
}
|
|
||||||
|
|
||||||
private final String host;
|
private final String host;
|
||||||
private final int port;
|
private final int port;
|
||||||
private final int count;
|
private final int count;
|
||||||
@ -87,4 +67,24 @@ public class FactorialClient {
|
|||||||
// Shut down all thread pools to exit.
|
// Shut down all thread pools to exit.
|
||||||
bootstrap.releaseExternalResources();
|
bootstrap.releaseExternalResources();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
// Print usage if no argument is specified.
|
||||||
|
if (args.length != 3) {
|
||||||
|
System.err.println(
|
||||||
|
"Usage: " + FactorialClient.class.getSimpleName() +
|
||||||
|
" <host> <port> <count>");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse options.
|
||||||
|
String host = args[0];
|
||||||
|
int port = Integer.parseInt(args[1]);
|
||||||
|
int count = Integer.parseInt(args[2]);
|
||||||
|
if (count <= 0) {
|
||||||
|
throw new IllegalArgumentException("count must be a positive integer.");
|
||||||
|
}
|
||||||
|
|
||||||
|
new FactorialClient(host, port, count).run();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,8 +27,10 @@ import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
|
|||||||
*/
|
*/
|
||||||
public class FactorialServer {
|
public class FactorialServer {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
private final int port;
|
||||||
new FactorialServer().run();
|
|
||||||
|
public FactorialServer(int port) {
|
||||||
|
this.port = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
@ -42,6 +44,16 @@ public class FactorialServer {
|
|||||||
bootstrap.setPipelineFactory(new FactorialServerPipelineFactory());
|
bootstrap.setPipelineFactory(new FactorialServerPipelineFactory());
|
||||||
|
|
||||||
// Bind and start to accept incoming connections.
|
// Bind and start to accept incoming connections.
|
||||||
bootstrap.bind(new InetSocketAddress(8080));
|
bootstrap.bind(new InetSocketAddress(port));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
int port;
|
||||||
|
if (args.length > 0) {
|
||||||
|
port = Integer.parseInt(args[0]);
|
||||||
|
} else {
|
||||||
|
port = 8080;
|
||||||
|
}
|
||||||
|
new FactorialServer(port).run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,14 @@ import org.jboss.netty.bootstrap.ServerBootstrap;
|
|||||||
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
|
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
|
||||||
|
|
||||||
public class HttpStaticFileServer {
|
public class HttpStaticFileServer {
|
||||||
public static void main(String[] args) {
|
|
||||||
|
private final int port;
|
||||||
|
|
||||||
|
public HttpStaticFileServer(int port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
// Configure the server.
|
// Configure the server.
|
||||||
ServerBootstrap bootstrap = new ServerBootstrap(
|
ServerBootstrap bootstrap = new ServerBootstrap(
|
||||||
new NioServerSocketChannelFactory(
|
new NioServerSocketChannelFactory(
|
||||||
@ -33,6 +40,16 @@ public class HttpStaticFileServer {
|
|||||||
bootstrap.setPipelineFactory(new HttpStaticFileServerPipelineFactory());
|
bootstrap.setPipelineFactory(new HttpStaticFileServerPipelineFactory());
|
||||||
|
|
||||||
// Bind and start to accept incoming connections.
|
// Bind and start to accept incoming connections.
|
||||||
bootstrap.bind(new InetSocketAddress(8080));
|
bootstrap.bind(new InetSocketAddress(port));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int port;
|
||||||
|
if (args.length > 0) {
|
||||||
|
port = Integer.parseInt(args[0]);
|
||||||
|
} else {
|
||||||
|
port = 8080;
|
||||||
|
}
|
||||||
|
new HttpStaticFileServer(port).run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,15 +36,13 @@ import org.jboss.netty.handler.codec.http.HttpVersion;
|
|||||||
*/
|
*/
|
||||||
public class HttpClient {
|
public class HttpClient {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
private final URI uri;
|
||||||
if (args.length != 1) {
|
|
||||||
System.err.println(
|
|
||||||
"Usage: " + HttpClient.class.getSimpleName() +
|
|
||||||
" <URL>");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
URI uri = new URI(args[0]);
|
public HttpClient(URI uri) {
|
||||||
|
this.uri = uri;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
String scheme = uri.getScheme() == null? "http" : uri.getScheme();
|
String scheme = uri.getScheme() == null? "http" : uri.getScheme();
|
||||||
String host = uri.getHost() == null? "localhost" : uri.getHost();
|
String host = uri.getHost() == null? "localhost" : uri.getHost();
|
||||||
int port = uri.getPort();
|
int port = uri.getPort();
|
||||||
@ -105,4 +103,16 @@ public class HttpClient {
|
|||||||
// Shut down executor threads to exit.
|
// Shut down executor threads to exit.
|
||||||
bootstrap.releaseExternalResources();
|
bootstrap.releaseExternalResources();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
if (args.length != 1) {
|
||||||
|
System.err.println(
|
||||||
|
"Usage: " + HttpClient.class.getSimpleName() +
|
||||||
|
" <URL>");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
URI uri = new URI(args[0]);
|
||||||
|
new HttpClient(uri).run();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,14 @@ import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
|
|||||||
* in a pretty plaintext form.
|
* in a pretty plaintext form.
|
||||||
*/
|
*/
|
||||||
public class HttpServer {
|
public class HttpServer {
|
||||||
public static void main(String[] args) {
|
|
||||||
|
private final int port;
|
||||||
|
|
||||||
|
public HttpServer(int port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
// Configure the server.
|
// Configure the server.
|
||||||
ServerBootstrap bootstrap = new ServerBootstrap(
|
ServerBootstrap bootstrap = new ServerBootstrap(
|
||||||
new NioServerSocketChannelFactory(
|
new NioServerSocketChannelFactory(
|
||||||
@ -37,6 +44,17 @@ public class HttpServer {
|
|||||||
bootstrap.setPipelineFactory(new HttpServerPipelineFactory());
|
bootstrap.setPipelineFactory(new HttpServerPipelineFactory());
|
||||||
|
|
||||||
// Bind and start to accept incoming connections.
|
// Bind and start to accept incoming connections.
|
||||||
bootstrap.bind(new InetSocketAddress(8080));
|
bootstrap.bind(new InetSocketAddress(port));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int port;
|
||||||
|
if (args.length > 0) {
|
||||||
|
port = Integer.parseInt(args[0]);
|
||||||
|
} else {
|
||||||
|
port = 8080;
|
||||||
|
}
|
||||||
|
new HttpServer(port).run();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
package org.jboss.netty.example.http.tunnel;
|
package org.jboss.netty.example.http.tunnel;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
@ -42,18 +43,13 @@ import org.jboss.netty.logging.InternalLogLevel;
|
|||||||
*/
|
*/
|
||||||
public class HttpTunnelingClientExample {
|
public class HttpTunnelingClientExample {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
private final URI uri;
|
||||||
if (args.length != 1) {
|
|
||||||
System.err.println(
|
|
||||||
"Usage: " + HttpTunnelingClientExample.class.getSimpleName() +
|
|
||||||
" <URL>");
|
|
||||||
System.err.println(
|
|
||||||
"Example: " + HttpTunnelingClientExample.class.getSimpleName() +
|
|
||||||
" http://localhost:8080/netty-tunnel");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
URI uri = new URI(args[0]);
|
public HttpTunnelingClientExample(URI uri) {
|
||||||
|
this.uri = uri;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() throws IOException {
|
||||||
String scheme = uri.getScheme() == null? "http" : uri.getScheme();
|
String scheme = uri.getScheme() == null? "http" : uri.getScheme();
|
||||||
|
|
||||||
// Configure the client.
|
// Configure the client.
|
||||||
@ -114,4 +110,19 @@ public class HttpTunnelingClientExample {
|
|||||||
// Shut down all threads.
|
// Shut down all threads.
|
||||||
b.releaseExternalResources();
|
b.releaseExternalResources();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
if (args.length != 1) {
|
||||||
|
System.err.println(
|
||||||
|
"Usage: " + HttpTunnelingClientExample.class.getSimpleName() +
|
||||||
|
" <URL>");
|
||||||
|
System.err.println(
|
||||||
|
"Example: " + HttpTunnelingClientExample.class.getSimpleName() +
|
||||||
|
" http://localhost:8080/netty-tunnel");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
URI uri = new URI(args[0]);
|
||||||
|
new HttpTunnelingClientExample(uri).run();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,9 +17,6 @@ package org.jboss.netty.example.http.websocketx.autobahn;
|
|||||||
|
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.logging.ConsoleHandler;
|
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
import org.jboss.netty.bootstrap.ServerBootstrap;
|
import org.jboss.netty.bootstrap.ServerBootstrap;
|
||||||
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
|
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
|
||||||
@ -28,13 +25,15 @@ import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
|
|||||||
* A Web Socket echo server for running the <a href="http://www.tavendo.de/autobahn/testsuite.html">autobahn</a> test
|
* A Web Socket echo server for running the <a href="http://www.tavendo.de/autobahn/testsuite.html">autobahn</a> test
|
||||||
* suite
|
* suite
|
||||||
*/
|
*/
|
||||||
public class WebSocketServer {
|
public class AutobahnServer {
|
||||||
public static void main(String[] args) {
|
|
||||||
ConsoleHandler ch = new ConsoleHandler();
|
|
||||||
ch.setLevel(Level.FINE);
|
|
||||||
Logger.getLogger("").addHandler(ch);
|
|
||||||
Logger.getLogger("").setLevel(Level.FINE);
|
|
||||||
|
|
||||||
|
private final int port;
|
||||||
|
|
||||||
|
public AutobahnServer(int port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
// Configure the server.
|
// Configure the server.
|
||||||
ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(
|
ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(
|
||||||
Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
|
Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
|
||||||
@ -42,11 +41,21 @@ public class WebSocketServer {
|
|||||||
// bootstrap.setOption("child.tcpNoDelay", true);
|
// bootstrap.setOption("child.tcpNoDelay", true);
|
||||||
|
|
||||||
// Set up the event pipeline factory.
|
// Set up the event pipeline factory.
|
||||||
bootstrap.setPipelineFactory(new WebSocketServerPipelineFactory());
|
bootstrap.setPipelineFactory(new AutobahnServerPipelineFactory());
|
||||||
|
|
||||||
// Bind and start to accept incoming connections.
|
// Bind and start to accept incoming connections.
|
||||||
bootstrap.bind(new InetSocketAddress(9000));
|
bootstrap.bind(new InetSocketAddress(port));
|
||||||
|
|
||||||
System.out.println("Web Socket Server started on localhost:9000.");
|
System.out.println("Web Socket Server started at port " + port);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int port;
|
||||||
|
if (args.length > 0) {
|
||||||
|
port = Integer.parseInt(args[0]);
|
||||||
|
} else {
|
||||||
|
port = 9000;
|
||||||
|
}
|
||||||
|
new AutobahnServer(port).run();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -47,8 +47,8 @@ import org.jboss.netty.util.CharsetUtil;
|
|||||||
/**
|
/**
|
||||||
* Handles handshakes and messages
|
* Handles handshakes and messages
|
||||||
*/
|
*/
|
||||||
public class WebSocketServerHandler extends SimpleChannelUpstreamHandler {
|
public class AutobahnServerHandler extends SimpleChannelUpstreamHandler {
|
||||||
private static final InternalLogger logger = InternalLoggerFactory.getInstance(WebSocketServerHandler.class);
|
private static final InternalLogger logger = InternalLoggerFactory.getInstance(AutobahnServerHandler.class);
|
||||||
|
|
||||||
private WebSocketServerHandshaker handshaker;
|
private WebSocketServerHandshaker handshaker;
|
||||||
|
|
@ -25,14 +25,14 @@ import org.jboss.netty.handler.codec.http.HttpResponseEncoder;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*/
|
*/
|
||||||
public class WebSocketServerPipelineFactory implements ChannelPipelineFactory {
|
public class AutobahnServerPipelineFactory implements ChannelPipelineFactory {
|
||||||
public ChannelPipeline getPipeline() throws Exception {
|
public ChannelPipeline getPipeline() throws Exception {
|
||||||
// Create a default pipeline implementation.
|
// Create a default pipeline implementation.
|
||||||
ChannelPipeline pipeline = pipeline();
|
ChannelPipeline pipeline = pipeline();
|
||||||
pipeline.addLast("decoder", new HttpRequestDecoder());
|
pipeline.addLast("decoder", new HttpRequestDecoder());
|
||||||
pipeline.addLast("aggregator", new HttpChunkAggregator(65536));
|
pipeline.addLast("aggregator", new HttpChunkAggregator(65536));
|
||||||
pipeline.addLast("encoder", new HttpResponseEncoder());
|
pipeline.addLast("encoder", new HttpResponseEncoder());
|
||||||
pipeline.addLast("handler", new WebSocketServerHandler());
|
pipeline.addLast("handler", new AutobahnServerHandler());
|
||||||
return pipeline;
|
return pipeline;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -47,7 +47,7 @@
|
|||||||
* }
|
* }
|
||||||
* </code>
|
* </code>
|
||||||
*
|
*
|
||||||
* <p>11. Run <tt>WebSocketServer</tt> in this package.
|
* <p>11. Run <tt>AutobahnServer</tt> in this package.
|
||||||
*
|
*
|
||||||
* <p>11. Run the test <tt>python fuzzing_client.py</tt>. Note that the actual test case python code is
|
* <p>11. Run the test <tt>python fuzzing_client.py</tt>. Note that the actual test case python code is
|
||||||
* located in <tt>/usr/local/lib/python2.6/dist-packages/autobahn-0.4.3-py2.6.egg/autobahn/cases</tt>
|
* located in <tt>/usr/local/lib/python2.6/dist-packages/autobahn-0.4.3-py2.6.egg/autobahn/cases</tt>
|
||||||
|
@ -1,126 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2011 The Netty Project
|
|
||||||
*
|
|
||||||
* The Netty Project licenses this file to you under the Apache License,
|
|
||||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
||||||
* with the License. You may obtain a copy of the License at:
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
||||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
||||||
* License for the specific language governing permissions and limitations
|
|
||||||
* under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.jboss.netty.example.http.websocketx.client;
|
|
||||||
|
|
||||||
import java.net.URI;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.logging.ConsoleHandler;
|
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
import org.jboss.netty.buffer.ChannelBuffers;
|
|
||||||
import org.jboss.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
|
|
||||||
import org.jboss.netty.handler.codec.http.websocketx.PingWebSocketFrame;
|
|
||||||
import org.jboss.netty.handler.codec.http.websocketx.PongWebSocketFrame;
|
|
||||||
import org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame;
|
|
||||||
import org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame;
|
|
||||||
import org.jboss.netty.handler.codec.http.websocketx.WebSocketVersion;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A HTTP client demo app
|
|
||||||
*/
|
|
||||||
public class App {
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
new App().runClient();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Send and receive some messages using a web socket client
|
|
||||||
*
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public void runClient() throws Exception {
|
|
||||||
|
|
||||||
MyCallbackHandler callbackHandler = new MyCallbackHandler();
|
|
||||||
WebSocketClientFactory factory = new WebSocketClientFactory();
|
|
||||||
|
|
||||||
HashMap<String, String> customHeaders = new HashMap<String, String>();
|
|
||||||
customHeaders.put("MyHeader", "MyValue");
|
|
||||||
|
|
||||||
// Connect with V13 (RFC 6455). You can change it to V08 or V00.
|
|
||||||
// If you change it to V00, ping is not supported and remember to change
|
|
||||||
// HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline.
|
|
||||||
WebSocketClient client = factory.newClient(new URI("ws://localhost:8080/websocket"), WebSocketVersion.V13,
|
|
||||||
callbackHandler, customHeaders);
|
|
||||||
|
|
||||||
// Connect
|
|
||||||
System.out.println("WebSocket Client connecting");
|
|
||||||
client.connect().awaitUninterruptibly();
|
|
||||||
Thread.sleep(200);
|
|
||||||
|
|
||||||
// Send 10 messages and wait for responses
|
|
||||||
System.out.println("WebSocket Client sending message");
|
|
||||||
for (int i = 0; i < 10; i++) {
|
|
||||||
client.send(new TextWebSocketFrame("Message #" + i));
|
|
||||||
}
|
|
||||||
Thread.sleep(1000);
|
|
||||||
|
|
||||||
// Ping
|
|
||||||
System.out.println("WebSocket Client sending ping");
|
|
||||||
client.send(new PingWebSocketFrame(ChannelBuffers.copiedBuffer(new byte[] { 1, 2, 3, 4, 5, 6 })));
|
|
||||||
Thread.sleep(1000);
|
|
||||||
|
|
||||||
// Close
|
|
||||||
System.out.println("WebSocket Client sending close");
|
|
||||||
client.send(new CloseWebSocketFrame());
|
|
||||||
Thread.sleep(1000);
|
|
||||||
|
|
||||||
// Disconnect
|
|
||||||
client.disconnect();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Our web socket callback handler for this app
|
|
||||||
*/
|
|
||||||
public static class MyCallbackHandler implements WebSocketCallback {
|
|
||||||
public boolean connected;
|
|
||||||
public ArrayList<String> messagesReceived = new ArrayList<String>();
|
|
||||||
|
|
||||||
public MyCallbackHandler() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public void onConnect(WebSocketClient client) {
|
|
||||||
System.out.println("WebSocket Client connected!");
|
|
||||||
connected = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void onDisconnect(WebSocketClient client) {
|
|
||||||
System.out.println("WebSocket Client disconnected!");
|
|
||||||
connected = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void onMessage(WebSocketClient client, WebSocketFrame frame) {
|
|
||||||
if (frame instanceof TextWebSocketFrame) {
|
|
||||||
TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
|
|
||||||
System.out.println("WebSocket Client received message:" + textFrame.getText());
|
|
||||||
messagesReceived.add(textFrame.getText());
|
|
||||||
} else if (frame instanceof PongWebSocketFrame) {
|
|
||||||
System.out.println("WebSocket Client received pong");
|
|
||||||
} else if (frame instanceof CloseWebSocketFrame) {
|
|
||||||
System.out.println("WebSocket Client received closing");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void onError(Throwable t) {
|
|
||||||
System.out.println("WebSocket Client error " + t.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,67 +0,0 @@
|
|||||||
//The MIT License
|
|
||||||
//
|
|
||||||
//Copyright (c) 2009 Carl Bystršm
|
|
||||||
//
|
|
||||||
//Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
//of this software and associated documentation files (the "Software"), to deal
|
|
||||||
//in the Software without restriction, including without limitation the rights
|
|
||||||
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
//copies of the Software, and to permit persons to whom the Software is
|
|
||||||
//furnished to do so, subject to the following conditions:
|
|
||||||
//
|
|
||||||
//The above copyright notice and this permission notice shall be included in
|
|
||||||
//all copies or substantial portions of the Software.
|
|
||||||
//
|
|
||||||
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
//THE SOFTWARE.
|
|
||||||
|
|
||||||
package org.jboss.netty.example.http.websocketx.client;
|
|
||||||
|
|
||||||
import org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Copied from https://github.com/cgbystrom/netty-tools
|
|
||||||
*
|
|
||||||
* Callbacks for the {@link WebSocketClient}. Implement and get notified when events happen.
|
|
||||||
*/
|
|
||||||
public interface WebSocketCallback {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Called when the client is connected to the server
|
|
||||||
*
|
|
||||||
* @param client
|
|
||||||
* Current client used to connect
|
|
||||||
*/
|
|
||||||
void onConnect(WebSocketClient client);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Called when the client got disconnected from the server.
|
|
||||||
*
|
|
||||||
* @param client
|
|
||||||
* Current client that was disconnected
|
|
||||||
*/
|
|
||||||
void onDisconnect(WebSocketClient client);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Called when a message arrives from the server.
|
|
||||||
*
|
|
||||||
* @param client
|
|
||||||
* Current client connected
|
|
||||||
* @param frame
|
|
||||||
* Data received from server
|
|
||||||
*/
|
|
||||||
void onMessage(WebSocketClient client, WebSocketFrame frame);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Called when an unhandled errors occurs.
|
|
||||||
*
|
|
||||||
* @param t
|
|
||||||
* The causing error
|
|
||||||
*/
|
|
||||||
void onError(Throwable t);
|
|
||||||
}
|
|
@ -21,34 +21,119 @@
|
|||||||
//THE SOFTWARE.
|
//THE SOFTWARE.
|
||||||
package org.jboss.netty.example.http.websocketx.client;
|
package org.jboss.netty.example.http.websocketx.client;
|
||||||
|
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
import org.jboss.netty.bootstrap.ClientBootstrap;
|
||||||
|
import org.jboss.netty.buffer.ChannelBuffers;
|
||||||
|
import org.jboss.netty.channel.Channel;
|
||||||
import org.jboss.netty.channel.ChannelFuture;
|
import org.jboss.netty.channel.ChannelFuture;
|
||||||
|
import org.jboss.netty.channel.ChannelPipeline;
|
||||||
|
import org.jboss.netty.channel.ChannelPipelineFactory;
|
||||||
|
import org.jboss.netty.channel.Channels;
|
||||||
|
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
|
||||||
|
import org.jboss.netty.handler.codec.http.HttpRequestEncoder;
|
||||||
|
import org.jboss.netty.handler.codec.http.HttpResponseDecoder;
|
||||||
|
import org.jboss.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
|
||||||
|
import org.jboss.netty.handler.codec.http.websocketx.PingWebSocketFrame;
|
||||||
|
import org.jboss.netty.handler.codec.http.websocketx.PongWebSocketFrame;
|
||||||
|
import org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame;
|
||||||
|
import org.jboss.netty.handler.codec.http.websocketx.WebSocketClientHandshaker;
|
||||||
|
import org.jboss.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory;
|
||||||
import org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame;
|
import org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame;
|
||||||
|
import org.jboss.netty.handler.codec.http.websocketx.WebSocketVersion;
|
||||||
|
|
||||||
/**
|
public class WebSocketClient {
|
||||||
* Copied from https://github.com/cgbystrom/netty-tools
|
|
||||||
*/
|
|
||||||
public interface WebSocketClient {
|
|
||||||
|
|
||||||
/**
|
private final URI uri;
|
||||||
* Connect to server Host and port is setup by the factory.
|
|
||||||
*
|
|
||||||
* @return Connect future. Fires when connected.
|
|
||||||
*/
|
|
||||||
ChannelFuture connect();
|
|
||||||
|
|
||||||
/**
|
public WebSocketClient(URI uri) {
|
||||||
* Disconnect from the server
|
this.uri = uri;
|
||||||
*
|
}
|
||||||
* @return Disconnect future. Fires when disconnected.
|
|
||||||
*/
|
|
||||||
ChannelFuture disconnect();
|
|
||||||
|
|
||||||
/**
|
public void run() throws Exception {
|
||||||
* Send data to server
|
ClientBootstrap bootstrap =
|
||||||
*
|
new ClientBootstrap(
|
||||||
* @param frame
|
new NioClientSocketChannelFactory(
|
||||||
* Data for sending
|
Executors.newCachedThreadPool(),
|
||||||
* @return Write future. Will fire when the data is sent.
|
Executors.newCachedThreadPool()));
|
||||||
*/
|
|
||||||
ChannelFuture send(WebSocketFrame frame);
|
String protocol = uri.getScheme();
|
||||||
|
if (!protocol.equals("ws")) {
|
||||||
|
throw new IllegalArgumentException("Unsupported protocol: " + protocol);
|
||||||
|
}
|
||||||
|
|
||||||
|
HashMap<String, String> customHeaders = new HashMap<String, String>();
|
||||||
|
customHeaders.put("MyHeader", "MyValue");
|
||||||
|
|
||||||
|
// Connect with V13 (RFC 6455). You can change it to V08 or V00.
|
||||||
|
// If you change it to V00, ping is not supported and remember to change
|
||||||
|
// HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline.
|
||||||
|
final WebSocketClientHandshaker handshaker =
|
||||||
|
new WebSocketClientHandshakerFactory().newHandshaker(
|
||||||
|
uri, WebSocketVersion.V13, null, false, customHeaders);
|
||||||
|
|
||||||
|
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
|
||||||
|
public ChannelPipeline getPipeline() throws Exception {
|
||||||
|
ChannelPipeline pipeline = Channels.pipeline();
|
||||||
|
|
||||||
|
// If you wish to support HyBi V00, you need to use
|
||||||
|
// WebSocketHttpResponseDecoder instead for
|
||||||
|
// HttpResponseDecoder.
|
||||||
|
pipeline.addLast("decoder", new HttpResponseDecoder());
|
||||||
|
|
||||||
|
pipeline.addLast("encoder", new HttpRequestEncoder());
|
||||||
|
pipeline.addLast("ws-handler", new WebSocketClientHandler(handshaker));
|
||||||
|
return pipeline;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Connect
|
||||||
|
System.out.println("WebSocket Client connecting");
|
||||||
|
ChannelFuture future =
|
||||||
|
bootstrap.connect(
|
||||||
|
new InetSocketAddress(uri.getHost(), uri.getPort()));
|
||||||
|
future.awaitUninterruptibly();
|
||||||
|
|
||||||
|
Channel ch = future.getChannel();
|
||||||
|
|
||||||
|
handshaker.performOpeningHandshake(ch);
|
||||||
|
|
||||||
|
Thread.sleep(1000);
|
||||||
|
|
||||||
|
// Send 10 messages and wait for responses
|
||||||
|
System.out.println("WebSocket Client sending message");
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
ch.write(new TextWebSocketFrame("Message #" + i));
|
||||||
|
}
|
||||||
|
Thread.sleep(1000);
|
||||||
|
|
||||||
|
// Ping
|
||||||
|
System.out.println("WebSocket Client sending ping");
|
||||||
|
ch.write(new PingWebSocketFrame(ChannelBuffers.copiedBuffer(new byte[] { 1, 2, 3, 4, 5, 6 })));
|
||||||
|
Thread.sleep(1000);
|
||||||
|
|
||||||
|
// Close
|
||||||
|
System.out.println("WebSocket Client sending close");
|
||||||
|
ch.write(new CloseWebSocketFrame());
|
||||||
|
|
||||||
|
// WebSocketClientHandler will close the connection when the server
|
||||||
|
// responds to the CloseWebSocketFrame.
|
||||||
|
ch.getCloseFuture().awaitUninterruptibly();
|
||||||
|
|
||||||
|
bootstrap.releaseExternalResources();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
URI uri;
|
||||||
|
if (args.length > 0) {
|
||||||
|
uri = new URI(args[0]);
|
||||||
|
} else {
|
||||||
|
uri = new URI("ws://localhost:8080/websocket");
|
||||||
|
}
|
||||||
|
new WebSocketClient(uri).run();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,92 +0,0 @@
|
|||||||
//The MIT License
|
|
||||||
//
|
|
||||||
//Copyright (c) 2009 Carl Bystršm
|
|
||||||
//
|
|
||||||
//Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
//of this software and associated documentation files (the "Software"), to deal
|
|
||||||
//in the Software without restriction, including without limitation the rights
|
|
||||||
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
//copies of the Software, and to permit persons to whom the Software is
|
|
||||||
//furnished to do so, subject to the following conditions:
|
|
||||||
//
|
|
||||||
//The above copyright notice and this permission notice shall be included in
|
|
||||||
//all copies or substantial portions of the Software.
|
|
||||||
//
|
|
||||||
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
//THE SOFTWARE.
|
|
||||||
|
|
||||||
package org.jboss.netty.example.http.websocketx.client;
|
|
||||||
|
|
||||||
import org.jboss.netty.bootstrap.ClientBootstrap;
|
|
||||||
import org.jboss.netty.channel.ChannelPipeline;
|
|
||||||
import org.jboss.netty.channel.ChannelPipelineFactory;
|
|
||||||
import org.jboss.netty.channel.Channels;
|
|
||||||
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
|
|
||||||
import org.jboss.netty.handler.codec.http.HttpRequestEncoder;
|
|
||||||
import org.jboss.netty.handler.codec.http.HttpResponseDecoder;
|
|
||||||
import org.jboss.netty.handler.codec.http.websocketx.WebSocketVersion;
|
|
||||||
|
|
||||||
import java.net.URI;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Copied from https://github.com/cgbystrom/netty-tools
|
|
||||||
*
|
|
||||||
* A factory for creating WebSocket clients. The entry point for creating and connecting a client. Can and should be
|
|
||||||
* used to create multiple instances.
|
|
||||||
*/
|
|
||||||
public class WebSocketClientFactory {
|
|
||||||
|
|
||||||
private final NioClientSocketChannelFactory socketChannelFactory = new NioClientSocketChannelFactory(
|
|
||||||
Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new WebSocket client
|
|
||||||
*
|
|
||||||
* @param url
|
|
||||||
* URL to connect to.
|
|
||||||
* @param version
|
|
||||||
* Web Socket version to support
|
|
||||||
* @param callback
|
|
||||||
* Callback interface to receive events
|
|
||||||
* @param customHeaders
|
|
||||||
* Map of custom headers to add to the client request
|
|
||||||
* @return A WebSocket client. Call {@link WebSocketClient#connect()} to connect.
|
|
||||||
*/
|
|
||||||
public WebSocketClient newClient(final URI url, final WebSocketVersion version, final WebSocketCallback callback,
|
|
||||||
final Map<String, String> customHeaders) {
|
|
||||||
ClientBootstrap bootstrap = new ClientBootstrap(socketChannelFactory);
|
|
||||||
|
|
||||||
String protocol = url.getScheme();
|
|
||||||
if (!protocol.equals("ws") && !protocol.equals("wss")) {
|
|
||||||
throw new IllegalArgumentException("Unsupported protocol: " + protocol);
|
|
||||||
}
|
|
||||||
|
|
||||||
final WebSocketClientHandler clientHandler = new WebSocketClientHandler(bootstrap, url, version, callback,
|
|
||||||
customHeaders);
|
|
||||||
|
|
||||||
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
|
|
||||||
|
|
||||||
public ChannelPipeline getPipeline() throws Exception {
|
|
||||||
ChannelPipeline pipeline = Channels.pipeline();
|
|
||||||
|
|
||||||
// If you wish to support HyBi V00, you need to use
|
|
||||||
// WebSocketHttpResponseDecoder instead for
|
|
||||||
// HttpResponseDecoder.
|
|
||||||
pipeline.addLast("decoder", new HttpResponseDecoder());
|
|
||||||
|
|
||||||
pipeline.addLast("encoder", new HttpRequestEncoder());
|
|
||||||
pipeline.addLast("ws-handler", clientHandler);
|
|
||||||
return pipeline;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return clientHandler;
|
|
||||||
}
|
|
||||||
}
|
|
@ -22,68 +22,39 @@
|
|||||||
|
|
||||||
package org.jboss.netty.example.http.websocketx.client;
|
package org.jboss.netty.example.http.websocketx.client;
|
||||||
|
|
||||||
import java.net.InetSocketAddress;
|
|
||||||
import java.net.URI;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.jboss.netty.bootstrap.ClientBootstrap;
|
|
||||||
import org.jboss.netty.channel.Channel;
|
import org.jboss.netty.channel.Channel;
|
||||||
import org.jboss.netty.channel.ChannelFuture;
|
|
||||||
import org.jboss.netty.channel.ChannelHandlerContext;
|
import org.jboss.netty.channel.ChannelHandlerContext;
|
||||||
import org.jboss.netty.channel.ChannelStateEvent;
|
import org.jboss.netty.channel.ChannelStateEvent;
|
||||||
import org.jboss.netty.channel.ExceptionEvent;
|
import org.jboss.netty.channel.ExceptionEvent;
|
||||||
import org.jboss.netty.channel.MessageEvent;
|
import org.jboss.netty.channel.MessageEvent;
|
||||||
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
|
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
|
||||||
import org.jboss.netty.handler.codec.http.HttpResponse;
|
import org.jboss.netty.handler.codec.http.HttpResponse;
|
||||||
|
import org.jboss.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
|
||||||
|
import org.jboss.netty.handler.codec.http.websocketx.PongWebSocketFrame;
|
||||||
|
import org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame;
|
||||||
import org.jboss.netty.handler.codec.http.websocketx.WebSocketClientHandshaker;
|
import org.jboss.netty.handler.codec.http.websocketx.WebSocketClientHandshaker;
|
||||||
import org.jboss.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory;
|
|
||||||
import org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame;
|
import org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame;
|
||||||
import org.jboss.netty.handler.codec.http.websocketx.WebSocketVersion;
|
|
||||||
import org.jboss.netty.util.CharsetUtil;
|
import org.jboss.netty.util.CharsetUtil;
|
||||||
|
|
||||||
/**
|
public class WebSocketClientHandler extends SimpleChannelUpstreamHandler {
|
||||||
* Copied from https://github.com/cgbystrom/netty-tools
|
|
||||||
*
|
|
||||||
* Handles socket communication for a connected WebSocket client Not intended for end-users. Please use
|
|
||||||
* {@link WebSocketClient} or {@link WebSocketCallback} for controlling your client.
|
|
||||||
*/
|
|
||||||
public class WebSocketClientHandler extends SimpleChannelUpstreamHandler implements WebSocketClient {
|
|
||||||
|
|
||||||
private final ClientBootstrap bootstrap;
|
private final WebSocketClientHandshaker handshaker;
|
||||||
private URI url;
|
|
||||||
private final WebSocketCallback callback;
|
|
||||||
private Channel channel;
|
|
||||||
private WebSocketClientHandshaker handshaker;
|
|
||||||
private final WebSocketVersion version;
|
|
||||||
private Map<String, String> customHeaders;
|
|
||||||
|
|
||||||
public WebSocketClientHandler(ClientBootstrap bootstrap, URI url, WebSocketVersion version,
|
public WebSocketClientHandler(WebSocketClientHandshaker handshaker) {
|
||||||
WebSocketCallback callback, Map<String, String> customHeaders) {
|
this.handshaker = handshaker;
|
||||||
this.bootstrap = bootstrap;
|
|
||||||
this.url = url;
|
|
||||||
this.version = version;
|
|
||||||
this.callback = callback;
|
|
||||||
this.customHeaders = customHeaders;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
|
|
||||||
channel = e.getChannel();
|
|
||||||
this.handshaker = new WebSocketClientHandshakerFactory()
|
|
||||||
.newHandshaker(url, version, null, false, customHeaders);
|
|
||||||
handshaker.performOpeningHandshake(channel);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
|
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
|
||||||
callback.onDisconnect(this);
|
System.out.println("WebSocket Client disconnected!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
|
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
|
||||||
|
Channel ch = ctx.getChannel();
|
||||||
if (!handshaker.isOpeningHandshakeCompleted()) {
|
if (!handshaker.isOpeningHandshakeCompleted()) {
|
||||||
handshaker.performClosingHandshake(ctx.getChannel(), (HttpResponse) e.getMessage());
|
handshaker.performClosingHandshake(ch, (HttpResponse) e.getMessage());
|
||||||
callback.onConnect(this);
|
System.out.println("WebSocket Client connected!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,34 +65,21 @@ public class WebSocketClientHandler extends SimpleChannelUpstreamHandler impleme
|
|||||||
}
|
}
|
||||||
|
|
||||||
WebSocketFrame frame = (WebSocketFrame) e.getMessage();
|
WebSocketFrame frame = (WebSocketFrame) e.getMessage();
|
||||||
callback.onMessage(this, frame);
|
if (frame instanceof TextWebSocketFrame) {
|
||||||
|
TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
|
||||||
|
System.out.println("WebSocket Client received message: " + textFrame.getText());
|
||||||
|
} else if (frame instanceof PongWebSocketFrame) {
|
||||||
|
System.out.println("WebSocket Client received pong");
|
||||||
|
} else if (frame instanceof CloseWebSocketFrame) {
|
||||||
|
System.out.println("WebSocket Client received closing");
|
||||||
|
ch.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
|
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
|
||||||
final Throwable t = e.getCause();
|
final Throwable t = e.getCause();
|
||||||
callback.onError(t);
|
t.printStackTrace();
|
||||||
e.getChannel().close();
|
e.getChannel().close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ChannelFuture connect() {
|
|
||||||
return bootstrap.connect(new InetSocketAddress(url.getHost(), url.getPort()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public ChannelFuture disconnect() {
|
|
||||||
return channel.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
public ChannelFuture send(WebSocketFrame frame) {
|
|
||||||
return channel.write(frame);
|
|
||||||
}
|
|
||||||
|
|
||||||
public URI getUrl() {
|
|
||||||
return url;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUrl(URI url) {
|
|
||||||
this.url = url;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -16,9 +16,8 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>This is an example web service client.
|
* <p>This is an example web service client.
|
||||||
* <p>To run this example, you must first start
|
* <p>To run this example, you must first start {@link WebSocketServer} and
|
||||||
* <tt>org.jboss.netty.example.http.websocketx.server.WebSocketServer</tt>
|
* then {@link WebSocketClient}.
|
||||||
* <p>Next, run <tt>org.jboss.netty.example.http.websocketx.client.App</tt>.
|
|
||||||
*/
|
*/
|
||||||
package org.jboss.netty.example.http.websocketx.client;
|
package org.jboss.netty.example.http.websocketx.client;
|
||||||
|
|
||||||
|
@ -43,12 +43,14 @@ import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
|
|||||||
* </ul>
|
* </ul>
|
||||||
*/
|
*/
|
||||||
public class WebSocketServer {
|
public class WebSocketServer {
|
||||||
public static void main(String[] args) {
|
|
||||||
ConsoleHandler ch = new ConsoleHandler();
|
|
||||||
ch.setLevel(Level.FINE);
|
|
||||||
Logger.getLogger("").addHandler(ch);
|
|
||||||
Logger.getLogger("").setLevel(Level.FINE);
|
|
||||||
|
|
||||||
|
private final int port;
|
||||||
|
|
||||||
|
public WebSocketServer(int port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
// Configure the server.
|
// Configure the server.
|
||||||
ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(
|
ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(
|
||||||
Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
|
Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
|
||||||
@ -57,9 +59,19 @@ public class WebSocketServer {
|
|||||||
bootstrap.setPipelineFactory(new WebSocketServerPipelineFactory());
|
bootstrap.setPipelineFactory(new WebSocketServerPipelineFactory());
|
||||||
|
|
||||||
// Bind and start to accept incoming connections.
|
// Bind and start to accept incoming connections.
|
||||||
bootstrap.bind(new InetSocketAddress(8080));
|
bootstrap.bind(new InetSocketAddress(port));
|
||||||
|
|
||||||
System.out
|
System.out.println("Web socket server started at port " + port + '.');
|
||||||
.println("Web Socket Server started on 8080. Open your browser and navigate to http://localhost:8080/");
|
System.out.println("Open your browser and navigate to http://localhost:" + port + '/');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int port;
|
||||||
|
if (args.length > 0) {
|
||||||
|
port = Integer.parseInt(args[0]);
|
||||||
|
} else {
|
||||||
|
port = 8080;
|
||||||
|
}
|
||||||
|
new WebSocketServer(port).run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,11 +43,35 @@ import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
|
|||||||
* </ul>
|
* </ul>
|
||||||
*/
|
*/
|
||||||
public class WebSocketSslServer {
|
public class WebSocketSslServer {
|
||||||
|
|
||||||
|
private final int port;
|
||||||
|
|
||||||
|
public WebSocketSslServer(int port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
// Configure the server.
|
||||||
|
ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(
|
||||||
|
Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
|
||||||
|
|
||||||
|
// Set up the event pipeline factory.
|
||||||
|
bootstrap.setPipelineFactory(new WebSocketSslServerPipelineFactory());
|
||||||
|
|
||||||
|
// Bind and start to accept incoming connections.
|
||||||
|
bootstrap.bind(new InetSocketAddress(port));
|
||||||
|
|
||||||
|
System.out.println("Web socket server started at port " + port + '.');
|
||||||
|
System.out.println("Open your browser and navigate to https://localhost:" + port + '/');
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ConsoleHandler ch = new ConsoleHandler();
|
int port;
|
||||||
ch.setLevel(Level.FINE);
|
if (args.length > 0) {
|
||||||
Logger.getLogger("").addHandler(ch);
|
port = Integer.parseInt(args[0]);
|
||||||
Logger.getLogger("").setLevel(Level.FINE);
|
} else {
|
||||||
|
port = 8443;
|
||||||
|
}
|
||||||
|
|
||||||
String keyStoreFilePath = System.getProperty("keystore.file.path");
|
String keyStoreFilePath = System.getProperty("keystore.file.path");
|
||||||
if (keyStoreFilePath == null || keyStoreFilePath.isEmpty()) {
|
if (keyStoreFilePath == null || keyStoreFilePath.isEmpty()) {
|
||||||
@ -61,17 +85,6 @@ public class WebSocketSslServer {
|
|||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configure the server.
|
new WebSocketSslServer(port).run();
|
||||||
ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(
|
|
||||||
Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
|
|
||||||
|
|
||||||
// Set up the event pipeline factory.
|
|
||||||
bootstrap.setPipelineFactory(new WebSocketSslServerPipelineFactory());
|
|
||||||
|
|
||||||
// Bind and start to accept incoming connections.
|
|
||||||
bootstrap.bind(new InetSocketAddress(8081));
|
|
||||||
|
|
||||||
System.out
|
|
||||||
.println("Web Socket Server started on 8081. Open your browser and navigate to https://localhost:8081/");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
package org.jboss.netty.example.local;
|
package org.jboss.netty.example.local;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
import org.jboss.netty.bootstrap.ClientBootstrap;
|
import org.jboss.netty.bootstrap.ClientBootstrap;
|
||||||
@ -34,9 +35,16 @@ import org.jboss.netty.handler.logging.LoggingHandler;
|
|||||||
import org.jboss.netty.logging.InternalLogLevel;
|
import org.jboss.netty.logging.InternalLogLevel;
|
||||||
|
|
||||||
public class LocalExample {
|
public class LocalExample {
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
|
private final String port;
|
||||||
|
|
||||||
|
public LocalExample(String port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() throws IOException {
|
||||||
// Address to bind on / connect to.
|
// Address to bind on / connect to.
|
||||||
LocalAddress socketAddress = new LocalAddress("1");
|
LocalAddress socketAddress = new LocalAddress(port);
|
||||||
|
|
||||||
// Configure the server.
|
// Configure the server.
|
||||||
ServerBootstrap sb = new ServerBootstrap(
|
ServerBootstrap sb = new ServerBootstrap(
|
||||||
@ -94,4 +102,8 @@ public class LocalExample {
|
|||||||
cb.releaseExternalResources();
|
cb.releaseExternalResources();
|
||||||
sb.releaseExternalResources();
|
sb.releaseExternalResources();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
new LocalExample("1").run();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,10 +32,16 @@ import org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor;
|
|||||||
import org.jboss.netty.handler.logging.LoggingHandler;
|
import org.jboss.netty.handler.logging.LoggingHandler;
|
||||||
import org.jboss.netty.logging.InternalLogLevel;
|
import org.jboss.netty.logging.InternalLogLevel;
|
||||||
|
|
||||||
public class LocalExampleMultthreaded {
|
public class LocalExampleMultithreaded {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
private final String port;
|
||||||
LocalAddress socketAddress = new LocalAddress("1");
|
|
||||||
|
public LocalExampleMultithreaded(String port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
LocalAddress socketAddress = new LocalAddress(port);
|
||||||
|
|
||||||
OrderedMemoryAwareThreadPoolExecutor eventExecutor =
|
OrderedMemoryAwareThreadPoolExecutor eventExecutor =
|
||||||
new OrderedMemoryAwareThreadPoolExecutor(
|
new OrderedMemoryAwareThreadPoolExecutor(
|
||||||
@ -92,4 +98,8 @@ public class LocalExampleMultthreaded {
|
|||||||
sb.releaseExternalResources();
|
sb.releaseExternalResources();
|
||||||
eventExecutor.shutdownNow();
|
eventExecutor.shutdownNow();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
new LocalExampleMultithreaded("1").run();
|
||||||
|
}
|
||||||
}
|
}
|
@ -18,6 +18,7 @@ package org.jboss.netty.example.localtime;
|
|||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
@ -33,21 +34,18 @@ import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
|
|||||||
*/
|
*/
|
||||||
public class LocalTimeClient {
|
public class LocalTimeClient {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
private final String host;
|
||||||
// Print usage if necessary.
|
private final int port;
|
||||||
if (args.length < 3) {
|
private final Collection<String> cities;
|
||||||
printUsage();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse options.
|
public LocalTimeClient(String host, int port, Collection<String> cities) {
|
||||||
String host = args[0];
|
this.host = host;
|
||||||
int port = Integer.parseInt(args[1]);
|
this.port = port;
|
||||||
Collection<String> cities = parseCities(args, 2);
|
this.cities = new ArrayList<String>();
|
||||||
if (cities == null) {
|
this.cities.addAll(cities);
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
public void run() {
|
||||||
// Set up.
|
// Set up.
|
||||||
ClientBootstrap bootstrap = new ClientBootstrap(
|
ClientBootstrap bootstrap = new ClientBootstrap(
|
||||||
new NioClientSocketChannelFactory(
|
new NioClientSocketChannelFactory(
|
||||||
@ -84,6 +82,24 @@ public class LocalTimeClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
// Print usage if necessary.
|
||||||
|
if (args.length < 3) {
|
||||||
|
printUsage();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse options.
|
||||||
|
String host = args[0];
|
||||||
|
int port = Integer.parseInt(args[1]);
|
||||||
|
Collection<String> cities = parseCities(args, 2);
|
||||||
|
if (cities == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
new LocalTimeClient(host, port, cities).run();
|
||||||
|
}
|
||||||
|
|
||||||
private static void printUsage() {
|
private static void printUsage() {
|
||||||
System.err.println(
|
System.err.println(
|
||||||
"Usage: " + LocalTimeClient.class.getSimpleName() +
|
"Usage: " + LocalTimeClient.class.getSimpleName() +
|
||||||
|
@ -27,7 +27,13 @@ import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
|
|||||||
*/
|
*/
|
||||||
public class LocalTimeServer {
|
public class LocalTimeServer {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
private final int port;
|
||||||
|
|
||||||
|
public LocalTimeServer(int port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
// Configure the server.
|
// Configure the server.
|
||||||
ServerBootstrap bootstrap = new ServerBootstrap(
|
ServerBootstrap bootstrap = new ServerBootstrap(
|
||||||
new NioServerSocketChannelFactory(
|
new NioServerSocketChannelFactory(
|
||||||
@ -38,6 +44,16 @@ public class LocalTimeServer {
|
|||||||
bootstrap.setPipelineFactory(new LocalTimeServerPipelineFactory());
|
bootstrap.setPipelineFactory(new LocalTimeServerPipelineFactory());
|
||||||
|
|
||||||
// Bind and start to accept incoming connections.
|
// Bind and start to accept incoming connections.
|
||||||
bootstrap.bind(new InetSocketAddress(8080));
|
bootstrap.bind(new InetSocketAddress(port));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
int port;
|
||||||
|
if (args.length > 0) {
|
||||||
|
port = Integer.parseInt(args[0]);
|
||||||
|
} else {
|
||||||
|
port = 8080;
|
||||||
|
}
|
||||||
|
new LocalTimeServer(port).run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,6 +32,37 @@ import org.jboss.netty.handler.codec.serialization.ObjectEncoder;
|
|||||||
*/
|
*/
|
||||||
public class ObjectEchoClient {
|
public class ObjectEchoClient {
|
||||||
|
|
||||||
|
private final String host;
|
||||||
|
private final int port;
|
||||||
|
private final int firstMessageSize;
|
||||||
|
|
||||||
|
public ObjectEchoClient(String host, int port, int firstMessageSize) {
|
||||||
|
this.host = host;
|
||||||
|
this.port = port;
|
||||||
|
this.firstMessageSize = firstMessageSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
// Configure the client.
|
||||||
|
ClientBootstrap bootstrap = new ClientBootstrap(
|
||||||
|
new NioClientSocketChannelFactory(
|
||||||
|
Executors.newCachedThreadPool(),
|
||||||
|
Executors.newCachedThreadPool()));
|
||||||
|
|
||||||
|
// Set up the pipeline factory.
|
||||||
|
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
|
||||||
|
public ChannelPipeline getPipeline() throws Exception {
|
||||||
|
return Channels.pipeline(
|
||||||
|
new ObjectEncoder(),
|
||||||
|
new ObjectDecoder(),
|
||||||
|
new ObjectEchoClientHandler(firstMessageSize));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Start the connection attempt.
|
||||||
|
bootstrap.connect(new InetSocketAddress(host, port));
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
// Print usage if no argument is specified.
|
// Print usage if no argument is specified.
|
||||||
if (args.length < 2 || args.length > 3) {
|
if (args.length < 2 || args.length > 3) {
|
||||||
@ -52,23 +83,6 @@ public class ObjectEchoClient {
|
|||||||
firstMessageSize = 256;
|
firstMessageSize = 256;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configure the client.
|
new ObjectEchoClient(host, port, firstMessageSize).run();
|
||||||
ClientBootstrap bootstrap = new ClientBootstrap(
|
|
||||||
new NioClientSocketChannelFactory(
|
|
||||||
Executors.newCachedThreadPool(),
|
|
||||||
Executors.newCachedThreadPool()));
|
|
||||||
|
|
||||||
// Set up the pipeline factory.
|
|
||||||
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
|
|
||||||
public ChannelPipeline getPipeline() throws Exception {
|
|
||||||
return Channels.pipeline(
|
|
||||||
new ObjectEncoder(),
|
|
||||||
new ObjectDecoder(),
|
|
||||||
new ObjectEchoClientHandler(firstMessageSize));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Start the connection attempt.
|
|
||||||
bootstrap.connect(new InetSocketAddress(host, port));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,7 @@ import org.jboss.netty.channel.ChannelPipelineFactory;
|
|||||||
import org.jboss.netty.channel.Channels;
|
import org.jboss.netty.channel.Channels;
|
||||||
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
|
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
|
||||||
import org.jboss.netty.example.echo.EchoServer;
|
import org.jboss.netty.example.echo.EchoServer;
|
||||||
|
import org.jboss.netty.handler.codec.serialization.ClassResolvers;
|
||||||
import org.jboss.netty.handler.codec.serialization.ObjectDecoder;
|
import org.jboss.netty.handler.codec.serialization.ObjectDecoder;
|
||||||
import org.jboss.netty.handler.codec.serialization.ObjectEncoder;
|
import org.jboss.netty.handler.codec.serialization.ObjectEncoder;
|
||||||
|
|
||||||
@ -32,7 +33,13 @@ import org.jboss.netty.handler.codec.serialization.ObjectEncoder;
|
|||||||
*/
|
*/
|
||||||
public class ObjectEchoServer {
|
public class ObjectEchoServer {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
private final int port;
|
||||||
|
|
||||||
|
public ObjectEchoServer(int port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
// Configure the server.
|
// Configure the server.
|
||||||
ServerBootstrap bootstrap = new ServerBootstrap(
|
ServerBootstrap bootstrap = new ServerBootstrap(
|
||||||
new NioServerSocketChannelFactory(
|
new NioServerSocketChannelFactory(
|
||||||
@ -50,6 +57,16 @@ public class ObjectEchoServer {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Bind and start to accept incoming connections.
|
// Bind and start to accept incoming connections.
|
||||||
bootstrap.bind(new InetSocketAddress(8080));
|
bootstrap.bind(new InetSocketAddress(port));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
int port;
|
||||||
|
if (args.length > 0) {
|
||||||
|
port = Integer.parseInt(args[0]);
|
||||||
|
} else {
|
||||||
|
port = 8080;
|
||||||
|
}
|
||||||
|
new ObjectEchoServer(port).run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,13 @@ import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
|
|||||||
*/
|
*/
|
||||||
public class PortUnificationServer {
|
public class PortUnificationServer {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
private final int port;
|
||||||
|
|
||||||
|
public PortUnificationServer(int port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
// Configure the server.
|
// Configure the server.
|
||||||
ServerBootstrap bootstrap = new ServerBootstrap(
|
ServerBootstrap bootstrap = new ServerBootstrap(
|
||||||
new NioServerSocketChannelFactory(
|
new NioServerSocketChannelFactory(
|
||||||
@ -48,6 +54,16 @@ public class PortUnificationServer {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Bind and start to accept incoming connections.
|
// Bind and start to accept incoming connections.
|
||||||
bootstrap.bind(new InetSocketAddress(8080));
|
bootstrap.bind(new InetSocketAddress(port));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
int port;
|
||||||
|
if (args.length > 0) {
|
||||||
|
port = Integer.parseInt(args[0]);
|
||||||
|
} else {
|
||||||
|
port = 8080;
|
||||||
|
}
|
||||||
|
new PortUnificationServer(port).run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,23 +26,6 @@ import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
|
|||||||
|
|
||||||
public class HexDumpProxy {
|
public class HexDumpProxy {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
// Validate command line options.
|
|
||||||
if (args.length != 3) {
|
|
||||||
System.err.println(
|
|
||||||
"Usage: " + HexDumpProxy.class.getSimpleName() +
|
|
||||||
" <local port> <remote host> <remote port>");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse command line options.
|
|
||||||
int localPort = Integer.parseInt(args[0]);
|
|
||||||
String remoteHost = args[1];
|
|
||||||
int remotePort = Integer.parseInt(args[2]);
|
|
||||||
|
|
||||||
new HexDumpProxy(localPort, remoteHost, remotePort).run();
|
|
||||||
}
|
|
||||||
|
|
||||||
private final int localPort;
|
private final int localPort;
|
||||||
private final String remoteHost;
|
private final String remoteHost;
|
||||||
private final int remotePort;
|
private final int remotePort;
|
||||||
@ -73,4 +56,21 @@ public class HexDumpProxy {
|
|||||||
// Start up the server.
|
// Start up the server.
|
||||||
sb.bind(new InetSocketAddress(localPort));
|
sb.bind(new InetSocketAddress(localPort));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
// Validate command line options.
|
||||||
|
if (args.length != 3) {
|
||||||
|
System.err.println(
|
||||||
|
"Usage: " + HexDumpProxy.class.getSimpleName() +
|
||||||
|
" <local port> <remote host> <remote port>");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse command line options.
|
||||||
|
int localPort = Integer.parseInt(args[0]);
|
||||||
|
String remoteHost = args[1];
|
||||||
|
int remotePort = Integer.parseInt(args[2]);
|
||||||
|
|
||||||
|
new HexDumpProxy(localPort, remoteHost, remotePort).run();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,13 @@ import org.jboss.netty.util.CharsetUtil;
|
|||||||
*/
|
*/
|
||||||
public class QuoteOfTheMomentClient {
|
public class QuoteOfTheMomentClient {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
private final int port;
|
||||||
|
|
||||||
|
public QuoteOfTheMomentClient(int port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
DatagramChannelFactory f =
|
DatagramChannelFactory f =
|
||||||
new NioDatagramChannelFactory(Executors.newCachedThreadPool());
|
new NioDatagramChannelFactory(Executors.newCachedThreadPool());
|
||||||
|
|
||||||
@ -74,7 +80,7 @@ public class QuoteOfTheMomentClient {
|
|||||||
DatagramChannel c = (DatagramChannel) b.bind(new InetSocketAddress(0));
|
DatagramChannel c = (DatagramChannel) b.bind(new InetSocketAddress(0));
|
||||||
|
|
||||||
// Broadcast the QOTM request to port 8080.
|
// Broadcast the QOTM request to port 8080.
|
||||||
c.write("QOTM?", new InetSocketAddress("255.255.255.255", 8080));
|
c.write("QOTM?", new InetSocketAddress("255.255.255.255", port));
|
||||||
|
|
||||||
// QuoteOfTheMomentClientHandler will close the DatagramChannel when a
|
// QuoteOfTheMomentClientHandler will close the DatagramChannel when a
|
||||||
// response is received. If the channel is not closed within 5 seconds,
|
// response is received. If the channel is not closed within 5 seconds,
|
||||||
@ -86,4 +92,14 @@ public class QuoteOfTheMomentClient {
|
|||||||
|
|
||||||
f.releaseExternalResources();
|
f.releaseExternalResources();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
int port;
|
||||||
|
if (args.length > 0) {
|
||||||
|
port = Integer.parseInt(args[0]);
|
||||||
|
} else {
|
||||||
|
port = 8080;
|
||||||
|
}
|
||||||
|
new QuoteOfTheMomentClient(port).run();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,13 @@ import org.jboss.netty.util.CharsetUtil;
|
|||||||
*/
|
*/
|
||||||
public class QuoteOfTheMomentServer {
|
public class QuoteOfTheMomentServer {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
private final int port;
|
||||||
|
|
||||||
|
public QuoteOfTheMomentServer(int port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
DatagramChannelFactory f =
|
DatagramChannelFactory f =
|
||||||
new NioDatagramChannelFactory(Executors.newCachedThreadPool());
|
new NioDatagramChannelFactory(Executors.newCachedThreadPool());
|
||||||
|
|
||||||
@ -71,6 +77,16 @@ public class QuoteOfTheMomentServer {
|
|||||||
new FixedReceiveBufferSizePredictorFactory(1024));
|
new FixedReceiveBufferSizePredictorFactory(1024));
|
||||||
|
|
||||||
// Bind to the port and start the service.
|
// Bind to the port and start the service.
|
||||||
b.bind(new InetSocketAddress(8080));
|
b.bind(new InetSocketAddress(port));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
int port;
|
||||||
|
if (args.length > 0) {
|
||||||
|
port = Integer.parseInt(args[0]);
|
||||||
|
} else {
|
||||||
|
port = 8080;
|
||||||
|
}
|
||||||
|
new QuoteOfTheMomentServer(port).run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
package org.jboss.netty.example.securechat;
|
package org.jboss.netty.example.securechat;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
@ -31,19 +32,15 @@ import org.jboss.netty.example.telnet.TelnetClient;
|
|||||||
*/
|
*/
|
||||||
public class SecureChatClient {
|
public class SecureChatClient {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
private final String host;
|
||||||
// Print usage if no argument is specified.
|
private final int port;
|
||||||
if (args.length != 2) {
|
|
||||||
System.err.println(
|
|
||||||
"Usage: " + SecureChatClient.class.getSimpleName() +
|
|
||||||
" <host> <port>");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse options.
|
public SecureChatClient(String host, int port) {
|
||||||
String host = args[0];
|
this.host = host;
|
||||||
int port = Integer.parseInt(args[1]);
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() throws IOException {
|
||||||
// Configure the client.
|
// Configure the client.
|
||||||
ClientBootstrap bootstrap = new ClientBootstrap(
|
ClientBootstrap bootstrap = new ClientBootstrap(
|
||||||
new NioClientSocketChannelFactory(
|
new NioClientSocketChannelFactory(
|
||||||
@ -96,4 +93,20 @@ public class SecureChatClient {
|
|||||||
// Shut down all thread pools to exit.
|
// Shut down all thread pools to exit.
|
||||||
bootstrap.releaseExternalResources();
|
bootstrap.releaseExternalResources();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
// Print usage if no argument is specified.
|
||||||
|
if (args.length != 2) {
|
||||||
|
System.err.println(
|
||||||
|
"Usage: " + SecureChatClient.class.getSimpleName() +
|
||||||
|
" <host> <port>");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse options.
|
||||||
|
String host = args[0];
|
||||||
|
int port = Integer.parseInt(args[1]);
|
||||||
|
|
||||||
|
new SecureChatClient(host, port).run();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,13 @@ import org.jboss.netty.example.telnet.TelnetServer;
|
|||||||
*/
|
*/
|
||||||
public class SecureChatServer {
|
public class SecureChatServer {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
private final int port;
|
||||||
|
|
||||||
|
public SecureChatServer(int port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
// Configure the server.
|
// Configure the server.
|
||||||
ServerBootstrap bootstrap = new ServerBootstrap(
|
ServerBootstrap bootstrap = new ServerBootstrap(
|
||||||
new NioServerSocketChannelFactory(
|
new NioServerSocketChannelFactory(
|
||||||
@ -38,6 +44,16 @@ public class SecureChatServer {
|
|||||||
bootstrap.setPipelineFactory(new SecureChatServerPipelineFactory());
|
bootstrap.setPipelineFactory(new SecureChatServerPipelineFactory());
|
||||||
|
|
||||||
// Bind and start to accept incoming connections.
|
// Bind and start to accept incoming connections.
|
||||||
bootstrap.bind(new InetSocketAddress(8080));
|
bootstrap.bind(new InetSocketAddress(port));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
int port;
|
||||||
|
if (args.length > 0) {
|
||||||
|
port = Integer.parseInt(args[0]);
|
||||||
|
} else {
|
||||||
|
port = 8443;
|
||||||
|
}
|
||||||
|
new SecureChatServer(port).run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
package org.jboss.netty.example.telnet;
|
package org.jboss.netty.example.telnet;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
@ -30,19 +31,15 @@ import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
|
|||||||
*/
|
*/
|
||||||
public class TelnetClient {
|
public class TelnetClient {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
private final String host;
|
||||||
// Print usage if no argument is specified.
|
private final int port;
|
||||||
if (args.length != 2) {
|
|
||||||
System.err.println(
|
|
||||||
"Usage: " + TelnetClient.class.getSimpleName() +
|
|
||||||
" <host> <port>");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse options.
|
public TelnetClient(String host, int port) {
|
||||||
String host = args[0];
|
this.host = host;
|
||||||
int port = Integer.parseInt(args[1]);
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() throws IOException {
|
||||||
// Configure the client.
|
// Configure the client.
|
||||||
ClientBootstrap bootstrap = new ClientBootstrap(
|
ClientBootstrap bootstrap = new ClientBootstrap(
|
||||||
new NioClientSocketChannelFactory(
|
new NioClientSocketChannelFactory(
|
||||||
@ -95,4 +92,20 @@ public class TelnetClient {
|
|||||||
// Shut down all thread pools to exit.
|
// Shut down all thread pools to exit.
|
||||||
bootstrap.releaseExternalResources();
|
bootstrap.releaseExternalResources();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
// Print usage if no argument is specified.
|
||||||
|
if (args.length != 2) {
|
||||||
|
System.err.println(
|
||||||
|
"Usage: " + TelnetClient.class.getSimpleName() +
|
||||||
|
" <host> <port>");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse options.
|
||||||
|
String host = args[0];
|
||||||
|
int port = Integer.parseInt(args[1]);
|
||||||
|
|
||||||
|
new TelnetClient(host, port).run();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,13 @@ import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
|
|||||||
*/
|
*/
|
||||||
public class TelnetServer {
|
public class TelnetServer {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
private final int port;
|
||||||
|
|
||||||
|
public TelnetServer(int port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
// Configure the server.
|
// Configure the server.
|
||||||
ServerBootstrap bootstrap = new ServerBootstrap(
|
ServerBootstrap bootstrap = new ServerBootstrap(
|
||||||
new NioServerSocketChannelFactory(
|
new NioServerSocketChannelFactory(
|
||||||
@ -37,6 +43,16 @@ public class TelnetServer {
|
|||||||
bootstrap.setPipelineFactory(new TelnetServerPipelineFactory());
|
bootstrap.setPipelineFactory(new TelnetServerPipelineFactory());
|
||||||
|
|
||||||
// Bind and start to accept incoming connections.
|
// Bind and start to accept incoming connections.
|
||||||
bootstrap.bind(new InetSocketAddress(8080));
|
bootstrap.bind(new InetSocketAddress(port));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
int port;
|
||||||
|
if (args.length > 0) {
|
||||||
|
port = Integer.parseInt(args[0]);
|
||||||
|
} else {
|
||||||
|
port = 8080;
|
||||||
|
}
|
||||||
|
new TelnetServer(port).run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,19 +42,15 @@ public class UptimeClient {
|
|||||||
// Reconnect when the server sends nothing for 10 seconds.
|
// Reconnect when the server sends nothing for 10 seconds.
|
||||||
private static final int READ_TIMEOUT = 10;
|
private static final int READ_TIMEOUT = 10;
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
private final String host;
|
||||||
// Print usage if no argument is specified.
|
private final int port;
|
||||||
if (args.length != 2) {
|
|
||||||
System.err.println(
|
|
||||||
"Usage: " + UptimeClient.class.getSimpleName() +
|
|
||||||
" <host> <port>");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse options.
|
public UptimeClient(String host, int port) {
|
||||||
String host = args[0];
|
this.host = host;
|
||||||
int port = Integer.parseInt(args[1]);
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
// Initialize the timer that schedules subsequent reconnection attempts.
|
// Initialize the timer that schedules subsequent reconnection attempts.
|
||||||
final Timer timer = new HashedWheelTimer();
|
final Timer timer = new HashedWheelTimer();
|
||||||
|
|
||||||
@ -85,4 +81,20 @@ public class UptimeClient {
|
|||||||
// UptimeClientHandler.
|
// UptimeClientHandler.
|
||||||
bootstrap.connect();
|
bootstrap.connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
// Print usage if no argument is specified.
|
||||||
|
if (args.length != 2) {
|
||||||
|
System.err.println(
|
||||||
|
"Usage: " + UptimeClient.class.getSimpleName() +
|
||||||
|
" <host> <port>");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse options.
|
||||||
|
String host = args[0];
|
||||||
|
int port = Integer.parseInt(args[1]);
|
||||||
|
|
||||||
|
new UptimeClient(host, port).run();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -83,8 +83,7 @@ public class UptimeClientHandler extends SimpleChannelUpstreamHandler {
|
|||||||
if (cause instanceof ReadTimeoutException) {
|
if (cause instanceof ReadTimeoutException) {
|
||||||
// The connection was OK but there was no traffic for last period.
|
// The connection was OK but there was no traffic for last period.
|
||||||
println("Disconnecting due to no inbound traffic");
|
println("Disconnecting due to no inbound traffic");
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
cause.printStackTrace();
|
cause.printStackTrace();
|
||||||
}
|
}
|
||||||
ctx.getChannel().close();
|
ctx.getChannel().close();
|
||||||
|
@ -27,10 +27,6 @@ class CachingClassResolver implements ClassResolver {
|
|||||||
this.classCache = classCache;
|
this.classCache = classCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* (non-Javadoc)
|
|
||||||
* @see org.jboss.netty.handler.codec.serialization.ClassResolver#resolve(java.lang.String)
|
|
||||||
*/
|
|
||||||
public Class<?> resolve(String className) throws ClassNotFoundException {
|
public Class<?> resolve(String className) throws ClassNotFoundException {
|
||||||
// Query the cache first.
|
// Query the cache first.
|
||||||
Class<?> clazz;
|
Class<?> clazz;
|
||||||
|
@ -23,11 +23,6 @@ class ClassLoaderClassResolver implements ClassResolver {
|
|||||||
this.classLoader = classLoader;
|
this.classLoader = classLoader;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* (non-Javadoc)
|
|
||||||
* @see org.jboss.netty.handler.codec.serialization.ClassResolver#resolve(java.lang.String)
|
|
||||||
*/
|
|
||||||
public Class<?> resolve(String className) throws ClassNotFoundException {
|
public Class<?> resolve(String className) throws ClassNotFoundException {
|
||||||
try {
|
try {
|
||||||
return classLoader.loadClass(className);
|
return classLoader.loadClass(className);
|
||||||
|
@ -38,101 +38,52 @@ abstract class ReferenceMap<K, V> implements Map<K, V> {
|
|||||||
return ref.get();
|
return ref.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* (non-Javadoc)
|
|
||||||
* @see java.util.Map#size()
|
|
||||||
*/
|
|
||||||
public int size() {
|
public int size() {
|
||||||
return delegate.size();
|
return delegate.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* (non-Javadoc)
|
|
||||||
* @see java.util.Map#isEmpty()
|
|
||||||
*/
|
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return delegate.isEmpty();
|
return delegate.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* (non-Javadoc)
|
|
||||||
* @see java.util.Map#containsKey(java.lang.Object)
|
|
||||||
*/
|
|
||||||
public boolean containsKey(Object key) {
|
public boolean containsKey(Object key) {
|
||||||
return delegate.containsKey(key);
|
return delegate.containsKey(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* (non-Javadoc)
|
|
||||||
* @see java.util.Map#containsValue(java.lang.Object)
|
|
||||||
*/
|
|
||||||
public boolean containsValue(Object value) {
|
public boolean containsValue(Object value) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* (non-Javadoc)
|
|
||||||
* @see java.util.Map#get(java.lang.Object)
|
|
||||||
*/
|
|
||||||
public V get(Object key) {
|
public V get(Object key) {
|
||||||
return unfold(delegate.get(key));
|
return unfold(delegate.get(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* (non-Javadoc)
|
|
||||||
* @see java.util.Map#put(java.lang.Object, java.lang.Object)
|
|
||||||
*/
|
|
||||||
public V put(K key, V value) {
|
public V put(K key, V value) {
|
||||||
return unfold(delegate.put(key, fold(value)));
|
return unfold(delegate.put(key, fold(value)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* (non-Javadoc)
|
|
||||||
* @see java.util.Map#remove(java.lang.Object)
|
|
||||||
*/
|
|
||||||
public V remove(Object key) {
|
public V remove(Object key) {
|
||||||
return unfold(delegate.remove(key));
|
return unfold(delegate.remove(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* (non-Javadoc)
|
|
||||||
* @see java.util.Map#putAll(java.util.Map)
|
|
||||||
*/
|
|
||||||
public void putAll(Map<? extends K, ? extends V> m) {
|
public void putAll(Map<? extends K, ? extends V> m) {
|
||||||
for (Entry<? extends K, ? extends V> entry : m.entrySet()) {
|
for (Entry<? extends K, ? extends V> entry : m.entrySet()) {
|
||||||
delegate.put(entry.getKey(), fold(entry.getValue()));
|
delegate.put(entry.getKey(), fold(entry.getValue()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* (non-Javadoc)
|
|
||||||
* @see java.util.Map#clear()
|
|
||||||
*/
|
|
||||||
public void clear() {
|
public void clear() {
|
||||||
delegate.clear();
|
delegate.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* (non-Javadoc)
|
|
||||||
* @see java.util.Map#keySet()
|
|
||||||
*/
|
|
||||||
public Set<K> keySet() {
|
public Set<K> keySet() {
|
||||||
return delegate.keySet();
|
return delegate.keySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* (non-Javadoc)
|
|
||||||
* @see java.util.Map#values()
|
|
||||||
*/
|
|
||||||
public Collection<V> values() {
|
public Collection<V> values() {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* (non-Javadoc)
|
|
||||||
* @see java.util.Map#entrySet()
|
|
||||||
*/
|
|
||||||
public Set<Entry<K, V>> entrySet() {
|
public Set<Entry<K, V>> entrySet() {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ public final class QueueFactory {
|
|||||||
* @param itemClass the {@link Class} type which will be used as {@link BlockingQueue} items
|
* @param itemClass the {@link Class} type which will be used as {@link BlockingQueue} items
|
||||||
* @return queue the {@link BlockingQueue} implementation
|
* @return queue the {@link BlockingQueue} implementation
|
||||||
*/
|
*/
|
||||||
public static final <T> BlockingQueue<T> createQueue(Class<T> itemClass) {
|
public static <T> BlockingQueue<T> createQueue(Class<T> itemClass) {
|
||||||
if (useUnsafe) {
|
if (useUnsafe) {
|
||||||
return new LinkedTransferQueue<T>();
|
return new LinkedTransferQueue<T>();
|
||||||
} else {
|
} else {
|
||||||
@ -54,7 +54,7 @@ public final class QueueFactory {
|
|||||||
* @param itemClass the {@link Class} type which will be used as {@link BlockingQueue} items
|
* @param itemClass the {@link Class} type which will be used as {@link BlockingQueue} items
|
||||||
* @return queue the {@link BlockingQueue} implementation
|
* @return queue the {@link BlockingQueue} implementation
|
||||||
*/
|
*/
|
||||||
public static final <T> BlockingQueue<T> createQueue(Collection<? extends T> collection, Class<T> itemClass) {
|
public static <T> BlockingQueue<T> createQueue(Collection<? extends T> collection, Class<T> itemClass) {
|
||||||
if (useUnsafe) {
|
if (useUnsafe) {
|
||||||
return new LinkedTransferQueue<T>(collection);
|
return new LinkedTransferQueue<T>(collection);
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user