leutu
2024-06-03 3ef35e6cd16bbfa206b26bb3271eac40ad020bcb
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
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.fastbee.base.session;
 
import com.fastbee.common.enums.ServerType;
import com.fastbee.common.utils.spring.SpringUtils;
import com.fastbee.base.service.ISessionStore;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
 
import java.net.InetSocketAddress;
import java.util.Collection;
import java.util.Locale;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
 
/**
 * session管理
 * @author gsb
 * @date 2022/11/7 8:55
 */
@Slf4j
public class SessionManager {
 
    private final Class<? extends Enum> sessionKeys;
 
    private final SessionListener sessionListener;
 
    /*Session会话存储*/
    private static ISessionStore sessionStore = SpringUtils.getBean(ISessionStore.class);
 
    public SessionManager(){
        this(null,null);
    }
 
    public SessionManager(SessionListener sessionListener){
        this(null,sessionListener);
    }
 
    public SessionManager(Class<? extends Enum> sessionKeys, SessionListener sessionListener){
        this.sessionKeys = sessionKeys;
        this.sessionListener = sessionListener;
    }
 
    /**
     * 获取Session
     */
    public Session getSession(String clientId) {
        return sessionStore.getSession(clientId);
    }
 
    /**
     * 获取所有session
     */
    public Collection<Session> all(){
        return sessionStore.getSessionMap().values();
    }
 
    /**
     * 新建session TCP
     * @return
     */
    public Session newInstance(Channel channel){
        InetSocketAddress sender = (InetSocketAddress) channel.remoteAddress();
        Session session = new Session(this, channel, sender, s -> {
            channel.close();
            return true;
        }, false, ServerType.TCP);
        if (sessionListener != null) {
            try {
                sessionListener.sessionCreated(session);
            } catch (Exception e) {
                log.error("sessionCreated", e);
            }
        }
        return session;
    }
 
    /**
     * 新建session UDP
     */
    public Session newInstance(Channel channel, InetSocketAddress sender, Function<Session, Boolean> remover) {
        Session session = new Session(this, channel, sender, remover, true,ServerType.UDP);
        if (sessionListener != null) {
            try {
                sessionListener.sessionCreated(session);
            } catch (Exception e) {
                log.error("sessionCreated", e);
            }
        }
        return session;
    }
 
    /**
     * 设备端离线
     */
    protected void remove(Session session){
        sessionStore.cleanSession(session.getClientId());
        if (null != sessionListener){
            try {
                //设备状态业务处理
                sessionListener.sessionDestroyed(session);
            }catch (Exception e){
                log.error("设备端离线异常",e);
            }
        }
    }
 
    /**
     * 设备端上线
     */
    protected void add(Session session){
        sessionStore.storeSession(session.getClientId().toUpperCase(),session);
        if (null != sessionListener){
            try {
                sessionListener.sessionRegistered(session);
            }catch (Exception e){
                log.error("设备端注册",e);
            }
        }
    }
 
    public Class<? extends Enum> getSessionKeys(){
        return sessionKeys;
    }
 
}