package com.annotations.openface.sdk.net.util;

import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;
/**
 * 代理服务器，每个Proxy一个对象
 * @author elba
 * Server的连接IP为 int
 * 
 * 连接id<<32 + 代理ip  组合成sessionId
 */
public class LinkProxy {
	//代理服务器的IP
	private int ipAddress;
	//代理的连接id, 这个是Server的id,一个代理服务器可能有多个连接
	private int linkId;
	//保存SessionId
	private Set<Long> sessions;
	
	public LinkProxy(int ipAddress,int linkId){
		this.ipAddress = ipAddress;
		this.linkId = linkId;
		sessions  = new ConcurrentSkipListSet<Long>();
	}
	//检查clientId是否已经对应了sessionId
	public long addClient(int clientId){
		long sessionId = clientId;
		sessionId <<= 32;
		sessionId |= (0xFFFFFFFFL & ipAddress);
		sessions.add(sessionId);
		return sessionId;
	}
	
	public Iterator<Long> getAllSessions(){
		return sessions.iterator();
	}
	
	public int getLinkId(){
		return linkId;
	}
	
	public void removeSession(long sessionId){
		sessions.remove(sessionId);
	}
	
	public int getIpAddress(){
		return ipAddress;
	}
}
