package com.stnetix.ariaddna.pushnotification.server;

import com.stnetix.ariaddna.commonutils.logger.AriaddnaLogger;
import org.eclipse.jetty.websocket.api.Session;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.*;

/**
 * Created by LugovoyAV on 04.04.2017.
 */
@Service
public class UserServiceImpl<E> implements UserService<E>{
    
    private ConcurrentHashMap<Session,ClientInfo> queueUsers = new ConcurrentHashMap<>();

    private ConcurrentHashMap<ClientInfo,BlockingQueue<E>> clientMap = new ConcurrentHashMap<>();
    private HashMap<ClientInfo,WorkerThread> workerHashMap = new HashMap<>();
    private static final AriaddnaLogger LOGGER = AriaddnaLogger.getLogger(UserServiceImpl.class);

    @Override
    //TODO individual message
    public void sendMessage(ClientInfo clientInfo, E message) throws IOException {
    }

    @Override
    public void sendMessageAll(E message) throws IOException {
        Set<Map.Entry<ClientInfo,BlockingQueue<E>>> set = clientMap.entrySet();
        for (Map.Entry<ClientInfo,BlockingQueue<E>> entry:set) {
           try {
                BlockingQueue<E> q = entry.getValue();
                q.put(message);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
    @Override
    public void removeClient(Session session) {
        ClientInfo client = queueUsers.get(session);
        if (client!=null) {
            WorkerThread worker = workerHashMap.get(session);
            if (worker != null)
                worker.setStopped(true);
        }
        LOGGER.debug("remove Client");
        clientMap.remove(client);
        queueUsers.remove(session);
    }

    private ClientInfo GetClientInfoBySession(Session session) {
        //TODO add sevice for getting client iformation
        ClientInfo client = new ClientInfo();
        clientMap.put(client,new LinkedBlockingQueue<>());
        return client;
    }

    public void addClient(Session session)
    {
        ClientInfo clientInfo = GetClientInfoBySession(session);
        queueUsers.put(session, clientInfo);
        ExecutorService executorService = Executors.newCachedThreadPool();
        WorkerThread worker = new WorkerThread(clientMap.get(clientInfo),session);
        workerHashMap.put(clientInfo,worker);
        executorService.execute(worker);

    }

        
}

@@ -1681,57 +1681,13 @@
 e);%0A
+%0A
     
-    %7D%0A        LOGGER.debug(%22remove Client%22);%0A
     
@@ -1716,16 +1716,20 @@
 lient);%0A
+    
         
@@ -1752,24 +1752,78 @@
 e(session);%0A
+            LOGGER.debug(%22remove Client%22);%0A        %7D%0A%0A
     %7D%0A%0A    p

resolve conflicts