// Path: cn.ctodb.push.reg/src/main/java/cn/ctodb/push/reg/entity/ServerNode.java
// public class ServerNode implements Serializable {
// 
//     private String id;
//     private String url;
//     private long lastCheckTime;
//     private long connectCount;
// 
//     public String getId() {
//         return id;
//     }
// 
//     public void setId(String id) {
//         this.id = id;
//     }
// 
//     public String getUrl() {
//         return url;
//     }
// 
//     public void setUrl(String url) {
//         this.url = url;
//     }
// 
//     public long getLastCheckTime() {
//         return lastCheckTime;
//     }
// 
//     public void setLastCheckTime(long lastCheckTime) {
//         this.lastCheckTime = lastCheckTime;
//     }
// 
//     public long getConnectCount() {
//         return connectCount;
//     }
// 
//     public void setConnectCount(long connectCount) {
//         this.connectCount = connectCount;
//     }
// }
// 
// Path: cn.ctodb.push.reg/src/main/java/cn/ctodb/push/reg/util/IpUtil.java
// public class IpUtil {
//     public static String getIpAddr(HttpServletRequest request) {
//         String ipAddress = null;
//         try {
//             ipAddress = request.getHeader("x-forwarded-for");
//             if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
//                 ipAddress = request.getHeader("Proxy-Client-IP");
//             }
//             if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
//                 ipAddress = request.getHeader("WL-Proxy-Client-IP");
//             }
//             if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
//                 ipAddress = request.getRemoteAddr();
//                 if (ipAddress.equals("127.0.0.1")) {
//                     // 根据网卡取本机配置的IP
//                     InetAddress inet = null;
//                     try {
//                         inet = InetAddress.getLocalHost();
//                     } catch (UnknownHostException e) {
//                         e.printStackTrace();
//                     }
//                     ipAddress = inet.getHostAddress();
//                 }
//             }
//             // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
//             if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
//                 // = 15
//                 if (ipAddress.indexOf(",") > 0) {
//                     ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
//                 }
//             }
//         } catch (Exception e) {
//             ipAddress = "";
//         }
//         // ipAddress = this.getRequest().getRemoteAddr();
// 
//         return ipAddress;
//     }
// }


import cn.ctodb.push.reg.entity.ServerNode;
import cn.ctodb.push.reg.util.IpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
            serverNode = new ServerNode();
            serverNode.setId(id);
            if (StringUtils.isEmpty(ip))
                ip = IpUtil.getIpAddr(request);
            serverNode.setUrl(ip + ":" + port);
            serverNode.setLastCheckTime(System.currentTimeMillis());
        } else {
            serverNode = (ServerNode) imKeep.get(id);
            serverNode.setLastCheckTime(System.currentTimeMillis());
        }
        logger.debug("keep:{} {} {}", id, ip, port);
        imKeep.put(id, serverNode);
        return serverNode;
    }

    @GetMapping("nodes")
    public Set<String> nodes() {
        BoundHashOperations<String, String, ServerNode> imKeep = getNodes();
        Set<String> set = new HashSet<>();
        for (ServerNode node : Objects.requireNonNull(imKeep.values())) {
            set.add(node.getUrl());
        }
        return set;
    }

    private BoundHashOperations getNodes() {
        BoundHashOperations imKeep = redisTemplate.boundHashOps("im_keep");
        return imKeep;
    }

// Path: cn.ctodb.push.reg/src/main/java/cn/ctodb/push/reg/entity/ServerNode.java
// public class ServerNode implements Serializable {
// 
//     private String id;
//     private String url;
//     private long lastCheckTime;
//     private long connectCount;
// 
//     public String getId() {
//         return id;
//     }
// 
//     public void setId(String id) {
//         this.id = id;
//     }
// 
//     public String getUrl() {
//         return url;
//     }
// 
//     public void setUrl(String url) {
//         this.url = url;
//     }
// 
//     public long getLastCheckTime() {
//         return lastCheckTime;
//     }
// 
//     public void setLastCheckTime(long lastCheckTime) {
//         this.lastCheckTime = lastCheckTime;
//     }
// 
//     public long getConnectCount() {
//         return connectCount;
//     }
// 
//     public void setConnectCount(long connectCount) {
//         this.connectCount = connectCount;
//     }
// }
// 
// Path: cn.ctodb.push.reg/src/main/java/cn/ctodb/push/reg/util/IpUtil.java
// public class IpUtil {
//     public static String getIpAddr(HttpServletRequest request) {
//         String ipAddress = null;
//         try {
//             ipAddress = request.getHeader("x-forwarded-for");
//             if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
//                 ipAddress = request.getHeader("Proxy-Client-IP");
//             }
//             if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
//                 ipAddress = request.getHeader("WL-Proxy-Client-IP");
//             }
//             if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
//                 ipAddress = request.getRemoteAddr();
//                 if (ipAddress.equals("127.0.0.1")) {
//                     // 根据网卡取本机配置的IP
//                     InetAddress inet = null;
//                     try {
//                         inet = InetAddress.getLocalHost();
//                     } catch (UnknownHostException e) {
//                         e.printStackTrace();
//                     }
//                     ipAddress = inet.getHostAddress();
//                 }
//             }
//             // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
//             if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
//                 // = 15
//                 if (ipAddress.indexOf(",") > 0) {
//                     ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
//                 }
//             }
//         } catch (Exception e) {
//             ipAddress = "";
//         }
//         // ipAddress = this.getRequest().getRemoteAddr();
// 
//         return ipAddress;
//     }
// }


// Path: cn.ctodb.push.reg/src/main/java/cn/ctodb/push/reg/controller/RegController.java
import cn.ctodb.push.reg.entity.ServerNode;
import cn.ctodb.push.reg.util.IpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

            serverNode = new ServerNode();
            serverNode.setId(id);
            if (StringUtils.isEmpty(ip))
                ip = IpUtil.getIpAddr(request);
            serverNode.setUrl(ip + ":" + port);
            serverNode.setLastCheckTime(System.currentTimeMillis());
        } else {
            serverNode = (ServerNode) imKeep.get(id);
            serverNode.setLastCheckTime(System.currentTimeMillis());
        }
        logger.debug("keep:{} {} {}", id, ip, port);
        imKeep.put(id, serverNode);
        return serverNode;
    }

    @GetMapping("nodes")
    public Set<String> nodes() {
        BoundHashOperations<String, String, ServerNode> imKeep = getNodes();
        Set<String> set = new HashSet<>();
        for (ServerNode node : Objects.requireNonNull(imKeep.values())) {
            set.add(node.getUrl());
        }
        return set;
    }

    private BoundHashOperations getNodes() {
        BoundHashOperations imKeep = redisTemplate.boundHashOps("im_keep");
        return imKeep;
    }

    @Scheduled(cron = "0/30 * * * * *")