Example usage for java.net InetSocketAddress getAddress

List of usage examples for java.net InetSocketAddress getAddress

Introduction

In this page you can find the example usage for java.net InetSocketAddress getAddress.

Prototype

public final InetAddress getAddress() 

Source Link

Document

Gets the InetAddress .

Usage

From source file:org.apache.bookkeeper.client.TestRegionAwareEnsemblePlacementPolicy.java

@Test(timeout = 60000)
public void testReplaceBookieWithEnoughBookiesInDifferentRegion() throws Exception {
    InetSocketAddress addr1 = new InetSocketAddress("127.0.0.2", 3181);
    InetSocketAddress addr2 = new InetSocketAddress("127.0.0.3", 3181);
    InetSocketAddress addr3 = new InetSocketAddress("127.0.0.4", 3181);
    InetSocketAddress addr4 = new InetSocketAddress("127.0.0.5", 3181);
    // update dns mapping
    StaticDNSResolver.addNodeToRack(addr1.getAddress().getHostAddress(), NetworkTopology.DEFAULT_RACK);
    StaticDNSResolver.addNodeToRack(addr2.getAddress().getHostAddress(), "/region1/r2");
    StaticDNSResolver.addNodeToRack(addr3.getAddress().getHostAddress(), "/region2/r3");
    StaticDNSResolver.addNodeToRack(addr4.getAddress().getHostAddress(), "/region3/r4");
    // Update cluster
    Set<InetSocketAddress> addrs = new HashSet<InetSocketAddress>();
    addrs.add(addr1);/*w ww  . j  a  v  a  2  s  .co m*/
    addrs.add(addr2);
    addrs.add(addr3);
    addrs.add(addr4);
    repp.onClusterChanged(addrs, new HashSet<InetSocketAddress>());
    // replace node under r2
    Set<InetSocketAddress> excludedAddrs = new HashSet<InetSocketAddress>();
    excludedAddrs.add(addr1);
    InetSocketAddress replacedBookie = repp.replaceBookie(addr2, excludedAddrs);

    assertFalse(addr1.equals(replacedBookie));
    assertTrue(addr3.equals(replacedBookie) || addr4.equals(replacedBookie));
}

From source file:org.apache.bookkeeper.client.TestRegionAwareEnsemblePlacementPolicy.java

@Test(timeout = 60000)
public void testNewEnsembleWithSingleRegion() throws Exception {
    InetSocketAddress addr1 = new InetSocketAddress("127.0.0.2", 3181);
    InetSocketAddress addr2 = new InetSocketAddress("127.0.0.3", 3181);
    InetSocketAddress addr3 = new InetSocketAddress("127.0.0.4", 3181);
    InetSocketAddress addr4 = new InetSocketAddress("127.0.0.5", 3181);
    // update dns mapping
    StaticDNSResolver.addNodeToRack(addr1.getAddress().getHostAddress(), "/region1/r2");
    StaticDNSResolver.addNodeToRack(addr2.getAddress().getHostAddress(), "/region1/r2");
    StaticDNSResolver.addNodeToRack(addr3.getAddress().getHostAddress(), "/region1/r2");
    StaticDNSResolver.addNodeToRack(addr4.getAddress().getHostAddress(), "/region1/r2");
    // Update cluster
    Set<InetSocketAddress> addrs = new HashSet<InetSocketAddress>();
    addrs.add(addr1);/* ww w.j  a  v a 2 s .  c  om*/
    addrs.add(addr2);
    addrs.add(addr3);
    addrs.add(addr4);
    repp.onClusterChanged(addrs, new HashSet<InetSocketAddress>());
    try {
        ArrayList<InetSocketAddress> ensemble = repp.newEnsemble(3, 2, new HashSet<InetSocketAddress>());
        assertEquals(0, getNumCoveredRegionsInWriteQuorum(ensemble, 2));
        ArrayList<InetSocketAddress> ensemble2 = repp.newEnsemble(4, 2, new HashSet<InetSocketAddress>());
        assertEquals(0, getNumCoveredRegionsInWriteQuorum(ensemble2, 2));
    } catch (BKNotEnoughBookiesException bnebe) {
        fail("Should not get not enough bookies exception even there is only one rack.");
    }
}

