this.invalidateTime = invalidateTime;
double distance = Double.MAX_VALUE;
- for(int i=0;i<urlstrs.length;++i) {
+ for (int i=0;i<urlstrs.length;++i) {
String[] info = Split.split('/', urlstrs[i]);
- if(info.length<3) {
+ if (info.length<3) {
throw new LocatorException("Configuration needs LAT and LONG, i.e. ip:port/lat/long");
}
try {
clients[i] = _newClient(urlstrs[i]);
failures[i] = 0L;
- } catch(LocatorException le) {
+ } catch (LocatorException le) {
failures[i] = System.currentTimeMillis()+invalidateTime;
}
distances[i]=d;
// find preferred server
- if(d<distance) {
+ if (d<distance) {
preferred = i;
distance=d;
}
}
access.printf(Level.INIT,"Preferred Client is %s",urlstrs[preferred]);
- for(int i=0;i<urlstrs.length;++i) {
- if(i!=preferred) {
+ for (int i=0;i<urlstrs.length;++i) {
+ if (i!=preferred) {
access.printf(Level.INIT,"Alternate Client is %s",urlstrs[i]);
}
}
@Override
public Item best() throws LocatorException {
- if(failures[preferred]==0L) {
+ if (failures[preferred]==0L) {
return new HPItem(preferred);
} else {
long now = System.currentTimeMillis();
int best = -1;
boolean tickle = false;
// try for best existing client
- for(int i=0;i<urlstrs.length;++i) {
- if(failures[i]<now && distances[i]<d) {
- if(clients[i]!=null) {
+ for (int i=0;i<urlstrs.length;++i) {
+ if (failures[i]<now && distances[i]<d) {
+ if (clients[i]!=null) {
best = i;
break;
} else {
}
}
}
- if(best<0 && tickle) {
+ if (best<0 && tickle) {
tickle=false;
- if(refresh()) {
+ if (refresh()) {
// try again
- for(int i=0;i<urlstrs.length;++i) {
- if(failures[i]==0L && distances[i]<d) {
- if(clients[i]!=null) {
+ for (int i=0;i<urlstrs.length;++i) {
+ if (failures[i]==0L && distances[i]<d) {
+ if (clients[i]!=null) {
best = i;
break;
}
* If a valid client is available, but there are some that can refresh, return the client immediately
* but start a Thread to do the background Client setup.
*/
- if(tickle) {
+ if (tickle) {
synchronized(clients) {
- if(refreshThread==null) {
+ if (refreshThread==null) {
refreshThread = new Thread(new Runnable(){
@Override
public void run() {
}
}
- if(best<0) {
+ if (best<0) {
throw new LocatorException("No Clients available");
}
public CLIENT get(Item item) throws LocatorException {
HPItem hpi = (HPItem)item;
CLIENT c = clients[hpi.idx];
- if(c==null) {
- if(failures[hpi.idx]>System.currentTimeMillis()) {
+ if (c==null) {
+ if (failures[hpi.idx]>System.currentTimeMillis()) {
throw new LocatorException("Client requested is invalid");
} else {
synchronized(clients) {
failures[hpi.idx]=0L;
}
}
- } else if(failures[hpi.idx]>0){
+ } else if (failures[hpi.idx]>0){
throw new LocatorException("Client requested is invalid");
}
return c;
public String info(Item item) {
HPItem hpi = (HPItem)item;
- if(hpi!=null && hpi.idx<urlstrs.length) {
+ if (hpi!=null && hpi.idx<urlstrs.length) {
return urlstrs[hpi.idx];
} else {
return "Invalid Item";
@Override
public boolean hasItems() {
- for(int i=0;i<clients.length;++i) {
- if(clients[i]!=null && failures[i]==0L) {
+ for (int i=0;i<clients.length;++i) {
+ if (clients[i]!=null && failures[i]==0L) {
return true;
}
}
@Override
public Item next(Item item) throws LocatorException {
HPItem hpi = (HPItem)item;
- if(++hpi.idx>=clients.length) {
+ if (++hpi.idx>=clients.length) {
return null;
}
return hpi;
boolean force = !hasItems(); // If no Items at all, reset
boolean rv = true;
long now = System.currentTimeMillis();
- for(int i=0;i<clients.length;++i) {
- if(failures[i]>0L && (failures[i]<now || force)) { // retry
+ for (int i=0;i<clients.length;++i) {
+ if (failures[i]>0L && (failures[i]<now || force)) { // retry
try {
synchronized(clients) {
- if(clients[i]==null) {
+ if (clients[i]==null) {
clients[i]=_newClient(urlstrs[i]);
}
failures[i]=0L;
@Override
public void destroy() {
- for(int i=0;i<clients.length;++i) {
- if(clients[i]!=null) {
+ for (int i=0;i<clients.length;++i) {
+ if (clients[i]!=null) {
_destroy(clients[i]);
clients[i] = null;
}
}
public boolean invalidate(CLIENT client) throws LocatorException {
- for(int i=0;i<clients.length;++i) {
- if(clients[i]==client) { // yes, "==" is appropriate here.. Comparing Java Object Reference
+ for (int i=0;i<clients.length;++i) {
+ if (clients[i]==client) { // yes, "==" is appropriate here.. Comparing Java Object Reference
invalidate(new HPItem(i));
return true;
}