From 764d04b497356ba6bcbb75fd42b51eca750f7223 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期三, 29 五月 2024 15:02:51 +0800
Subject: [PATCH] 调整上级观看消息的发送

---
 src/main/java/com/genersoft/iot/vmp/gb28181/task/impl/CatalogSubscribeTask.java |   80 +++++++++++++++++++++------------------
 1 files changed, 43 insertions(+), 37 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/task/impl/CatalogSubscribeTask.java b/src/main/java/com/genersoft/iot/vmp/gb28181/task/impl/CatalogSubscribeTask.java
old mode 100644
new mode 100755
index 577e724..d9270bb
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/task/impl/CatalogSubscribeTask.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/task/impl/CatalogSubscribeTask.java
@@ -1,19 +1,20 @@
 package com.genersoft.iot.vmp.gb28181.task.impl;
 
+import com.genersoft.iot.vmp.common.CommonCallback;
 import com.genersoft.iot.vmp.conf.DynamicTask;
 import com.genersoft.iot.vmp.gb28181.bean.Device;
 import com.genersoft.iot.vmp.gb28181.task.ISubscribeTask;
 import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommander;
+import gov.nist.javax.sip.message.SIPRequest;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.scheduling.annotation.Async;
-import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 
-import javax.sip.Dialog;
 import javax.sip.DialogState;
+import javax.sip.InvalidArgumentException;
 import javax.sip.ResponseEvent;
-import java.util.Timer;
-import java.util.TimerTask;
+import javax.sip.SipException;
+import javax.sip.header.ToHeader;
+import java.text.ParseException;
 
 /**
  * 鐩綍璁㈤槄浠诲姟
@@ -23,7 +24,7 @@
     private final Logger logger = LoggerFactory.getLogger(CatalogSubscribeTask.class);
     private Device device;
     private final ISIPCommander sipCommander;
-    private Dialog dialog;
+    private SIPRequest request;
 
     private DynamicTask dynamicTask;
 
@@ -41,28 +42,36 @@
         if (dynamicTask.get(taskKey) != null) {
             dynamicTask.stop(taskKey);
         }
-        sipCommander.catalogSubscribe(device, dialog, eventResult -> {
-            if (eventResult.dialog != null || eventResult.dialog.getState().equals(DialogState.CONFIRMED)) {
-                dialog = eventResult.dialog;
-            }
-            ResponseEvent event = (ResponseEvent) eventResult.event;
-            if (event.getResponse().getRawContent() != null) {
+        SIPRequest sipRequest = null;
+        try {
+            sipRequest = sipCommander.catalogSubscribe(device, request, eventResult -> {
+                ResponseEvent event = (ResponseEvent) eventResult.event;
                 // 鎴愬姛
                 logger.info("[鐩綍璁㈤槄]鎴愬姛锛� {}", device.getDeviceId());
-            }else {
-                // 鎴愬姛
-                logger.info("[鐩綍璁㈤槄]鎴愬姛锛� {}", device.getDeviceId());
-            }
-        },eventResult -> {
-            dialog = null;
-            // 澶辫触
-            logger.warn("[鐩綍璁㈤槄]澶辫触锛屼俊浠ゅ彂閫佸け璐ワ細 {}-{} ", device.getDeviceId(), eventResult.msg);
-            dynamicTask.startDelay(taskKey, CatalogSubscribeTask.this, 2000);
-        });
+                ToHeader toHeader = (ToHeader)event.getResponse().getHeader(ToHeader.NAME);
+                try {
+                    this.request.getToHeader().setTag(toHeader.getTag());
+                } catch (ParseException e) {
+                    logger.info("[鐩綍璁㈤槄]鎴愬姛锛� 浣嗕负request璁剧疆ToTag澶辫触");
+                    this.request = null;
+                }
+            },eventResult -> {
+                this.request = null;
+                // 澶辫触
+                logger.warn("[鐩綍璁㈤槄]澶辫触锛屼俊浠ゅ彂閫佸け璐ワ細 {}-{} ", device.getDeviceId(), eventResult.msg);
+                dynamicTask.startDelay(taskKey, CatalogSubscribeTask.this, 2000);
+            });
+        } catch (InvalidArgumentException | SipException | ParseException e) {
+            logger.error("[鍛戒护鍙戦�佸け璐 鐩綍璁㈤槄: {}", e.getMessage());
+
+        }
+        if (sipRequest != null) {
+            this.request = sipRequest;
+        }
     }
 
     @Override
-    public void stop() {
+    public void stop(CommonCallback<Boolean> callback) {
         /**
          * dialog 鐨勫悇涓姸鎬�
          * EARLY-> Early state鐘舵��-鍒濆璇锋眰鍙戦�佷互鍚庯紝鏀跺埌浜嗕竴涓复鏃跺搷搴旀秷鎭�
@@ -74,29 +83,26 @@
         if (dynamicTask.get(taskKey) != null) {
             dynamicTask.stop(taskKey);
         }
-        if (dialog != null && dialog.getState().equals(DialogState.CONFIRMED)) {
-            device.setSubscribeCycleForCatalog(0);
-            sipCommander.catalogSubscribe(device, dialog, eventResult -> {
+        device.setSubscribeCycleForCatalog(0);
+        try {
+            sipCommander.catalogSubscribe(device, request, eventResult -> {
                 ResponseEvent event = (ResponseEvent) eventResult.event;
                 if (event.getResponse().getRawContent() != null) {
                     // 鎴愬姛
-                    logger.info("[鍙栨秷鐩綍璁㈤槄璁㈤槄]鎴愬姛锛� {}", device.getDeviceId());
+                    logger.info("[鍙栨秷鐩綍璁㈤槄]鎴愬姛锛� {}", device.getDeviceId());
                 }else {
                     // 鎴愬姛
-                    logger.info("[鍙栨秷鐩綍璁㈤槄璁㈤槄]鎴愬姛锛� {}", device.getDeviceId());
+                    logger.info("[鍙栨秷鐩綍璁㈤槄]鎴愬姛锛� {}", device.getDeviceId());
+                }
+                if (callback != null) {
+                    callback.run(event.getResponse().getRawContent() != null);
                 }
             },eventResult -> {
                 // 澶辫触
-                logger.warn("[鍙栨秷鐩綍璁㈤槄璁㈤槄]澶辫触锛屼俊浠ゅ彂閫佸け璐ワ細 {}-{} ", device.getDeviceId(), eventResult.msg);
+                logger.warn("[鍙栨秷鐩綍璁㈤槄]澶辫触锛屼俊浠ゅ彂閫佸け璐ワ細 {}-{} ", device.getDeviceId(), eventResult.msg);
             });
+        } catch (InvalidArgumentException | SipException | ParseException e) {
+            logger.error("[鍛戒护鍙戦�佸け璐 鍙栨秷鐩綍璁㈤槄: {}", e.getMessage());
         }
-    }
-
-    @Override
-    public DialogState getDialogState() {
-        if (dialog == null) {
-            return null;
-        }
-        return dialog.getState();
     }
 }

--
Gitblit v1.8.0