From source file:org.apache.bookkeeper.client.TestRegionAwareEnsemblePlacementPolicy.java

@Test(timeout = 60000)
public void testReplaceBookieWithNotEnoughBookies() throws Exception {
    InetSocketAddress addr1 = new InetSocketAddress("127.0.0.2", 3181);
    InetSocketAddress addr2 = new InetSocketAddress("127.0.0.3", 3181);
    InetSocketAddress addr3 = new InetSocketAddress("127.0.0.4", 3181);
    InetSocketAddress addr4 = new InetSocketAddress("127.0.0.5", 3181);
    // update dns mapping
    StaticDNSResolver.addNodeToRack(addr1.getAddress().getHostAddress(), NetworkTopology.DEFAULT_RACK);
    StaticDNSResolver.addNodeToRack(addr2.getAddress().getHostAddress(), "/region2/r2");
    StaticDNSResolver.addNodeToRack(addr3.getAddress().getHostAddress(), "/region3/r3");
    StaticDNSResolver.addNodeToRack(addr4.getAddress().getHostAddress(), "/region4/r4");
    // Update cluster
    Set<InetSocketAddress> addrs = new HashSet<InetSocketAddress>();
    addrs.add(addr1);//  w ww. j a  v  a 2 s .c  o m
    addrs.add(addr2);
    addrs.add(addr3);
    addrs.add(addr4);
    repp.onClusterChanged(addrs, new HashSet<InetSocketAddress>());
    // replace node under r2
    Set<InetSocketAddress> excludedAddrs = new HashSet<InetSocketAddress>();
    excludedAddrs.add(addr1);
    excludedAddrs.add(addr3);
    excludedAddrs.add(addr4);
    try {
        repp.replaceBookie(addr2, excludedAddrs);
        fail("Should throw BKNotEnoughBookiesException when there is not enough bookies");
    } catch (BKNotEnoughBookiesException bnebe) {
        // should throw not enou
    }
}

From source file:org.apache.bookkeeper.client.TestRegionAwareEnsemblePlacementPolicy.java

@Test(timeout = 60000)
public void testNewEnsembleWithMultipleRegions() throws Exception {
    InetSocketAddress addr1 = new InetSocketAddress("127.0.0.2", 3181);
    InetSocketAddress addr2 = new InetSocketAddress("127.0.0.3", 3181);
    InetSocketAddress addr3 = new InetSocketAddress("127.0.0.4", 3181);
    InetSocketAddress addr4 = new InetSocketAddress("127.0.0.5", 3181);
    // update dns mapping
    StaticDNSResolver.addNodeToRack(addr1.getAddress().getHostAddress(), NetworkTopology.DEFAULT_RACK);
    StaticDNSResolver.addNodeToRack(addr2.getAddress().getHostAddress(), "/region1/r2");
    StaticDNSResolver.addNodeToRack(addr3.getAddress().getHostAddress(), "/region1/r2");
    StaticDNSResolver.addNodeToRack(addr4.getAddress().getHostAddress(), "/region1/r2");
    // Update cluster
    Set<InetSocketAddress> addrs = new HashSet<InetSocketAddress>();
    addrs.add(addr1);//from  www  .  j  a v a 2s  .  c o  m
    addrs.add(addr2);
    addrs.add(addr3);
    addrs.add(addr4);
    repp.onClusterChanged(addrs, new HashSet<InetSocketAddress>());
    try {
        ArrayList<InetSocketAddress> ensemble = repp.newEnsemble(3, 2, new HashSet<InetSocketAddress>());
        int numCovered = getNumCoveredRegionsInWriteQuorum(ensemble, 2);
        assertTrue(numCovered >= 1);
        assertTrue(numCovered < 3);
        ArrayList<InetSocketAddress> ensemble2 = repp.newEnsemble(4, 2, new HashSet<InetSocketAddress>());
        numCovered = getNumCoveredRegionsInWriteQuorum(ensemble2, 2);
        assertTrue(numCovered >= 1 && numCovered < 3);
    } catch (BKNotEnoughBookiesException bnebe) {
        fail("Should not get not enough bookies exception even there is only one rack.");
    }
}

From source file:edu.tsinghua.lumaqq.qq.net.Socks5Proxy.java

