Commit 756d2ef67f0be6dfa1734f1c9fae23603639660d

Authored by Hu Chunming
1 parent 12040639

修改缓存设备状态的更新方式

.vscode/c_cpp_properties.json 0 → 100644
  1 +{
  2 + "configurations": [
  3 + {
  4 + "name": "linux-gcc-arm64",
  5 + "includePath": [
  6 + "${workspaceFolder}/**"
  7 + ],
  8 + "compilerPath": "/usr/bin/gcc",
  9 + "cStandard": "${default}",
  10 + "cppStandard": "${default}",
  11 + "intelliSenseMode": "linux-gcc-arm64",
  12 + "compilerArgs": [
  13 + ""
  14 + ]
  15 + }
  16 + ],
  17 + "version": 4
  18 +}
0 \ No newline at end of file 19 \ No newline at end of file
.vscode/settings.json
1 { 1 {
2 - "files.associations": {  
3 - "*.ipp": "cpp",  
4 - "cctype": "cpp",  
5 - "clocale": "cpp",  
6 - "cmath": "cpp",  
7 - "csetjmp": "cpp",  
8 - "csignal": "cpp",  
9 - "cstdarg": "cpp",  
10 - "cstddef": "cpp",  
11 - "cstdio": "cpp",  
12 - "cstdlib": "cpp",  
13 - "cstring": "cpp",  
14 - "ctime": "cpp",  
15 - "cwchar": "cpp",  
16 - "cwctype": "cpp",  
17 - "any": "cpp",  
18 - "array": "cpp",  
19 - "atomic": "cpp",  
20 - "strstream": "cpp",  
21 - "bit": "cpp",  
22 - "*.tcc": "cpp",  
23 - "bitset": "cpp",  
24 - "cfenv": "cpp",  
25 - "charconv": "cpp",  
26 - "chrono": "cpp",  
27 - "cinttypes": "cpp",  
28 - "codecvt": "cpp",  
29 - "complex": "cpp",  
30 - "condition_variable": "cpp",  
31 - "cstdint": "cpp",  
32 - "deque": "cpp",  
33 - "forward_list": "cpp",  
34 - "list": "cpp",  
35 - "map": "cpp",  
36 - "set": "cpp",  
37 - "unordered_map": "cpp",  
38 - "unordered_set": "cpp",  
39 - "vector": "cpp",  
40 - "exception": "cpp",  
41 - "algorithm": "cpp",  
42 - "functional": "cpp",  
43 - "iterator": "cpp",  
44 - "memory": "cpp",  
45 - "memory_resource": "cpp",  
46 - "numeric": "cpp",  
47 - "optional": "cpp",  
48 - "random": "cpp",  
49 - "ratio": "cpp",  
50 - "regex": "cpp",  
51 - "source_location": "cpp",  
52 - "string": "cpp",  
53 - "string_view": "cpp",  
54 - "system_error": "cpp",  
55 - "tuple": "cpp",  
56 - "type_traits": "cpp",  
57 - "utility": "cpp",  
58 - "rope": "cpp",  
59 - "slist": "cpp",  
60 - "fstream": "cpp",  
61 - "future": "cpp",  
62 - "initializer_list": "cpp",  
63 - "iomanip": "cpp",  
64 - "iosfwd": "cpp",  
65 - "iostream": "cpp",  
66 - "istream": "cpp",  
67 - "limits": "cpp",  
68 - "mutex": "cpp",  
69 - "new": "cpp",  
70 - "ostream": "cpp",  
71 - "scoped_allocator": "cpp",  
72 - "shared_mutex": "cpp",  
73 - "sstream": "cpp",  
74 - "stdexcept": "cpp",  
75 - "streambuf": "cpp",  
76 - "thread": "cpp",  
77 - "typeindex": "cpp",  
78 - "typeinfo": "cpp",  
79 - "valarray": "cpp",  
80 - "variant": "cpp"  
81 - } 2 + "files.associations": {
  3 + "*.ipp": "cpp",
  4 + "cctype": "cpp",
  5 + "clocale": "cpp",
  6 + "cmath": "cpp",
  7 + "csetjmp": "cpp",
  8 + "csignal": "cpp",
  9 + "cstdarg": "cpp",
  10 + "cstddef": "cpp",
  11 + "cstdio": "cpp",
  12 + "cstdlib": "cpp",
  13 + "cstring": "cpp",
  14 + "ctime": "cpp",
  15 + "cwchar": "cpp",
  16 + "cwctype": "cpp",
  17 + "any": "cpp",
  18 + "array": "cpp",
  19 + "atomic": "cpp",
  20 + "strstream": "cpp",
  21 + "bit": "cpp",
  22 + "*.tcc": "cpp",
  23 + "bitset": "cpp",
  24 + "cfenv": "cpp",
  25 + "charconv": "cpp",
  26 + "chrono": "cpp",
  27 + "cinttypes": "cpp",
  28 + "codecvt": "cpp",
  29 + "complex": "cpp",
  30 + "condition_variable": "cpp",
  31 + "cstdint": "cpp",
  32 + "deque": "cpp",
  33 + "forward_list": "cpp",
  34 + "list": "cpp",
  35 + "map": "cpp",
  36 + "set": "cpp",
  37 + "unordered_map": "cpp",
  38 + "unordered_set": "cpp",
  39 + "vector": "cpp",
  40 + "exception": "cpp",
  41 + "algorithm": "cpp",
  42 + "functional": "cpp",
  43 + "iterator": "cpp",
  44 + "memory": "cpp",
  45 + "memory_resource": "cpp",
  46 + "numeric": "cpp",
  47 + "optional": "cpp",
  48 + "random": "cpp",
  49 + "ratio": "cpp",
  50 + "regex": "cpp",
  51 + "source_location": "cpp",
  52 + "string": "cpp",
  53 + "string_view": "cpp",
  54 + "system_error": "cpp",
  55 + "tuple": "cpp",
  56 + "type_traits": "cpp",
  57 + "utility": "cpp",
  58 + "rope": "cpp",
  59 + "slist": "cpp",
  60 + "fstream": "cpp",
  61 + "future": "cpp",
  62 + "initializer_list": "cpp",
  63 + "iomanip": "cpp",
  64 + "iosfwd": "cpp",
  65 + "iostream": "cpp",
  66 + "istream": "cpp",
  67 + "limits": "cpp",
  68 + "mutex": "cpp",
  69 + "new": "cpp",
  70 + "ostream": "cpp",
  71 + "scoped_allocator": "cpp",
  72 + "shared_mutex": "cpp",
  73 + "sstream": "cpp",
  74 + "stdexcept": "cpp",
  75 + "streambuf": "cpp",
  76 + "thread": "cpp",
  77 + "typeindex": "cpp",
  78 + "typeinfo": "cpp",
  79 + "valarray": "cpp",
  80 + "variant": "cpp"
  81 + },
  82 + "C_Cpp_Runner.cCompilerPath": "gcc",
  83 + "C_Cpp_Runner.cppCompilerPath": "g++",
  84 + "C_Cpp_Runner.debuggerPath": "gdb",
  85 + "C_Cpp_Runner.cStandard": "",
  86 + "C_Cpp_Runner.cppStandard": "",
  87 + "C_Cpp_Runner.msvcBatchPath": "",
  88 + "C_Cpp_Runner.useMsvc": false,
  89 + "C_Cpp_Runner.warnings": [
  90 + "-Wall",
  91 + "-Wextra",
  92 + "-Wpedantic",
  93 + "-Wshadow",
  94 + "-Wformat=2",
  95 + "-Wcast-align",
  96 + "-Wconversion",
  97 + "-Wsign-conversion",
  98 + "-Wnull-dereference"
  99 + ],
  100 + "C_Cpp_Runner.msvcWarnings": [
  101 + "/W4",
  102 + "/permissive-",
  103 + "/w14242",
  104 + "/w14287",
  105 + "/w14296",
  106 + "/w14311",
  107 + "/w14826",
  108 + "/w44062",
  109 + "/w44242",
  110 + "/w14905",
  111 + "/w14906",
  112 + "/w14263",
  113 + "/w44265",
  114 + "/w14928"
  115 + ],
  116 + "C_Cpp_Runner.enableWarnings": true,
  117 + "C_Cpp_Runner.warningsAsError": false,
  118 + "C_Cpp_Runner.compilerArgs": [],
  119 + "C_Cpp_Runner.linkerArgs": [],
  120 + "C_Cpp_Runner.includePaths": [],
  121 + "C_Cpp_Runner.includeSearch": [
  122 + "*",
  123 + "**/*"
  124 + ],
  125 + "C_Cpp_Runner.excludeSearch": [
  126 + "**/build",
  127 + "**/build/**",
  128 + "**/.*",
  129 + "**/.*/**",
  130 + "**/.vscode",
  131 + "**/.vscode/**"
  132 + ],
  133 + "C_Cpp_Runner.useAddressSanitizer": false,
  134 + "C_Cpp_Runner.useUndefinedSanitizer": false,
  135 + "C_Cpp_Runner.useLeakSanitizer": false,
  136 + "C_Cpp_Runner.showCompilationTime": false,
  137 + "C_Cpp_Runner.useLinkTimeOptimization": false,
  138 + "C_Cpp_Runner.msvcSecureNoWarnings": false
82 } 139 }
83 \ No newline at end of file 140 \ No newline at end of file
bin/sip_server deleted
No preview for this file type
bin/sip_server_cfg.xml
@@ -9,5 +9,5 @@ @@ -9,5 +9,5 @@
9 <Password></Password> 9 <Password></Password>
10 <Timeout>1800</Timeout> 10 <Timeout>1800</Timeout>
11 <Expiry>3600</Expiry> 11 <Expiry>3600</Expiry>
12 - <WsPort>9006</WsPort> 12 + <WsPort>9007</WsPort>
13 </ROOT> 13 </ROOT>
sip/ConfigParser.hpp
@@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
2 #define __CONFIG_PARSER_H__ 2 #define __CONFIG_PARSER_H__
3 3
4 #include <fstream> 4 #include <fstream>
5 -#include "./Message/CatalogParser.h" 5 +#include "./Message/GBXmlParser.h"
6 #include "sip_header.h" 6 #include "sip_header.h"
7 #include "./Utils/logger.hpp" 7 #include "./Utils/logger.hpp"
8 8
@@ -48,7 +48,7 @@ bool ConfigParser::init() { @@ -48,7 +48,7 @@ bool ConfigParser::init() {
48 { 48 {
49 strConfig += str; 49 strConfig += str;
50 } 50 }
51 - CCatalogParser catPaser; 51 + GBXmlParser catPaser;
52 mInfo = catPaser.DecodeServerConfig(strConfig.c_str()); 52 mInfo = catPaser.DecodeServerConfig(strConfig.c_str());
53 } else { 53 } else {
54 LOG_ERROR("read config file failed!"); 54 LOG_ERROR("read config file failed!");
sip/Message/CatalogParser.cpp renamed to sip/Message/GBXmlParser.cpp
1 -#include "CatalogParser.h" 1 +#include "GBXmlParser.h"
2 #include <sstream> 2 #include <sstream>
3 #include <list> 3 #include <list>
4 4
5 #include "../Utils/logger.hpp" 5 #include "../Utils/logger.hpp"
  6 +#include "../Utils/StringTools.hpp"
6 7
7 static const char* g_event_desc[] = 8 static const char* g_event_desc[] =
8 { 9 {
@@ -16,7 +17,7 @@ static const char* g_event_desc[] = @@ -16,7 +17,7 @@ static const char* g_event_desc[] =
16 }; 17 };
17 18
18 19
19 -bool CCatalogParser::Encode( std::string &message ) 20 +bool GBXmlParser::Encode( std::string &message )
20 { 21 {
21 std::ostringstream content; 22 std::ostringstream content;
22 content<<"<?xml version=\"1.0\"?>\r\n"; 23 content<<"<?xml version=\"1.0\"?>\r\n";
@@ -170,7 +171,7 @@ bool CCatalogParser::Encode( std::string &amp;message ) @@ -170,7 +171,7 @@ bool CCatalogParser::Encode( std::string &amp;message )
170 return true; 171 return true;
171 } 172 }
172 173
173 -std::vector< DeviceInfo > CCatalogParser::Decode( const std::vector< tinyxml2::XMLNode* > &nodes) 174 +std::vector< DeviceInfo > GBXmlParser::Decode( const std::vector< tinyxml2::XMLNode* > &nodes)
174 { 175 {
175 std::vector< DeviceInfo > cat_list; 176 std::vector< DeviceInfo > cat_list;
176 // 必须参数校验 177 // 必须参数校验
@@ -439,7 +440,13 @@ std::vector&lt; DeviceInfo &gt; CCatalogParser::Decode( const std::vector&lt; tinyxml2::X @@ -439,7 +440,13 @@ std::vector&lt; DeviceInfo &gt; CCatalogParser::Decode( const std::vector&lt; tinyxml2::X
439 } 440 }
440 else if( CGBMessage::CompareNoCase( type, "Status" ) ) 441 else if( CGBMessage::CompareNoCase( type, "Status" ) )
441 { 442 {
442 - catalog.status = value; 443 + string status = StringTools::trim(value);
  444 + transform(status.begin(), status.end(), status.begin(),::tolower);
  445 + if (status == "on"){
  446 + catalog.status = STATUS_ON;
  447 + } else {
  448 + catalog.status = STATUS_OFF;
  449 + }
443 } 450 }
444 else if( CGBMessage::CompareNoCase( type, "Longitude" ) ) 451 else if( CGBMessage::CompareNoCase( type, "Longitude" ) )
445 { 452 {
@@ -557,7 +564,7 @@ std::vector&lt; DeviceInfo &gt; CCatalogParser::Decode( const std::vector&lt; tinyxml2::X @@ -557,7 +564,7 @@ std::vector&lt; DeviceInfo &gt; CCatalogParser::Decode( const std::vector&lt; tinyxml2::X
557 return m_devices; 564 return m_devices;
558 } 565 }
559 566
560 -std::vector< DeviceInfo > CCatalogParser::DecodeCatlog(const char* body) 567 +std::vector< DeviceInfo > GBXmlParser::DecodeCatlog(const char* body)
561 { 568 {
562 std::vector< DeviceInfo > cat_list; 569 std::vector< DeviceInfo > cat_list;
563 if (body == NULL) 570 if (body == NULL)
@@ -632,7 +639,7 @@ std::vector&lt; DeviceInfo &gt; CCatalogParser::DecodeCatlog(const char* body) @@ -632,7 +639,7 @@ std::vector&lt; DeviceInfo &gt; CCatalogParser::DecodeCatlog(const char* body)
632 return cat_list; 639 return cat_list;
633 } 640 }
634 641
635 -DeviceInfo CCatalogParser::DecodeDeviceInfo(const char* body) 642 +DeviceInfo GBXmlParser::DecodeDeviceInfo(const char* body)
636 { 643 {
637 DeviceInfo info; 644 DeviceInfo info;
638 645
@@ -671,7 +678,7 @@ DeviceInfo CCatalogParser::DecodeDeviceInfo(const char* body) @@ -671,7 +678,7 @@ DeviceInfo CCatalogParser::DecodeDeviceInfo(const char* body)
671 return info; 678 return info;
672 } 679 }
673 680
674 -string CCatalogParser::getItemValue(tinyxml2::XMLNode *pItem) { 681 +string GBXmlParser::getItemValue(tinyxml2::XMLNode *pItem) {
675 682
676 tinyxml2::XMLNode *pValue = pItem->FirstChild(); 683 tinyxml2::XMLNode *pValue = pItem->FirstChild();
677 if( pValue == NULL ) { 684 if( pValue == NULL ) {
@@ -688,7 +695,7 @@ string CCatalogParser::getItemValue(tinyxml2::XMLNode *pItem) { @@ -688,7 +695,7 @@ string CCatalogParser::getItemValue(tinyxml2::XMLNode *pItem) {
688 return value; 695 return value;
689 } 696 }
690 697
691 -ServerInfo CCatalogParser::DecodeServerConfig(const char* body) 698 +ServerInfo GBXmlParser::DecodeServerConfig(const char* body)
692 { 699 {
693 ServerInfo info; 700 ServerInfo info;
694 if (body == NULL) { 701 if (body == NULL) {
@@ -757,4 +764,45 @@ ServerInfo CCatalogParser::DecodeServerConfig(const char* body) @@ -757,4 +764,45 @@ ServerInfo CCatalogParser::DecodeServerConfig(const char* body)
757 } 764 }
758 765
759 return info; 766 return info;
  767 +}
  768 +
  769 +vector<string> GBXmlParser::ParseKeepalive(const char* body) {
  770 + // LOG_DEBUG( "{}", body);
  771 +
  772 + std::vector< string > device_id_list;
  773 + if (body == NULL)
  774 + {
  775 + return device_id_list;
  776 + }
  777 +
  778 + tinyxml2::XMLDocument doc;
  779 + doc.Parse(body);
  780 + tinyxml2::XMLElement* pRoot = doc.RootElement();
  781 + if (pRoot == nullptr) {
  782 + return device_id_list;
  783 + }
  784 +
  785 + tinyxml2::XMLNode* pInfo = nullptr;
  786 + std::vector< tinyxml2::XMLNode* > nodes;
  787 + for (tinyxml2::XMLNode* pNode = pRoot->FirstChild(); pNode != 0; pNode = pNode->NextSibling()) {
  788 + std::string value = pNode->Value();
  789 + if (value == "Info") {
  790 + pInfo = pNode;
  791 + break;
  792 + }
  793 + }
  794 +
  795 + if (pInfo == nullptr) {
  796 + return device_id_list;
  797 + }
  798 +
  799 + for (tinyxml2::XMLNode* pNode = pInfo->FirstChild(); pNode != 0; pNode = pNode->NextSibling()) {
  800 + std::string type = pNode->Value();
  801 +
  802 + if (CGBMessage::CompareNoCase( type, "DeviceID" )) {
  803 + device_id_list.push_back(getItemValue(pNode));
  804 + }
  805 + }
  806 +
  807 + return device_id_list;
760 } 808 }
761 \ No newline at end of file 809 \ No newline at end of file
sip/Message/CatalogParser.h renamed to sip/Message/GBXmlParser.h
@@ -6,10 +6,10 @@ @@ -6,10 +6,10 @@
6 6
7 #include "../sip_header.h" 7 #include "../sip_header.h"
8 8
9 -class CCatalogParser : public CGBMessage 9 +class GBXmlParser : public CGBMessage
10 { 10 {
11 public: 11 public:
12 - CCatalogParser() 12 + GBXmlParser()
13 { 13 {
14 } 14 }
15 15
@@ -22,6 +22,8 @@ public: @@ -22,6 +22,8 @@ public:
22 22
23 ServerInfo DecodeServerConfig(const char* body); 23 ServerInfo DecodeServerConfig(const char* body);
24 24
  25 + vector<string> ParseKeepalive(const char* body);
  26 +
25 static std::string GetStrName(EEventType eType) 27 static std::string GetStrName(EEventType eType)
26 { 28 {
27 switch (eType) 29 switch (eType)
sip/SipServer.cpp
@@ -35,16 +35,6 @@ static void event_loop_thread(void* arg) { @@ -35,16 +35,6 @@ static void event_loop_thread(void* arg) {
35 } 35 }
36 } 36 }
37 37
38 -static void timing_getcatlog_thread(void* arg) {  
39 - SipServer* _this = (SipServer*)arg;  
40 - if (_this != nullptr) {  
41 - _this->timing_getcatlog();  
42 - }  
43 - else {  
44 - LOG_ERROR("timing_getcatlog 线程启动失败 !");  
45 - }  
46 -}  
47 -  
48 static void dt_printSipMsg(osip_message_t* msg) { 38 static void dt_printSipMsg(osip_message_t* msg) {
49 osip_message_t* clone_event = NULL; 39 osip_message_t* clone_event = NULL;
50 size_t length = 0; 40 size_t length = 0;
@@ -276,8 +266,6 @@ void SipServer::event_loop() { @@ -276,8 +266,6 @@ void SipServer::event_loop() {
276 } 266 }
277 267
278 LOG_INFO("sip server init succeed: {}:{}", mInfo.getIp(), mInfo.getPort()); 268 LOG_INFO("sip server init succeed: {}:{}", mInfo.getIp(), mInfo.getPort());
279 -  
280 - thread* timing_getcatlog_threadptr = new std::thread(timing_getcatlog_thread, this);  
281 269
282 while(!mQuit) { 270 while(!mQuit) {
283 eXosip_event_t *evtp = eXosip_event_wait(mSipCtx, 0, 20); 271 eXosip_event_t *evtp = eXosip_event_wait(mSipCtx, 0, 20);
@@ -292,11 +280,6 @@ void SipServer::event_loop() { @@ -292,11 +280,6 @@ void SipServer::event_loop() {
292 } 280 }
293 281
294 mQuit = true; 282 mQuit = true;
295 - if (timing_getcatlog_threadptr) {  
296 - timing_getcatlog_threadptr->join();  
297 - delete timing_getcatlog_threadptr;  
298 - timing_getcatlog_threadptr = nullptr;  
299 - }  
300 } 283 }
301 284
302 void SipServer::Close() { 285 void SipServer::Close() {
@@ -310,14 +293,6 @@ void SipServer::Close() { @@ -310,14 +293,6 @@ void SipServer::Close() {
310 293
311 } 294 }
312 295
313 -void SipServer::timing_getcatlog() {  
314 - while(!mQuit) {  
315 - // 5分钟更新一次  
316 - std::this_thread::sleep_for(std::chrono::minutes(5));  
317 - cacheCatalog();  
318 - }  
319 -}  
320 -  
321 void SipServer::response_message_answer(eXosip_event_t *evtp,int code){ 296 void SipServer::response_message_answer(eXosip_event_t *evtp,int code){
322 297
323 int returnCode = 0 ; 298 int returnCode = 0 ;
@@ -497,10 +472,13 @@ void SipServer::cacheClient(osip_uri_t *url, int expiry) { @@ -497,10 +472,13 @@ void SipServer::cacheClient(osip_uri_t *url, int expiry) {
497 if (type >= 111 && type <= 130) { 472 if (type >= 111 && type <= 130) {
498 // NVR注册成功,立即请求设备目录 473 // NVR注册成功,立即请求设备目录
499 RequestCatalog(client); 474 RequestCatalog(client);
  475 + client->setDeviceType(DEVICE_TYPE_NVR);
500 } else if (type >= 131 && type <= 199) { 476 } else if (type >= 131 && type <= 199) {
501 RequestDeviceInfo(client); 477 RequestDeviceInfo(client);
  478 + client->setDeviceType(DEVICE_TYPE_IPC);
502 } else if (type >= 200 && type <= 299) { 479 } else if (type >= 200 && type <= 299) {
503 RequestCatalog(client); 480 RequestCatalog(client);
  481 + client->setDeviceType(DEVICE_TYPE_PLATFORM);
504 } else { 482 } else {
505 LOG_WARN("device type is not supported:{}",type); 483 LOG_WARN("device type is not supported:{}",type);
506 } 484 }
@@ -555,12 +533,12 @@ void SipServer::response_message(eXosip_event_t *evtp) { @@ -555,12 +533,12 @@ void SipServer::response_message(eXosip_event_t *evtp) {
555 return; 533 return;
556 } 534 }
557 535
558 - LOG_INFO("CmdType={},DeviceID={}", CmdType, DeviceID); 536 + LOG_INFO("{} - {}", CmdType, DeviceID);
559 537
560 if(!strcmp(CmdType, "Catalog")) { 538 if(!strcmp(CmdType, "Catalog")) {
561 this->response_message_answer(evtp,200); 539 this->response_message_answer(evtp,200);
562 // 需要根据对方的Catelog请求,做一些相应的应答请求 540 // 需要根据对方的Catelog请求,做一些相应的应答请求
563 - CCatalogParser catPaser; 541 + GBXmlParser catPaser;
564 std::vector<DeviceInfo> vec_device = catPaser.DecodeCatlog(body->body); 542 std::vector<DeviceInfo> vec_device = catPaser.DecodeCatlog(body->body);
565 printDevice(vec_device); 543 printDevice(vec_device);
566 544
@@ -572,9 +550,9 @@ void SipServer::response_message(eXosip_event_t *evtp) { @@ -572,9 +550,9 @@ void SipServer::response_message(eXosip_event_t *evtp) {
572 } else if (!strcmp(CmdType, "DeviceInfo")) { 550 } else if (!strcmp(CmdType, "DeviceInfo")) {
573 this->response_message_answer(evtp, 200); 551 this->response_message_answer(evtp, 200);
574 // 需要根据对方的Catelog请求,做一些相应的应答请求 552 // 需要根据对方的Catelog请求,做一些相应的应答请求
575 - CCatalogParser catPaser; 553 + GBXmlParser catPaser;
576 DeviceInfo info = catPaser.DecodeDeviceInfo(body->body); 554 DeviceInfo info = catPaser.DecodeDeviceInfo(body->body);
577 - info.status = "on"; // 有值返回就已经可以表明设备状态在线了 555 + info.status = STATUS_ON; // 有值返回就已经可以表明设备状态在线了
578 556
579 std::lock_guard<std::mutex> l(m_device_map_mtx); 557 std::lock_guard<std::mutex> l(m_device_map_mtx);
580 m_device_map[info.id] = info; 558 m_device_map[info.id] = info;
@@ -583,8 +561,35 @@ void SipServer::response_message(eXosip_event_t *evtp) { @@ -583,8 +561,35 @@ void SipServer::response_message(eXosip_event_t *evtp) {
583 std::lock_guard<std::mutex> l_c(m_client_map_mtx); 561 std::lock_guard<std::mutex> l_c(m_client_map_mtx);
584 auto it = mClientMap.find(DeviceID); 562 auto it = mClientMap.find(DeviceID);
585 if (it != mClientMap.end()) { 563 if (it != mClientMap.end()) {
  564 +
  565 + Client* client = it->second;
  566 + client->updateHeartBeat(Utools::get_cur_time_ms());
  567 + DeviceType type = client->getDeviceType();
  568 + if (DEVICE_TYPE_NVR == type || DEVICE_TYPE_PLATFORM == type) {
  569 + GBXmlParser xmlPaser;
  570 + vector<string> vec_id = xmlPaser.ParseKeepalive(body->body);
  571 +
  572 + std::lock_guard<std::mutex> l_d(m_device_map_mtx);
  573 + for (auto it_device = m_device_map.begin(); it_device != m_device_map.end(); it_device++) {
  574 + string device_id = it_device->first;
  575 + bool bOff = false;
  576 + for (size_t i = 0; i < vec_id.size(); i++) {
  577 + string dev_id = vec_id[i];
  578 + if (dev_id == device_id) {
  579 + it_device->second.status = STATUS_OFF;
  580 + bOff = true;
  581 + }
  582 + }
  583 + if (!bOff) {
  584 + //修正短时间off又on后无法变回on的问题
  585 + it_device->second.status = STATUS_ON;
  586 + }
  587 +
  588 + }
  589 + }
  590 +
586 response_message_answer(evtp,200); 591 response_message_answer(evtp,200);
587 - it->second->updateHeartBeat(Utools::get_cur_time_ms()); 592 +
588 return ; 593 return ;
589 } else { 594 } else {
590 // 未注册设备发保活信息,超过设备的最大超时次数后,设备将进行初始注册 595 // 未注册设备发保活信息,超过设备的最大超时次数后,设备将进行初始注册
@@ -602,9 +607,8 @@ bool SipServer::check_device_status(string id) { @@ -602,9 +607,8 @@ bool SipServer::check_device_status(string id) {
602 return false; 607 return false;
603 } 608 }
604 609
605 - string status = StringTools::trim(it_info->second.status);  
606 - transform(status.begin(), status.end(), status.begin(),::tolower);  
607 - if (status == "on"){ 610 + DeviceStatus status = it_info->second.status;
  611 + if (STATUS_ON == status){
608 return true; 612 return true;
609 } 613 }
610 614
@@ -844,59 +848,6 @@ int SipServer::RequestInvite_TCP_a(const char* dst_channel, const char* rtpIp, i @@ -844,59 +848,6 @@ int SipServer::RequestInvite_TCP_a(const char* dst_channel, const char* rtpIp, i
844 return call_id; 848 return call_id;
845 } 849 }
846 850
847 -void SipServer::cacheCatalog() {  
848 -  
849 - std::lock_guard<std::mutex> l(m_client_map_mtx);  
850 -  
851 - if (mClientMap.size() <= 0){  
852 - LOG_WARN("NO IPC");  
853 - return ;  
854 - }  
855 -  
856 - long cur_ts = Utools::get_cur_time_ms();  
857 -  
858 - vector<string> vec_sip_id;  
859 -  
860 - LOG_INFO("client size:{}", mClientMap.size());  
861 - for (auto it = mClientMap.begin(); it != mClientMap.end(); it++) {  
862 - //RequestCatalog(it->second);  
863 - Client* client = it->second;  
864 - if (!client) {  
865 - continue;  
866 - }  
867 -  
868 - string sip_id = client->getDevice();  
869 -  
870 - unsigned long gap = client->getHeartBeatGap();  
871 - if (gap > 0 && (cur_ts - client->getHeartBeat()) > 10 * gap) {  
872 - LOG_WARN("{}:超过10个心跳周期未收到心跳", sip_id);  
873 - vec_sip_id.push_back(sip_id);  
874 - continue;  
875 - }  
876 -  
877 - int type = check_device_type(sip_id);  
878 - if (-1 == type) {  
879 - continue;  
880 - }  
881 -  
882 - if (type >= 111 && type <= 130) {  
883 - // NVR注册成功,立即请求设备目录  
884 - RequestCatalog(client);  
885 - } else if (type >= 131 && type <= 199) {  
886 - RequestDeviceInfo(client);  
887 - } else if (type >= 200 && type <= 299) {  
888 - RequestCatalog(client);  
889 - } else {  
890 - LOG_WARN("device type is not supported:{}",type);  
891 - }  
892 - }  
893 -  
894 - // 超过10个心跳周期未收到心跳,做下线处理  
895 - for (size_t i = 0; i < vec_sip_id.size(); i++) {  
896 - deleteClientByDevice(vec_sip_id[i]);  
897 - }  
898 -}  
899 -  
900 void SipServer::RequestCatalog(Client* client) { 851 void SipServer::RequestCatalog(Client* client) {
901 852
902 eXosip_lock(mSipCtx); 853 eXosip_lock(mSipCtx);
sip/SipServer.h
@@ -13,11 +13,18 @@ extern &quot;C&quot; { @@ -13,11 +13,18 @@ extern &quot;C&quot; {
13 #include <mutex> 13 #include <mutex>
14 #include <tuple> 14 #include <tuple>
15 15
16 -#include "./Message/CatalogParser.h" 16 +#include "./Message/GBXmlParser.h"
17 #include "sip_header.h" 17 #include "sip_header.h"
18 18
19 using namespace std; 19 using namespace std;
20 20
  21 +enum DeviceType {
  22 + DEVICE_TYPE_UNKNOWN,
  23 + DEVICE_TYPE_IPC,
  24 + DEVICE_TYPE_NVR,
  25 + DEVICE_TYPE_PLATFORM
  26 +};
  27 +
21 class Client { 28 class Client {
22 public: 29 public:
23 Client(string ip, int port, string device) : 30 Client(string ip, int port, string device) :
@@ -25,6 +32,7 @@ public: @@ -25,6 +32,7 @@ public:
25 { 32 {
26 mRtpPort = 0; 33 mRtpPort = 0;
27 mIsReg = false; 34 mIsReg = false;
  35 + mType = DEVICE_TYPE_UNKNOWN;
28 } 36 }
29 37
30 ~Client() = default; 38 ~Client() = default;
@@ -75,6 +83,14 @@ public: @@ -75,6 +83,14 @@ public:
75 mExipry = expiry; 83 mExipry = expiry;
76 } 84 }
77 85
  86 + DeviceType getDeviceType() {
  87 + return mType;
  88 + }
  89 +
  90 + void setDeviceType(DeviceType type) {
  91 + mType = type;
  92 + }
  93 +
78 private: 94 private:
79 string mIp; // client ip 95 string mIp; // client ip
80 int mPort; // client port 96 int mPort; // client port
@@ -86,6 +102,7 @@ private: @@ -86,6 +102,7 @@ private:
86 unsigned long mHeartBeatGap{1}; 102 unsigned long mHeartBeatGap{1};
87 unsigned long mHeartBeatCount{0}; 103 unsigned long mHeartBeatCount{0};
88 long mExipry{0}; 104 long mExipry{0};
  105 + DeviceType mType;
89 }; 106 };
90 107
91 108
@@ -108,14 +125,12 @@ public: @@ -108,14 +125,12 @@ public:
108 125
109 public: 126 public:
110 void event_loop(); 127 void event_loop();
111 - void timing_getcatlog();  
112 128
113 private: 129 private:
114 int init_sip_server(); 130 int init_sip_server();
115 int sip_event_handle(eXosip_event_t *evtp); 131 int sip_event_handle(eXosip_event_t *evtp);
116 132
117 void RequestCatalog(Client* client); 133 void RequestCatalog(Client* client);
118 - void cacheCatalog();  
119 134
120 void RequestDeviceInfo(Client* client); 135 void RequestDeviceInfo(Client* client);
121 136
sip/sip_header.h
@@ -18,6 +18,12 @@ enum EEventType @@ -18,6 +18,12 @@ enum EEventType
18 EVENT_UNKNOW, 18 EVENT_UNKNOW,
19 }; 19 };
20 20
  21 +enum DeviceStatus {
  22 + STATUS_UNKNOWN,
  23 + STATUS_ON,
  24 + STATUS_OFF
  25 +};
  26 +
21 struct DeviceInfo 27 struct DeviceInfo
22 { 28 {
23 EEventType event; 29 EEventType event;
@@ -41,7 +47,7 @@ struct DeviceInfo @@ -41,7 +47,7 @@ struct DeviceInfo
41 std::string ip; 47 std::string ip;
42 std::string port; 48 std::string port;
43 std::string password; 49 std::string password;
44 - std::string status; 50 + DeviceStatus status;
45 std::string longitude; 51 std::string longitude;
46 std::string latitude; 52 std::string latitude;
47 std::string ptz; 53 std::string ptz;