SipSubscribe.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package com.genersoft.iot.vmp.gb28181.event;
  2. import com.genersoft.iot.vmp.gb28181.bean.DeviceNotFoundEvent;
  3. import gov.nist.javax.sip.message.SIPRequest;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.scheduling.annotation.Scheduled;
  7. import org.springframework.stereotype.Component;
  8. import javax.sip.*;
  9. import javax.sip.header.CallIdHeader;
  10. import javax.sip.message.Response;
  11. import java.text.ParseException;
  12. import java.time.Instant;
  13. import java.util.Map;
  14. import java.util.concurrent.ConcurrentHashMap;
  15. import java.util.concurrent.TimeUnit;
  16. /**
  17. * @author lin
  18. */
  19. @Component
  20. public class SipSubscribe {
  21. private final Logger logger = LoggerFactory.getLogger(SipSubscribe.class);
  22. private Map<String, SipSubscribe.Event> errorSubscribes = new ConcurrentHashMap<>();
  23. private Map<String, SipSubscribe.Event> okSubscribes = new ConcurrentHashMap<>();
  24. private Map<String, Instant> okTimeSubscribes = new ConcurrentHashMap<>();
  25. private Map<String, Instant> errorTimeSubscribes = new ConcurrentHashMap<>();
  26. // @Scheduled(cron="*/5 * * * * ?") //每五秒执行一次
  27. // @Scheduled(fixedRate= 100 * 60 * 60 )
  28. @Scheduled(cron="0 0/5 * * * ?") //每5分钟执行一次
  29. public void execute(){
  30. logger.info("[定时任务] 清理过期的SIP订阅信息");
  31. Instant instant = Instant.now().minusMillis(TimeUnit.MINUTES.toMillis(5));
  32. for (String key : okTimeSubscribes.keySet()) {
  33. if (okTimeSubscribes.get(key).isBefore(instant)){
  34. okSubscribes.remove(key);
  35. okTimeSubscribes.remove(key);
  36. }
  37. }
  38. for (String key : errorTimeSubscribes.keySet()) {
  39. if (errorTimeSubscribes.get(key).isBefore(instant)){
  40. errorSubscribes.remove(key);
  41. errorTimeSubscribes.remove(key);
  42. }
  43. }
  44. logger.debug("okTimeSubscribes.size:{}",okTimeSubscribes.size());
  45. logger.debug("okSubscribes.size:{}",okSubscribes.size());
  46. logger.debug("errorTimeSubscribes.size:{}",errorTimeSubscribes.size());
  47. logger.debug("errorSubscribes.size:{}",errorSubscribes.size());
  48. }
  49. public interface Event { void response(EventResult eventResult) ;
  50. }
  51. /**
  52. *
  53. */
  54. public enum EventResultType{
  55. // 超时
  56. timeout,
  57. // 回复
  58. response,
  59. // 事务已结束
  60. transactionTerminated,
  61. // 会话已结束
  62. dialogTerminated,
  63. // 设备未找到
  64. deviceNotFoundEvent
  65. }
  66. public static class EventResult<EventObject>{
  67. public int statusCode;
  68. public EventResultType type;
  69. public String msg;
  70. public String callId;
  71. public EventObject event;
  72. public EventResult(EventObject event) {
  73. this.event = event;
  74. if (event instanceof ResponseEvent) {
  75. ResponseEvent responseEvent = (ResponseEvent)event;
  76. Response response = responseEvent.getResponse();
  77. this.type = EventResultType.response;
  78. if (response != null) {
  79. this.msg = response.getReasonPhrase();
  80. this.statusCode = response.getStatusCode();
  81. }
  82. this.callId = ((CallIdHeader)response.getHeader(CallIdHeader.NAME)).getCallId();
  83. }else if (event instanceof TimeoutEvent) {
  84. TimeoutEvent timeoutEvent = (TimeoutEvent)event;
  85. this.type = EventResultType.timeout;
  86. this.msg = "消息超时未回复";
  87. this.statusCode = -1024;
  88. if (timeoutEvent.isServerTransaction()) {
  89. this.callId = ((SIPRequest)timeoutEvent.getServerTransaction().getRequest()).getCallIdHeader().getCallId();
  90. }else {
  91. this.callId = ((SIPRequest)timeoutEvent.getClientTransaction().getRequest()).getCallIdHeader().getCallId();
  92. }
  93. }else if (event instanceof TransactionTerminatedEvent) {
  94. TransactionTerminatedEvent transactionTerminatedEvent = (TransactionTerminatedEvent)event;
  95. this.type = EventResultType.transactionTerminated;
  96. this.msg = "事务已结束";
  97. this.statusCode = -1024;
  98. if (transactionTerminatedEvent.isServerTransaction()) {
  99. this.callId = ((SIPRequest)transactionTerminatedEvent.getServerTransaction().getRequest()).getCallIdHeader().getCallId();
  100. }else {
  101. this.callId = ((SIPRequest)transactionTerminatedEvent.getClientTransaction().getRequest()).getCallIdHeader().getCallId();
  102. }
  103. }else if (event instanceof DialogTerminatedEvent) {
  104. DialogTerminatedEvent dialogTerminatedEvent = (DialogTerminatedEvent)event;
  105. this.type = EventResultType.dialogTerminated;
  106. this.msg = "会话已结束";
  107. this.statusCode = -1024;
  108. this.callId = dialogTerminatedEvent.getDialog().getCallId().getCallId();
  109. }else if (event instanceof DeviceNotFoundEvent) {
  110. this.type = EventResultType.deviceNotFoundEvent;
  111. this.msg = "设备未找到";
  112. this.statusCode = -1024;
  113. this.callId = ((DeviceNotFoundEvent) event).getCallId();
  114. }
  115. }
  116. }
  117. public void addErrorSubscribe(String key, SipSubscribe.Event event) {
  118. errorSubscribes.put(key, event);
  119. errorTimeSubscribes.put(key, Instant.now());
  120. }
  121. public void addOkSubscribe(String key, SipSubscribe.Event event) {
  122. okSubscribes.put(key, event);
  123. okTimeSubscribes.put(key, Instant.now());
  124. }
  125. public SipSubscribe.Event getErrorSubscribe(String key) {
  126. return errorSubscribes.get(key);
  127. }
  128. public void removeErrorSubscribe(String key) {
  129. if(key == null){
  130. return;
  131. }
  132. errorSubscribes.remove(key);
  133. errorTimeSubscribes.remove(key);
  134. }
  135. public SipSubscribe.Event getOkSubscribe(String key) {
  136. return okSubscribes.get(key);
  137. }
  138. public void removeOkSubscribe(String key) {
  139. if(key == null){
  140. return;
  141. }
  142. okSubscribes.remove(key);
  143. okTimeSubscribes.remove(key);
  144. }
  145. public int getErrorSubscribesSize(){
  146. return errorSubscribes.size();
  147. }
  148. public int getOkSubscribesSize(){
  149. return okSubscribes.size();
  150. }
  151. }