SipLayer.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package com.genersoft.iot.vmp.gb28181;
  2. import java.text.ParseException;
  3. import java.util.Properties;
  4. import java.util.TooManyListenersException;
  5. import java.util.concurrent.LinkedBlockingQueue;
  6. import java.util.concurrent.ThreadPoolExecutor;
  7. import java.util.concurrent.TimeUnit;
  8. import javax.sip.*;
  9. import javax.sip.message.Response;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.context.annotation.Bean;
  14. import org.springframework.context.annotation.DependsOn;
  15. import org.springframework.stereotype.Component;
  16. import com.genersoft.iot.vmp.conf.SipConfig;
  17. import com.genersoft.iot.vmp.gb28181.transmit.SIPProcessorFactory;
  18. import com.genersoft.iot.vmp.gb28181.transmit.response.ISIPResponseProcessor;
  19. import gov.nist.javax.sip.SipStackImpl;
  20. @Component
  21. public class SipLayer implements SipListener {
  22. private final static Logger logger = LoggerFactory.getLogger(SipLayer.class);
  23. @Autowired
  24. private SipConfig sipConfig;
  25. @Autowired
  26. private SIPProcessorFactory processorFactory;
  27. private SipStack sipStack;
  28. private SipFactory sipFactory;
  29. /**
  30. * 消息处理器线程池
  31. */
  32. private ThreadPoolExecutor processThreadPool;
  33. @Bean("initSipServer")
  34. private ThreadPoolExecutor initSipServer() {
  35. int processThreadNum = Runtime.getRuntime().availableProcessors() * 10;
  36. LinkedBlockingQueue<Runnable> processQueue = new LinkedBlockingQueue<Runnable>(10000);
  37. processThreadPool = new ThreadPoolExecutor(processThreadNum,processThreadNum,
  38. 0L,TimeUnit.MILLISECONDS,processQueue,
  39. new ThreadPoolExecutor.CallerRunsPolicy());
  40. return processThreadPool;
  41. }
  42. @Bean("sipFactory")
  43. @DependsOn("initSipServer")
  44. private SipFactory createSipFactory() {
  45. sipFactory = SipFactory.getInstance();
  46. sipFactory.setPathName("gov.nist");
  47. return sipFactory;
  48. }
  49. @Bean("sipStack")
  50. @DependsOn({"initSipServer", "sipFactory"})
  51. private SipStack createSipStack() throws PeerUnavailableException {
  52. Properties properties = new Properties();
  53. properties.setProperty("javax.sip.STACK_NAME", "GB28181_SIP");
  54. properties.setProperty("javax.sip.IP_ADDRESS", sipConfig.getSipIp());
  55. properties.setProperty("gov.nist.javax.sip.LOG_MESSAGE_CONTENT", "false");
  56. /**
  57. * sip_server_log.log 和 sip_debug_log.log public static final int TRACE_NONE =
  58. * 0; public static final int TRACE_MESSAGES = 16; public static final int
  59. * TRACE_EXCEPTION = 17; public static final int TRACE_DEBUG = 32;
  60. */
  61. properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "0");
  62. properties.setProperty("gov.nist.javax.sip.SERVER_LOG", "sip_server_log");
  63. properties.setProperty("gov.nist.javax.sip.DEBUG_LOG", "sip_debug_log");
  64. sipStack = (SipStackImpl) sipFactory.createSipStack(properties);
  65. return sipStack;
  66. }
  67. @Bean("tcpSipProvider")
  68. @DependsOn("sipStack")
  69. private SipProvider startTcpListener() {
  70. ListeningPoint tcpListeningPoint = null;
  71. SipProvider tcpSipProvider = null;
  72. try {
  73. tcpListeningPoint = sipStack.createListeningPoint(sipConfig.getSipIp(), sipConfig.getSipPort(), "TCP");
  74. tcpSipProvider = sipStack.createSipProvider(tcpListeningPoint);
  75. tcpSipProvider.addSipListener(this);
  76. logger.info("Sip Server TCP 启动成功 port {" + sipConfig.getSipPort() + "}");
  77. } catch (TransportNotSupportedException | InvalidArgumentException | TooManyListenersException | ObjectInUseException e) {
  78. logger.error(String.format("创建SIP服务失败: %s", e.getMessage()));
  79. }
  80. return tcpSipProvider;
  81. }
  82. @Bean("udpSipProvider")
  83. @DependsOn("sipStack")
  84. private SipProvider startUdpListener() throws Exception {
  85. ListeningPoint udpListeningPoint = sipStack.createListeningPoint(sipConfig.getSipIp(), sipConfig.getSipPort(), "UDP");
  86. SipProvider udpSipProvider = sipStack.createSipProvider(udpListeningPoint);
  87. udpSipProvider.addSipListener(this);
  88. logger.info("Sip Server UDP 启动成功 port {" + sipConfig.getSipPort() + "}");
  89. return udpSipProvider;
  90. }
  91. /**
  92. * SIP服务端接收消息的方法 Content 里面是GBK编码 This method is called by the SIP stack when a
  93. * new request arrives.
  94. */
  95. @Override
  96. public void processRequest(RequestEvent evt) {
  97. logger.debug(evt.getRequest().toString());
  98. // 由于jainsip是单线程程序,为提高性能并发处理
  99. processThreadPool.execute(() -> {
  100. processorFactory.createRequestProcessor(evt).process();
  101. });
  102. }
  103. @Override
  104. public void processResponse(ResponseEvent evt) {
  105. Response response = evt.getResponse();
  106. logger.debug(evt.getResponse().toString());
  107. int status = response.getStatusCode();
  108. if ((status >= 200) && (status < 300)) { // Success!
  109. ISIPResponseProcessor processor = processorFactory.createResponseProcessor(evt);
  110. try {
  111. processor.process(evt, this, sipConfig);
  112. } catch (ParseException e) {
  113. // TODO Auto-generated catch block
  114. e.printStackTrace();
  115. }
  116. // } else if (status == Response.TRYING) {
  117. // trying不会回复
  118. } else if ((status >= 100) && (status < 200)) {
  119. // 增加其它无需回复的响应,如101、180等
  120. } else {
  121. logger.warn("接收到失败的response响应!status:" + status + ",message:" + response.getReasonPhrase()/* .getContent().toString()*/);
  122. }
  123. // trying不会回复
  124. // if (status == Response.TRYING) {
  125. // }
  126. }
  127. /**
  128. * <p>
  129. * Title: processTimeout
  130. * </p>
  131. * <p>
  132. * Description:
  133. * </p>
  134. *
  135. * @param timeoutEvent
  136. */
  137. @Override
  138. public void processTimeout(TimeoutEvent timeoutEvent) {
  139. // TODO Auto-generated method stub
  140. }
  141. /**
  142. * <p>
  143. * Title: processIOException
  144. * </p>
  145. * <p>
  146. * Description:
  147. * </p>
  148. *
  149. * @param exceptionEvent
  150. */
  151. @Override
  152. public void processIOException(IOExceptionEvent exceptionEvent) {
  153. // TODO Auto-generated method stub
  154. }
  155. /**
  156. * <p>
  157. * Title: processTransactionTerminated
  158. * </p>
  159. * <p>
  160. * Description:
  161. * </p>
  162. *
  163. * @param transactionTerminatedEvent
  164. */
  165. @Override
  166. public void processTransactionTerminated(TransactionTerminatedEvent transactionTerminatedEvent) {
  167. // TODO Auto-generated method stub
  168. }
  169. /**
  170. * <p>
  171. * Title: processDialogTerminated
  172. * </p>
  173. * <p>
  174. * Description:
  175. * </p>
  176. *
  177. * @param dialogTerminatedEvent
  178. */
  179. @Override
  180. public void processDialogTerminated(DialogTerminatedEvent dialogTerminatedEvent) {
  181. // TODO Auto-generated method stub
  182. }
  183. }