package cc.blynk.server.dao;

import cc.blynk.server.model.auth.ChannelState;
import cc.blynk.server.model.auth.Session;
import cc.blynk.server.model.auth.User;
import io.netty.channel.Channel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Holds session info related to specific user.
 *
 * The Blynk Project.
 * Created by Dmitriy Dumanskiy.
 * Created on 2/18/2015.
 */
public class SessionsHolder {

    private static final Logger log = LogManager.getLogger(SessionsHolder.class);

    private Map<User, Session> userSession = new ConcurrentHashMap<>();

    public void addAppChannelToGroup(User user, Channel channel, int msgId) {
        Session session = getSessionByUser(user);
        session.addAppChannel(channel, msgId);
    }

    public void addHardwareChannelToGroup(User user, Channel channel, int msgId) {
        Session session = getSessionByUser(user);
        session.addHardwareChannel(channel, msgId);
    }

    public void removeFromSession(ChannelState channel) {
        if (channel.user != null) {
            Session session = userSession.get(channel.user);
            if (session != null) {
                session.remove(channel);
            }
        }
    }

    public Map<User, Session> getUserSession() {
        return userSession;
    }

    private Session getSessionByUser(User user) {
        Session group = userSession.get(user);
        //only one side came
        if (group == null) {
            log.trace("Creating unique session for user: {}", user);
            Session value = new Session();
            group = userSession.putIfAbsent(user, value);
            if (group == null) {
                return value;
            }
        }

        return group;
    }

}
@@ -1384,24 +1384,41 @@
 ion;%0A    %7D%0A%0A
+    //threadsafe%0A
     private 
@@ -1451,24 +1451,24 @@
 ser user) %7B%0A
-
         Sess
@@ -1564,77 +1564,8 @@
 ) %7B%0A
-            log.trace(%22Creating unique session for user: %7B%7D%22, user);%0A
     
@@ -1661,16 +1661,16 @@
 value);%0A
-
         
@@ -1686,32 +1686,105 @@
 roup == null) %7B%0A
+                log.trace(%22Creating unique session for user: %7B%7D%22, user);%0A
                 

Correct log place.