package org.msgpack.rpc.server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelPipelineCoverage;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;

@ChannelPipelineCoverage("all")
public class Server {
    protected InetSocketAddress addr;
    protected ServerBootstrap bootstrap;
    
    public Server(String host, int port, Object userHandler) {
        this(new InetSocketAddress(host, port), userHandler);
    }
    
    public Server(InetSocketAddress addr, Object userHandler) {
        this.addr = addr;
        ChannelFactory factory = new NioServerSocketChannelFactory(
                Executors.newCachedThreadPool(),
                Executors.newCachedThreadPool());
        bootstrap = new ServerBootstrap(factory);
        bootstrap.setPipelineFactory(new RPCServerPipelineFactory(userHandler));
        bootstrap.setOption("reuseAddress", true);
        bootstrap.setOption("child.tcpNoDelay", true);
        bootstrap.setOption("child.keepAlive", true);
    }

    public void serv() throws IOException {
        bootstrap.bind(addr);
    }

}

@@ -179,16 +179,56 @@
 tstrap;%0A
+import org.jboss.netty.channel.Channel;%0A
 import o
@@ -464,16 +464,22 @@
 otected 
+final 
 InetSock
@@ -508,16 +508,22 @@
 otected 
+final 
 ServerBo
@@ -541,24 +541,90 @@
 tstrap;%0A    
+protected final ChannelFactory factory;%0A    protected Channel ch;%0A
 %0A    public 
@@ -846,31 +846,21 @@
         
-ChannelFactory 
+this.
 factory 
@@ -1304,16 +1304,29 @@
  public 
+synchronized 
 void ser
@@ -1349,32 +1349,37 @@
 eption %7B%0A       
+ ch =
  bootstrap.bind(
@@ -1391,11 +1391,174 @@
 ;%0A    %7D%0A
+    %0A    public synchronized void stop() %7B%0A        if (ch != null)%0A            ch.close().awaitUninterruptibly();%0A        factory.releaseExternalResources();%0A    %7D
 %0A%7D%0A

add stop() method for Server class.