/**
* $Id: RemotingUtil.java 1831 2013-05-16 01:39:51Z shijia.wxr $
*/
package com.alibaba.rocketmq.remoting.common;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.channels.spi.SelectorProvider;
import java.util.Enumeration;
/**
* ������ط���
*
* @author vintage.wang@gmail.com shijia.wxr@taobao.com
*/
public class RemotingUtil {
public static final String OS_NAME = System.getProperty("os.name");
private static boolean isLinuxPlatform = false;
static {
if (OS_NAME != null && OS_NAME.toLowerCase().indexOf("linux") >= 0) {
isLinuxPlatform = true;
}
}
public static boolean isLinuxPlatform() {
return isLinuxPlatform;
}
public static Selector openSelector() throws IOException {
Selector result = null;
// ��linuxƽ̨����������epollʵ��
if (isLinuxPlatform()) {
try {
final Class<?> providerClazz = Class.forName("sun.nio.ch.EPollSelectorProvider");
if (providerClazz != null) {
try {
final Method method = providerClazz.getMethod("provider");
if (method != null) {
final SelectorProvider selectorProvider = (SelectorProvider) method.invoke(null);
if (selectorProvider != null) {
result = selectorProvider.openSelector();
}
}
}
catch (final Exception e) {
// ignore
}
}
}
catch (final Exception e) {
// ignore
}
}
if (result == null) {
result = Selector.open();
}
return result;
}
public static String getLocalAddress() {
try {
// ��������������һ���ǻ�·ip��ַ������
Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
InetAddress ipv6Address = null;
while (enumeration.hasMoreElements()) {
final NetworkInterface networkInterface = enumeration.nextElement();
final Enumeration<InetAddress> en = networkInterface.getInetAddresses();
while (en.hasMoreElements()) {
final InetAddress address = en.nextElement();
if (!address.isLoopbackAddress()) {
if (address instanceof Inet6Address) {
ipv6Address = address;
}
else {
// ����ʹ��ipv4
return normalizeHostAddress(address);
}
}
}
}
// û��ipv4����ʹ��ipv6
if (ipv6Address != null) {
return normalizeHostAddress(ipv6Address);
}
final InetAddress localHost = InetAddress.getLocalHost();
return normalizeHostAddress(localHost);
}
catch (SocketException e) {
e.printStackTrace();
}
catch (UnknownHostException e) {
e.printStackTrace();
}
return null;
}
public static String normalizeHostAddress(final InetAddress localHost) {
if (localHost instanceof Inet6Address) {
return "[" + localHost.getHostAddress() + "]";
}
else {
return localHost.getHostAddress();
}
}
/**
* IP:PORT
*/
public static SocketAddress string2SocketAddress(final String addr) {
String[] s = addr.split(":");
InetSocketAddress isa = new InetSocketAddress(s[0], Integer.valueOf(s[1]));
return isa;
}
public static String socketAddress2String(final 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 SocketChannel connect(SocketAddress remote) {
return connect(remote, 1000 * 5);
}
public static SocketChannel connect(SocketAddress remote, final int timeoutMillis) {
SocketChannel sc = null;
try {
sc = SocketChannel.open();
sc.configureBlocking(true);
sc.socket().setSoLinger(false, -1);
sc.socket().setTcpNoDelay(true);
sc.socket().setReceiveBufferSize(1024 * 64);
sc.socket().setSendBufferSize(1024 * 64);
sc.socket().connect(remote, timeoutMillis);
sc.configureBlocking(false);
return sc;
}
catch (Exception e) {
if (sc != null) {
try {
sc.close();
}
catch (IOException e1) {
e1.printStackTrace();
}
}
}
return null;
}
}