Example usage for com.google.common.util.concurrent SettableFuture isDone

List of usage examples for com.google.common.util.concurrent SettableFuture isDone

Introduction

In this page you can find the example usage for com.google.common.util.concurrent SettableFuture isDone.

Prototype

@Override
    public boolean isDone() 

Source Link

Usage

From source file:com.google.bitcoin.examples.PrintPeers.java

public static void main(String[] args) throws Exception {
    BriefLogFormatter.init();/*www .j  a  va 2s  .  c om*/
    System.out.println("=== DNS ===");
    printDNS();
    System.out.println("=== Version/chain heights ===");

    ArrayList<InetAddress> addrs = new ArrayList<InetAddress>();
    for (InetSocketAddress peer : dnsPeers)
        addrs.add(peer.getAddress());
    System.out.println("Scanning " + addrs.size() + " peers:");

    final NetworkParameters params = MainNetParams.get();
    final Object lock = new Object();
    final long[] bestHeight = new long[1];

    List<ListenableFuture<Void>> futures = Lists.newArrayList();
    NioClientManager clientManager = new NioClientManager();
    for (final InetAddress addr : addrs) {
        InetSocketAddress address = new InetSocketAddress(addr, params.getPort());
        final Peer peer = new Peer(params, new VersionMessage(params, 0), null, new PeerAddress(address));
        final SettableFuture future = SettableFuture.create();
        // Once the connection has completed version handshaking ...
        peer.addEventListener(new AbstractPeerEventListener() {
            public void onPeerConnected(Peer p, int peerCount) {
                // Check the chain height it claims to have.
                VersionMessage ver = peer.getPeerVersionMessage();
                long nodeHeight = ver.bestHeight;
                synchronized (lock) {
                    long diff = bestHeight[0] - nodeHeight;
                    if (diff > 0) {
                        System.out.println("Node is behind by " + diff + " blocks: " + addr);
                    } else if (diff == 0) {
                        System.out.println("Node " + addr + " has " + nodeHeight + " blocks");
                        bestHeight[0] = nodeHeight;
                    } else if (diff < 0) {
                        System.out.println("Node is ahead by " + Math.abs(diff) + " blocks: " + addr);
                        bestHeight[0] = nodeHeight;
                    }
                }
                // Now finish the future and close the connection
                future.set(null);
                peer.close();
            }

            public void onPeerDisconnected(Peer p, int peerCount) {
                if (!future.isDone())
                    System.out.println("Failed to talk to " + addr);
                future.set(null);
            }
        });
        clientManager.openConnection(address, peer);
        futures.add(future);
    }
    // Wait for every tried connection to finish.
    Futures.successfulAsList(futures).get();
}

From source file:com.google.worldcoin.examples.PrintPeers.java

public static void main(String[] args) throws Exception {
    BriefLogFormatter.init();//from  w w w  .j a v  a2 s  .  co m
    System.out.println("=== DNS ===");
    printDNS();
    System.out.println("=== Version/chain heights ===");

    ArrayList<InetAddress> addrs = new ArrayList<InetAddress>();
    for (InetSocketAddress peer : dnsPeers)
        addrs.add(peer.getAddress());
    //addrs.add(convertAddress());

    System.out.println("Scanning " + addrs.size() + " peers:");

    final NetworkParameters params = MainNetParams.get();
    final Object lock = new Object();
    final long[] bestHeight = new long[1];

    List<ListenableFuture<Void>> futures = Lists.newArrayList();
    NioClientManager clientManager = new NioClientManager();
    for (final InetAddress addr : addrs) {
        InetSocketAddress address = new InetSocketAddress(addr, params.getPort());
        final Peer peer = new Peer(params, new VersionMessage(params, 0), null, new PeerAddress(address));
        final SettableFuture future = SettableFuture.create();
        // Once the connection has completed version handshaking ...
        peer.addEventListener(new AbstractPeerEventListener() {
            public void onPeerConnected(Peer p, int peerCount) {
                // Check the chain height it claims to have.
                VersionMessage ver = peer.getPeerVersionMessage();
                long nodeHeight = ver.bestHeight;
                synchronized (lock) {
                    long diff = bestHeight[0] - nodeHeight;
                    if (diff > 0) {
                        System.out.println("Node is behind by " + diff + " blocks: " + addr);
                    } else if (diff == 0) {
                        System.out.println("Node " + addr + " has " + nodeHeight + " blocks");
                        bestHeight[0] = nodeHeight;
                    } else if (diff < 0) {
                        System.out.println("Node is ahead by " + Math.abs(diff) + " blocks: " + addr);
                        bestHeight[0] = nodeHeight;
                    }
                }
                // Now finish the future and close the connection
                future.set(null);
                peer.close();
            }

            public void onPeerDisconnected(Peer p, int peerCount) {
                if (!future.isDone())
                    System.out.println("Failed to talk to " + addr);
                future.set(null);
            }
        });
        clientManager.start();
        clientManager.openConnection(address, peer);
        futures.add(future);
    }
    // Wait for every tried connection to finish.
    Futures.successfulAsList(futures).get();
}

