From 23782c0f8c78612350273920c830b267f7e01d3a Mon Sep 17 00:00:00 2001 From: shikong <919411476@qq.com> Date: Tue, 12 Sep 2023 17:16:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=81=E8=A3=85=20SipSender?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mocking/config/sip/ServerConfig.java | 3 -- .../gb28181/mocking/config/sip/SipConfig.java | 3 ++ .../core/sip/request/SipRequestBuilder.java | 2 +- .../mocking/core/sip/sender/SipSender.java | 54 +++++++++++++++++++ .../gb28181/register/RegisterService.java | 22 ++------ .../src/main/resources/application.yml | 1 + 6 files changed, 63 insertions(+), 22 deletions(-) create mode 100644 gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/core/sip/sender/SipSender.java diff --git a/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/config/sip/ServerConfig.java b/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/config/sip/ServerConfig.java index a59d4a1..b529822 100644 --- a/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/config/sip/ServerConfig.java +++ b/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/config/sip/ServerConfig.java @@ -5,8 +5,6 @@ import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; -import javax.sip.ListeningPoint; - @Component @ConfigurationProperties(prefix = "gb28181.server", ignoreInvalidFields = true) @Order(0) @@ -17,5 +15,4 @@ public class ServerConfig { private String ip; private int port; private String password; - private String transport = ListeningPoint.UDP; } diff --git a/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/config/sip/SipConfig.java b/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/config/sip/SipConfig.java index ad5b60d..a078a17 100644 --- a/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/config/sip/SipConfig.java +++ b/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/config/sip/SipConfig.java @@ -7,6 +7,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; +import javax.sip.ListeningPoint; import java.util.List; @Component @@ -32,6 +33,8 @@ public class SipConfig { Integer registerTimeInterval = 120; + private String transport = ListeningPoint.UDP; + private boolean alarm; public List getShowIp() { diff --git a/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/core/sip/request/SipRequestBuilder.java b/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/core/sip/request/SipRequestBuilder.java index a676236..c634ff1 100644 --- a/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/core/sip/request/SipRequestBuilder.java +++ b/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/core/sip/request/SipRequestBuilder.java @@ -90,7 +90,7 @@ public class SipRequestBuilder implements ApplicationContextAware { String target = StringUtils.joinWith(":", serverConfig.getIp(), serverConfig.getPort()); SipURI requestURI = getSipURI(serverConfig.getId(), target); // via - List viaHeaders = getViaHeaders(serverConfig.getIp(), serverConfig.getPort(), serverConfig.getTransport(), viaTag); + List viaHeaders = getViaHeaders(serverConfig.getIp(), serverConfig.getPort(), sipConfig.getTransport(), viaTag); // from String from = StringUtils.joinWith(":", ip, port); diff --git a/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/core/sip/sender/SipSender.java b/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/core/sip/sender/SipSender.java new file mode 100644 index 0000000..f7e1b30 --- /dev/null +++ b/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/core/sip/sender/SipSender.java @@ -0,0 +1,54 @@ +package cn.skcks.docking.gb28181.mocking.core.sip.sender; + +import cn.skcks.docking.gb28181.core.sip.service.SipService; +import cn.skcks.docking.gb28181.mocking.config.sip.SipConfig; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import javax.sip.ListeningPoint; +import javax.sip.SipException; +import javax.sip.SipProvider; +import javax.sip.message.Request; +import java.util.List; +import java.util.Objects; + +@Slf4j +@RequiredArgsConstructor +@Component +public class SipSender { + private final SipService sipService; + private final SipConfig sipConfig; + + public SipProvider getProvider(String transport, String ip) { + return sipService.getProvider(transport, ip); + } + + public List getProviders() { + return sipConfig.getIp().stream().map(item -> getProvider(sipConfig.getTransport(), item)) + .filter(Objects::nonNull) + .toList(); + } + + public void sendRequest(SendRequest request) { + getProviders().parallelStream().forEach(sipProvider -> { + log.info("{}", sipProvider); + ListeningPoint[] listeningPoints = sipProvider.getListeningPoints(); + if (listeningPoints == null || listeningPoints.length == 0) { + log.error("发送请求失败, 未找到有效的监听地址"); + return; + } + ListeningPoint listeningPoint = listeningPoints[0]; + String ip = listeningPoint.getIPAddress(); + try { + sipProvider.sendRequest(request.build(sipProvider, ip)); + } catch (SipException e) { + log.error("向{} {}:{} 发送请求失败, 异常: {}", ip, listeningPoint.getPort(), listeningPoint.getTransport(), e.getMessage()); + } + }); + } + + public interface SendRequest { + Request build(SipProvider provider, String ip); + } +} diff --git a/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/service/gb28181/register/RegisterService.java b/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/service/gb28181/register/RegisterService.java index ed7e4dd..eef4927 100644 --- a/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/service/gb28181/register/RegisterService.java +++ b/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/service/gb28181/register/RegisterService.java @@ -6,16 +6,12 @@ import cn.skcks.docking.gb28181.core.sip.service.SipService; import cn.skcks.docking.gb28181.core.sip.utils.SipUtil; import cn.skcks.docking.gb28181.mocking.config.sip.SipConfig; import cn.skcks.docking.gb28181.mocking.core.sip.request.SipRequestBuilder; +import cn.skcks.docking.gb28181.mocking.core.sip.sender.SipSender; import cn.skcks.docking.gb28181.mocking.service.device.DeviceService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; -import javax.sip.ListeningPoint; -import javax.sip.SipException; -import javax.sip.SipProvider; -import javax.sip.message.Request; - @Slf4j @RequiredArgsConstructor @Service @@ -23,23 +19,13 @@ public class RegisterService { private final SipConfig sipConfig; private final SipListener sipListener; private final SipService sipService; - private final DeviceService deviceService; + private final SipSender sender; + public boolean register(){ deviceService.getAllDevice().parallelStream().forEach(device -> { - sipConfig.getIp().parallelStream().forEach(ip->{ - SipProvider provider = sipService.getProvider(ListeningPoint.UDP, ip); - if(provider == null){ - return; - } - Request request = SipRequestBuilder.createRegisterRequest(device,ip, sipConfig.getPort(),1, SipUtil.generateFromTag(),null, provider.getNewCallId()); - try { - provider.sendRequest(request); - } catch (SipException e) { - throw new RuntimeException(e); - } - }); + sender.sendRequest((provider, ip) -> SipRequestBuilder.createRegisterRequest(device, ip, sipConfig.getPort(), 1, SipUtil.generateFromTag(), null, provider.getNewCallId())); }); return true; diff --git a/gb28181-mocking-starter/src/main/resources/application.yml b/gb28181-mocking-starter/src/main/resources/application.yml index cf3546e..78c17bc 100644 --- a/gb28181-mocking-starter/src/main/resources/application.yml +++ b/gb28181-mocking-starter/src/main/resources/application.yml @@ -49,6 +49,7 @@ gb28181: # [可选] 默认设备认证密码,后续扩展使用设备单独密码, 移除密码将不进行校验 password: 123456 expire: 3600 + transport: "UDP" server: ip: 192.168.10.32 # ip: 192.168.3.12