封装 SipSender
This commit is contained in:
parent
68f5964a42
commit
23782c0f8c
@ -5,8 +5,6 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
|||||||
import org.springframework.core.annotation.Order;
|
import org.springframework.core.annotation.Order;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import javax.sip.ListeningPoint;
|
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@ConfigurationProperties(prefix = "gb28181.server", ignoreInvalidFields = true)
|
@ConfigurationProperties(prefix = "gb28181.server", ignoreInvalidFields = true)
|
||||||
@Order(0)
|
@Order(0)
|
||||||
@ -17,5 +15,4 @@ public class ServerConfig {
|
|||||||
private String ip;
|
private String ip;
|
||||||
private int port;
|
private int port;
|
||||||
private String password;
|
private String password;
|
||||||
private String transport = ListeningPoint.UDP;
|
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
|||||||
import org.springframework.core.annotation.Order;
|
import org.springframework.core.annotation.Order;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.sip.ListeningPoint;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@ -32,6 +33,8 @@ public class SipConfig {
|
|||||||
|
|
||||||
Integer registerTimeInterval = 120;
|
Integer registerTimeInterval = 120;
|
||||||
|
|
||||||
|
private String transport = ListeningPoint.UDP;
|
||||||
|
|
||||||
private boolean alarm;
|
private boolean alarm;
|
||||||
|
|
||||||
public List<String> getShowIp() {
|
public List<String> getShowIp() {
|
||||||
|
@ -90,7 +90,7 @@ public class SipRequestBuilder implements ApplicationContextAware {
|
|||||||
String target = StringUtils.joinWith(":", serverConfig.getIp(), serverConfig.getPort());
|
String target = StringUtils.joinWith(":", serverConfig.getIp(), serverConfig.getPort());
|
||||||
SipURI requestURI = getSipURI(serverConfig.getId(), target);
|
SipURI requestURI = getSipURI(serverConfig.getId(), target);
|
||||||
// via
|
// via
|
||||||
List<ViaHeader> viaHeaders = getViaHeaders(serverConfig.getIp(), serverConfig.getPort(), serverConfig.getTransport(), viaTag);
|
List<ViaHeader> viaHeaders = getViaHeaders(serverConfig.getIp(), serverConfig.getPort(), sipConfig.getTransport(), viaTag);
|
||||||
|
|
||||||
// from
|
// from
|
||||||
String from = StringUtils.joinWith(":", ip, port);
|
String from = StringUtils.joinWith(":", ip, port);
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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.core.sip.utils.SipUtil;
|
||||||
import cn.skcks.docking.gb28181.mocking.config.sip.SipConfig;
|
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.request.SipRequestBuilder;
|
||||||
|
import cn.skcks.docking.gb28181.mocking.core.sip.sender.SipSender;
|
||||||
import cn.skcks.docking.gb28181.mocking.service.device.DeviceService;
|
import cn.skcks.docking.gb28181.mocking.service.device.DeviceService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.sip.ListeningPoint;
|
|
||||||
import javax.sip.SipException;
|
|
||||||
import javax.sip.SipProvider;
|
|
||||||
import javax.sip.message.Request;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@Service
|
@Service
|
||||||
@ -23,23 +19,13 @@ public class RegisterService {
|
|||||||
private final SipConfig sipConfig;
|
private final SipConfig sipConfig;
|
||||||
private final SipListener sipListener;
|
private final SipListener sipListener;
|
||||||
private final SipService sipService;
|
private final SipService sipService;
|
||||||
|
|
||||||
private final DeviceService deviceService;
|
private final DeviceService deviceService;
|
||||||
|
|
||||||
|
private final SipSender sender;
|
||||||
|
|
||||||
public boolean register(){
|
public boolean register(){
|
||||||
deviceService.getAllDevice().parallelStream().forEach(device -> {
|
deviceService.getAllDevice().parallelStream().forEach(device -> {
|
||||||
sipConfig.getIp().parallelStream().forEach(ip->{
|
sender.sendRequest((provider, ip) -> SipRequestBuilder.createRegisterRequest(device, ip, sipConfig.getPort(), 1, SipUtil.generateFromTag(), null, provider.getNewCallId()));
|
||||||
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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -49,6 +49,7 @@ gb28181:
|
|||||||
# [可选] 默认设备认证密码,后续扩展使用设备单独密码, 移除密码将不进行校验
|
# [可选] 默认设备认证密码,后续扩展使用设备单独密码, 移除密码将不进行校验
|
||||||
password: 123456
|
password: 123456
|
||||||
expire: 3600
|
expire: 3600
|
||||||
|
transport: "UDP"
|
||||||
server:
|
server:
|
||||||
ip: 192.168.10.32
|
ip: 192.168.10.32
|
||||||
# ip: 192.168.3.12
|
# ip: 192.168.3.12
|
||||||
|
Loading…
Reference in New Issue
Block a user