/** * Finds the closest non-excluded peer. * @param exclude the set of locations to exclude, may be null * @return the closest non-excluded peer's location, or NaN if none is found */ public double getClosestPeerLocation(double l, Set<Double> exclude) { return location.getClosestPeerLocation(l, exclude); }
/** Should only be called in the constructor */ public void setPeerLocations(String[] peerLocationsString) { if(peerLocationsString != null) { double[] peerLocations = new double[peerLocationsString.length]; for(int i = 0; i < peerLocationsString.length; i++) peerLocations[i] = Location.getLocation(peerLocationsString[i]); updateLocation(currentLocation, peerLocations); } }
/** * Determines the degree of the peer via the locations of its peers it provides. * @return The number of peers this peer reports having, or 0 if this peer does not provide that information. */ public int getDegree() { return location.getDegree(); }
public void testGetClosestPeerLocation() { for (double[] peers : PEER_LOCATIONS) { PeerLocation pl = new PeerLocation("0.0"); assertTrue(pl.updateLocation(0.0, peers)); for (double target : TARGET_LOCATIONS) { for (Set<Double> exclude : omit(peers)) { double closest = pl.getClosestPeerLocation(target, exclude); assertFalse(exclude.contains(closest)); double ref = trivialFindClosestDistance(peers, target, exclude); if (!Double.isInfinite(ref)) { assertFalse(Double.isNaN(closest)); boolean isPeer = false; for (double peer : peers) { if (closest == peer) { isPeer = true; break; } } assertTrue(isPeer); assertEquals(ref, Location.distance(closest, target), EPSILON); } else { assertTrue(Double.isNaN(closest)); } } } } }
location = new PeerLocation(locationString); location.setPeerLocations(fs.getAll("peersLocation"));
return Double.NaN; final int closest = findClosestLocation(locs, l); if (exclude == null || !exclude.contains(locs[closest])) { return locs[closest];
if(pn.isRoutable()) { synchronized(l) { double ploc = l.getLocation(); if(Location.equals(ploc, myLoc)) { if(now - l.getLocationSetTime() > MINUTES.toMillis(2) && now - timeLocSet > MINUTES.toMillis(2)) { myFlag = true;
/** Returns an array copy of locations of this PeerNode's peers, or null if unknown. */ double[] getPeersLocationArray() { return location.getPeersLocationArray(); }
/** * Returns this peer's current keyspace location, or -1 if it is unknown. */ public double getLocation() { return location.getLocation(); }
public long getLocSetTime() { return location.getLocationSetTime(); }
/** * Returns true if requests can be routed through this peer. True if the peer's location is known, presently * connected, and routing-compatible. That is, ignoring backoff, the peer's location is known, build number * is compatible, and routing has not been explicitly disabled. * * Note possible deadlocks! PeerManager calls this, we call * PeerManager in e.g. verified. */ @Override public boolean isRoutable() { if((!isConnected()) || (!isRoutingCompatible())) return false; return location.isValidLocation(); }
/** * Finds the position of the closest location in the sorted list of locations. * This is a binary search of logarithmic complexity. */ static int findClosestLocation(final double[] locs, final double l) { assert(locs.length > 0); if (locs.length == 1) { return 0; } final int firstGreater = findFirstGreater(locs, l); final int left; final int right; if (firstGreater == -1 || firstGreater == 0) { // All locations are greater, or all locations are smaller. // Closest location must be either smallest or greatest location. left = locs.length - 1; right = 0; } else { left = firstGreater - 1; right = firstGreater; } if (Location.distance(l, locs[left]) <= Location.distance(l, locs[right])) { return left; } return right; }
public void testFindClosestLocation() { for (double[] peers : PEER_LOCATIONS) { for (double target : TARGET_LOCATIONS) { int closest = PeerLocation.findClosestLocation(peers, target); double ref = trivialFindClosestDistance(peers, target); assertEquals(ref, Location.distance(peers[closest], target), EPSILON); } } }
public void updateLocation(double newLoc, double[] newLocs) { boolean anythingChanged = location.updateLocation(newLoc, newLocs); node.peers.updatePMUserAlert(); if(anythingChanged) writePeers(); setPeerNodeStatus(System.currentTimeMillis()); }