GB28181:基于JAVA的Catalog目录获取[part3]
1、国标文件里面catalog的描述
image.png
2、java组装SIP信令
/**
* This method uses the SIP stack to send a message. 第一个参数:用户名 第二个参数:IP地址 第三个参数:设备ID
*/
public synchronized void sendCatalog(String toIp,Integer toPort, String deviceId)
throws ParseException, InvalidArgumentException, SipException {
log.info("发送 sendCatalog ");
String catalog = String.format(CATALOG_FORMAT, SN.incrementAndGet(), deviceId);
Request request = createRequest(deviceId, toIp+":"+toPort, Request.MESSAGE, null, "QQ346875436CatalogTag", "QQ346875436CatalogBranch", Request.MESSAGE,20L,null);
ContentTypeHeader contentTypeHeader = headerFactory.createContentTypeHeader("Application", "MANSCDP+xml");
request.setContent(catalog, contentTypeHeader);
sipProvider.sendRequest(request);
log.info("发送 sendCatalog 结束.....");
}
private Request createRequest(String deviceId, String toIpAddPort, String method, String callId,
String tag, String branch, String CSeqMethod, Long CSeq, String toTag)
throws ParseException, InvalidArgumentException {
SipURI from = addressFactory
.createSipURI(deviceId, ip + ":" + port);
Address fromNameAddress = addressFactory.createAddress(from);
FromHeader fromHeader = headerFactory.createFromHeader(fromNameAddress, tag);
SipURI toAddress = addressFactory.createSipURI(deviceId, toIpAddPort);
Address toNameAddress = addressFactory.createAddress(toAddress);
ToHeader toHeader = headerFactory.createToHeader(toNameAddress, toTag);
SipURI requestURI = addressFactory.createSipURI(deviceId, toIpAddPort);
ArrayList<ViaHeader> viaHeaders = new ArrayList<ViaHeader>();
ViaHeader viaHeader = headerFactory
.createViaHeader(ip, port, "udp", branch);
viaHeaders.add(viaHeader);
CallIdHeader callIdHeader = StringUtils.isEmpty(callId) ? sipProvider.getNewCallId()
: headerFactory.createCallIdHeader(callId);
CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(CSeq == null ? 1L : CSeq, CSeqMethod);
MaxForwardsHeader maxForwards = headerFactory.createMaxForwardsHeader(70);
return messageFactory.createRequest(requestURI, method, callIdHeader, cSeqHeader,
fromHeader, toHeader, viaHeaders, maxForwards);
}
3、发送的抓包信令
MESSAGE sip:34020000001110000001@192.168.1.200:5060 SIP/2.0
Call-ID: fce0e85df3e239bd013fcdd7c0e9336a@192.168.1.18
CSeq: 20 MESSAGE
From: <sip:34020000001110000001@192.168.1.18:5060>;tag=QQ346875436CatalogTag
To: <sip:34020000001110000001@192.168.1.200:5060>
Via: SIP/2.0/UDP 172.18.100.18:5060;branch=QQ136320186CatalogBranch
Max-Forwards: 70
Content-Type: Application/MANSCDP+xml
Content-Length: 123
<?xml version="1.0"?>
<Query>
<CmdType>Catalog</CmdType>
<SN>20001</SN>
<DeviceID>34020000001110000001</DeviceID>
</Query>
4、设备返回的代码处理
@Override
public void processRequest(RequestEvent requestEvent, AddressFactory addressFactory, MessageFactory messageFactory, HeaderFactory headerFactory, SipProvider sipProvider) {
Request request = requestEvent.getRequest();
if (null == request) {
log.error("processRequest RequestEvent is null");
return;
}
switch (request.getMethod().toUpperCase()) {
case Request.MESSAGE:
log.debug("收到MESSAGE的请求");
doRequestMessage(requestEvent, addressFactory,messageFactory, headerFactory, sipProvider);
break;
default:
log.info("不处理的requestMethod:" + cSeqHeader.getMethod().toUpperCase());
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void doRequestMessage(RequestEvent requestEvent, AddressFactory addressFactory, MessageFactory messageFactory, HeaderFactory headerFactory, SipProvider sipProvider) {
try {
Request request = requestEvent.getRequest();
String encode = request.toString();
if (org.apache.commons.lang3.StringUtils.contains(encode, MST_TYPE_KEEPALIVE)) {
log.debug("收到下级域发来的心跳请求,{}", request.getRequestURI());
doSuccess(requestEvent, addressFactory, messageFactory, headerFactory, sipProvider);
} else if (org.apache.commons.lang3.StringUtils.contains(encode, MST_TYPE_CATALOG)) {
ViaHeader viaHeader = (ViaHeader) request.getHeader(ViaHeader.NAME);
log.info("收到目录检索返回,IP={},Port={}", viaHeader.getReceived(), viaHeader.getRPort());
doSuccess(requestEvent, addressFactory, messageFactory, headerFactory, sipProvider);
doRequestCatalog(requestEvent.getRequest(),encode);
}
}catch (Exception e) {
log.error("doRequestMessage Error:{}",e.getMessage());
e.printStackTrace();
}
}
3、源码
源码:给个star吧https://gitee.com/yuntian_admin/srymy
网友评论