Java Code Examples for java.net.Inet4Address#getHostAddress()

The following examples show how to use java.net.Inet4Address#getHostAddress() . 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: WifiDisplayController.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private WifiDisplaySessionInfo getSessionInfo(WifiP2pGroup info, int session) {
    if (info == null) {
        return null;
    }
    Inet4Address addr = getInterfaceAddress(info);
    WifiDisplaySessionInfo sessionInfo = new WifiDisplaySessionInfo(
            !info.getOwner().deviceAddress.equals(mThisDevice.deviceAddress),
            session,
            info.getOwner().deviceAddress + " " + info.getNetworkName(),
            info.getPassphrase(),
            (addr != null) ? addr.getHostAddress() : "");
    if (DEBUG) {
        Slog.d(TAG, sessionInfo.toString());
    }
    return sessionInfo;
}
 
Example 2
Source File: EurekaUpdatingListenerTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
private static InstanceInfo expectedInstanceInfo(Server application) {
    final InstanceInfoBuilder builder = new InstanceInfoBuilder().appName(APP_NAME)
                                                                 .instanceId(INSTANCE_ID)
                                                                 .hostname(application.defaultHostname())
                                                                 .renewalIntervalSeconds(2);
    final Inet4Address inet4Address = SystemInfo.defaultNonLoopbackIpV4Address();
    final String hostnameOrIpAddr;
    if (inet4Address != null) {
        final String ipAddr = inet4Address.getHostAddress();
        builder.ipAddr(ipAddr);
        hostnameOrIpAddr = ipAddr;
    } else {
        hostnameOrIpAddr = null;
    }
    final int port = application.activePort(SessionProtocol.HTTP).localAddress().getPort();
    final int securePort = application.activePort(SessionProtocol.HTTPS).localAddress().getPort();
    builder.vipAddress(application.defaultHostname() + ':' + port)
           .secureVipAddress(application.defaultHostname() + ':' + securePort)
           .port(port)
           .securePort(securePort)
           .healthCheckUrl("http://" + hostnameOrIpAddr + ':' + port + "/health")
           .secureHealthCheckUrl("https://" + hostnameOrIpAddr + ':' + securePort + "/health")
           .dataCenterName(DEFAULT_DATA_CENTER_NAME);
    return builder.build();
}
 
Example 3
Source File: PhantomJSProcess.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
private URI listenURI(Inet4Address listenAddress, int listenPort)
{
	try
	{
		return new URI("http", null, listenAddress.getHostAddress(), listenPort, null, null, null);
	}
	catch (URISyntaxException e)
	{
		throw new JRRuntimeException(e);
	}
}
 
Example 4
Source File: ZooKeeperUpdatingListener.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static String defaultAddress(Server server) {
    final Inet4Address inet4Address = SystemInfo.defaultNonLoopbackIpV4Address();
    return inet4Address != null ? inet4Address.getHostAddress() : server.defaultHostname();
}
 
