this.context = context;
}
@Override
protected void doExecute() throws InvalidProcessStateException, ProcessExecutionException {
DownloadTaskDirect task = context.getTask();
logger.debug("Getting the locations to download {} in a blocking manner.", task.getDestinationName());
List<Locations> locations = task.consumeLocationsBlocking();
logger.debug("Got the locations to download {}.", task.getDestinationName());
if (task.isAborted()) {
logger.warn("Not executing step because task is aborted");
return;
}
// prefer own user name
PeerAddress selectedOwnPeer = null;
for (Locations location : locations) {
if (location.getUserId().equals(task.getOwnUserName())) {
selectedOwnPeer = selectAddressOwnUser(location.getPeerAddresses());
break;
}
}
if (selectedOwnPeer != null) {
logger.debug("Found peer of own user to contact for the file {}", task.getDestinationName());
context.setSelectedPeer(selectedOwnPeer, task.getOwnUserName());
return;
}
// if own peer is not possible, take a foreign sharer
Random rnd = new Random();
while (!locations.isEmpty()) {
Locations randomLocation = locations.get(rnd.nextInt(locations.size()));
List<PeerAddress> addresses = new ArrayList<PeerAddress>(randomLocation.getPeerAddresses());
if (addresses.isEmpty()) {
// does not contain any addresses, kick it
locations.remove(randomLocation);
} else {
logger.debug("Found peer of foreign user to contact for the file {}", task.getDestinationName());
PeerAddress rndAddress = addresses.get(rnd.nextInt(addresses.size()));
context.setSelectedPeer(rndAddress, randomLocation.getUserId());
return;
}
}
logger.warn("No online peer found that could be contacted to get the file {}", task.getDestinationName());
throw new ProcessExecutionException("No online peer found that could be contacted");
}