From source file:com.dogecoin.dogecoinj.examples.PrintPeers.java

public static void main(String[] args) throws Exception {
    BriefLogFormatter.init();/* ww  w.j  a  v a2 s . co  m*/
    System.out.println("=== DNS ===");
    printDNS();
    System.out.println("=== Version/chain heights ===");

    ArrayList<InetAddress> addrs = new ArrayList<InetAddress>();
    for (InetSocketAddress peer : dnsPeers)
        addrs.add(peer.getAddress());
    System.out.println("Scanning " + addrs.size() + " peers:");

    final NetworkParameters params = MainNetParams.get();
    final Object lock = new Object();
    final long[] bestHeight = new long[1];

    List<ListenableFuture<Void>> futures = Lists.newArrayList();
    NioClientManager clientManager = new NioClientManager();
    for (final InetAddress addr : addrs) {
        InetSocketAddress address = new InetSocketAddress(addr, params.getPort());
        final Peer peer = new Peer(params, new VersionMessage(params, 0), null, new PeerAddress(address));
        final SettableFuture<Void> future = SettableFuture.create();
        // Once the connection has completed version handshaking ...
        peer.addEventListener(new AbstractPeerEventListener() {
            @Override
            public void onPeerConnected(Peer p, int peerCount) {
                // Check the chain height it claims to have.
                VersionMessage ver = peer.getPeerVersionMessage();
                long nodeHeight = ver.bestHeight;
                synchronized (lock) {
                    long diff = bestHeight[0] - nodeHeight;
                    if (diff > 0) {
                        System.out.println("Node is behind by " + diff + " blocks: " + addr);
                    } else if (diff == 0) {
                        System.out.println("Node " + addr + " has " + nodeHeight + " blocks");
                        bestHeight[0] = nodeHeight;
                    } else if (diff < 0) {
                        System.out.println("Node is ahead by " + Math.abs(diff) + " blocks: " + addr);
                        bestHeight[0] = nodeHeight;
                    }
                }
                // Now finish the future and close the connection
                future.set(null);
                peer.close();
            }

            @Override
            public void onPeerDisconnected(Peer p, int peerCount) {
                if (!future.isDone())
                    System.out.println("Failed to talk to " + addr);
                future.set(null);
            }
        });
        clientManager.openConnection(address, peer);
        futures.add(future);
    }
    // Wait for every tried connection to finish.
    Futures.successfulAsList(futures).get();
}

From source file:org.guldenj.examples.PrintPeers.java

