// Path: src/main/java/com/my/lionrpc/common/ApplicationWorkerCenter.java
// public class ApplicationWorkerCenter{
// 
//     private ThreadPoolExecutor executorService;
// 
//     private AtomicInteger threadCount = new AtomicInteger(1);
// 
//     public ApplicationWorkerCenter(int coreSize,int maxSize,long keepAliveSecond,int queueSize){
//         executorService = new ThreadPoolExecutor(coreSize, maxSize, keepAliveSecond, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(queueSize), new ThreadFactory() {
//             public Thread newThread(Runnable r) {
//                 Thread thread = new Thread(r);
//                 thread.setName("ApplicationWorker-"+threadCount);
//                 threadCount.incrementAndGet();
//                 return thread;
//             }
//         });
//     }
// 
//     public void dispatchWorker(Runnable worker){
//         executorService.submit(worker);
//     }
// 
// 
// }
// 
// Path: src/main/java/com/my/lionrpc/registry/ServiceRegistry.java
// public class ServiceRegistry {
// 
//     private static final Logger LOGGER = LoggerFactory.getLogger(ServiceRegistry.class);
// 
//     private CountDownLatch latch = new CountDownLatch(1);
// 
//     private String registryAddress;
//     
//     private String data;
// 
//     public ServiceRegistry(String registryAddress) {
//         this.registryAddress = registryAddress;
//     }
// 
//     public void register(String data) {
//     	this.data = data;
//         if (data != null) {
//             ZooKeeper zk = connectServer();
//             if (zk != null) {
//                 addRootNode(zk); // Add root node if not exist
//                 createNode(zk, data);
//             }
//         }
//     }
//     
//     public void reRegister() {
//         if (data != null) {
//             ZooKeeper zk = connectServer();
//             if (zk != null) {
//                 addRootNode(zk); // Add root node if not exist
//                 createNode(zk, data);
//             }
//         }
//     }
// 
//     private ZooKeeper connectServer() {
//         ZooKeeper zk = null;
//         try {
//             zk = new ZooKeeper(registryAddress, Constant.ZK_SESSION_TIMEOUT, new Watcher() {
// 
//                 public void process(WatchedEvent event) {
//                     if (event.getState() == Event.KeeperState.SyncConnected) {
//                         latch.countDown();
//                     }else{
//                     	if(event.getState() == Event.KeeperState.Expired || event.getState() == Event.KeeperState.Disconnected){
//                     		LOGGER.info("ServiceRegistry reRegister...");
//                     		reRegister();
//                     	}
//                     }
//                 }
//             });
//             latch.await();
//         } catch (IOException e) {
//             LOGGER.error("", e);
//         }catch (InterruptedException ex){
//             LOGGER.error("", ex);
//         }
//         return zk;
//     }
// 
//     private void addRootNode(ZooKeeper zk){
//         try {
//             Stat s = zk.exists(Constant.ZK_REGISTRY_PATH, false);
//             if (s == null) {
//                 zk.create(Constant.ZK_REGISTRY_PATH, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
//             }
//         } catch (KeeperException e) {
//             LOGGER.error(e.toString());
//         } catch (InterruptedException e) {
//             LOGGER.error(e.toString());
//         }
//     }
// 
//     private void createNode(ZooKeeper zk, String data) {
//         try {
//             byte[] bytes = data.getBytes();
//             String path = zk.create(Constant.ZK_DATA_PATH, bytes, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
//             LOGGER.debug("create zookeeper node ({} => {})", path, data);
//         } catch (KeeperException e) {
//             LOGGER.error("", e);
//         }
//         catch (InterruptedException ex){
//             LOGGER.error("", ex);
//         }
//     }
// }


import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import com.my.lionrpc.annotation.LionService;
import com.my.lionrpc.common.ApplicationWorkerCenter;
import com.my.lionrpc.registry.ServiceRegistry;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollServerSocketChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
package com.my.lionrpc.server;






/**
 * Created by littlechen on 17/5/21.
 */
public class LionServer implements ApplicationContextAware,InitializingBean{

    private Logger LOG = LoggerFactory.getLogger(LionServer.class);


    private String serverAddress;
    private ApplicationWorkerCenter workerCenter;
    private ServiceRegistry registry;
 

