[Assorted-commits] SF.net SVN: assorted:[1463] sandbox/trunk/src/java/nettytest
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-07-22 20:42:21
|
Revision: 1463 http://assorted.svn.sourceforge.net/assorted/?rev=1463&view=rev Author: yangzhang Date: 2009-07-22 20:42:15 +0000 (Wed, 22 Jul 2009) Log Message: ----------- more work on nettytest; close to working Modified Paths: -------------- sandbox/trunk/src/java/nettytest/pom.xml Added Paths: ----------- sandbox/trunk/src/java/nettytest/src/main/java/nettytest/ sandbox/trunk/src/java/nettytest/src/main/java/nettytest/Client.java sandbox/trunk/src/java/nettytest/src/main/java/nettytest/Common.java sandbox/trunk/src/java/nettytest/src/main/java/nettytest/Server.java sandbox/trunk/src/java/nettytest/src/test/java/nettytest/ sandbox/trunk/src/java/nettytest/src/test/java/nettytest/TestSystem.java sandbox/trunk/src/java/nettytest/src/test/resources/nettytest/ sandbox/trunk/src/java/nettytest/src/test/resources/nettytest/p12.pass sandbox/trunk/src/java/nettytest/src/test/resources/nettytest/server.p12 Removed Paths: ------------- sandbox/trunk/src/java/nettytest/src/main/java/Client.java sandbox/trunk/src/java/nettytest/src/main/java/Common.java sandbox/trunk/src/java/nettytest/src/main/java/Server.java Modified: sandbox/trunk/src/java/nettytest/pom.xml =================================================================== --- sandbox/trunk/src/java/nettytest/pom.xml 2009-07-22 02:16:59 UTC (rev 1462) +++ sandbox/trunk/src/java/nettytest/pom.xml 2009-07-22 20:42:15 UTC (rev 1463) @@ -12,9 +12,15 @@ </repositories> <dependencies> <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.6</version> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.jboss.netty</groupId> <artifactId>netty</artifactId> - <version>3.1.0.BETA3</version> + <version>3.1.0.CR1</version> </dependency> </dependencies> <build> @@ -26,6 +32,15 @@ <target>6</target> </configuration> </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-eclipse-plugin</artifactId> + <version>2.6</version> + <configuration> + <downloadSources>true</downloadSources> + <addVersionToProjectName>true</addVersionToProjectName> + </configuration> + </plugin> </plugins> </build> </project> Deleted: sandbox/trunk/src/java/nettytest/src/main/java/Client.java =================================================================== --- sandbox/trunk/src/java/nettytest/src/main/java/Client.java 2009-07-22 02:16:59 UTC (rev 1462) +++ sandbox/trunk/src/java/nettytest/src/main/java/Client.java 2009-07-22 20:42:15 UTC (rev 1463) @@ -1,62 +0,0 @@ -import java.net.InetAddress; -import java.net.InetSocketAddress; -import java.util.concurrent.Callable; -import java.util.concurrent.Executors; - -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLEngine; - -import org.jboss.netty.bootstrap.ClientBootstrap; -import org.jboss.netty.channel.ChannelFuture; -import org.jboss.netty.channel.ChannelFutureListener; -import org.jboss.netty.channel.ChannelPipeline; -import org.jboss.netty.channel.ChannelPipelineCoverage; -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.ssl.SslHandler; - -public class Client extends Common implements Callable<Void> { - - public SSLEngine getSslEngine() throws Exception { - SSLContext ctx = SSLContext.getInstance("TLS"); - ctx.init(null, getTrustManagers(), null); - SSLEngine engine = ctx.createSSLEngine(); - engine.setUseClientMode(true); - return engine; - } - - public static void main(String[] args) throws Exception { - Client client = new Client(); - client.call(); - } - - public Void call() throws Exception { - @ChannelPipelineCoverage("all") - class Cpf implements ChannelPipelineFactory { - public ChannelPipeline getPipeline() throws Exception { - ChannelPipeline pipeline = Channels.pipeline(); - pipeline.addLast("ssl", new SslHandler(getSslEngine())); - return pipeline; - } - } - ChannelPipelineFactory pipelineFactory = new Cpf(); - - ClientBootstrap bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors - .newCachedThreadPool())); - bootstrap.setPipelineFactory(pipelineFactory); - bootstrap.setOption("tcpNoDelay", true); - bootstrap.setOption("keepAlive", true); - - ChannelFuture future = bootstrap.connect(new InetSocketAddress(InetAddress.getLocalHost(), 9876)); - future.addListener(new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture arg0) throws Exception { - System.out.println(arg0.isSuccess() ? "success" : "failure"); - } - }); - - return null; - } - -} Deleted: sandbox/trunk/src/java/nettytest/src/main/java/Common.java =================================================================== --- sandbox/trunk/src/java/nettytest/src/main/java/Common.java 2009-07-22 02:16:59 UTC (rev 1462) +++ sandbox/trunk/src/java/nettytest/src/main/java/Common.java 2009-07-22 20:42:15 UTC (rev 1463) @@ -1,30 +0,0 @@ -import java.security.cert.CertificateException; -import java.security.cert.X509Certificate; - -import javax.net.ssl.TrustManager; -import javax.net.ssl.X509TrustManager; - -public class Common { - - protected TrustManager[] getTrustManagers() { - return new TrustManager[] { new X509TrustManager() { - // Always trust, even if invalid. - - @Override - public X509Certificate[] getAcceptedIssuers() { - return new X509Certificate[0]; - } - - @Override - public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { - // Always trust. - } - - @Override - public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { - // Always trust. - } - } }; - } - -} Deleted: sandbox/trunk/src/java/nettytest/src/main/java/Server.java =================================================================== --- sandbox/trunk/src/java/nettytest/src/main/java/Server.java 2009-07-22 02:16:59 UTC (rev 1462) +++ sandbox/trunk/src/java/nettytest/src/main/java/Server.java 2009-07-22 20:42:15 UTC (rev 1463) @@ -1,113 +0,0 @@ -import java.io.FileInputStream; -import java.io.InputStreamReader; -import java.net.InetSocketAddress; -import java.security.KeyStore; -import java.util.concurrent.Executors; - -import javax.net.ssl.KeyManagerFactory; -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLEngine; - -import org.jboss.netty.bootstrap.ServerBootstrap; -import org.jboss.netty.channel.ChannelHandlerContext; -import org.jboss.netty.channel.ChannelPipeline; -import org.jboss.netty.channel.ChannelPipelineCoverage; -import org.jboss.netty.channel.ChannelPipelineFactory; -import org.jboss.netty.channel.ChannelStateEvent; -import org.jboss.netty.channel.Channels; -import org.jboss.netty.channel.MessageEvent; -import org.jboss.netty.channel.SimpleChannelHandler; -import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; -import org.jboss.netty.handler.ssl.SslHandler; - -public class Server extends Common { - - String passPath; - String certPath; - - public Server(String passPath, String certPath) { - this.passPath = passPath; - this.certPath = certPath; - } - - public SSLEngine getSslEngine() throws Exception { - // Read the password. - InputStreamReader passf = new InputStreamReader(new FileInputStream(passPath)); - char[] pass; - try { - StringBuffer sb = new StringBuffer(); - char[] chunk = new char[4096]; - while (true) { - int nread = passf.read(chunk, 0, chunk.length); - if (nread < 0) break; - sb.append(chunk, 0, nread); - } - String s = sb.toString(); - if (s.charAt(s.length() - 1) == '\n') - s = s.substring(0, s.length() - 1); - pass = s.toCharArray(); - } finally { - passf.close(); - } - - // Load our Java key store. - KeyStore ks = KeyStore.getInstance("pkcs12"); - ks.load(new FileInputStream(certPath), pass); - - // Like ssh-agent. - KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); - kmf.init(ks, pass); - - // Create the SSL context. - SSLContext ctx = SSLContext.getInstance("TLS"); - ctx.init(kmf.getKeyManagers(), getTrustManagers(), null); - - SSLEngine engine = ctx.createSSLEngine(); - engine.setUseClientMode(true); - return engine; - } - - public static void main(String[] args) throws Exception { - Server server = new Server(args[0], args[1]); - server.call(); - } - - @ChannelPipelineCoverage("all") - class Cpf implements ChannelPipelineFactory { - public ChannelPipeline getPipeline() throws Exception { - ChannelPipeline pipeline = Channels.pipeline(); - pipeline.addLast("ssl", new SslHandler(getSslEngine())); - pipeline.addLast("hdl", new SimpleChannelHandler() { - - @Override - public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { - System.out.println("received"); - } - - @Override - public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { - System.out.println("connected"); - } - }); - return pipeline; - } - } - - public Void call() throws Exception { - ChannelPipelineFactory pipelineFactory = new Cpf(); - - ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors - .newCachedThreadPool(), Executors.newCachedThreadPool())); - bootstrap.setPipelineFactory(pipelineFactory); - bootstrap.setOption("child.tcpNoDelay", true); - bootstrap.setOption("child.keepAlive", true); - bootstrap.setOption("reuseAddress", true); - - bootstrap.bind(new InetSocketAddress(9876)); - - return null; - } - -} - -// vim: et sw=4 ts=4 Copied: sandbox/trunk/src/java/nettytest/src/main/java/nettytest/Client.java (from rev 1460, sandbox/trunk/src/java/nettytest/src/main/java/Client.java) =================================================================== --- sandbox/trunk/src/java/nettytest/src/main/java/nettytest/Client.java (rev 0) +++ sandbox/trunk/src/java/nettytest/src/main/java/nettytest/Client.java 2009-07-22 20:42:15 UTC (rev 1463) @@ -0,0 +1,111 @@ +package nettytest; + +import java.net.InetSocketAddress; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.LinkedBlockingQueue; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLEngine; + +import org.jboss.netty.bootstrap.ClientBootstrap; +import org.jboss.netty.channel.ChannelFactory; +import org.jboss.netty.channel.ChannelFuture; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.channel.ChannelPipeline; +import org.jboss.netty.channel.ChannelPipelineCoverage; +import org.jboss.netty.channel.ChannelPipelineFactory; +import org.jboss.netty.channel.ChannelStateEvent; +import org.jboss.netty.channel.Channels; +import org.jboss.netty.channel.MessageEvent; +import org.jboss.netty.channel.SimpleChannelHandler; +import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; +import org.jboss.netty.handler.codec.string.StringDecoder; +import org.jboss.netty.handler.codec.string.StringEncoder; +import org.jboss.netty.handler.ssl.SslHandler; + +public class Client extends Common implements Callable<Void> { + + final String host; + final int port; + final ExecutorService bossExecutor, workExecutor; + + public Client(String host, int port, ExecutorService bossExecutor, ExecutorService workExecutor) { + this.host = host; + this.port = port; + this.bossExecutor = bossExecutor; + this.workExecutor = workExecutor; + } + + public SSLEngine getSslEngine() throws Exception { + SSLContext ctx = SSLContext.getInstance("TLS"); + ctx.init(null, getTrustManagers(), null); + SSLEngine engine = ctx.createSSLEngine(); + engine.setUseClientMode(true); + return engine; + } + + public static void main(String[] args) throws Exception { + Client client = new Client(args[0], Integer.parseInt(args[1]), Executors.newCachedThreadPool(), Executors.newCachedThreadPool()); + client.call(); + } + + public Void call() throws Exception { + System.out.println("starting client"); + + ChannelFactory factory = new NioClientSocketChannelFactory(bossExecutor, workExecutor); + final LinkedBlockingQueue<Boolean> onDisconnect = new LinkedBlockingQueue<Boolean>(); + + @ChannelPipelineCoverage("all") + class Cpf implements ChannelPipelineFactory { + public ChannelPipeline getPipeline() throws Exception { + ChannelPipeline pipeline = Channels.pipeline(); + pipeline.addLast("ssl", new SslHandler(getSslEngine())); + pipeline.addLast("decoder", new StringDecoder()); + pipeline.addLast("encoder", new StringEncoder()); + pipeline.addLast("hdl", new SimpleChannelHandler() { + + @Override + public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { + System.out.println("connected"); + ctx.getPipeline().get(SslHandler.class).handshake(e.getChannel()); + } + + @Override + public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { + System.out.println("received: " + e.getMessage()); + e.getChannel().close(); + } + + @Override + public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { + onDisconnect.add(true); + } + }); + return pipeline; + } + } + ChannelPipelineFactory pipelineFactory = new Cpf(); + + ClientBootstrap bootstrap = new ClientBootstrap(factory); + bootstrap.setPipelineFactory(pipelineFactory); + bootstrap.setOption("tcpNoDelay", true); + bootstrap.setOption("keepAlive", true); + + System.out.println("connecting"); + ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)) + .awaitUninterruptibly(); + if (future.isSuccess()) { + System.out.println("connected"); + onDisconnect.take(); + future.getChannel().close().awaitUninterruptibly(); + } else { + future.getCause().printStackTrace(); + } + factory.releaseExternalResources(); + + return null; + } + +} Copied: sandbox/trunk/src/java/nettytest/src/main/java/nettytest/Common.java (from rev 1460, sandbox/trunk/src/java/nettytest/src/main/java/Common.java) =================================================================== --- sandbox/trunk/src/java/nettytest/src/main/java/nettytest/Common.java (rev 0) +++ sandbox/trunk/src/java/nettytest/src/main/java/nettytest/Common.java 2009-07-22 20:42:15 UTC (rev 1463) @@ -0,0 +1,31 @@ +package nettytest; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; + +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; + +public class Common { + + protected TrustManager[] getTrustManagers() { + return new TrustManager[] { new X509TrustManager() { + // Always trust, even if invalid. + + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { + // Always trust. + } + + @Override + public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { + // Always trust. + } + } }; + } + +} Copied: sandbox/trunk/src/java/nettytest/src/main/java/nettytest/Server.java (from rev 1462, sandbox/trunk/src/java/nettytest/src/main/java/Server.java) =================================================================== --- sandbox/trunk/src/java/nettytest/src/main/java/nettytest/Server.java (rev 0) +++ sandbox/trunk/src/java/nettytest/src/main/java/nettytest/Server.java 2009-07-22 20:42:15 UTC (rev 1463) @@ -0,0 +1,154 @@ +package nettytest; + +import java.io.FileInputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.InetSocketAddress; +import java.security.KeyStore; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.LinkedBlockingQueue; + +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLEngine; + +import org.jboss.netty.bootstrap.ServerBootstrap; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelFactory; +import org.jboss.netty.channel.ChannelFuture; +import org.jboss.netty.channel.ChannelFutureListener; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.channel.ChannelPipeline; +import org.jboss.netty.channel.ChannelPipelineCoverage; +import org.jboss.netty.channel.ChannelPipelineFactory; +import org.jboss.netty.channel.ChannelStateEvent; +import org.jboss.netty.channel.Channels; +import org.jboss.netty.channel.MessageEvent; +import org.jboss.netty.channel.SimpleChannelHandler; +import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; +import org.jboss.netty.handler.codec.string.StringDecoder; +import org.jboss.netty.handler.codec.string.StringEncoder; +import org.jboss.netty.handler.ssl.SslHandler; + +public class Server extends Common implements Callable<Void> { + + final InputStream passStream, certStream; + final Runnable onListen; + final ExecutorService bossExecutor, workExecutor; + + public Server(InputStream passStream, InputStream certStream, Runnable onListen, ExecutorService bossExecutor, + ExecutorService workExecutor) { + this.passStream = passStream; + this.certStream = certStream; + this.onListen = onListen; + this.bossExecutor = bossExecutor; + this.workExecutor = workExecutor; + } + + public SSLEngine getSslEngine() throws Exception { + // Read the password. + InputStreamReader passf = new InputStreamReader(passStream); + char[] pass; + StringBuffer sb = new StringBuffer(); + char[] chunk = new char[4096]; + while (true) { + int nread = passf.read(chunk, 0, chunk.length); + if (nread < 0) + break; + sb.append(chunk, 0, nread); + } + String s = sb.toString(); + if (s.charAt(s.length() - 1) == '\n') + s = s.substring(0, s.length() - 1); + pass = s.toCharArray(); + + // Load our Java key store. + KeyStore ks = KeyStore.getInstance("pkcs12"); + ks.load(certStream, pass); + + // Like ssh-agent. + KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); + kmf.init(ks, pass); + + // Create the SSL context. + SSLContext ctx = SSLContext.getInstance("TLS"); + ctx.init(kmf.getKeyManagers(), getTrustManagers(), null); + + SSLEngine engine = ctx.createSSLEngine(); + engine.setUseClientMode(false); + return engine; + } + + public static void main(String[] args) throws Exception { + Server server = new Server(new FileInputStream(args[0]), new FileInputStream(args[1]), null, Executors + .newCachedThreadPool(), Executors.newCachedThreadPool()); + server.call(); + } + + public Void call() throws Exception { + final ChannelFactory factory = new NioServerSocketChannelFactory(bossExecutor, workExecutor); + + final LinkedBlockingQueue<Boolean> onDisconnect = new LinkedBlockingQueue<Boolean>(); + + @ChannelPipelineCoverage("all") + class Cpf implements ChannelPipelineFactory { + public ChannelPipeline getPipeline() throws Exception { + ChannelPipeline pipeline = Channels.pipeline(); + pipeline.addLast("ssl", new SslHandler(getSslEngine())); + pipeline.addLast("decoder", new StringDecoder()); + pipeline.addLast("encoder", new StringEncoder()); + pipeline.addLast("hdl", new SimpleChannelHandler() { + @Override + public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { + onDisconnect.add(true); + } + + @Override + public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { + System.out.println("received: " + e.getMessage()); + } + + @Override + public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { + System.out.println("got connection"); + ctx.getPipeline().get(SslHandler.class).handshake(e.getChannel()).addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) throws Exception { + if (future.isSuccess()) { + System.out.println("handshake succeeded"); + future.getChannel().write("hello\n"); + } else { + System.out.println("handshake failed"); + future.getChannel().close(); + } + } + }); + } + }); + return pipeline; + } + } + ChannelPipelineFactory pipelineFactory = new Cpf(); + + ServerBootstrap bootstrap = new ServerBootstrap(factory); + bootstrap.setPipelineFactory(pipelineFactory); + bootstrap.setOption("child.tcpNoDelay", true); + bootstrap.setOption("child.keepAlive", true); + bootstrap.setOption("reuseAddress", true); + + Channel channel = bootstrap.bind(new InetSocketAddress(9876)); + System.out.println("listening"); + if (onListen != null) + onListen.run(); + onDisconnect.take(); + channel.close().awaitUninterruptibly(); + factory.releaseExternalResources(); + + return null; + } + +} + +// vim: et sw=4 ts=4 Added: sandbox/trunk/src/java/nettytest/src/test/java/nettytest/TestSystem.java =================================================================== --- sandbox/trunk/src/java/nettytest/src/test/java/nettytest/TestSystem.java (rev 0) +++ sandbox/trunk/src/java/nettytest/src/test/java/nettytest/TestSystem.java 2009-07-22 20:42:15 UTC (rev 1463) @@ -0,0 +1,38 @@ +package nettytest; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; + +import junit.framework.TestCase; + +import org.junit.Test; + +public class TestSystem extends TestCase { + + @Test + public void testSystem() throws Exception { + final LinkedBlockingQueue<Boolean> queue = new LinkedBlockingQueue<Boolean>(); + ExecutorService clientExecutor = Executors.newCachedThreadPool(), mainExecutor = Executors + .newCachedThreadPool(), serverExecutor = Executors.newCachedThreadPool(); + mainExecutor.submit(new Server(getClass().getResourceAsStream("p12.pass"), getClass().getResourceAsStream( + "server.p12"), new Runnable() { + + @Override + public void run() { + System.out.println("listening"); + queue.add(true); + } + + }, serverExecutor, serverExecutor)); + queue.take(); + mainExecutor.submit(new Client("localhost", 9876, clientExecutor, clientExecutor)); + clientExecutor.awaitTermination(5, TimeUnit.DAYS); + serverExecutor.awaitTermination(5, TimeUnit.DAYS); + mainExecutor.shutdown(); +// mainExecutor.awaitTermination(5, TimeUnit.DAYS); +// clientExecutor.awaitTermination(5, TimeUnit.DAYS); + } + +} Added: sandbox/trunk/src/java/nettytest/src/test/resources/nettytest/p12.pass =================================================================== --- sandbox/trunk/src/java/nettytest/src/test/resources/nettytest/p12.pass (rev 0) +++ sandbox/trunk/src/java/nettytest/src/test/resources/nettytest/p12.pass 2009-07-22 20:42:15 UTC (rev 1463) @@ -0,0 +1 @@ +eUySvp2phM2Wk Added: sandbox/trunk/src/java/nettytest/src/test/resources/nettytest/server.p12 =================================================================== (Binary files differ) Property changes on: sandbox/trunk/src/java/nettytest/src/test/resources/nettytest/server.p12 ___________________________________________________________________ Added: svn:mime-type + application/octet-stream This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |