封装 SipSender

This commit is contained in:
shikong 2023-09-12 17:16:28 +08:00
parent 68f5964a42
commit 23782c0f8c
6 changed files with 63 additions and 22 deletions

View File

@ -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;
}

View File

@ -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<String> getShowIp() {

View File

@ -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<ViaHeader> viaHeaders = getViaHeaders(serverConfig.getIp(), serverConfig.getPort(), serverConfig.getTransport(), viaTag);
List<ViaHeader> viaHeaders = getViaHeaders(serverConfig.getIp(), serverConfig.getPort(), sipConfig.getTransport(), viaTag);
// from
String from = StringUtils.joinWith(":", ip, port);

View File

@ -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<SipProvider> 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);
}
}

View File

@ -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;

View File

@ -49,6 +49,7 @@ gb28181:
# [可选] 默认设备认证密码,后续扩展使用设备单独密码, 移除密码将不进行校验
password: 123456
expire: 3600
transport: "UDP"
server:
ip: 192.168.10.32
# ip: 192.168.3.12