diff --git a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/generic/SipBuilder.java b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/generic/SipBuilder.java index fa5a9ae..77683a0 100644 --- a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/generic/SipBuilder.java +++ b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/generic/SipBuilder.java @@ -162,7 +162,7 @@ public class SipBuilder { } @SneakyThrows - public static EventHeader createExpiresHeader(String event){ + public static EventHeader createEventHeader(String event){ return getHeaderFactory().createEventHeader(event); } diff --git a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/RequestBuilder.java b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/RequestBuilder.java index af762fb..a690afc 100644 --- a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/RequestBuilder.java +++ b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/RequestBuilder.java @@ -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()); + } } diff --git a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/subscribe/request/SubscribeRequestBuilder.java b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/subscribe/request/SubscribeRequestBuilder.java index 49e3259..28ecd50 100644 --- a/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/subscribe/request/SubscribeRequestBuilder.java +++ b/gb28181-sip/src/main/java/cn/skcks/docking/gb28181/sip/method/subscribe/request/SubscribeRequestBuilder.java @@ -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)); } } diff --git a/gb28181-sip/src/test/java/cn/skcks/docking/gb28181/sip/process/RequestTest.java b/gb28181-sip/src/test/java/cn/skcks/docking/gb28181/sip/process/RequestTest.java index 01543ed..1e63fb0 100644 --- a/gb28181-sip/src/test/java/cn/skcks/docking/gb28181/sip/process/RequestTest.java +++ b/gb28181-sip/src/test/java/cn/skcks/docking/gb28181/sip/process/RequestTest.java @@ -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()