SubscribeRequestBuilder

This commit is contained in:
shikong 2023-09-27 17:04:40 +08:00
parent 1911877d96
commit f44440c42a
4 changed files with 65 additions and 4 deletions

View File

@ -162,7 +162,7 @@ public class SipBuilder {
}
@SneakyThrows
public static EventHeader createExpiresHeader(String event){
public static EventHeader createEventHeader(String event){
return getHeaderFactory().createEventHeader(event);
}

View File

@ -1,6 +1,8 @@
package cn.skcks.docking.gb28181.sip.method;
import cn.skcks.docking.gb28181.sdp.GB28181Description;
import cn.skcks.docking.gb28181.sip.generic.SipBuilder;
import cn.skcks.docking.gb28181.sip.generic.SipContentType;
import cn.skcks.docking.gb28181.sip.generic.SipRequestBuilder;
import cn.skcks.docking.gb28181.sip.utils.SipUtil;
import lombok.Data;
@ -35,4 +37,34 @@ public class RequestBuilder {
SipBuilder.createViaHeaders(getTargetIp(), getTargetPort(), getTransport(), SipUtil.generateViaTag()),
SipBuilder.createMaxForwardsHeader(70));
}
public Request createRequest(String method, String callId, long cSeq, byte[] content) {
String local = SipBuilder.createHostAddress(getLocalIp(), getLocalPort());
Address localAddress = SipBuilder.createAddress(SipBuilder.createSipURI(getLocalId(), local));
String target = SipBuilder.createHostAddress(getTargetIp(), getTargetPort());
SipURI targetUri = SipBuilder.createSipURI(getTargetId(), target);
return SipRequestBuilder.createRequest(targetUri, method,
SipBuilder.createCallIdHeader(callId),
SipBuilder.createCSeqHeader(cSeq, method),
SipBuilder.createFromHeader(localAddress, SipUtil.generateFromTag()),
SipBuilder.createToHeader(localAddress),
SipBuilder.createViaHeaders(getTargetIp(), getTargetPort(), getTransport(), SipUtil.generateViaTag()),
SipBuilder.createMaxForwardsHeader(70), SipContentType.XML, content);
}
public Request createRequest(String method, String callId, long cSeq, GB28181Description description) {
String local = SipBuilder.createHostAddress(getLocalIp(), getLocalPort());
Address localAddress = SipBuilder.createAddress(SipBuilder.createSipURI(getLocalId(), local));
String target = SipBuilder.createHostAddress(getTargetIp(), getTargetPort());
SipURI targetUri = SipBuilder.createSipURI(getTargetId(), target);
return SipRequestBuilder.createRequest(targetUri, method,
SipBuilder.createCallIdHeader(callId),
SipBuilder.createCSeqHeader(cSeq, method),
SipBuilder.createFromHeader(localAddress, SipUtil.generateFromTag()),
SipBuilder.createToHeader(localAddress),
SipBuilder.createViaHeaders(getTargetIp(), getTargetPort(), getTransport(), SipUtil.generateViaTag()),
SipBuilder.createMaxForwardsHeader(70), SipContentType.SDP, description.toString());
}
}

View File

@ -1,5 +1,6 @@
package cn.skcks.docking.gb28181.sip.method.subscribe.request;
import cn.skcks.docking.gb28181.sip.generic.SipBuilder;
import cn.skcks.docking.gb28181.sip.method.RequestBuilder;
import cn.skcks.docking.gb28181.sip.method.subscribe.SubscribeBuilder;
import lombok.Data;
@ -14,7 +15,8 @@ import javax.sip.message.Request;
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class SubscribeRequestBuilder extends RequestBuilder implements SubscribeBuilder {
public Request createSubscribeRequest(){
return null;
public Request createSubscribeRequest(String callId,long cSeq,String event,byte[] content){
return SipBuilder.addHeaders(createRequest(METHOD,callId,cSeq,content),
SipBuilder.createEventHeader(event));
}
}

View File

@ -1,8 +1,11 @@
package cn.skcks.docking.gb28181.sip.process;
import cn.hutool.core.util.IdUtil;
import cn.skcks.docking.gb28181.sip.manscdp.catalog.query.CatalogQueryDTO;
import cn.skcks.docking.gb28181.sip.method.register.request.RegisterRequestBuilder;
import cn.skcks.docking.gb28181.sip.method.register.response.RegisterResponseBuilder;
import cn.skcks.docking.gb28181.sip.method.subscribe.request.SubscribeRequestBuilder;
import cn.skcks.docking.gb28181.sip.utils.MANSCDPUtils;
import cn.skcks.docking.gb28181.sip.utils.SipUtil;
import gov.nist.javax.sip.message.SIPResponse;
import lombok.SneakyThrows;
@ -28,7 +31,31 @@ public class RequestTest {
@Test
@SneakyThrows
void test() {
void subscribeTest(){
// 服务端 客户端 发起订阅
SubscribeRequestBuilder subscribeRequestBuilder = SubscribeRequestBuilder.builder()
.localIp(remoteIp)
.localPort(remotePort)
.localId(remoteId)
.targetIp(localIp)
.targetPort(localPort)
.targetId(localId)
.transport(ListeningPoint.UDP)
.build();
String callId = SipUtil.nanoId(10);
CatalogQueryDTO catalogQueryDTO = CatalogQueryDTO.builder()
.deviceId(localId)
.sn(String.valueOf(1))
.build();
Request subscribeRequest = subscribeRequestBuilder.createSubscribeRequest(callId,
1, catalogQueryDTO.getCmdType(), MANSCDPUtils.toByteXml(catalogQueryDTO));
log.info("\n{}",subscribeRequest);
}
@Test
@SneakyThrows
void registerTest() {
SipUtil.setUserAgentVersion("0.1.0");
String callId = IdUtil.fastSimpleUUID();
RegisterRequestBuilder registerRequestBuilder = RegisterRequestBuilder.builder()