public static void main(String[] args) throws Exception {
    BriefLogFormatter.init();//w w w .j  a  va 2s .  co  m
    System.out.println("=== DNS ===");
    printDNS();
    System.out.println("=== Version/chain heights ===");

    ArrayList<InetAddress> addrs = new ArrayList<InetAddress>();
    for (InetSocketAddress peer : dnsPeers)
        addrs.add(peer.getAddress());
    System.out.println("Scanning " + addrs.size() + " peers:");

    final NetworkParameters params = MainNetParams.get();
    final Object lock = new Object();
    final long[] bestHeight = new long[1];

    List<ListenableFuture<Void>> futures = Lists.newArrayList();
    NioClientManager clientManager = new NioClientManager();
    for (final InetAddress addr : addrs) {
        InetSocketAddress address = new InetSocketAddress(addr, params.getPort());
        final Peer peer = new Peer(params, new VersionMessage(params, 0), null, new PeerAddress(address));
        final SettableFuture<Void> future = SettableFuture.create();
        // Once the connection has completed version handshaking ...
        peer.addConnectedEventListener(new PeerConnectedEventListener() {
            @Override
            public void onPeerConnected(Peer p, int peerCount) {
                // Check the chain height it claims to have.
                VersionMessage ver = peer.getPeerVersionMessage();
                long nodeHeight = ver.bestHeight;
                synchronized (lock) {
                    long diff = bestHeight[0] - nodeHeight;
                    if (diff > 0) {
                        System.out.println("Node is behind by " + diff + " blocks: " + addr);
                    } else if (diff == 0) {
                        System.out.println("Node " + addr + " has " + nodeHeight + " blocks");
                        bestHeight[0] = nodeHeight;
                    } else if (diff < 0) {
                        System.out.println("Node is ahead by " + Math.abs(diff) + " blocks: " + addr);
                        bestHeight[0] = nodeHeight;
                    }
                }
                // Now finish the future and close the connection
                future.set(null);
                peer.close();
            }
        });
        peer.addDisconnectedEventListener(new PeerDisconnectedEventListener() {
            @Override
            public void onPeerDisconnected(Peer p, int peerCount) {
                if (!future.isDone())
                    System.out.println("Failed to talk to " + addr);
                future.set(null);
            }
        });
        clientManager.openConnection(address, peer);
        futures.add(future);
    }
    // Wait for every tried connection to finish.
    Futures.successfulAsList(futures).get();
}

From source file:org.bitcoinj.examples.PrintPeers.java

public static void main(String[] args) throws Exception {
    BriefLogFormatter.init();//  w  w  w  .jav  a 2  s  .  com
    System.out.println("=== DNS ===");
    printDNS();
    System.out.println("=== Version/chain heights ===");

    ArrayList<InetAddress> addrs = new ArrayList<InetAddress>();
    for (InetSocketAddress peer : dnsPeers)
        addrs.add(peer.getAddress());
    System.out.println("Scanning " + addrs.size() + " peers:");

    final NetworkParameters params = MainNetParams.get();
    final Object lock = new Object();
    final long[] bestHeight = new long[1];

    List<ListenableFuture<Void>> futures = Lists.newArrayList();
    NioClientManager clientManager = new NioClientManager();
    for (final InetAddress addr : addrs) {
        InetSocketAddress address = new InetSocketAddress(addr, params.getPort());
        final Peer peer = new Peer(params, new VersionMessage(params, 0), null,
                new PeerAddress(params, address));
        final SettableFuture<Void> future = SettableFuture.create();
        // Once the connection has completed version handshaking ...
        peer.addConnectedEventListener(new PeerConnectedEventListener() {
            @Override
            public void onPeerConnected(Peer p, int peerCount) {
                // Check the chain height it claims to have.
                VersionMessage ver = peer.getPeerVersionMessage();
                long nodeHeight = ver.bestHeight;
                synchronized (lock) {
                    long diff = bestHeight[0] - nodeHeight;
                    if (diff > 0) {
                        System.out.println("Node is behind by " + diff + " blocks: " + addr);
                    } else if (diff == 0) {
                        System.out.println("Node " + addr + " has " + nodeHeight + " blocks");
                        bestHeight[0] = nodeHeight;
                    } else if (diff < 0) {
                        System.out.println("Node is ahead by " + Math.abs(diff) + " blocks: " + addr);
                        bestHeight[0] = nodeHeight;
                    }
                }
                // Now finish the future and close the connection
                future.set(null);
                peer.close();
            }
        });
        peer.addDisconnectedEventListener(new PeerDisconnectedEventListener() {
            @Override
            public void onPeerDisconnected(Peer p, int peerCount) {
                if (!future.isDone())
                    System.out.println("Failed to talk to " + addr);
                future.set(null);
            }
        });
        clientManager.openConnection(address, peer);
        futures.add(future);
    }
    // Wait for every tried connection to finish.
    Futures.successfulAsList(futures).get();
}

From source file:com.continuuity.weave.zookeeper.ZKOperations.java

