Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***
- *
- * Platform-independent reactive server program that accepts
- * a connection from a client and echos back what the client sent .
- *
- * @author Olemis Lang
- *
- * p.s. compiled against netty-3.6.10.Final.jar
- *
- */
- import java.net.InetSocketAddress;
- import java.util.Date;
- import java.util.concurrent.Executors;
- import org.jboss.netty.bootstrap.ServerBootstrap;
- import org.jboss.netty.channel.*;
- import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
- public class Program {
- /***
- *
- * Echo client data one chunk at a time .
- *
- * @author Olemis Lang
- *
- * This class is a Wrapper Facade encapsulating callbacks received from
- * low-level sockets implementations. It also implements the Reactor pattern
- * by acting as an Event handler processing ECHO protocol requests/responses.
- *
- */
- static private class EchoServerHandler extends SimpleChannelUpstreamHandler {
- /***
- * Handle message received event by sending received data back to the client.
- *
- */
- @Override
- public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
- throws Exception {
- // Write data back to the client
- e.getChannel().write(e.getMessage());
- }
- /***
- * Print stack trace once an exception is detected
- */
- @Override
- public void exceptionCaught(ChannelHandlerContext ctx,
- ExceptionEvent e) throws Exception {
- // Print exception details onto standard error
- e.getCause().printStackTrace();
- // Close connection
- e.getChannel().close();
- }
- @Override
- public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
- throws Exception {
- System.out.println(new Date().toString() + " => connect from " +
- e.getChannel().getRemoteAddress().toString());
- }
- @Override
- public void channelDisconnected(ChannelHandlerContext ctx,
- ChannelStateEvent e) throws Exception {
- System.out.println(new Date().toString() + " => disconnecting " +
- e.getChannel().getRemoteAddress().toString());
- }
- }
- public static void exit(String msg, int errcode) {
- System.err.println(msg);
- System.exit(errcode);
- }
- public static void main(String[] args) {
- if (args.length == 0) {
- exit("Missing port number", 2);
- }
- int port = 0;
- try {
- port = Integer.parseInt(args[0]);
- }
- catch (NumberFormatException e) {
- exit("Port number expected", 1);
- }
- // Channels and factories play the role of Acceptors
- // by passively waiting for remote connection handshake
- // and initializing upon request arrival the event
- // handlers processing pipeline .
- ChannelFactory f = new NioServerSocketChannelFactory(
- Executors.newCachedThreadPool(),
- Executors.newCachedThreadPool());
- // Server bootstrap and channel instances act as
- // Facade Wrapper since they
- // provide access to low-level bind socket primitive.
- // The former is a helper class with shortcuts to set up
- // a server .
- ServerBootstrap sb = new ServerBootstrap(f);
- // Pipeline factories create channel pipelines acting as
- // initiation dispatchers in Reactor pattern
- // and are used to register, removing and dispatching
- // event handlers.
- sb.setPipelineFactory(new ChannelPipelineFactory() {
- @Override
- public ChannelPipeline getPipeline() throws Exception {
- return Channels.pipeline(new EchoServerHandler());
- }
- });
- // Set TCP connection options
- sb.setOption("child.tcpNoDelay", true);
- sb.setOption("child.keepAlive", true);
- // Bind to socket , start listening for incoming requests
- sb.bind(new InetSocketAddress(port));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement