ClusterUtils.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package com.its.common.cluster.utils;
  2. import io.netty.channel.Channel;
  3. import io.netty.channel.EventLoopGroup;
  4. import io.netty.channel.epoll.Epoll;
  5. import io.netty.channel.epoll.EpollEventLoopGroup;
  6. import io.netty.channel.epoll.EpollServerSocketChannel;
  7. import io.netty.channel.epoll.EpollSocketChannel;
  8. import io.netty.channel.nio.NioEventLoopGroup;
  9. import io.netty.channel.socket.ServerSocketChannel;
  10. import io.netty.channel.socket.SocketChannel;
  11. import io.netty.channel.socket.nio.NioServerSocketChannel;
  12. import io.netty.channel.socket.nio.NioSocketChannel;
  13. import io.netty.util.concurrent.DefaultThreadFactory;
  14. import java.net.*;
  15. import java.util.ArrayList;
  16. import java.util.Enumeration;
  17. public class ClusterUtils {
  18. public static final String OS_NAME = System.getProperty("os.name");
  19. private static boolean isLinuxPlatform = false;
  20. private static boolean isWindowsPlatform = false;
  21. private ClusterUtils() {
  22. }
  23. public static String getAddress(Channel ch) {
  24. String localIp = "local-unknown";
  25. String remoteIp = "remote-unknown";
  26. int localPort = 0;
  27. int remotePort = 0;
  28. InetSocketAddress localAddr = (InetSocketAddress)ch.localAddress();
  29. if (localAddr != null) {
  30. localIp = localAddr.getAddress().getHostAddress();
  31. localPort = localAddr.getPort();
  32. }
  33. InetSocketAddress remoteAddr = (InetSocketAddress)ch.remoteAddress();
  34. if (remoteAddr != null) {
  35. remoteIp = remoteAddr.getAddress().getHostAddress();
  36. remotePort = remoteAddr.getPort();
  37. }
  38. return "[Local #(" + localIp + ":" + localPort + ") Remote #(" + remoteIp + ":" + remotePort + ")]";
  39. }
  40. public static String getRemoteAddress(Channel ch) {
  41. String ip = getRemoteIpAddress(ch);
  42. int port = getRemotePort(ch);
  43. return "[Remote #(" + ip + ":" + port + ")]";
  44. }
  45. public static String getLocalAddress(Channel ch) {
  46. String ip = getLocalIpAddress(ch);
  47. int port = getLocalPort(ch);
  48. return "[Local #(" + ip + ":" + port + ")]";
  49. }
  50. public static String getRemoteIpAddress(Channel ch) {
  51. String ip = "255.255.255.255";
  52. InetSocketAddress inetAddr = (InetSocketAddress)ch.remoteAddress();
  53. if (inetAddr != null) {
  54. ip = inetAddr.getAddress().getHostAddress();
  55. }
  56. return ip;
  57. }
  58. public static long getRemoteIpAddressToLong(Channel ch) {
  59. String[] ipAddressInArray = getRemoteIpAddress(ch).split("\\.");
  60. long result = 0L;
  61. for(int i = 0; i < ipAddressInArray.length; ++i) {
  62. int power = 3 - i;
  63. int ip = Integer.parseInt(ipAddressInArray[i]);
  64. result += (long)((double)ip * Math.pow((double)256.0F, (double)power));
  65. }
  66. return result;
  67. }
  68. public static int getRemotePort(Channel ch) {
  69. int port = 0;
  70. InetSocketAddress inetAddr = (InetSocketAddress)ch.remoteAddress();
  71. if (inetAddr != null) {
  72. port = inetAddr.getPort();
  73. }
  74. return port;
  75. }
  76. public static String getLocalIpAddress(Channel ch) {
  77. String ip = "127.0.0.1";
  78. InetSocketAddress inetAddr = (InetSocketAddress)ch.localAddress();
  79. if (inetAddr != null) {
  80. ip = inetAddr.getAddress().getHostAddress();
  81. }
  82. return ip;
  83. }
  84. public static int getLocalPort(Channel ch) {
  85. int port = 0;
  86. InetSocketAddress inetAddr = (InetSocketAddress)ch.localAddress();
  87. if (inetAddr != null) {
  88. port = inetAddr.getPort();
  89. }
  90. return port;
  91. }
  92. public static boolean isEpollAvailable() {
  93. return Epoll.isAvailable();
  94. }
  95. public static EventLoopGroup newEventLoopGroup(int nThreads, String threadPoolName) {
  96. if (isEpollAvailable()) {
  97. return threadPoolName.isEmpty() ? new EpollEventLoopGroup(nThreads) : new EpollEventLoopGroup(nThreads, new DefaultThreadFactory("epo" + threadPoolName));
  98. } else {
  99. return threadPoolName.isEmpty() ? new NioEventLoopGroup(nThreads) : new NioEventLoopGroup(nThreads, new DefaultThreadFactory("nio" + threadPoolName));
  100. }
  101. }
  102. public static Class<? extends SocketChannel> getSocketChannel() {
  103. return isEpollAvailable() ? EpollSocketChannel.class : NioSocketChannel.class;
  104. }
  105. public static Class<? extends ServerSocketChannel> getServerSocketChannel() {
  106. return isEpollAvailable() ? EpollServerSocketChannel.class : NioServerSocketChannel.class;
  107. }
  108. public static String getTcpAddress(Channel ch) {
  109. String localIp = "local-unknown";
  110. String remoteIp = "remote-unknown";
  111. int localPort = 0;
  112. int remotePort = 0;
  113. InetSocketAddress localAddr = (InetSocketAddress)ch.localAddress();
  114. if (localAddr != null) {
  115. localIp = localAddr.getAddress().getHostAddress();
  116. localPort = localAddr.getPort();
  117. }
  118. InetSocketAddress remoteAddr = (InetSocketAddress)ch.remoteAddress();
  119. if (remoteAddr != null) {
  120. remoteIp = remoteAddr.getAddress().getHostAddress();
  121. remotePort = remoteAddr.getPort();
  122. }
  123. return "[Local #(" + localIp + ":" + localPort + ") Remote #(" + remoteIp + ":" + remotePort + ")]";
  124. }
  125. public static String getLocalAddress() {
  126. Enumeration<NetworkInterface> enumeration = null;
  127. try {
  128. enumeration = NetworkInterface.getNetworkInterfaces();
  129. } catch (SocketException var7) {
  130. return null;
  131. }
  132. ArrayList<String> ipv4Result = new ArrayList<>();
  133. ArrayList<String> ipv6Result = new ArrayList<>();
  134. while(enumeration.hasMoreElements()) {
  135. NetworkInterface networkInterface = (NetworkInterface)enumeration.nextElement();
  136. Enumeration<InetAddress> en = networkInterface.getInetAddresses();
  137. while(en.hasMoreElements()) {
  138. InetAddress address = (InetAddress)en.nextElement();
  139. if (!address.isLoopbackAddress()) {
  140. if (address instanceof Inet6Address) {
  141. ipv6Result.add(normalizeHostAddress(address));
  142. } else {
  143. ipv4Result.add(normalizeHostAddress(address));
  144. }
  145. }
  146. }
  147. }
  148. if (!ipv4Result.isEmpty()) {
  149. for(String ip : ipv4Result) {
  150. if (!ip.startsWith("127.0") && !ip.startsWith("192.168")) {
  151. return ip;
  152. }
  153. }
  154. return (String)ipv4Result.get(ipv4Result.size() - 1);
  155. } else if (!ipv6Result.isEmpty()) {
  156. return (String)ipv6Result.get(0);
  157. } else {
  158. InetAddress localHost;
  159. try {
  160. localHost = InetAddress.getLocalHost();
  161. } catch (UnknownHostException var6) {
  162. return null;
  163. }
  164. return normalizeHostAddress(localHost);
  165. }
  166. }
  167. public static String normalizeHostAddress(InetAddress localHost) {
  168. return localHost instanceof Inet6Address ? "[" + localHost.getHostAddress() + "]" : localHost.getHostAddress();
  169. }
  170. public static SocketAddress string2SocketAddress(String addr) {
  171. String[] s = addr.split(":");
  172. InetSocketAddress isa = new InetSocketAddress(s[0], Integer.parseInt(s[1]));
  173. return isa;
  174. }
  175. public static String socketAddress2String(SocketAddress addr) {
  176. StringBuilder sb = new StringBuilder();
  177. InetSocketAddress inetSocketAddress = (InetSocketAddress)addr;
  178. sb.append(inetSocketAddress.getAddress().getHostAddress());
  179. sb.append(":");
  180. sb.append(inetSocketAddress.getPort());
  181. return sb.toString();
  182. }
  183. public static String parseChannelRemoteAddr(Channel channel) {
  184. if (null == channel) {
  185. return "";
  186. } else {
  187. SocketAddress remote = channel.remoteAddress();
  188. String addr = remote != null ? remote.toString() : "";
  189. if (addr.length() > 0) {
  190. int index = addr.lastIndexOf("/");
  191. return index >= 0 ? addr.substring(index + 1) : addr;
  192. } else {
  193. return "";
  194. }
  195. }
  196. }
  197. public static String parseSocketAddressAddr(SocketAddress socketAddress) {
  198. if (socketAddress != null) {
  199. String addr = socketAddress.toString();
  200. if (addr.length() > 0) {
  201. return addr.startsWith("/") ? addr.substring(1) : addr;
  202. }
  203. }
  204. return "";
  205. }
  206. static {
  207. if (OS_NAME != null && OS_NAME.toLowerCase().contains("linux")) {
  208. isLinuxPlatform = true;
  209. }
  210. if (OS_NAME != null && OS_NAME.toLowerCase().contains("windows")) {
  211. isWindowsPlatform = true;
  212. }
  213. }
  214. }