package org.stevewinfield.sunrise.game.sessions;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.webbitserver.WebSocketConnection;

public class SessionManager {
	private ConcurrentMap<Integer, Session> sessions;
	
	public SessionManager() {
		this.sessions = new ConcurrentHashMap<Integer, Session>();
	}
	
	public ConcurrentMap<Integer, Session> getSessions() {
		return this.sessions;
	}

	public Session get(WebSocketConnection channel) {
		return this.sessions.containsKey(channel.hashCode()) ? this.sessions.get(channel.hashCode()) : null;
	}
	
	public boolean makeSession(WebSocketConnection channel) {
		Session session = new Session(channel.hashCode(), channel);
		return this.sessions.putIfAbsent(channel.hashCode(), session) == null;
	}
	
	public void removeSession(WebSocketConnection channel) {
		try {
			this.sessions.remove(channel.hashCode());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
