SipSubscribe.java 5.8 KB

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