/** * Get the keeping item from random network node. Only for permanet network mode. * * @param itemId is {@link HashId} of the looking item * * @return keeping item or null if it is missing */ public Approvable getKeepingItemFromNetwork(HashId itemId) { if (!config.isPermanetMode()) return null; try { NodeInfo source = Do.sample(network.allNodes()); Approvable item = network.getItem(itemId, source, config.getMaxGetItemTime()); if (item != null) return item; } catch (InterruptedException e) { e.printStackTrace(); } return null; }
/** * Create new client protocol session. It loads network configuration and creates client protocol session with random node. Allows delayed start of http client * @param someNodeUrl url on some node in network * @param clientPrivateKey client private key * @param session set to null or to the reconstructed instance * @param delayedStart indicates if start of http client should be delayed * @throws IOException */ public Client(String someNodeUrl, PrivateKey clientPrivateKey, BasicHttpClientSession session, boolean delayedStart) throws IOException { this.clientPrivateKey = clientPrivateKey; loadNetworkFrom(someNodeUrl); clients = new ArrayList<>(size()); for (int i = 0; i < size(); i++) { clients.add(null); } NodeRecord r = Do.sample(nodes); httpClient = new BasicHttpClient(r.url); this.nodePublicKey = r.key; if(!delayedStart) httpClient.start(clientPrivateKey, r.key, session); }
@Test public void sample() throws Exception { HashSet<String> x = new HashSet<>(Do.listOf("aa", "bb", "cc", "cd")); // RandomAccess-ed lists have different algorithms, so test them separately HashSet<String> y = new HashSet<>(); ArrayList<String> z = new ArrayList<>(Do.listOf("aa", "bb", "cc", "cd")); HashSet<String> t = new HashSet<>(); int repetitions = 10000; for (int i = 0; i < repetitions; i++) { y.add(Do.sample(x)); t.add(Do.sample(z)); } assertEquals(y,x); }
private void download() { if(processingState.canContinue()) { while (!isPollingExpired() && item == null) { if (sources.isEmpty()) { log.e("empty sources for download tasks, stopping"); return; } else { try { // first we have to wait for sources NodeInfo source; // Important: it could be disturbed by notifications synchronized (sources) { source = Do.sample(sources); } item = network.getItem(itemId, source, config.getMaxGetItemTime()); if (item != null) { itemDownloaded(); return; } else { Thread.sleep(100); } } catch (InterruptedException e) { e.printStackTrace(); } } } } }
source = Do.sample(sources);