123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- package com.its.common.cluster.utils;
- import io.netty.channel.Channel;
- 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.epoll.EpollSocketChannel;
- import io.netty.channel.nio.NioEventLoopGroup;
- import io.netty.channel.socket.ServerSocketChannel;
- import io.netty.channel.socket.SocketChannel;
- import io.netty.channel.socket.nio.NioServerSocketChannel;
- import io.netty.channel.socket.nio.NioSocketChannel;
- import io.netty.util.concurrent.DefaultThreadFactory;
- import java.net.*;
- import java.util.ArrayList;
- import java.util.Enumeration;
- public class ClusterUtils {
- public static final String OS_NAME = System.getProperty("os.name");
- private static boolean isLinuxPlatform = false;
- private static boolean isWindowsPlatform = false;
- private ClusterUtils() {
- }
- public static String getAddress(Channel ch) {
- String localIp = "local-unknown";
- String remoteIp = "remote-unknown";
- int localPort = 0;
- int remotePort = 0;
- InetSocketAddress localAddr = (InetSocketAddress)ch.localAddress();
- if (localAddr != null) {
- localIp = localAddr.getAddress().getHostAddress();
- localPort = localAddr.getPort();
- }
- InetSocketAddress remoteAddr = (InetSocketAddress)ch.remoteAddress();
- if (remoteAddr != null) {
- remoteIp = remoteAddr.getAddress().getHostAddress();
- remotePort = remoteAddr.getPort();
- }
- return "[Local #(" + localIp + ":" + localPort + ") Remote #(" + remoteIp + ":" + remotePort + ")]";
- }
- public static String getRemoteAddress(Channel ch) {
- String ip = getRemoteIpAddress(ch);
- int port = getRemotePort(ch);
- return "[Remote #(" + ip + ":" + port + ")]";
- }
- public static String getLocalAddress(Channel ch) {
- String ip = getLocalIpAddress(ch);
- int port = getLocalPort(ch);
- return "[Local #(" + ip + ":" + port + ")]";
- }
- public static String getRemoteIpAddress(Channel ch) {
- String ip = "255.255.255.255";
- InetSocketAddress inetAddr = (InetSocketAddress)ch.remoteAddress();
- if (inetAddr != null) {
- ip = inetAddr.getAddress().getHostAddress();
- }
- return ip;
- }
- public static long getRemoteIpAddressToLong(Channel ch) {
- String[] ipAddressInArray = getRemoteIpAddress(ch).split("\\.");
- long result = 0L;
- for(int i = 0; i < ipAddressInArray.length; ++i) {
- int power = 3 - i;
- int ip = Integer.parseInt(ipAddressInArray[i]);
- result += (long)((double)ip * Math.pow((double)256.0F, (double)power));
- }
- return result;
- }
- public static int getRemotePort(Channel ch) {
- int port = 0;
- InetSocketAddress inetAddr = (InetSocketAddress)ch.remoteAddress();
- if (inetAddr != null) {
- port = inetAddr.getPort();
- }
- return port;
- }
- public static String getLocalIpAddress(Channel ch) {
- String ip = "127.0.0.1";
- InetSocketAddress inetAddr = (InetSocketAddress)ch.localAddress();
- if (inetAddr != null) {
- ip = inetAddr.getAddress().getHostAddress();
- }
- return ip;
- }
- public static int getLocalPort(Channel ch) {
- int port = 0;
- InetSocketAddress inetAddr = (InetSocketAddress)ch.localAddress();
- if (inetAddr != null) {
- port = inetAddr.getPort();
- }
- return port;
- }
- public static boolean isEpollAvailable() {
- return Epoll.isAvailable();
- }
- public static EventLoopGroup newEventLoopGroup(int nThreads, String threadPoolName) {
- if (isEpollAvailable()) {
- return threadPoolName.isEmpty() ? new EpollEventLoopGroup(nThreads) : new EpollEventLoopGroup(nThreads, new DefaultThreadFactory("epo" + threadPoolName));
- } else {
- return threadPoolName.isEmpty() ? new NioEventLoopGroup(nThreads) : new NioEventLoopGroup(nThreads, new DefaultThreadFactory("nio" + threadPoolName));
- }
- }
- public static Class<? extends SocketChannel> getSocketChannel() {
- return isEpollAvailable() ? EpollSocketChannel.class : NioSocketChannel.class;
- }
- public static Class<? extends ServerSocketChannel> getServerSocketChannel() {
- return isEpollAvailable() ? EpollServerSocketChannel.class : NioServerSocketChannel.class;
- }
- public static String getTcpAddress(Channel ch) {
- String localIp = "local-unknown";
- String remoteIp = "remote-unknown";
- int localPort = 0;
- int remotePort = 0;
- InetSocketAddress localAddr = (InetSocketAddress)ch.localAddress();
- if (localAddr != null) {
- localIp = localAddr.getAddress().getHostAddress();
- localPort = localAddr.getPort();
- }
- InetSocketAddress remoteAddr = (InetSocketAddress)ch.remoteAddress();
- if (remoteAddr != null) {
- remoteIp = remoteAddr.getAddress().getHostAddress();
- remotePort = remoteAddr.getPort();
- }
- return "[Local #(" + localIp + ":" + localPort + ") Remote #(" + remoteIp + ":" + remotePort + ")]";
- }
- public static String getLocalAddress() {
- Enumeration<NetworkInterface> enumeration = null;
- try {
- enumeration = NetworkInterface.getNetworkInterfaces();
- } catch (SocketException var7) {
- return null;
- }
- ArrayList<String> ipv4Result = new ArrayList<>();
- ArrayList<String> ipv6Result = new ArrayList<>();
- while(enumeration.hasMoreElements()) {
- NetworkInterface networkInterface = (NetworkInterface)enumeration.nextElement();
- Enumeration<InetAddress> en = networkInterface.getInetAddresses();
- while(en.hasMoreElements()) {
- InetAddress address = (InetAddress)en.nextElement();
- if (!address.isLoopbackAddress()) {
- if (address instanceof Inet6Address) {
- ipv6Result.add(normalizeHostAddress(address));
- } else {
- ipv4Result.add(normalizeHostAddress(address));
- }
- }
- }
- }
- if (!ipv4Result.isEmpty()) {
- for(String ip : ipv4Result) {
- if (!ip.startsWith("127.0") && !ip.startsWith("192.168")) {
- return ip;
- }
- }
- return (String)ipv4Result.get(ipv4Result.size() - 1);
- } else if (!ipv6Result.isEmpty()) {
- return (String)ipv6Result.get(0);
- } else {
- InetAddress localHost;
- try {
- localHost = InetAddress.getLocalHost();
- } catch (UnknownHostException var6) {
- return null;
- }
- return normalizeHostAddress(localHost);
- }
- }
- public static String normalizeHostAddress(InetAddress localHost) {
- return localHost instanceof Inet6Address ? "[" + localHost.getHostAddress() + "]" : localHost.getHostAddress();
- }
- public static SocketAddress string2SocketAddress(String addr) {
- String[] s = addr.split(":");
- InetSocketAddress isa = new InetSocketAddress(s[0], Integer.parseInt(s[1]));
- return isa;
- }
- public static String socketAddress2String(SocketAddress addr) {
- StringBuilder sb = new StringBuilder();
- InetSocketAddress inetSocketAddress = (InetSocketAddress)addr;
- sb.append(inetSocketAddress.getAddress().getHostAddress());
- sb.append(":");
- sb.append(inetSocketAddress.getPort());
- return sb.toString();
- }
- public static String parseChannelRemoteAddr(Channel channel) {
- if (null == channel) {
- return "";
- } else {
- SocketAddress remote = channel.remoteAddress();
- String addr = remote != null ? remote.toString() : "";
- if (addr.length() > 0) {
- int index = addr.lastIndexOf("/");
- return index >= 0 ? addr.substring(index + 1) : addr;
- } else {
- return "";
- }
- }
- }
- public static String parseSocketAddressAddr(SocketAddress socketAddress) {
- if (socketAddress != null) {
- String addr = socketAddress.toString();
- if (addr.length() > 0) {
- return addr.startsWith("/") ? addr.substring(1) : addr;
- }
- }
- return "";
- }
- static {
- if (OS_NAME != null && OS_NAME.toLowerCase().contains("linux")) {
- isLinuxPlatform = true;
- }
- if (OS_NAME != null && OS_NAME.toLowerCase().contains("windows")) {
- isWindowsPlatform = true;
- }
- }
- }
|