import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * 初始化化一致环
 * **/
public class UniformLoop {
	
	public TreeMap<Long,Object> nodes = null;
	
	public void init(SeverList serverlist)
	{

       // shards.add("192.168.0.0-服务器0");

	
	  
      nodes = new TreeMap<Long,Object>();
       
       while(serverlist.getNext() !=null )
       {
           Object serverlist_name = serverlist.get_host_name();
           
           System.out.println(serverlist_name);
           
           for(int j=0; j<VIRTUAL_NUM; j++) {
           	//node产生的节点数为服务器*虚拟节点数，key=shard-i-node-j和j共同产生的hash值，value=实质那台服务器
               nodes.put(hash(computeMd5("SHARD-" + i + "-NODE-" + j),j), shardInfo);
               System.out.println("SHARD-" + i + "-NODE-" + j);
               System.out.println(computeMd5("SHARD-" + i + "-NODE-" + j)+":"+hash(computeMd5("SHARD-" + i + "-NODE-" + j),j));
               
           }
       }
		
	}
	
	
	
	/**
	 * Get the md5 of the given key.
     * 计算MD5值
	 */
	 public byte[] computeMd5(String k) {
		MessageDigest md5;
		try {
			md5 = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException("MD5 not supported", e);
		}
		md5.reset();
		byte[] keyBytes = null;
		try {
			keyBytes = k.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException("Unknown string :" + k, e);
		}

		md5.update(keyBytes);
		return md5.digest();
	 }


}
