|  |  | 
 |  |  | import com.genersoft.iot.vmp.gb28181.transmit.event.request.ISIPRequestProcessor; | 
 |  |  | import com.genersoft.iot.vmp.gb28181.transmit.event.response.ISIPResponseProcessor; | 
 |  |  | import com.genersoft.iot.vmp.gb28181.transmit.event.timeout.ITimeoutProcessor; | 
 |  |  | import gov.nist.javax.sip.message.SIPRequest; | 
 |  |  | import org.slf4j.Logger; | 
 |  |  | import org.slf4j.LoggerFactory; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.scheduling.annotation.Async; | 
 |  |  | import org.springframework.stereotype.Component; | 
 |  |  |  | 
 |  |  | import javax.sip.*; | 
 |  |  | import javax.sip.header.CSeqHeader; | 
 |  |  | import javax.sip.header.CallIdHeader; | 
 |  |  | import javax.sip.header.Header; | 
 |  |  | import javax.sip.message.Response; | 
 |  |  | import java.util.Map; | 
 |  |  | import java.util.concurrent.ConcurrentHashMap; | 
 |  |  | 
 |  |  |  * @date:   2021年11月5日 下午15:32 | 
 |  |  |  */ | 
 |  |  | @Component | 
 |  |  | public class SIPProcessorObserver implements SipListener { | 
 |  |  | public class SIPProcessorObserver implements ISIPProcessorObserver { | 
 |  |  |  | 
 |  |  |     private final static Logger logger = LoggerFactory.getLogger(SIPProcessorObserver.class); | 
 |  |  |  | 
 |  |  | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private SipSubscribe sipSubscribe; | 
 |  |  |  | 
 |  |  | //    @Autowired | 
 |  |  | //    @Qualifier(value = "taskExecutor") | 
 |  |  | //    private ThreadPoolTaskExecutor poolTaskExecutor; | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 添加 request订阅 | 
 |  |  | 
 |  |  |      * @param requestEvent RequestEvent事件 | 
 |  |  |      */ | 
 |  |  |     @Override | 
 |  |  |     @Async | 
 |  |  |     public void processRequest(RequestEvent requestEvent) { | 
 |  |  |         logger.debug("\n收到请求:\n{}", requestEvent.getRequest()); | 
 |  |  |         String method = requestEvent.getRequest().getMethod(); | 
 |  |  |         ISIPRequestProcessor sipRequestProcessor = requestProcessorMap.get(method); | 
 |  |  |         if (sipRequestProcessor == null) { | 
 |  |  |             logger.warn("不支持方法{}的request", method); | 
 |  |  |             return; | 
 |  |  |         } | 
 |  |  |         requestProcessorMap.get(requestEvent.getRequest().getMethod()).process(requestEvent); | 
 |  |  |         requestProcessorMap.get(method).process(requestEvent); | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  | 
 |  |  |      * @param responseEvent responseEvent事件 | 
 |  |  |      */ | 
 |  |  |     @Override | 
 |  |  |     @Async | 
 |  |  |     public void processResponse(ResponseEvent responseEvent) { | 
 |  |  |         logger.debug(responseEvent.getResponse().toString()); | 
 |  |  | //        CSeqHeader cseqHeader = (CSeqHeader) responseEvent.getResponse().getHeader(CSeqHeader.NAME); | 
 |  |  | //        String method = cseqHeader.getMethod(); | 
 |  |  | //        ISIPResponseProcessor sipRequestProcessor = responseProcessorMap.get(method); | 
 |  |  | //        if (sipRequestProcessor == null) { | 
 |  |  | //            logger.warn("不支持方法{}的response", method); | 
 |  |  | //            return; | 
 |  |  | //        } | 
 |  |  | //        sipRequestProcessor.process(responseEvent); | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         Response response = responseEvent.getResponse(); | 
 |  |  |         logger.debug(responseEvent.getResponse().toString()); | 
 |  |  |         logger.debug("\n收到响应:\n{}", responseEvent.getResponse()); | 
 |  |  |         int status = response.getStatusCode(); | 
 |  |  |         if (((status >= 200) && (status < 300)) || status == 401) { // Success! | 
 |  |  | //            ISIPResponseProcessor processor = processorFactory.createResponseProcessor(evt); | 
 |  |  |             CSeqHeader cseqHeader = (CSeqHeader) responseEvent.getResponse().getHeader(CSeqHeader.NAME); | 
 |  |  |             String method = cseqHeader.getMethod(); | 
 |  |  |             ISIPResponseProcessor sipRequestProcessor = responseProcessorMap.get(method); | 
 |  |  | 
 |  |  |                     SipSubscribe.Event subscribe = sipSubscribe.getOkSubscribe(callIdHeader.getCallId()); | 
 |  |  |                     if (subscribe != null) { | 
 |  |  |                         SipSubscribe.EventResult eventResult = new SipSubscribe.EventResult(responseEvent); | 
 |  |  |                         sipSubscribe.removeOkSubscribe(callIdHeader.getCallId()); | 
 |  |  |                         subscribe.response(eventResult); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  | 
 |  |  |                     if (subscribe != null) { | 
 |  |  |                         SipSubscribe.EventResult eventResult = new SipSubscribe.EventResult(responseEvent); | 
 |  |  |                         subscribe.response(eventResult); | 
 |  |  |                         sipSubscribe.removeErrorSubscribe(callIdHeader.getCallId()); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |             if (responseEvent.getDialog() != null) { | 
 |  |  |                 responseEvent.getDialog().delete(); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public void processIOException(IOExceptionEvent exceptionEvent) { | 
 |  |  |  | 
 |  |  |         System.out.println("processIOException"); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public void processTransactionTerminated(TransactionTerminatedEvent transactionTerminatedEvent) { | 
 |  |  | //        Transaction transaction = null; | 
 |  |  | //        System.out.println("processTransactionTerminated"); | 
 |  |  | //        if (transactionTerminatedEvent.isServerTransaction()) { | 
 |  |  | //            transaction = transactionTerminatedEvent.getServerTransaction(); | 
 |  |  | //        }else { | 
 |  |  | //            transaction = transactionTerminatedEvent.getClientTransaction(); | 
 |  |  | //        } | 
 |  |  | // | 
 |  |  | //        System.out.println(transaction.getBranchId()); | 
 |  |  | //        System.out.println(transaction.getState()); | 
 |  |  | //        System.out.println(transaction.getRequest().getMethod()); | 
 |  |  | //        CallIdHeader header = (CallIdHeader)transaction.getRequest().getHeader(CallIdHeader.NAME); | 
 |  |  | //        SipSubscribe.EventResult<TransactionTerminatedEvent> terminatedEventEventResult = new SipSubscribe.EventResult<>(transactionTerminatedEvent); | 
 |  |  |  | 
 |  |  | //        sipSubscribe.getErrorSubscribe(header.getCallId()).response(terminatedEventEventResult); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public void processDialogTerminated(DialogTerminatedEvent dialogTerminatedEvent) { | 
 |  |  |  | 
 |  |  |         CallIdHeader callId = dialogTerminatedEvent.getDialog().getCallId(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  |