@Override
public void setRemoteAddress(InetSocketAddress serverAddress) {
    super.setRemoteAddress(serverAddress);
    // ???//w ww. j a  va  2s  .co  m
    if (serverAddress.isUnresolved()) {
        /* ??? */
        isIp = false;
        byte[] b = serverAddress.getHostName().getBytes();
        remoteAddress = new byte[b.length + 1];
        remoteAddress[0] = (byte) (b.length & 0xFF);
        System.arraycopy(b, 0, remoteAddress, 1, b.length);
    } else {
        /* ???IPSocks5??????ipip */
        isIp = true;
        remoteAddress = serverAddress.getAddress().getAddress();
    }
    remotePort = serverAddress.getPort();
}

From source file:com.all.dht.DhtManager.java

public Set<InetAddress> getOwners(String primaryKey) {
    Set<? extends DHTValueEntity> entities = get(createKUIDFromString(primaryKey));
    Set<InetAddress> owners = new HashSet<InetAddress>();
    for (DHTValueEntity valueEntity : entities) {
        InetSocketAddress contactAddress = (InetSocketAddress) valueEntity.getCreator().getContactAddress();
        owners.add(contactAddress.getAddress());
    }//from  w ww  .  java  2 s.  c o  m
    return owners;
}

From source file:org.apache.james.protocols.pop3.AbstractPOP3ServerTest.java

@Test
public void testNoop() throws Exception {
    InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort());

    ProtocolServer server = null;//from ww w  . j av a 2 s  .  c o m
    try {
        String identifier = "id";
        TestPassCmdHandler factory = new TestPassCmdHandler();

        factory.add("valid", new MockMailbox(identifier));
        server = createServer(createProtocol(factory), address);
        server.bind();

        POP3Client client = createClient();
        client.connect(address.getAddress().getHostAddress(), address.getPort());

        assertThat(client.login("valid", "valid")).isTrue();
        assertThat(client.noop()).isTrue();
        assertThat(client.logout()).isTrue();

    } finally {
        if (server != null) {
            server.unbind();
        }
    }

}

From source file:rpc.TestRPC.java

@Test
public void testServerAddress() throws IOException {
    Server server = new RPC.Builder(conf).setProtocol(TestProtocol.class).setInstance(new TestImpl())
            .setBindAddress(ADDRESS).setPort(0).setNumHandlers(5).setVerbose(true).build();
    InetSocketAddress bindAddr = null;
    try {//from ww w .jav a 2  s.  c  o  m
        bindAddr = NetUtils.getConnectAddress(server);
    } finally {
        server.stop();
    }
    assertEquals(InetAddress.getLocalHost(), bindAddr.getAddress());
}

From source file:org.apache.james.protocols.pop3.AbstractPOP3ServerTest.java

@Test
public void testEmptyInbox() throws Exception {
    InetSocketAddress address = new InetSocketAddress("127.0.0.1", TestUtils.getFreePort());

    ProtocolServer server = null;//from   w  w w .  j  a va 2  s.c o  m
    try {
        String identifier = "id";
        TestPassCmdHandler handler = new TestPassCmdHandler();

        handler.add("valid", new MockMailbox(identifier));
        server = createServer(createProtocol(handler), address);
        server.bind();

        POP3Client client = createClient();
        client.connect(address.getAddress().getHostAddress(), address.getPort());

        assertThat(client.login("valid", "valid")).isTrue();
        POP3MessageInfo[] info = client.listMessages();
        assertThat(info.length).isEqualTo(0);

        info = client.listUniqueIdentifiers();
        assertThat(info.length).isEqualTo(0);
        assertThat(client.logout()).isTrue();

    } finally {
        if (server != null) {
            server.unbind();
        }
    }

}

From source file:com.matthewmitchell.peercoin_android_wallet.service.BlockchainServiceImpl.java