    private static AtomicInteger serverGroupCount = new AtomicInteger(1);
    private Map<String,Object> serverHandlerMap = new HashMap<String,Object>();
    
    

// Path: src/main/java/com/my/lionrpc/common/ApplicationWorkerCenter.java
// public class ApplicationWorkerCenter{
// 
//     private ThreadPoolExecutor executorService;
// 
//     private AtomicInteger threadCount = new AtomicInteger(1);
// 
//     public ApplicationWorkerCenter(int coreSize,int maxSize,long keepAliveSecond,int queueSize){
//         executorService = new ThreadPoolExecutor(coreSize, maxSize, keepAliveSecond, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(queueSize), new ThreadFactory() {
//             public Thread newThread(Runnable r) {
//                 Thread thread = new Thread(r);
//                 thread.setName("ApplicationWorker-"+threadCount);
//                 threadCount.incrementAndGet();
//                 return thread;
//             }
//         });
//     }
// 
//     public void dispatchWorker(Runnable worker){
//         executorService.submit(worker);
//     }
// 
// 
// }
// 
// Path: src/main/java/com/my/lionrpc/registry/ServiceRegistry.java
// public class ServiceRegistry {
// 
//     private static final Logger LOGGER = LoggerFactory.getLogger(ServiceRegistry.class);
// 
//     private CountDownLatch latch = new CountDownLatch(1);
// 
//     private String registryAddress;
//     
//     private String data;
// 
//     public ServiceRegistry(String registryAddress) {
//         this.registryAddress = registryAddress;
//     }
// 
//     public void register(String data) {
//     	this.data = data;
//         if (data != null) {
//             ZooKeeper zk = connectServer();
//             if (zk != null) {
//                 addRootNode(zk); // Add root node if not exist
//                 createNode(zk, data);
//             }
//         }
//     }
//     
//     public void reRegister() {
//         if (data != null) {
//             ZooKeeper zk = connectServer();
//             if (zk != null) {
//                 addRootNode(zk); // Add root node if not exist
//                 createNode(zk, data);
//             }
//         }
//     }
// 
//     private ZooKeeper connectServer() {
//         ZooKeeper zk = null;
//         try {
//             zk = new ZooKeeper(registryAddress, Constant.ZK_SESSION_TIMEOUT, new Watcher() {
// 
//                 public void process(WatchedEvent event) {
//                     if (event.getState() == Event.KeeperState.SyncConnected) {
//                         latch.countDown();
//                     }else{
//                     	if(event.getState() == Event.KeeperState.Expired || event.getState() == Event.KeeperState.Disconnected){
//                     		LOGGER.info("ServiceRegistry reRegister...");
//                     		reRegister();
//                     	}
//                     }
//                 }
//             });
//             latch.await();
//         } catch (IOException e) {
//             LOGGER.error("", e);
//         }catch (InterruptedException ex){
//             LOGGER.error("", ex);
//         }
//         return zk;
//     }
// 
//     private void addRootNode(ZooKeeper zk){
//         try {
//             Stat s = zk.exists(Constant.ZK_REGISTRY_PATH, false);
//             if (s == null) {
//                 zk.create(Constant.ZK_REGISTRY_PATH, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
//             }
//         } catch (KeeperException e) {
//             LOGGER.error(e.toString());
//         } catch (InterruptedException e) {
//             LOGGER.error(e.toString());
//         }
//     }
// 
//     private void createNode(ZooKeeper zk, String data) {
//         try {
//             byte[] bytes = data.getBytes();
//             String path = zk.create(Constant.ZK_DATA_PATH, bytes, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
//             LOGGER.debug("create zookeeper node ({} => {})", path, data);
//         } catch (KeeperException e) {
//             LOGGER.error("", e);
//         }
//         catch (InterruptedException ex){
//             LOGGER.error("", ex);
//         }
//     }
// }


// Path: src/main/java/com/my/lionrpc/server/LionServer.java
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import com.my.lionrpc.annotation.LionService;
import com.my.lionrpc.common.ApplicationWorkerCenter;
import com.my.lionrpc.registry.ServiceRegistry;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollServerSocketChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

package com.my.lionrpc.server;






/**
 * Created by littlechen on 17/5/21.
 */
public class LionServer implements ApplicationContextAware,InitializingBean{

    private Logger LOG = LoggerFactory.getLogger(LionServer.class);


    private String serverAddress;
    private ApplicationWorkerCenter workerCenter;
    private ServiceRegistry registry;
 

    private static AtomicInteger serverGroupCount = new AtomicInteger(1);
    private Map<String,Object> serverHandlerMap = new HashMap<String,Object>();
    
    

    public LionServer(String serverAddress, ApplicationWorkerCenter workerCenter, ServiceRegistry registry) {