From 91bfbc36f10382434a9e8c79eeeaf07a531b08b5 Mon Sep 17 00:00:00 2001 From: 648540858 <648540858@qq.com> Date: 星期二, 28 三月 2023 14:09:41 +0800 Subject: [PATCH] 优化设备注册,支持到期续订,优化国标级联到期续订。 --- src/main/java/com/genersoft/iot/vmp/gb28181/bean/Device.java | 11 ++ src/main/java/com/genersoft/iot/vmp/service/impl/DeviceServiceImpl.java | 10 + src/main/java/com/genersoft/iot/vmp/gb28181/task/SipRunner.java | 2 src/main/java/com/genersoft/iot/vmp/service/impl/PlatformServiceImpl.java | 43 +++++--- src/main/java/com/genersoft/iot/vmp/gb28181/bean/ParentPlatformCatch.java | 10 ++ src/main/java/com/genersoft/iot/vmp/conf/SipPlatformRunner.java | 9 + src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/response/cmd/DeviceStatusResponseMessageHandler.java | 2 src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/SIPRequestHeaderPlarformProvider.java | 19 +-- src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/RegisterRequestProcessor.java | 52 +++++++++ pom.xml | 2 src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/KeepaliveNotifyMessageHandler.java | 2 src/main/java/com/genersoft/iot/vmp/service/IDeviceService.java | 3 src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/platform/PlatformController.java | 7 + src/main/java/com/genersoft/iot/vmp/service/IPlatformService.java | 3 src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookListener.java | 2 src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/control/cmd/DeviceControlQueryMessageHandler.java | 61 ++++++----- src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommanderFroPlatform.java | 37 +++++- src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/response/impl/RegisterResponseProcessor.java | 14 +- src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/ISIPCommanderForPlatform.java | 6 19 files changed, 205 insertions(+), 90 deletions(-) diff --git a/pom.xml b/pom.xml index a675c6f..7cf44e7 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ <groupId>com.genersoft</groupId> <artifactId>wvp-pro</artifactId> - <version>2.6.7</version> + <version>2.6.8</version> <name>web video platform</name> <description>鍥芥爣28181瑙嗛骞冲彴</description> <packaging>${project.packaging}</packaging> diff --git a/src/main/java/com/genersoft/iot/vmp/conf/SipPlatformRunner.java b/src/main/java/com/genersoft/iot/vmp/conf/SipPlatformRunner.java index b3f144a..0a8405b 100644 --- a/src/main/java/com/genersoft/iot/vmp/conf/SipPlatformRunner.java +++ b/src/main/java/com/genersoft/iot/vmp/conf/SipPlatformRunner.java @@ -40,17 +40,20 @@ List<ParentPlatform> parentPlatforms = storager.queryEnableParentPlatformList(true); for (ParentPlatform parentPlatform : parentPlatforms) { + + ParentPlatformCatch parentPlatformCatchOld = redisCatchStorage.queryPlatformCatchInfo(parentPlatform.getServerGBId()); + // 鏇存柊缂撳瓨 ParentPlatformCatch parentPlatformCatch = new ParentPlatformCatch(); parentPlatformCatch.setParentPlatform(parentPlatform); parentPlatformCatch.setId(parentPlatform.getServerGBId()); redisCatchStorage.updatePlatformCatchInfo(parentPlatformCatch); - // 璁剧疆鎵�鏈夊钩鍙扮绾� - platformService.offline(parentPlatform, true); // 鍙栨秷璁㈤槄 - sipCommanderForPlatform.unregister(parentPlatform, null, (eventResult)->{ + sipCommanderForPlatform.unregister(parentPlatform, parentPlatformCatchOld.getSipTransactionInfo(), null, (eventResult)->{ platformService.login(parentPlatform); }); + // 璁剧疆鎵�鏈夊钩鍙扮绾� + platformService.offline(parentPlatform, true); } } } diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/bean/Device.java b/src/main/java/com/genersoft/iot/vmp/gb28181/bean/Device.java index 83b9c6a..9093970 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/bean/Device.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/bean/Device.java @@ -191,6 +191,9 @@ @Schema(description = "鏄惁浣滀负娑堟伅閫氶亾") private boolean asMessageChannel; + @Schema(description = "璁惧娉ㄥ唽鐨勪簨鍔′俊鎭�") + private SipTransactionInfo sipTransactionInfo; + public String getDeviceId() { return deviceId; @@ -439,4 +442,12 @@ public void setAsMessageChannel(boolean asMessageChannel) { this.asMessageChannel = asMessageChannel; } + + public SipTransactionInfo getSipTransactionInfo() { + return sipTransactionInfo; + } + + public void setSipTransactionInfo(SipTransactionInfo sipTransactionInfo) { + this.sipTransactionInfo = sipTransactionInfo; + } } diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/bean/ParentPlatformCatch.java b/src/main/java/com/genersoft/iot/vmp/gb28181/bean/ParentPlatformCatch.java index a53d26e..6ff2fe3 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/bean/ParentPlatformCatch.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/bean/ParentPlatformCatch.java @@ -16,6 +16,8 @@ private ParentPlatform parentPlatform; + private SipTransactionInfo sipTransactionInfo; + public String getId() { return id; } @@ -55,4 +57,12 @@ public void setCallId(String callId) { this.callId = callId; } + + public SipTransactionInfo getSipTransactionInfo() { + return sipTransactionInfo; + } + + public void setSipTransactionInfo(SipTransactionInfo sipTransactionInfo) { + this.sipTransactionInfo = sipTransactionInfo; + } } diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/task/SipRunner.java b/src/main/java/com/genersoft/iot/vmp/gb28181/task/SipRunner.java index 25f07f5..3352838 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/task/SipRunner.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/task/SipRunner.java @@ -63,7 +63,7 @@ if (deviceService.expire(device)){ deviceService.offline(device.getDeviceId(), "娉ㄥ唽宸茶繃鏈�"); }else { - deviceService.online(device); + deviceService.online(device, null); } } // 閲嶇疆cseq璁℃暟 diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/ISIPCommanderForPlatform.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/ISIPCommanderForPlatform.java index 0425356..08d5b2e 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/ISIPCommanderForPlatform.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/ISIPCommanderForPlatform.java @@ -18,14 +18,16 @@ * @return */ void register(ParentPlatform parentPlatform, SipSubscribe.Event errorEvent , SipSubscribe.Event okEvent) throws InvalidArgumentException, ParseException, SipException; - void register(ParentPlatform parentPlatform, String callId, WWWAuthenticateHeader www, SipSubscribe.Event errorEvent , SipSubscribe.Event okEvent, boolean registerAgain, boolean isRegister) throws SipException, InvalidArgumentException, ParseException; + + void register(ParentPlatform parentPlatform, SipTransactionInfo sipTransactionInfo, SipSubscribe.Event errorEvent , SipSubscribe.Event okEvent) throws InvalidArgumentException, ParseException, SipException; + void register(ParentPlatform parentPlatform, SipTransactionInfo sipTransactionInfo, WWWAuthenticateHeader www, SipSubscribe.Event errorEvent , SipSubscribe.Event okEvent, boolean registerAgain, boolean isRegister) throws SipException, InvalidArgumentException, ParseException; /** * 鍚戜笂绾у钩鍙版敞閿� * @param parentPlatform * @return */ - void unregister(ParentPlatform parentPlatform, SipSubscribe.Event errorEvent , SipSubscribe.Event okEvent) throws InvalidArgumentException, ParseException, SipException; + void unregister(ParentPlatform parentPlatform, SipTransactionInfo sipTransactionInfo, SipSubscribe.Event errorEvent , SipSubscribe.Event okEvent) throws InvalidArgumentException, ParseException, SipException; /** diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/SIPRequestHeaderPlarformProvider.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/SIPRequestHeaderPlarformProvider.java index 0fe11c0..86da536 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/SIPRequestHeaderPlarformProvider.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/SIPRequestHeaderPlarformProvider.java @@ -14,7 +14,8 @@ import org.springframework.stereotype.Component; import org.springframework.util.DigestUtils; -import javax.sip.*; +import javax.sip.InvalidArgumentException; +import javax.sip.PeerUnavailableException; import javax.sip.address.Address; import javax.sip.address.SipURI; import javax.sip.header.*; @@ -22,7 +23,6 @@ import javax.validation.constraints.NotNull; import java.text.ParseException; import java.util.ArrayList; -import java.util.List; import java.util.UUID; /** @@ -45,7 +45,7 @@ @Autowired private IRedisCatchStorage redisCatchStorage; - public Request createRegisterRequest(@NotNull ParentPlatform parentPlatform, long CSeq, String fromTag, String viaTag, CallIdHeader callIdHeader, boolean isRegister) throws ParseException, InvalidArgumentException, PeerUnavailableException { + public Request createRegisterRequest(@NotNull ParentPlatform parentPlatform, long CSeq, String fromTag, String toTag, CallIdHeader callIdHeader, boolean isRegister) throws ParseException, InvalidArgumentException, PeerUnavailableException { Request request = null; String sipAddress = parentPlatform.getDeviceIp() + ":" + parentPlatform.getDevicePort(); //璇锋眰琛� @@ -53,7 +53,8 @@ parentPlatform.getServerIP() + ":" + parentPlatform.getServerPort()); //via ArrayList<ViaHeader> viaHeaders = new ArrayList<ViaHeader>(); - ViaHeader viaHeader = sipLayer.getSipFactory().createHeaderFactory().createViaHeader(parentPlatform.getServerIP(), parentPlatform.getServerPort(), parentPlatform.getTransport(), viaTag); + ViaHeader viaHeader = sipLayer.getSipFactory().createHeaderFactory().createViaHeader(parentPlatform.getServerIP(), + parentPlatform.getServerPort(), parentPlatform.getTransport(), SipUtils.getNewViaTag()); viaHeader.setRPort(); viaHeaders.add(viaHeader); //from @@ -63,7 +64,7 @@ //to SipURI toSipURI = sipLayer.getSipFactory().createAddressFactory().createSipURI(parentPlatform.getDeviceGBId(), sipConfig.getDomain()); Address toAddress = sipLayer.getSipFactory().createAddressFactory().createAddress(toSipURI); - ToHeader toHeader = sipLayer.getSipFactory().createHeaderFactory().createToHeader(toAddress,null); + ToHeader toHeader = sipLayer.getSipFactory().createHeaderFactory().createToHeader(toAddress,toTag); //Forwards MaxForwardsHeader maxForwards = sipLayer.getSipFactory().createHeaderFactory().createMaxForwardsHeader(70); @@ -85,11 +86,11 @@ return request; } - public Request createRegisterRequest(@NotNull ParentPlatform parentPlatform, String fromTag, String viaTag, - String callId, WWWAuthenticateHeader www , CallIdHeader callIdHeader, boolean isRegister) throws ParseException, PeerUnavailableException, InvalidArgumentException { + public Request createRegisterRequest(@NotNull ParentPlatform parentPlatform, String fromTag, String toTag, + WWWAuthenticateHeader www , CallIdHeader callIdHeader, boolean isRegister) throws ParseException, PeerUnavailableException, InvalidArgumentException { - Request registerRequest = createRegisterRequest(parentPlatform, redisCatchStorage.getCSEQ(), fromTag, viaTag, callIdHeader, isRegister); + Request registerRequest = createRegisterRequest(parentPlatform, redisCatchStorage.getCSEQ(), fromTag, toTag, callIdHeader, isRegister); SipURI requestURI = sipLayer.getSipFactory().createAddressFactory().createSipURI(parentPlatform.getServerGBId(), parentPlatform.getServerIP() + ":" + parentPlatform.getServerPort()); if (www == null) { AuthorizationHeader authorizationHeader = sipLayer.getSipFactory().createHeaderFactory().createAuthorizationHeader("Digest"); @@ -106,8 +107,6 @@ // 鍙傝�� https://blog.csdn.net/y673533511/article/details/88388138 // qop 淇濇姢璐ㄩ噺 鍖呭惈auth锛堥粯璁ょ殑锛夊拰auth-int锛堝鍔犱簡鎶ユ枃瀹屾暣鎬ф娴嬶級涓ょ绛栫暐 String qop = www.getQop(); - - callIdHeader.setCallId(callId); String cNonce = null; String nc = "00000001"; diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommanderFroPlatform.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommanderFroPlatform.java index dc5c31a..af31818 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommanderFroPlatform.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommanderFroPlatform.java @@ -75,20 +75,40 @@ } @Override - public void unregister(ParentPlatform parentPlatform, SipSubscribe.Event errorEvent , SipSubscribe.Event okEvent) throws InvalidArgumentException, ParseException, SipException { - register(parentPlatform, null, null, errorEvent, okEvent, false, false); + public void register(ParentPlatform parentPlatform, SipTransactionInfo sipTransactionInfo, SipSubscribe.Event errorEvent , SipSubscribe.Event okEvent) throws InvalidArgumentException, ParseException, SipException { + + register(parentPlatform, sipTransactionInfo, null, errorEvent, okEvent, false, true); } @Override - public void register(ParentPlatform parentPlatform, @Nullable String callId, @Nullable WWWAuthenticateHeader www, + public void unregister(ParentPlatform parentPlatform, SipTransactionInfo sipTransactionInfo, SipSubscribe.Event errorEvent , SipSubscribe.Event okEvent) throws InvalidArgumentException, ParseException, SipException { + register(parentPlatform, sipTransactionInfo, null, errorEvent, okEvent, false, false); + } + + @Override + public void register(ParentPlatform parentPlatform, @Nullable SipTransactionInfo sipTransactionInfo, @Nullable WWWAuthenticateHeader www, SipSubscribe.Event errorEvent , SipSubscribe.Event okEvent, boolean registerAgain, boolean isRegister) throws SipException, InvalidArgumentException, ParseException { Request request; - if (!registerAgain ) { - CallIdHeader callIdHeader = sipSender.getNewCallIdHeader(parentPlatform.getDeviceIp(),parentPlatform.getTransport()); + CallIdHeader callIdHeader = sipSender.getNewCallIdHeader(parentPlatform.getDeviceIp(),parentPlatform.getTransport()); + String fromTag = SipUtils.getNewFromTag(); + String toTag = null; + if (sipTransactionInfo != null ) { + if (sipTransactionInfo.getCallId() != null) { + callIdHeader.setCallId(sipTransactionInfo.getCallId()); + } + if (sipTransactionInfo.getFromTag() != null) { + fromTag = sipTransactionInfo.getFromTag(); + } + if (sipTransactionInfo.getToTag() != null) { + toTag = sipTransactionInfo.getToTag(); + } + } + + if (!registerAgain ) { request = headerProviderPlatformProvider.createRegisterRequest(parentPlatform, - redisCatchStorage.getCSEQ(), SipUtils.getNewFromTag(), - SipUtils.getNewViaTag(), callIdHeader, isRegister); + redisCatchStorage.getCSEQ(), fromTag, + toTag, callIdHeader, isRegister); // 灏� callid 鍐欏叆缂撳瓨锛� 绛夋敞鍐屾垚鍔熷彲浠ユ洿鏂扮姸鎬� String callIdFromHeader = callIdHeader.getCallId(); redisCatchStorage.updatePlatformRegisterInfo(callIdFromHeader, PlatformRegisterInfo.getInstance(parentPlatform.getServerGBId(), isRegister)); @@ -106,8 +126,7 @@ }); }else { - CallIdHeader callIdHeader = sipSender.getNewCallIdHeader(parentPlatform.getDeviceIp(),parentPlatform.getTransport()); - request = headerProviderPlatformProvider.createRegisterRequest(parentPlatform, SipUtils.getNewFromTag(), null, callId, www, callIdHeader, isRegister); + request = headerProviderPlatformProvider.createRegisterRequest(parentPlatform, fromTag, toTag, www, callIdHeader, isRegister); } sipSender.transmitRequest(parentPlatform.getDeviceIp(), request, null, okEvent); diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/RegisterRequestProcessor.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/RegisterRequestProcessor.java index d307941..72da660 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/RegisterRequestProcessor.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/RegisterRequestProcessor.java @@ -5,6 +5,7 @@ import com.genersoft.iot.vmp.gb28181.auth.DigestServerAuthenticationHelper; import com.genersoft.iot.vmp.gb28181.bean.Device; import com.genersoft.iot.vmp.gb28181.bean.RemoteAddressInfo; +import com.genersoft.iot.vmp.gb28181.bean.SipTransactionInfo; import com.genersoft.iot.vmp.gb28181.bean.WvpSipDate; import com.genersoft.iot.vmp.gb28181.transmit.SIPProcessorObserver; import com.genersoft.iot.vmp.gb28181.transmit.SIPSender; @@ -18,6 +19,7 @@ import gov.nist.javax.sip.address.SipUri; import gov.nist.javax.sip.header.SIPDateHeader; import gov.nist.javax.sip.message.SIPRequest; +import gov.nist.javax.sip.message.SIPResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.InitializingBean; @@ -31,6 +33,7 @@ import javax.sip.header.ContactHeader; import javax.sip.header.FromHeader; import javax.sip.header.ViaHeader; +import javax.sip.message.Request; import javax.sip.message.Response; import java.security.NoSuchAlgorithmException; import java.text.ParseException; @@ -102,6 +105,30 @@ SipUri uri = (SipUri) address.getURI(); String deviceId = uri.getUser(); Device device = deviceService.getDevice(deviceId); + + RemoteAddressInfo remoteAddressInfo = SipUtils.getRemoteAddressFromRequest(request, + userSetting.getSipUseSourceIpAsRemoteAddress()); + + if (device != null && + device.getSipTransactionInfo() != null && + request.getCallIdHeader().getCallId().equals(device.getSipTransactionInfo().getCallId())) { + logger.info("[娉ㄥ唽璇锋眰] 娉ㄥ唽缁: {}", device.getDeviceId()); + device.setExpires(request.getExpires().getExpires()); + device.setIp(remoteAddressInfo.getIp()); + device.setPort(remoteAddressInfo.getPort()); + device.setHostAddress(remoteAddressInfo.getIp().concat(":").concat(String.valueOf(remoteAddressInfo.getPort()))); + device.setLocalIp(request.getLocalAddress().getHostAddress()); + Response registerOkResponse = getRegisterOkResponse(request); + // 鍒ゆ柇TCP杩樻槸UDP + ViaHeader reqViaHeader = (ViaHeader) request.getHeader(ViaHeader.NAME); + String transport = reqViaHeader.getTransport(); + device.setTransport("TCP".equalsIgnoreCase(transport) ? "TCP" : "UDP"); + sipSender.transmitRequest(request.getLocalAddress().getHostAddress(), registerOkResponse); + device.setRegisterTime(DateUtil.getNow()); + SipTransactionInfo sipTransactionInfo = new SipTransactionInfo((SIPResponse)registerOkResponse); + deviceService.online(device, sipTransactionInfo); + return; + } String password = (device != null && !ObjectUtils.isEmpty(device.getPassword()))? device.getPassword() : sipConfig.getPassword(); AuthorizationHeader authHead = (AuthorizationHeader) request.getHeader(AuthorizationHeader.NAME); if (authHead == null && !ObjectUtils.isEmpty(password)) { @@ -144,9 +171,6 @@ // 娣诲姞Expires澶� response.addHeader(request.getExpires()); - RemoteAddressInfo remoteAddressInfo = SipUtils.getRemoteAddressFromRequest(request, - userSetting.getSipUseSourceIpAsRemoteAddress()); - if (device == null) { device = new Device(); device.setStreamMode("UDP"); @@ -179,7 +203,8 @@ if (registerFlag) { logger.info("[娉ㄥ唽鎴愬姛] deviceId: {}->{}", deviceId, requestAddress); device.setRegisterTime(DateUtil.getNow()); - deviceService.online(device); + SipTransactionInfo sipTransactionInfo = new SipTransactionInfo((SIPResponse)response); + deviceService.online(device, sipTransactionInfo); } else { logger.info("[娉ㄩ攢鎴愬姛] deviceId: {}->{}" ,deviceId, requestAddress); deviceService.offline(deviceId, "涓诲姩娉ㄩ攢"); @@ -188,4 +213,23 @@ logger.error("鏈鐞嗙殑寮傚父 ", e); } } + + private Response getRegisterOkResponse(Request request) throws ParseException { + // 鎼哄甫鎺堟潈澶村苟涓斿瘑鐮佹纭� + Response response = getMessageFactory().createResponse(Response.OK, request); + // 娣诲姞date澶� + SIPDateHeader dateHeader = new SIPDateHeader(); + // 浣跨敤鑷繁淇敼鐨� + WvpSipDate wvpSipDate = new WvpSipDate(Calendar.getInstance(Locale.ENGLISH).getTimeInMillis()); + dateHeader.setDate(wvpSipDate); + response.addHeader(dateHeader); + + // 娣诲姞Contact澶� + response.addHeader(request.getHeader(ContactHeader.NAME)); + // 娣诲姞Expires澶� + response.addHeader(request.getExpires()); + + return response; + + } } diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/control/cmd/DeviceControlQueryMessageHandler.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/control/cmd/DeviceControlQueryMessageHandler.java index 4ac83de..b2dd76b 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/control/cmd/DeviceControlQueryMessageHandler.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/control/cmd/DeviceControlQueryMessageHandler.java @@ -73,35 +73,38 @@ String channelId = getText(rootElement, "DeviceID"); // 杩滅▼鍚姩鍔熻兘 if (!ObjectUtils.isEmpty(getText(rootElement, "TeleBoot"))) { - if (parentPlatform.getServerGBId().equals(targetGBId)) { - // 杩滅▼鍚姩鏈钩鍙帮細闇�瑕佸湪閲嶆柊鍚姩绋嬪簭鍚庡厛瀵筍ipStack瑙g粦 - logger.info("鎵ц杩滅▼鍚姩鏈钩鍙板懡浠�"); - try { - cmderFroPlatform.unregister(parentPlatform, null, null); - } catch (InvalidArgumentException | ParseException | SipException e) { - logger.error("[鍛戒护鍙戦�佸け璐 鍥芥爣绾ц仈 娉ㄩ攢: {}", e.getMessage()); - } - taskExecutor.execute(() -> { - // 杩滅▼鍚姩 -// try { -// Thread.sleep(3000); -// SipProvider up = (SipProvider) SpringBeanFactory.getBean("udpSipProvider"); -// SipStackImpl stack = (SipStackImpl)up.getSipStack(); -// stack.stop(); -// Iterator listener = stack.getListeningPoints(); -// while (listener.hasNext()) { -// stack.deleteListeningPoint((ListeningPoint) listener.next()); -// } -// Iterator providers = stack.getSipProviders(); -// while (providers.hasNext()) { -// stack.deleteSipProvider((SipProvider) providers.next()); -// } -// VManageBootstrap.restart(); -// } catch (InterruptedException | ObjectInUseException e) { -// logger.error("[浠诲姟鎵ц澶辫触] 鏈嶅姟閲嶅惎: {}", e.getMessage()); -// } - }); - } + // TODO 鎷掔粷杩滅▼鍚姩鍛戒护 + logger.warn("[鍥芥爣绾ц仈]鏀跺埌骞冲彴鐨勮繙绋嬪惎鍔ㄥ懡浠わ紝 涓嶅鐞�"); + +// if (parentPlatform.getServerGBId().equals(targetGBId)) { +// // 杩滅▼鍚姩鏈钩鍙帮細闇�瑕佸湪閲嶆柊鍚姩绋嬪簭鍚庡厛瀵筍ipStack瑙g粦 +// logger.info("鎵ц杩滅▼鍚姩鏈钩鍙板懡浠�"); +// try { +// cmderFroPlatform.unregister(parentPlatform, null, null); +// } catch (InvalidArgumentException | ParseException | SipException e) { +// logger.error("[鍛戒护鍙戦�佸け璐 鍥芥爣绾ц仈 娉ㄩ攢: {}", e.getMessage()); +// } +// taskExecutor.execute(() -> { +// // 杩滅▼鍚姩 +//// try { +//// Thread.sleep(3000); +//// SipProvider up = (SipProvider) SpringBeanFactory.getBean("udpSipProvider"); +//// SipStackImpl stack = (SipStackImpl)up.getSipStack(); +//// stack.stop(); +//// Iterator listener = stack.getListeningPoints(); +//// while (listener.hasNext()) { +//// stack.deleteListeningPoint((ListeningPoint) listener.next()); +//// } +//// Iterator providers = stack.getSipProviders(); +//// while (providers.hasNext()) { +//// stack.deleteSipProvider((SipProvider) providers.next()); +//// } +//// VManageBootstrap.restart(); +//// } catch (InterruptedException | ObjectInUseException e) { +//// logger.error("[浠诲姟鎵ц澶辫触] 鏈嶅姟閲嶅惎: {}", e.getMessage()); +//// } +// }); +// } } DeviceControlType deviceControlType = DeviceControlType.typeOf(rootElement); logger.info("[鎺ュ彈deviceControl鍛戒护] 鍛戒护: {}", deviceControlType); diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/KeepaliveNotifyMessageHandler.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/KeepaliveNotifyMessageHandler.java index b6c27ab..865b662 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/KeepaliveNotifyMessageHandler.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/KeepaliveNotifyMessageHandler.java @@ -88,7 +88,7 @@ // 瀵逛簬宸茬粡绂荤嚎鐨勮澶囧垽鏂粬鐨勬敞鍐屾槸鍚﹀凡缁忚繃鏈� if (!deviceService.expire(device)){ device.setOnline(0); - deviceService.online(device); + deviceService.online(device, null); } } // 鍒锋柊杩囨湡浠诲姟 diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/response/cmd/DeviceStatusResponseMessageHandler.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/response/cmd/DeviceStatusResponseMessageHandler.java index 7dee8dc..da4743f 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/response/cmd/DeviceStatusResponseMessageHandler.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/response/cmd/DeviceStatusResponseMessageHandler.java @@ -71,7 +71,7 @@ } String text = onlineElement.getText(); if ("ONLINE".equalsIgnoreCase(text.trim())) { - deviceService.online(device); + deviceService.online(device, null); }else { deviceService.offline(device.getDeviceId(), "璁惧鐘舵�佹煡璇㈢粨鏋滐細" + text.trim()); } diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/response/impl/RegisterResponseProcessor.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/response/impl/RegisterResponseProcessor.java index 14d1f84..0294ba2 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/response/impl/RegisterResponseProcessor.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/response/impl/RegisterResponseProcessor.java @@ -2,6 +2,7 @@ import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform; import com.genersoft.iot.vmp.gb28181.bean.ParentPlatformCatch; +import com.genersoft.iot.vmp.gb28181.bean.SipTransactionInfo; import com.genersoft.iot.vmp.gb28181.bean.SubscribeHolder; import com.genersoft.iot.vmp.gb28181.transmit.SIPProcessorObserver; import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommanderForPlatform; @@ -10,6 +11,7 @@ import com.genersoft.iot.vmp.storager.IRedisCatchStorage; import com.genersoft.iot.vmp.storager.IVideoManagerStorage; import com.genersoft.iot.vmp.storager.dao.dto.PlatformRegisterInfo; +import gov.nist.javax.sip.message.SIPResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -18,7 +20,6 @@ import javax.sip.InvalidArgumentException; import javax.sip.ResponseEvent; import javax.sip.SipException; -import javax.sip.header.CallIdHeader; import javax.sip.header.WWWAuthenticateHeader; import javax.sip.message.Response; import java.text.ParseException; @@ -65,9 +66,8 @@ */ @Override public void process(ResponseEvent evt) { - Response response = evt.getResponse(); - CallIdHeader callIdHeader = (CallIdHeader) response.getHeader(CallIdHeader.NAME); - String callId = callIdHeader.getCallId(); + SIPResponse response = (SIPResponse)evt.getResponse(); + String callId = response.getCallIdHeader().getCallId(); PlatformRegisterInfo platformRegisterInfo = redisCatchStorage.queryPlatformRegisterInfo(callId); if (platformRegisterInfo == null) { logger.info(String.format("[鍥芥爣绾ц仈]鏈壘鍒癱allId锛� %s 鐨勬敞鍐�/娉ㄩ攢骞冲彴id", callId )); @@ -90,15 +90,17 @@ if (response.getStatusCode() == Response.UNAUTHORIZED) { WWWAuthenticateHeader www = (WWWAuthenticateHeader)response.getHeader(WWWAuthenticateHeader.NAME); + SipTransactionInfo sipTransactionInfo = new SipTransactionInfo(response); try { - sipCommanderForPlatform.register(parentPlatform, callId, www, null, null, true, platformRegisterInfo.isRegister()); + sipCommanderForPlatform.register(parentPlatform, sipTransactionInfo, www, null, null, true, platformRegisterInfo.isRegister()); } catch (SipException | InvalidArgumentException | ParseException e) { logger.error("[鍛戒护鍙戦�佸け璐 鍥芥爣绾ц仈 鍐嶆娉ㄥ唽: {}", e.getMessage()); } }else if (response.getStatusCode() == Response.OK){ if (platformRegisterInfo.isRegister()) { - platformService.online(parentPlatform); + SipTransactionInfo sipTransactionInfo = new SipTransactionInfo(response); + platformService.online(parentPlatform, sipTransactionInfo); }else { platformService.offline(parentPlatform, false); } diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookListener.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookListener.java index ca5bb13..f2eed11 100644 --- a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookListener.java +++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookListener.java @@ -116,7 +116,7 @@ @PostMapping(value = "/on_server_keepalive", produces = "application/json;charset=UTF-8") public HookResult onServerKeepalive(@RequestBody OnServerKeepaliveHookParam param) { - logger.info("[ZLM HOOK] 鏀跺埌zlm蹇冭烦锛�" + param.getMediaServerId()); +// logger.info("[ZLM HOOK] 鏀跺埌zlm蹇冭烦锛�" + param.getMediaServerId()); taskExecutor.execute(() -> { List<ZlmHttpHookSubscribe.Event> subscribes = this.subscribe.getSubscribes(HookType.on_server_keepalive); diff --git a/src/main/java/com/genersoft/iot/vmp/service/IDeviceService.java b/src/main/java/com/genersoft/iot/vmp/service/IDeviceService.java index 5ef4ef4..d977c47 100644 --- a/src/main/java/com/genersoft/iot/vmp/service/IDeviceService.java +++ b/src/main/java/com/genersoft/iot/vmp/service/IDeviceService.java @@ -2,6 +2,7 @@ import com.genersoft.iot.vmp.gb28181.bean.Device; import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel; +import com.genersoft.iot.vmp.gb28181.bean.SipTransactionInfo; import com.genersoft.iot.vmp.gb28181.bean.SyncStatus; import com.genersoft.iot.vmp.vmanager.bean.BaseTree; import com.genersoft.iot.vmp.vmanager.bean.ResourceBaceInfo; @@ -18,7 +19,7 @@ * 璁惧涓婄嚎 * @param device 璁惧淇℃伅 */ - void online(Device device); + void online(Device device, SipTransactionInfo sipTransactionInfo); /** * 璁惧涓嬬嚎 diff --git a/src/main/java/com/genersoft/iot/vmp/service/IPlatformService.java b/src/main/java/com/genersoft/iot/vmp/service/IPlatformService.java index 12a03b4..e9cddff 100644 --- a/src/main/java/com/genersoft/iot/vmp/service/IPlatformService.java +++ b/src/main/java/com/genersoft/iot/vmp/service/IPlatformService.java @@ -1,6 +1,7 @@ package com.genersoft.iot.vmp.service; import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform; +import com.genersoft.iot.vmp.gb28181.bean.SipTransactionInfo; import com.github.pagehelper.PageInfo; /** @@ -35,7 +36,7 @@ * 骞冲彴涓婄嚎 * @param parentPlatform 骞冲彴淇℃伅 */ - void online(ParentPlatform parentPlatform); + void online(ParentPlatform parentPlatform, SipTransactionInfo sipTransactionInfo); /** * 骞冲彴绂荤嚎 diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/DeviceServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/DeviceServiceImpl.java index 91b4805..a3dd6a7 100644 --- a/src/main/java/com/genersoft/iot/vmp/service/impl/DeviceServiceImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/service/impl/DeviceServiceImpl.java @@ -89,7 +89,7 @@ private IMediaServerService mediaServerService; @Override - public void online(Device device) { + public void online(Device device, SipTransactionInfo sipTransactionInfo) { logger.info("[璁惧涓婄嚎] deviceId锛歿}->{}:{}", device.getDeviceId(), device.getIp(), device.getPort()); Device deviceInRedis = redisCatchStorage.getDevice(device.getDeviceId()); Device deviceInDb = deviceMapper.getDeviceByDeviceId(device.getDeviceId()); @@ -104,6 +104,14 @@ // 榛樿蹇冭烦闂撮殧60 device.setKeepaliveIntervalTime(60); } + if (sipTransactionInfo != null) { + device.setSipTransactionInfo(sipTransactionInfo); + }else { + if (deviceInRedis != null) { + device.setSipTransactionInfo(deviceInRedis.getSipTransactionInfo()); + } + } + // 绗竴娆′笂绾� 鎴栧垯璁惧涔嬪墠鏄绾跨姸鎬�--杩涜閫氶亾鍚屾鍜岃澶囦俊鎭煡璇� if (device.getCreateTime() == null) { device.setOnline(1); diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/PlatformServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/PlatformServiceImpl.java index d7d9f6c..a173557 100644 --- a/src/main/java/com/genersoft/iot/vmp/service/impl/PlatformServiceImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/service/impl/PlatformServiceImpl.java @@ -123,8 +123,10 @@ @Override public boolean update(ParentPlatform parentPlatform) { + logger.info("[鍥芥爣绾ц仈]鏇存柊骞冲彴 {}", parentPlatform.getDeviceGBId()); parentPlatform.setCharacterSet(parentPlatform.getCharacterSet().toUpperCase()); ParentPlatform parentPlatformOld = platformMapper.getParentPlatById(parentPlatform.getId()); + ParentPlatformCatch parentPlatformCatchOld = redisCatchStorage.queryPlatformCatchInfo(parentPlatformOld.getServerGBId()); parentPlatform.setUpdateTime(DateUtil.getNow()); if (!parentPlatformOld.getTreeType().equals(parentPlatform.getTreeType())) { // 鐩綍缁撴瀯鍙戠敓鍙樺寲锛屾竻绌轰箣鍓嶇殑鍏宠仈鍏崇郴 @@ -134,6 +136,7 @@ platformGbStreamMapper.delByPlatformId(parentPlatformOld.getServerGBId()); } + // 鍋滄蹇冭烦瀹氭椂 final String keepaliveTaskKey = KEEPALIVE_KEY_PREFIX + parentPlatformOld.getServerGBId(); dynamicTask.stop(keepaliveTaskKey); @@ -142,9 +145,13 @@ dynamicTask.stop(registerTaskKey); // 娉ㄩ攢鏃х殑 try { - commanderForPlatform.unregister(parentPlatformOld, null, eventResult -> { - logger.info("[鍥芥爣绾ц仈] 娉ㄩ攢鎴愬姛锛� 骞冲彴锛歿}", parentPlatformOld.getServerGBId()); - }); + if (parentPlatformOld.isStatus()) { + logger.info("淇濆瓨骞冲彴{}鏃跺彂鐜版晳骞冲彴鍦ㄧ嚎锛屽彂閫佹敞閿�鍛戒护", parentPlatform.getDeviceGBId()); + commanderForPlatform.unregister(parentPlatformOld, parentPlatformCatchOld.getSipTransactionInfo(), null, eventResult -> { + logger.info("[鍥芥爣绾ц仈] 娉ㄩ攢鎴愬姛锛� 骞冲彴锛歿}", parentPlatformOld.getServerGBId()); + }); + } + } catch (InvalidArgumentException | ParseException | SipException e) { logger.error("[鍛戒护鍙戦�佸け璐 鍥芥爣绾ц仈 娉ㄩ攢: {}", e.getMessage()); } @@ -185,36 +192,36 @@ @Override - public void online(ParentPlatform parentPlatform) { - logger.info("[鍥芥爣绾ц仈]锛歿}, 骞冲彴涓婄嚎/鏇存柊娉ㄥ唽", parentPlatform.getServerGBId()); + public void online(ParentPlatform parentPlatform, SipTransactionInfo sipTransactionInfo) { + logger.info("[鍥芥爣绾ц仈]锛歿}, 骞冲彴涓婄嚎", parentPlatform.getServerGBId()); platformMapper.updateParentPlatformStatus(parentPlatform.getServerGBId(), true); ParentPlatformCatch parentPlatformCatch = redisCatchStorage.queryPlatformCatchInfo(parentPlatform.getServerGBId()); - if (parentPlatformCatch != null) { - parentPlatformCatch.getParentPlatform().setStatus(true); - redisCatchStorage.updatePlatformCatchInfo(parentPlatformCatch); - }else { + if (parentPlatformCatch == null) { parentPlatformCatch = new ParentPlatformCatch(); parentPlatformCatch.setParentPlatform(parentPlatform); parentPlatformCatch.setId(parentPlatform.getServerGBId()); parentPlatform.setStatus(true); parentPlatformCatch.setParentPlatform(parentPlatform); - redisCatchStorage.updatePlatformCatchInfo(parentPlatformCatch); } + + parentPlatformCatch.getParentPlatform().setStatus(true); + parentPlatformCatch.setSipTransactionInfo(sipTransactionInfo); + redisCatchStorage.updatePlatformCatchInfo(parentPlatformCatch); final String registerTaskKey = REGISTER_KEY_PREFIX + parentPlatform.getServerGBId(); if (!dynamicTask.isAlive(registerTaskKey)) { + logger.info("[鍥芥爣绾ц仈]锛歿}, 娣诲姞瀹氭椂娉ㄥ唽浠诲姟", parentPlatform.getServerGBId()); // 娣诲姞娉ㄥ唽浠诲姟 dynamicTask.startCron(registerTaskKey, // 娉ㄥ唽澶辫触锛堟敞鍐屾垚鍔熸椂鐢辩▼搴忕洿鎺ヨ皟鐢ㄤ簡online鏂规硶锛� - ()-> { - registerTask(parentPlatform); - }, - (parentPlatform.getExpires()) *1000); + ()-> registerTask(parentPlatform, sipTransactionInfo), + parentPlatform.getExpires() * 1000); } final String keepaliveTaskKey = KEEPALIVE_KEY_PREFIX + parentPlatform.getServerGBId(); if (!dynamicTask.contains(keepaliveTaskKey)) { + logger.info("[鍥芥爣绾ц仈]锛歿}, 娣诲姞瀹氭椂蹇冭烦浠诲姟", parentPlatform.getServerGBId()); // 娣诲姞蹇冭烦浠诲姟 dynamicTask.startCron(keepaliveTaskKey, ()-> { @@ -259,7 +266,7 @@ } } - private void registerTask(ParentPlatform parentPlatform){ + private void registerTask(ParentPlatform parentPlatform, SipTransactionInfo sipTransactionInfo){ try { // 璁剧疆瓒呮椂閲嶅彂锛� 鍚庣画浠庡簳灞傛敮鎸佹秷鎭噸鍙� String key = KEEPALIVE_KEY_PREFIX + parentPlatform.getServerGBId() + "_timeout"; @@ -267,10 +274,10 @@ return; } dynamicTask.startDelay(key, ()->{ - registerTask(parentPlatform); + registerTask(parentPlatform, sipTransactionInfo); }, 1000); - logger.info("[鍥芥爣绾ц仈] 骞冲彴锛歿}娉ㄥ唽鍗冲皢鍒版湡锛岄噸鏂版敞鍐�", parentPlatform.getServerGBId()); - commanderForPlatform.register(parentPlatform, eventResult -> { + logger.info("[鍥芥爣绾ц仈] 骞冲彴锛歿}娉ㄥ唽鍗冲皢鍒版湡锛屽紑濮嬬画璁�", parentPlatform.getServerGBId()); + commanderForPlatform.register(parentPlatform, sipTransactionInfo, eventResult -> { dynamicTask.stop(key); offline(parentPlatform, false); },eventResult -> { diff --git a/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/platform/PlatformController.java b/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/platform/PlatformController.java index e233d1b..1b2a8f3 100644 --- a/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/platform/PlatformController.java +++ b/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/platform/PlatformController.java @@ -7,6 +7,7 @@ import com.genersoft.iot.vmp.conf.UserSetting; import com.genersoft.iot.vmp.conf.exception.ControllerException; import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform; +import com.genersoft.iot.vmp.gb28181.bean.ParentPlatformCatch; import com.genersoft.iot.vmp.gb28181.bean.PlatformCatalog; import com.genersoft.iot.vmp.gb28181.bean.SubscribeHolder; import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommanderForPlatform; @@ -229,12 +230,16 @@ throw new ControllerException(ErrorCode.ERROR400); } ParentPlatform parentPlatform = storager.queryParentPlatByServerGBId(serverGBId); + ParentPlatformCatch parentPlatformCatch = redisCatchStorage.queryPlatformCatchInfo(serverGBId); if (parentPlatform == null) { + throw new ControllerException(ErrorCode.ERROR100.getCode(), "骞冲彴涓嶅瓨鍦�"); + } + if (parentPlatformCatch == null) { throw new ControllerException(ErrorCode.ERROR100.getCode(), "骞冲彴涓嶅瓨鍦�"); } // 鍙戦�佺绾挎秷鎭�,鏃犺鏄惁鎴愬姛閮藉垹闄ょ紦瀛� try { - commanderForPlatform.unregister(parentPlatform, (event -> { + commanderForPlatform.unregister(parentPlatform, parentPlatformCatch.getSipTransactionInfo(), (event -> { // 娓呯┖redis缂撳瓨 redisCatchStorage.delPlatformCatchInfo(parentPlatform.getServerGBId()); redisCatchStorage.delPlatformKeepalive(parentPlatform.getServerGBId()); -- Gitblit v1.8.0