SipSubscribe.java 6.6 KB

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