package com.xjeffrose.xio.client.loadbalancer.strategies;

import com.google.common.collect.ImmutableList;
import com.google.common.hash.Funnels;
import com.xjeffrose.xio.client.RendezvousHash;
import com.xjeffrose.xio.client.loadbalancer.Filter;
import com.xjeffrose.xio.client.loadbalancer.Node;
import com.xjeffrose.xio.client.loadbalancer.Strategy;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

public class ConsistantHashLoadBalancer implements Strategy {

  public ConsistantHashLoadBalancer() {
  }

  @Override
  public boolean okToPick(Node node) {
    return true;
  }

  @Override
  public Node getNextNode(ImmutableList<Node> pool, Map<UUID, Node> okNodes) {

//    List<String> nodeList = new ArrayList<>();
//
//    pool.stream().filter(xs -> okNodes.containsValue(xs)).forEach(xs -> nodeList.add(((InetSocketAddress) xs.getAddress()).getHostString()));
//
//    RendezvousHash rendezvousHash = new RendezvousHash(Funnels.stringFunnel(Charset.defaultCharset()), nodeList, 1);
//    return rendezvousHash.get();

    // Need to pass in req val to hash against for consistent hashing
    return null;
  }
}

@@ -191,61 +191,8 @@
 sh;%0A
-import com.xjeffrose.xio.client.loadbalancer.Filter;%0A
 impo
@@ -297,43 +297,8 @@
 gy;%0A
-import java.net.InetSocketAddress;%0A
 impo
@@ -358,38 +358,8 @@
 st;%0A
-import java.util.Collections;%0A
 impo
@@ -420,87 +420,12 @@
 til.
-Random;%0Aimport java.util.UUID;%0Aimport java.util.concurrent.atomic.AtomicInteger
+UUID
 ;%0A%0Ap
@@ -695,19 +695,16 @@
 odes) %7B%0A
-%0A//
     List
@@ -712,24 +712,25 @@
 String%3E 
-nodeList
+idStrings
  = new A
@@ -747,143 +747,71 @@
 ();%0A
-//%0A//    pool.stream().filter(xs -%3E okNodes.containsValue(xs)).forEach(xs -%3E nodeList.add(((InetSocketAddress) xs.getAddress()).getHost
+    okNodes.keySet().stream().forEach(xs -%3E idStrings.add(xs.to
 Stri
@@ -822,13 +822,9 @@
 ));%0A
-//%0A//
+%0A
     
@@ -926,127 +926,450 @@
 )), 
-nodeList, 1);%0A//    return rendezvousHash.get();%0A%0A    // Need to pass in req val to hash against for consistent hashing
+idStrings, idStrings.size());%0A%0A    // TODO(JR): Will need to determine how to pass in session ID for consistant hashing%0A    for (Object nodeID : rendezvousHash.get(%22Constant%22.getBytes())) %7B%0A      if (nodeID instanceof String) %7B%0A        UUID id = UUID.fromString((String) nodeID);%0A        Node nextNode = okNodes.get(id);%0A        if (okToPick(nextNode)) %7B%0A          return nextNode;%0A        %7D%0A      %7D else %7B%0A        return null;%0A      %7D%0A    %7D%0A
 %0A   

add consistant hashing for session affinity