Skip to content

Commit

Permalink
Add MultiplexingDiscovery.forServices() that builds a suitable set of…
Browse files Browse the repository at this point in the history
… peer discoveries for

(optional) a set of desired services.
  • Loading branch information
schildbach committed Aug 25, 2015
1 parent 1be6548 commit 72763f2
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
package org.bitcoinj.net.discovery;

import com.google.common.collect.Lists;
import com.squareup.okhttp.OkHttpClient;

import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.VersionMessage;
import org.bitcoinj.net.discovery.HttpDiscovery;
import org.bitcoinj.net.discovery.DnsDiscovery.DnsSeedDiscovery;
import org.bitcoinj.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -43,6 +48,30 @@ public class MultiplexingDiscovery implements PeerDiscovery {
protected final NetworkParameters netParams;
private volatile ExecutorService vThreadPool;

/**
* Builds a suitable set of peer discoveries. Will query them in parallel before producing a merged response.
* If specific services are required, DNS is not used as the protocol can't handle it.
* @param params Network to use.
* @param services Required services as a bitmask, e.g. {@link VersionMessage#NODE_NETWORK}.
*/
public static MultiplexingDiscovery forServices(NetworkParameters params, long services) {
List<PeerDiscovery> discoveries = Lists.newArrayList();
HttpDiscovery.Details[] httpSeeds = params.getHttpSeeds();
if (httpSeeds != null) {
OkHttpClient httpClient = new OkHttpClient();
for (HttpDiscovery.Details httpSeed : httpSeeds)
discoveries.add(new HttpDiscovery(params, httpSeed, httpClient));
}
// Also use DNS seeds if there is no specific service requirement
if (services == 0) {
String[] dnsSeeds = params.getDnsSeeds();
if (dnsSeeds != null)
for (String dnsSeed : dnsSeeds)
discoveries.add(new DnsSeedDiscovery(params, dnsSeed));
}
return new MultiplexingDiscovery(params, discoveries);
}

/**
* Will query the given seeds in parallel before producing a merged response.
*/
Expand Down

0 comments on commit 72763f2

Please sign in to comment.