liusuyi
2026-05-06 99ab0d46d30b095d0e1c9d31349f42e0cefc9881
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.ard.gb28181.transmit.event;
 
import com.ard.gb28181.api.bean.DeviceNotFoundEvent;
import com.ard.gb28181.transmit.event.sip.SipEvent;
import gov.nist.javax.sip.message.SIPRequest;
import gov.nist.javax.sip.message.SIPResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import javax.sip.DialogTerminatedEvent;
import javax.sip.ResponseEvent;
import javax.sip.TimeoutEvent;
import javax.sip.TransactionTerminatedEvent;
import javax.sip.header.WarningHeader;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.DelayQueue;
 
/**
 * @author lin
 */
@Slf4j
@Component
public class SipSubscribe {
 
    private final Map<String, SipEvent> subscribes = new ConcurrentHashMap<>();
 
    private final DelayQueue<SipEvent> delayQueue = new DelayQueue<>();
 
 
    @Scheduled(fixedDelay = 200)
    public void execute() {
        // 1. 使用 poll() 替代 take()
        // poll() 会立即返回:如果有已超期的任务则返回,否则返回 null
        SipEvent take = delayQueue.poll();
 
        // 2. 循环处理所有已超期的任务
        while (take != null) {
            try {
                // 出现超时异常
                if (take.getErrorEvent() != null) {
                    EventResult<Object> eventResult = new EventResult<>();
                    eventResult.type = EventResultType.timeout;
                    eventResult.msg = "消息超时未回复";
                    eventResult.statusCode = -1024;
                    take.getErrorEvent().response(eventResult);
                }
                subscribes.remove(take.getKey());
            } catch (Exception e) {
                // 建议捕获 Exception,防止单个任务异常导致循环中断
                log.error("[SIP超时检查] 处理超时任务异常", e);
            }
            // 3. 继续尝试获取下一个已超期的任务
            take = delayQueue.poll();
        }
        // 4. 如果 poll() 返回 null,说明当前没有超时的任务,方法直接结束
        // 线程释放,等待下一次 200ms 调度
    }
 
    public interface Event {
        void response(EventResult eventResult);
    }
 
    /**
     *
     */
    public enum EventResultType {
        // 超时
        timeout,
        // 回复
        response,
        // 事务已结束
        transactionTerminated,
        // 会话已结束
        dialogTerminated,
        // 设备未找到
        deviceNotFoundEvent,
        // 消息发送失败
        cmdSendFailEvent,
        // 消息发送失败
        failedToGetPort,
        // 收到失败的回复
        failedResult
    }
 
    public static class EventResult<T> {
        public int statusCode;
        public EventResultType type;
        public String msg;
        public String callId;
        public T event;
 
        public EventResult() {
        }
 
        public EventResult(T event) {
            this.event = event;
            if (event instanceof ResponseEvent) {
                ResponseEvent responseEvent = (ResponseEvent) event;
                SIPResponse response = (SIPResponse) responseEvent.getResponse();
                this.type = EventResultType.response;
                if (response != null) {
                    WarningHeader warningHeader = (WarningHeader) response.getHeader(WarningHeader.NAME);
                    if (warningHeader != null && !ObjectUtils.isEmpty(warningHeader.getText())) {
                        this.msg = "";
                        if (warningHeader.getCode() > 0) {
                            this.msg += warningHeader.getCode() + ":";
                        }
                        if (warningHeader.getAgent() != null) {
                            this.msg += warningHeader.getCode() + ":";
                        }
                        if (warningHeader.getText() != null) {
                            this.msg += warningHeader.getText();
                        }
                    } else {
                        this.msg = response.getReasonPhrase();
                    }
                    this.statusCode = response.getStatusCode();
                    this.callId = response.getCallIdHeader().getCallId();
                }
            } else if (event instanceof TimeoutEvent) {
                TimeoutEvent timeoutEvent = (TimeoutEvent) event;
                this.type = EventResultType.timeout;
                this.msg = "消息超时未回复";
                this.statusCode = -1024;
                if (timeoutEvent.isServerTransaction()) {
                    this.callId = ((SIPRequest) timeoutEvent.getServerTransaction().getRequest()).getCallIdHeader().getCallId();
                } else {
                    this.callId = ((SIPRequest) timeoutEvent.getClientTransaction().getRequest()).getCallIdHeader().getCallId();
                }
            } else if (event instanceof TransactionTerminatedEvent) {
                TransactionTerminatedEvent transactionTerminatedEvent = (TransactionTerminatedEvent) event;
                this.type = EventResultType.transactionTerminated;
                this.msg = "事务已结束";
                this.statusCode = -1024;
                if (transactionTerminatedEvent.isServerTransaction()) {
                    this.callId = ((SIPRequest) transactionTerminatedEvent.getServerTransaction().getRequest()).getCallIdHeader().getCallId();
                } else {
                    this.callId = ((SIPRequest) transactionTerminatedEvent.getClientTransaction().getRequest()).getCallIdHeader().getCallId();
                }
            } else if (event instanceof DialogTerminatedEvent) {
                DialogTerminatedEvent dialogTerminatedEvent = (DialogTerminatedEvent) event;
                this.type = EventResultType.dialogTerminated;
                this.msg = "会话已结束";
                this.statusCode = -1024;
                this.callId = dialogTerminatedEvent.getDialog().getCallId().getCallId();
            } else if (event instanceof DeviceNotFoundEvent) {
                this.type = EventResultType.deviceNotFoundEvent;
                this.msg = "设备未找到";
                this.statusCode = -1024;
                this.callId = ((DeviceNotFoundEvent) event).getCallId();
            }
        }
    }
 
 
    public void addSubscribe(String key, SipEvent event) {
        SipEvent sipEvent = subscribes.get(key);
        if (sipEvent != null) {
            subscribes.remove(key);
            delayQueue.remove(sipEvent);
        }
        subscribes.put(key, event);
        delayQueue.offer(event);
    }
 
    public SipEvent getSubscribe(String key) {
        return subscribes.get(key);
    }
 
    public void removeSubscribe(String key) {
        if (key == null) {
            return;
        }
        SipEvent sipEvent = subscribes.get(key);
        if (sipEvent != null) {
            subscribes.remove(key);
            delayQueue.remove(sipEvent);
        }
    }
 
    public boolean isEmpty() {
        return subscribes.isEmpty();
    }
 
    public Integer size() {
        return subscribes.size();
    }
}