sean.zhou
2022-07-22 9b2eedb85d53ca32610c32c6e50b5230ab3b16cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.dji.sample.component.websocket.model;
 
import com.dji.sample.component.websocket.config.ConcurrentWebSocketSession;
import com.dji.sample.manage.model.enums.UserTypeEnum;
import lombok.extern.slf4j.Slf4j;
 
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * Manage all WebSocket connection objects.
 * @author sean.zhou
 * @date 2021/11/16
 * @version 0.1
 */
@Slf4j
public class WebSocketManager {
 
    private static final ConcurrentHashMap<String,
            ConcurrentHashMap<String,
                    ConcurrentHashMap<String, ConcurrentWebSocketSession>>> MANAGER = new ConcurrentHashMap<>(16);
 
    /**
     * WebSocket connection from the pilot.
     */
    private static final Set<ConcurrentWebSocketSession> PILOT_SESSION = ConcurrentHashMap.newKeySet(16);
 
    /**
     * WebSocket connection from the web.
     */
    private static final Set<ConcurrentWebSocketSession> WEB_SESSION = ConcurrentHashMap.newKeySet(16);
 
    public static void put(String key, ConcurrentWebSocketSession val) {
 
        String[] name = key.split("/");
        if (name.length != 3) {
            log.debug("The key is out of format. [{workspaceId}/{userType}/{userId}]");
            return;
        }
 
        ConcurrentHashMap<String, ConcurrentHashMap<String, ConcurrentWebSocketSession>> workspaceSessions =
                MANAGER.getOrDefault(name[0], new ConcurrentHashMap<>(16));
 
        ConcurrentHashMap<String, ConcurrentWebSocketSession> userSessions = workspaceSessions.getOrDefault(
                name[2], new ConcurrentHashMap<>(16));
        userSessions.put(val.getId(), val);
        workspaceSessions.put(name[2], userSessions);
        MANAGER.put(name[0], workspaceSessions);
 
        getSetByUserType(Integer.valueOf(name[1])).add(val);
 
    }
 
    public static void remove(String key, String sessionId) {
        String[] name = key.split("/");
        if (name.length != 3) {
            log.debug("The key is out of format. [{workspaceId}/{userType}/{userId}]");
            return;
        }
        ConcurrentHashMap<String, ConcurrentWebSocketSession> userSession = MANAGER.get(name[0]).get(name[2]);
 
        Set<ConcurrentWebSocketSession> typeSession = getSetByUserType(Integer.valueOf(name[1]));
 
        ConcurrentWebSocketSession session = userSession.get(sessionId);
        typeSession.remove(session);
        userSession.remove(sessionId);
    }
 
    public static int getConnectedCount() {
        return PILOT_SESSION.size() + WEB_SESSION.size();
    }
 
    public static Collection<ConcurrentWebSocketSession> getValueWithWorkspace(String workspaceId) {
        Set<ConcurrentWebSocketSession> sessions = ConcurrentHashMap.newKeySet();
 
        MANAGER.get(workspaceId)
                .forEach((userId, userSessions) -> {
                    sessions.addAll(userSessions.values());
                });
        return sessions;
    }
 
    public static Collection<ConcurrentWebSocketSession> getValueWithWorkspaceAndUserType(String workspaceId, Integer userType) {
        Set<ConcurrentWebSocketSession> sessions = ConcurrentHashMap.newKeySet();
        Set<ConcurrentWebSocketSession> typeSessions = getSetByUserType(userType);
 
        MANAGER.getOrDefault(workspaceId, new ConcurrentHashMap<>())
                .forEach((userId, userSessions) -> {
                    Collection<ConcurrentWebSocketSession> sessionList = userSessions.values();
                    if (!sessionList.isEmpty()) {
                        ConcurrentWebSocketSession session = sessionList.iterator().next();
                        if (typeSessions.contains(session)) {
                            sessions.addAll(sessionList);
                        }
                    }
                });
        return sessions;
    }
 
    private static Set<ConcurrentWebSocketSession> getSetByUserType(Integer userType) {
        if (UserTypeEnum.PILOT.getVal() == userType) {
            return PILOT_SESSION;
        }
 
        if (UserTypeEnum.WEB.getVal() == userType) {
            return WEB_SESSION;
        }
        return new HashSet<>();
    }
}