欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

GB28181:基于JAVA的Catalog目录获取[part3]

程序员文章站 2022-03-17 14:07:27
...

GB28181:基于JAVA的Catalog目录获取[part3]

1、国标文件里面catalog的描述

GB28181:基于JAVA的Catalog目录获取[part3]

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, "QQ136320186CatalogTag", "QQ136320186CatalogBranch", 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:aaa@qq.com:5060 SIP/2.0
Call-ID: aaa@qq.com
CSeq: 20 MESSAGE
From: <sip:aaa@qq.com:5060>;tag=QQ136320186CatalogTag
To: <sip:aaa@qq.com: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
扣 群 号:

5_4_1_4_1_0_1_3_2

相关标签: GB28181 SIP JAVA