/**
 * Watch for the given path until it exists.
 * @param zkClient The {@link ZKClient} to use.
 * @param path A ZooKeeper path to watch for existent.
 *///from  w  w  w. ja  v  a2s  .  c  om
private static void watchExists(final ZKClient zkClient, final String path,
        final SettableFuture<String> completion) {
    Futures.addCallback(zkClient.exists(path, new Watcher() {
        @Override
        public void process(WatchedEvent event) {
            if (!completion.isDone()) {
                watchExists(zkClient, path, completion);
            }
        }
    }), new FutureCallback<Stat>() {
        @Override
        public void onSuccess(Stat result) {
            if (result != null) {
                completion.set(path);
            }
        }

        @Override
        public void onFailure(Throwable t) {
            completion.setException(t);
        }
    });
}

From source file:com.continuuity.weave.zookeeper.ZKOperations.java

public static void watchDeleted(final ZKClient zkClient, final String path,
        final SettableFuture<String> completion) {

    Futures.addCallback(zkClient.exists(path, new Watcher() {
        @Override//from  ww  w . j a  v a  2  s .co m
        public void process(WatchedEvent event) {
            if (!completion.isDone()) {
                if (event.getType() == Event.EventType.NodeDeleted) {
                    completion.set(path);
                } else {
                    watchDeleted(zkClient, path, completion);
                }
            }
        }
    }), new FutureCallback<Stat>() {
        @Override
        public void onSuccess(Stat result) {
            if (result == null) {
                completion.set(path);
            }
        }

        @Override
        public void onFailure(Throwable t) {
            completion.setException(t);
        }
    });
}

From source file:io.prestosql.operator.OperatorContext.java

private static void updateMemoryFuture(ListenableFuture<?> memoryPoolFuture,
        AtomicReference<SettableFuture<?>> targetFutureReference) {
    if (!memoryPoolFuture.isDone()) {
        SettableFuture<?> currentMemoryFuture = targetFutureReference.get();
        while (currentMemoryFuture.isDone()) {
            SettableFuture<?> settableFuture = SettableFuture.create();
            // We can't replace one that's not done, because the task may be blocked on that future
            if (targetFutureReference.compareAndSet(currentMemoryFuture, settableFuture)) {
                currentMemoryFuture = settableFuture;
            } else {
                currentMemoryFuture = targetFutureReference.get();
            }/*from w w  w .j av a 2  s  .co m*/
        }

        SettableFuture<?> finalMemoryFuture = currentMemoryFuture;
        // Create a new future, so that this operator can un-block before the pool does, if it's moved to a new pool
        memoryPoolFuture.addListener(() -> finalMemoryFuture.set(null), directExecutor());
    }
}

From source file:com.google.devtools.kythe.extractors.shared.ExtractorUtils.java

public static List<FileData> processRequiredInputs(Iterable<String> files) throws ExtractionException {
    final SettableFuture<ExtractionException> exception = SettableFuture.create();

    List<FileData> result = Lists.newArrayList(Iterables.transform(files, new Function<String, FileData>() {
        @Override/*from  www.  ja v a 2  s  .c  o  m*/
        public FileData apply(String path) {
            byte[] content = new byte[0];
            try {
                content = Files.toByteArray(new File(path));
            } catch (IOException e) {
                exception.set(new ExtractionException(e, false));
            }
            if (content == null) {
                exception.set(new ExtractionException(String.format("Unable to locate required input %s", path),
                        false));
                return null;
            }
            String digest = getContentDigest(content);
            return createFileData(path, digest, content);
        }
    }));
    if (exception.isDone()) {
        try {
            throw exception.get();
        } catch (InterruptedException e) {
            throw new ExtractionException(e, true);
        } catch (ExecutionException e) {
            throw new ExtractionException(e, false);
        }
    }
    return result;
}

From source file:io.crate.jobs.ESJobContext.java

@Override
protected void innerClose(@Nullable Throwable t) {
    if (t != null) {
        for (SettableFuture<Long> resultFuture : resultFutures) {
            if (!resultFuture.isDone()) {
                resultFuture.setException(t);
            }/*w  w  w .j a  v  a2 s  . com*/
        }
    }
}