// Path: redant-core/src/main/java/com/redant/core/common/exception/InvalidSessionException.java
// public class InvalidSessionException extends RuntimeException{
// 	private static final long serialVersionUID = 1L;
// 
// 	public InvalidSessionException(String s) {
//         super(s);
//     }
// 
// }


import com.redant.core.common.exception.InvalidSessionException;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelId;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
package com.redant.core.session;



/**
 * Session辅助器
 * @author houyi.wh
 * @date 2017/11/6
 */
public class SessionHelper {

    /**
     * 保存session对象的map
     */
    private Map<ChannelId,HttpSession> sessionMap;

    private static SessionHelper manager;

    private SessionHelper(){

    }


    //======================================


    /**
     * 获取单例
     * @return
     */
    public static SessionHelper instange(){
        synchronized (SessionHelper.class) {
            if (manager == null) {
                manager = new SessionHelper();
                if (manager.sessionMap == null) {
                    // 需要线程安全的Map
                    manager.sessionMap = new ConcurrentHashMap<ChannelId,HttpSession>();
                }
            }
        }
        return manager;
    }

    /**
     * 判断session是否存在
     * @param context
     * @return
     */
    public boolean containsSession(ChannelHandlerContext context){
        return context!=null && context.channel()!=null && context.channel().id()!=null && manager.sessionMap.get(context.channel().id())!=null;
    }

    /**
     * 添加一个session
     * @param context
     * @param session
     */
    public void addSession(ChannelHandlerContext context,HttpSession session){
        if(context==null || context.channel()==null || context.channel().id()==null || session==null){
// Path: redant-core/src/main/java/com/redant/core/common/exception/InvalidSessionException.java
// public class InvalidSessionException extends RuntimeException{
// 	private static final long serialVersionUID = 1L;
// 
// 	public InvalidSessionException(String s) {
//         super(s);
//     }
// 
// }


// Path: redant-core/src/main/java/com/redant/core/session/SessionHelper.java
import com.redant.core.common.exception.InvalidSessionException;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelId;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

package com.redant.core.session;



/**
 * Session辅助器
 * @author houyi.wh
 * @date 2017/11/6
 */
public class SessionHelper {

    /**
     * 保存session对象的map
     */
    private Map<ChannelId,HttpSession> sessionMap;

    private static SessionHelper manager;

    private SessionHelper(){

    }


    //======================================


    /**
     * 获取单例
     * @return
     */
    public static SessionHelper instange(){
        synchronized (SessionHelper.class) {
            if (manager == null) {
                manager = new SessionHelper();
                if (manager.sessionMap == null) {
                    // 需要线程安全的Map
                    manager.sessionMap = new ConcurrentHashMap<ChannelId,HttpSession>();
                }
            }
        }
        return manager;
    }

    /**
     * 判断session是否存在
     * @param context
     * @return
     */
    public boolean containsSession(ChannelHandlerContext context){
        return context!=null && context.channel()!=null && context.channel().id()!=null && manager.sessionMap.get(context.channel().id())!=null;
    }

    /**
     * 添加一个session
     * @param context
     * @param session
     */
    public void addSession(ChannelHandlerContext context,HttpSession session){
        if(context==null || context.channel()==null || context.channel().id()==null || session==null){
            throw new InvalidSessionException("context or session is null");