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;
}
}