@SuppressLint("Wakelock")
private void check() {
    final Wallet wallet = application.getWallet();

    if (impediments.isEmpty() && bcd != null && peerGroup == null) {
        log.debug("acquiring wakelock");
        wakeLock.acquire();/*  ww  w.  j a  v  a  2s .  c  om*/

        // consistency check
        final int walletLastBlockSeenHeight = wallet.getLastBlockSeenHeight();
        final int bestChainHeight = bcd.blockChain.getBestChainHeight();
        if (walletLastBlockSeenHeight != -1 && walletLastBlockSeenHeight != bestChainHeight) {
            final String message = "wallet/blockchain out of sync: " + walletLastBlockSeenHeight + "/"
                    + bestChainHeight;
            log.error(message);
            CrashReporter.saveBackgroundTrace(new RuntimeException(message), application.packageInfo());
        }

        log.info("starting peergroup");
        peerGroup = new PeerGroup(Constants.NETWORK_PARAMETERS, bcd.blockChain);
        peerGroup.setDownloadTxDependencies(false); // recursive implementation causes StackOverflowError
        peerGroup.addWallet(wallet);
        peerGroup.setUserAgent(Constants.USER_AGENT, application.packageInfo().versionName);
        peerGroup.addEventListener(peerConnectivityListener);

        final int maxConnectedPeers = application.maxConnectedPeers();

        final String trustedPeerHost = config.getTrustedPeerHost();
        final boolean hasTrustedPeer = !trustedPeerHost.isEmpty();

        final boolean connectTrustedPeerOnly = hasTrustedPeer && config.getTrustedPeerOnly();
        peerGroup.setMaxConnections(connectTrustedPeerOnly ? 1 : maxConnectedPeers);
        peerGroup.setConnectTimeoutMillis(Constants.PEER_TIMEOUT_MS);

        if (!connectTrustedPeerOnly) {
            loadPDB = new PeerDBDiscoveryLoader(this, peerGroup);
            loadPDB.registerListener(0, new OnLoadCompleteListener<PeerDBDiscovery>() {

                @Override
                public void onLoadComplete(Loader<PeerDBDiscovery> loader, PeerDBDiscovery dbDiscovery) {
                    peerGroup.addPeerDiscovery(dbDiscovery);
                    dbDiscovery.listenForPeers(peerGroup);
                }

            });
            loadPDB.startLoading();

            // Use backup nodes when needed
            DnsDiscovery backup = new DnsDiscovery(backupDNS, Constants.NETWORK_PARAMETERS);
            peerGroup.addPeerDiscovery(backup, true);
        }

        peerGroup.addPeerDiscovery(new PeerDiscovery() {
            private final PeerDiscovery normalPeerDiscovery = new DnsDiscovery(Constants.NETWORK_PARAMETERS);

            @Override
            public InetSocketAddress[] getPeers(final long timeoutValue, final TimeUnit timeoutUnit)
                    throws PeerDiscoveryException {
                final List<InetSocketAddress> peers = new LinkedList<InetSocketAddress>();

                boolean needsTrimPeersWorkaround = false;

                if (hasTrustedPeer) {
                    log.info(
                            "trusted peer '" + trustedPeerHost + "'" + (connectTrustedPeerOnly ? " only" : ""));

                    final InetSocketAddress addr = new InetSocketAddress(trustedPeerHost,
                            Constants.NETWORK_PARAMETERS.getPort());
                    if (addr.getAddress() != null) {
                        peers.add(addr);
                        needsTrimPeersWorkaround = true;
                    }
                }

                if (!connectTrustedPeerOnly)
                    peers.addAll(Arrays.asList(normalPeerDiscovery.getPeers(timeoutValue, timeoutUnit)));

                // workaround because PeerGroup will shuffle peers
                if (needsTrimPeersWorkaround)
                    while (peers.size() >= maxConnectedPeers)
                        peers.remove(peers.size() - 1);

                return peers.toArray(new InetSocketAddress[0]);
            }

            @Override
            public void shutdown() {
                normalPeerDiscovery.shutdown();
            }
        });

        // start peergroup
        peerGroup.startAsync();
        peerGroup.startBlockChainDownload(blockchainDownloadListener);
    } else if (!impediments.isEmpty() && peerGroup != null) {
        log.info("stopping peergroup");
        peerGroup.removeEventListener(peerConnectivityListener);
        peerGroup.removeWallet(wallet);
        peerGroup.stopAsync();
        peerGroup = null;

        log.debug("releasing wakelock");
        wakeLock.release();
    }

    broadcastBlockchainState();
}