package software.sitb.dswrs.server.rpc;

import io.netty.channel.ChannelPipeline;
import org.springframework.context.ApplicationContext;
import software.sitb.dswrs.core.ServiceRegistry;
import software.sitb.dswrs.core.config.DswrsProperties;
import software.sitb.dswrs.core.netty.ObjectServer;
import software.sitb.dswrs.core.rpc.RpcRequest;
import software.sitb.dswrs.core.rpc.RpcResponse;
import software.sitb.dswrs.core.zookeeper.ZooKeeperConfig;

import java.util.HashMap;
import java.util.Map;

/**
 * RPC 服务器
 *
 * @author Sean sean.snow@live.com
 */
public class RpcServer extends ObjectServer<RpcRequest, RpcResponse> {

    private DswrsProperties properties;

    private ServiceRegistry serviceRegistry;

    private Map<String, Object> rpcBeans = new HashMap<>(0);

    private ApplicationContext applicationContext;

    public RpcServer(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
        this.init();
    }

    @Override
    public int getPort() {
        return properties.getPort();
    }

    @Override
    public String getHost() {
        return properties.getHost();
    }

    @Override
    public Class<RpcRequest> getRequestClazz() {
        return RpcRequest.class;
    }

    @Override
    public Class<RpcResponse> getResponseClazz() {
        return RpcResponse.class;
    }

    /**
     * 添加数据处理通道
     *
     * @param pipeline 管道
     */
    @Override
    public void addMessageHandler(ChannelPipeline pipeline) {
        RpcServerHandler handler = new RpcServerHandler();
        handler.setRpcServiceBean(this.rpcBeans);
        pipeline.addLast(handler);
    }

    protected void init() {
        this.properties = applicationContext.getBean(DswrsProperties.class);
        this.serviceRegistry = applicationContext.getBean(ServiceRegistry.class);
        Map<String, Object> rpcBeans = applicationContext.getBeansWithAnnotation(RpcService.class);
        if (null != rpcBeans && !rpcBeans.isEmpty()) {
            this.rpcBeans = new HashMap<>(rpcBeans.size());
            rpcBeans.forEach((key, value) -> {
                String interfaceName = value.getClass().getAnnotation(RpcService.class).value().getName();
                this.rpcBeans.put(interfaceName, value);
            });
        }
    }


    /**
     * 添加一个RPC服务Bean
     *
     * @param clazz 接口Class
     * @param bean  接口实现示例
     */
    public void addRpcBeans(Class<?> clazz, Object bean) {
        if (null != clazz && null != bean) {
            String interfaceName = clazz.getName();
            this.rpcBeans.put(interfaceName, bean);
        }
    }

    public void startRpcServer() throws InterruptedException {
        this.start(() -> {
            ZooKeeperConfig config = this.properties.getZooKeeper();
            if (config.getEnabled()) {
                String data = this.getHost() + ":" + this.getPort();
                this.serviceRegistry.register(data);
            }
        });
    }

}

@@ -75,16 +75,132 @@
 peline;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0Aimport org.springframework.beans.factory.InitializingBean;%0A
 import o
@@ -560,16 +560,101 @@
 onfig;%0A%0A
+import java.lang.reflect.InvocationTargetException;%0Aimport java.lang.reflect.Method;%0A
 import j
@@ -820,17 +820,129 @@
 sponse%3E 
-%7B
+implements InitializingBean %7B%0A%0A    private static final Logger LOGGER = LoggerFactory.getLogger(RpcServer.class);
 %0A%0A    pr
@@ -1256,29 +1256,8 @@
 xt;%0A
-        this.init();%0A
     
@@ -1941,29 +1941,71 @@
     
-protected void init()
+@Override%0A    public void afterPropertiesSet() throws Exception
  %7B%0A 
@@ -2444,47 +2444,581 @@
     
-String interfaceName = 
+Class%3C?%3E clazz = value.getClass();%0A                try %7B%0A                    Method method = clazz.getMethod(%22getTargetClass%22);%0A                    Class%3C?%3E result = (Class%3C?%3E) method.invoke(value);%0A                    String interfaceName = result.getAnnotation(RpcService.class).
 value
+()
 .get
-Class()
+Name();%0A                    addRpcBean(interfaceName, value);%0A                    LOGGER.debug(%22RPC Bean %5B%7B%7D%5D%22, interfaceName);%0A                %7D catch (NoSuchMethodException %7C IllegalAccessException %7C InvocationTargetException e) %7B%0A                    String interfaceName = clazz
 .get
@@ -3081,33 +3081,30 @@
         
-this.r
+    addR
 pcBean
-s.put
 (interfa
@@ -3111,32 +3111,116 @@
 ceName, value);%0A
+                    LOGGER.debug(%22RPC Bean %5B%7B%7D%5D%22, interfaceName);%0A                %7D%0A
             %7D);%0A
@@ -3236,17 +3236,16 @@
 %0A    %7D%0A%0A
-%0A
     /**%0A
@@ -3361,17 +3361,16 @@
 dRpcBean
-s
 (Class%3C?
@@ -3506,25 +3506,18 @@
     
-this.r
+addR
 pcBean
-s.put
 (int
@@ -3548,24 +3548,134 @@
    %7D%0A    %7D%0A%0A
+    private void addRpcBean(String beanName, Object bean) %7B%0A        this.rpcBeans.put(beanName, bean);%0A    %7D%0A%0A
     public v
@@ -3717,32 +3717,75 @@
 ptedException %7B%0A
+        LOGGER.debug(%22start rpc server.%22);%0A
         this.sta
@@ -4058,10 +4058,11 @@
 %0A    %7D%0A%0A
+%0A
 %7D%0A

初始化保存RPC Bean 动态添加Bean 手动启动RPC服务