Java Code Examples for io.netty.util.NetUtil#toAddressString()

The following examples show how to use io.netty.util.NetUtil#toAddressString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BuilderUtils.java    From servicetalk with Apache License 2.0 6 votes vote down vote up
/**
 * Format an address into a canonical numeric format.
 *
 * @param address socket address
 * @return formatted address
 */
public static String formatCanonicalAddress(SocketAddress address) {
    // Try to return the "raw" address (without resolved host name, etc)
    if (address instanceof InetSocketAddress) {
        InetSocketAddress inetSocketAddress = (InetSocketAddress) address;
        InetAddress inetAddress = inetSocketAddress.getAddress();
        // inetAddress could be null if SocketAddress is in an unresolved form
        if (inetAddress == null) {
            return address.toString();
        } else if (inetAddress instanceof Inet6Address) {
            return '[' + NetUtil.toAddressString(inetAddress) + "]:" + inetSocketAddress.getPort();
        } else {
            return NetUtil.toAddressString(inetAddress) + ':' + inetSocketAddress.getPort();
        }
    }
    return address.toString();
}
 
Example 2
Source File: ZooKeeperReplicationConfig.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
private static int findServerId(Map<Integer, ZooKeeperAddress> servers, int currentServerId,
                                InetAddress addr) {
    final String ip = NetUtil.toAddressString(addr, true);
    for (Entry<Integer, ZooKeeperAddress> entry : servers.entrySet()) {
        final String zkAddr;
        try {
            zkAddr = NetUtil.toAddressString(InetAddress.getByName(entry.getValue().host()), true);
        } catch (UnknownHostException uhe) {
            throw new IllegalStateException(
                    "failed to resolve the IP address of the server name: " + entry.getValue().host());
        }

        if (zkAddr.equals(ip)) {
            final int serverId = entry.getKey().intValue();
            if (currentServerId < 0) {
                currentServerId = serverId;
            } else if (currentServerId != serverId) {
                throw new IllegalStateException(
                        "cannot auto-detect server ID because there are more than one IP address match. " +
                        "Both server ID " + currentServerId + " and " + serverId +
                        " have a matching IP address. Consider specifying server ID explicitly.");
            }
        }
    }
    return currentServerId;
}
 
Example 3
Source File: CentralDogmaConfig.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ServerPort value,
                      JsonGenerator gen, SerializerProvider serializers) throws IOException {

    final InetSocketAddress localAddr = value.localAddress();
    final int port = localAddr.getPort();
    final String host;

    if (localAddr.getAddress().isAnyLocalAddress()) {
        host = "*";
    } else {
        final String hs = localAddr.getHostString();
        if (NetUtil.isValidIpV6Address(hs)) {
            // Try to get the platform-independent consistent IPv6 address string.
            host = NetUtil.toAddressString(localAddr.getAddress());
        } else {
            host = hs;
        }
    }

    gen.writeStartObject();
    gen.writeObjectFieldStart("localAddress");
    gen.writeStringField("host", host);
    gen.writeNumberField("port", port);
    gen.writeEndObject();
    gen.writeArrayFieldStart("protocols");
    for (final SessionProtocol protocol : value.protocols()) {
        gen.writeString(protocol.uriText());
    }
    gen.writeEndArray();
    gen.writeEndObject();
}