Example 5
Source File: EurekaUpdatingListener.java    From armeria with Apache License 2.0 4 votes vote down vote up
private InstanceInfo fillAndCreateNewInfo(InstanceInfo oldInfo, Server server) {
    final String defaultHostname = server.defaultHostname();
    final String hostName = oldInfo.getHostName() != null ? oldInfo.getHostName() : defaultHostname;
    appName = oldInfo.getAppName() != null ? oldInfo.getAppName() : hostName;
    final String instanceId = oldInfo.getInstanceId() != null ? oldInfo.getInstanceId() : hostName;

    final Inet4Address defaultInet4Address = SystemInfo.defaultNonLoopbackIpV4Address();
    final String defaultIpAddr = defaultInet4Address != null ? defaultInet4Address.getHostAddress()
                                                             : null;
    final String ipAddr = oldInfo.getIpAddr() != null ? oldInfo.getIpAddr() : defaultIpAddr;
    final PortWrapper oldPortWrapper = oldInfo.getPort();
    final PortWrapper portWrapper = portWrapper(server, oldPortWrapper, SessionProtocol.HTTP);
    final PortWrapper oldSecurePortWrapper = oldInfo.getSecurePort();
    final PortWrapper securePortWrapper = portWrapper(server, oldSecurePortWrapper, SessionProtocol.HTTPS);

    final String vipAddress = vipAddress(oldInfo.getVipAddress(), hostName, portWrapper);
    final String secureVipAddress = vipAddress(oldInfo.getSecureVipAddress(), hostName, securePortWrapper);

    final Optional<ServiceConfig> healthCheckService =
            server.serviceConfigs()
                  .stream()
                  .filter(cfg -> cfg.service().as(HealthCheckService.class) != null)
                  .findFirst();

    final String hostnameOrIpAddr;
    if (oldInfo.getHostName() != null) {
        hostnameOrIpAddr = oldInfo.getHostName();
    } else if (ipAddr != null) {
        hostnameOrIpAddr = ipAddr;
    } else {
        hostnameOrIpAddr = hostName;
    }
    final String healthCheckUrl = healthCheckUrl(hostnameOrIpAddr, oldInfo.getHealthCheckUrl(), portWrapper,
                                                 healthCheckService, SessionProtocol.HTTP);
    final String secureHealthCheckUrl =
            healthCheckUrl(hostnameOrIpAddr, oldInfo.getSecureHealthCheckUrl(), securePortWrapper,
                           healthCheckService, SessionProtocol.HTTPS);

    return new InstanceInfo(instanceId, appName, oldInfo.getAppGroupName(), hostName, ipAddr,
                            vipAddress, secureVipAddress, portWrapper, securePortWrapper, InstanceStatus.UP,
                            oldInfo.getHomePageUrl(), oldInfo.getStatusPageUrl(), healthCheckUrl,
                            secureHealthCheckUrl, oldInfo.getDataCenterInfo(),
                            oldInfo.getLeaseInfo(), oldInfo.getMetadata());
}
 
Example 6
Source File: TestInetAddressServlet.java    From appengine-java-vm-runtime with Apache License 2.0 4 votes vote down vote up
/**
 * Test properties of an instance of Inet4Address obtained via getLocalHost(). This method
 * is identical to testGetLocalHost(InetAddress, HttpServletResponse),  except that the
 * parameter is of type Inet4Address instead of InetAddress. This will test a different
 * code-path through our byte-rewriting.
 * @param localHost The instance of InetAddress being tested
 * @param response HttpServletResponse used to return failure message
 * @throws IOException If method being tested throws this.
 * @throws AssertionFailedException
 */
private static void testLocalHost4(Inet4Address localHost, HttpServletResponse response)
    throws IOException, AssertionFailedException {
  assertNotNull("localhost", localHost, response);
  assertTrue("instanceof Inet4Addr", localHost instanceof Inet4Address, response);

  //getAddress
  byte[] bytes = localHost.getAddress();
  assertNotNull("localhost address-bytes", bytes, response);
  assertEquals("localhost address bytes.length", 4, bytes.length, response);
  assertEquals("first byte of localhost address", 127, bytes[0], response);
  assertEquals("last byte of localhost address", 1, bytes[3], response);

  String name = localHost.getCanonicalHostName();
  assertEquals("getCanonicalHostName", "localhost", name, response);

  //getHostAddress should return the loopback address
  String address = localHost.getHostAddress();
  assertEquals("getHostAddress", "127.0.0.1", address, response);

  //getHostName
  name = localHost.getHostName();
  assertEquals("getHostName", "localhost", name, response);

  //Misc Properties
  assertFalse("isAnyLocalAddress", localHost.isAnyLocalAddress(), response);
  assertFalse("isLinkLocalAddress", localHost.isLinkLocalAddress(), response);
  assertTrue("isLoopbackAddress", localHost.isLoopbackAddress(), response);
  assertFalse("isLoopbackAddress", localHost.isMCGlobal(), response);
  assertFalse("isMCLinkLoca", localHost.isMCLinkLocal(), response);
  assertFalse("isMCNodeLocal", localHost.isMCNodeLocal(), response);
  assertFalse("isMCOrgLocal", localHost.isMCOrgLocal(), response);
  assertFalse("isMCSiteLoca", localHost.isMCSiteLocal(), response);
  assertFalse("isMulticastAddress", localHost.isMulticastAddress(), response);
  assertFalse("isSiteLocalAddress", localHost.isSiteLocalAddress(), response);

  //toString
  String s = localHost.toString();
  assertEquals("toString", "localhost/127.0.0.1", s, response);

  //isReachable
  assertFalse("isReachable", localHost.isReachable(1000), response);
  //Can't test version of isReachable() that takes a NetworkInterface
  //because it is not possible ot get a network interface without trigger ptrace sandbox
  //localHost.isReachable(netif,  ttl,  timeout);
}