subscribe 订阅

This commit is contained in:
shikong 2023-09-28 14:43:57 +08:00
parent 7eee14510e
commit 2bf19edb4e
4 changed files with 87 additions and 2 deletions

View File

@ -1,5 +1,6 @@
package cn.skcks.docking.gb28181.sip.generic;
import cn.skcks.docking.gb28181.constant.GB28181Constant;
import cn.skcks.docking.gb28181.sip.header.XGBVerHeader;
import cn.skcks.docking.gb28181.sip.header.impl.XGBVerHeaderImpl;
import cn.skcks.docking.gb28181.sip.utils.SipUtil;
@ -17,7 +18,6 @@ import javax.sip.header.*;
import javax.sip.message.MessageFactory;
import javax.sip.message.Request;
import javax.sip.message.Response;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
@ -26,7 +26,7 @@ import java.util.List;
@Slf4j
public class SipBuilder {
@Setter
public static String DEFAULT_CHARSET = StandardCharsets.UTF_8.name();
public static String DEFAULT_CHARSET = GB28181Constant.CHARSET;
@Setter
public static XGBVerHeader GB_VERSION = XGBVerHeaderImpl.GB28181_2016;
@ -166,6 +166,11 @@ public class SipBuilder {
return getHeaderFactory().createEventHeader(event);
}
@SneakyThrows
public static SubscriptionStateHeader createSubscriptionStateHeader(String state){
return getHeaderFactory().createSubscriptionStateHeader(state);
}
@SneakyThrows
public static XGBVerHeader createXGBVerHeader(int m,int n){
return new XGBVerHeaderImpl(m,n);

View File

@ -0,0 +1,7 @@
package cn.skcks.docking.gb28181.sip.method.notify;
import javax.sip.message.Request;
public interface NotifyBuilder {
String METHOD = Request.NOTIFY;
}

View File

@ -0,0 +1,41 @@
package cn.skcks.docking.gb28181.sip.method.notify.request;
import cn.skcks.docking.gb28181.sip.generic.SipBuilder;
import cn.skcks.docking.gb28181.sip.method.RequestBuilder;
import cn.skcks.docking.gb28181.sip.method.notify.NotifyBuilder;
import gov.nist.javax.sip.header.SubscriptionState;
import gov.nist.javax.sip.message.SIPRequest;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.SneakyThrows;
import lombok.ToString;
import lombok.experimental.SuperBuilder;
import org.apache.commons.lang3.StringUtils;
import javax.sip.message.Request;
@Data
@SuperBuilder
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class NotifyRequestBuilder extends RequestBuilder implements NotifyBuilder {
@SneakyThrows
public Request createNotifyRequest(String callId, long cSeq, String event, byte[] content, String toTag, int expire) {
SIPRequest notifyRequest = (SIPRequest) createNotifyRequest(callId, cSeq, event, content, toTag);
SubscriptionState subscriptionState = (SubscriptionState) notifyRequest.getHeader(SubscriptionState.NAME);
subscriptionState.setExpires(expire);
return notifyRequest;
}
@SneakyThrows
public Request createNotifyRequest(String callId, long cSeq, String event, byte[] content, String toTag) {
SIPRequest request = (SIPRequest) createRequest(METHOD, callId, cSeq, content);
if(StringUtils.isNotBlank(toTag)){
request.getToHeader().setTag(toTag);
}
return SipBuilder.addHeaders(request,
SipBuilder.createEventHeader(event),
SipBuilder.createSubscriptionStateHeader("active"));
}
}

View File

@ -1,14 +1,19 @@
package cn.skcks.docking.gb28181.sip.process;
import cn.hutool.core.util.IdUtil;
import cn.skcks.docking.gb28181.constant.CmdType;
import cn.skcks.docking.gb28181.sip.manscdp.catalog.query.CatalogQueryDTO;
import cn.skcks.docking.gb28181.sip.manscdp.catalog.response.CatalogResponseDTO;
import cn.skcks.docking.gb28181.sip.manscdp.catalog.response.CatalogSubscribeResponseDTO;
import cn.skcks.docking.gb28181.sip.manscdp.keepalive.notify.KeepaliveNotifyDTO;
import cn.skcks.docking.gb28181.sip.method.notify.request.NotifyRequestBuilder;
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.method.subscribe.response.SubscribeResponseBuilder;
import cn.skcks.docking.gb28181.sip.utils.MANSCDPUtils;
import cn.skcks.docking.gb28181.sip.utils.SipUtil;
import gov.nist.javax.sip.message.SIPRequest;
import gov.nist.javax.sip.message.SIPResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
@ -62,6 +67,33 @@ public class RequestTest {
SubscribeResponseBuilder subscribeResponseBuilder = SubscribeResponseBuilder.builder().build();
Response subscribeResponse = subscribeResponseBuilder.createSubscribeResponse(subscribeRequest, MANSCDPUtils.toByteXml(catalogSubscribeResponseDTO));
log.info("\n{}",subscribeResponse);
// 客户端向服务端发送 事件通知
NotifyRequestBuilder notifyRequestBuilder = NotifyRequestBuilder.builder()
.localIp(localIp)
.localPort(localPort)
.localId(localId)
.targetIp(remoteIp)
.targetPort(remotePort)
.targetId(remoteId)
.transport(ListeningPoint.TCP)
.build();
KeepaliveNotifyDTO keepaliveNotifyDTO = KeepaliveNotifyDTO.builder()
.sn(String.valueOf(2))
.deviceId(localId)
.build();
Request notifyRequest = notifyRequestBuilder.createNotifyRequest(callId, 2, CmdType.KEEPALIVE, MANSCDPUtils.toByteXml(keepaliveNotifyDTO), null);
log.info("\n{}", notifyRequest);
CatalogResponseDTO catalogResponseDTO = CatalogResponseDTO.builder()
.sn(String.valueOf(2))
.deviceId(localId)
.build();
String toTag = ((SIPRequest)subscribeRequest).getFromTag();
Request catalogNotifyRequest = notifyRequestBuilder.createNotifyRequest(callId, 3, CmdType.CATALOG, MANSCDPUtils.toByteXml(catalogResponseDTO), toTag);
log.info("\n{}", catalogNotifyRequest);
}
@Test