Commit fe625b061c73922cdb5d940208cbce056dccf6df

Authored by Hu Chunming
1 parent 0af3b245

设备状态的检查与定时更新

src/decoder/gb28181/sip/SipServer.cpp
... ... @@ -17,6 +17,7 @@
17 17 #include "./Utils/HTTPDigest.h"
18 18  
19 19 #include <sstream>
  20 +#include <algorithm>
20 21  
21 22 using namespace std;
22 23  
... ... @@ -30,6 +31,16 @@ static void event_loop_thread(void* arg) {
30 31 }
31 32 }
32 33  
  34 +static void timing_getcatlog_thread(void* arg) {
  35 + SipServer* _this = (SipServer*)arg;
  36 + if (_this != nullptr) {
  37 + _this->timing_getcatlog();
  38 + }
  39 + else {
  40 + LOG_ERROR("event_loop线程启动失败 !");
  41 + }
  42 +}
  43 +
33 44 static void dt_printSipMsg(osip_message_t* msg) {
34 45 osip_message_t* clone_event = NULL;
35 46 size_t length = 0;
... ... @@ -39,6 +50,17 @@ static void dt_printSipMsg(osip_message_t* msg) {
39 50 LOG_INFO("{}", message);
40 51 }
41 52  
  53 +static bool isntspace(const char &ch) {
  54 + return !isspace(ch);
  55 +}
  56 +
  57 +static std::string trim(const std::string &s) {
  58 + std::string::const_iterator iter1 = find_if(s.begin(), s.end(), isntspace);
  59 + std::string::const_iterator iter2 = find_if(s.rbegin(), s.rend(), isntspace).base();
  60 +
  61 + return iter1 < iter2 ? string(iter1, iter2) : std::string("");
  62 +}
  63 +
42 64 SipServer::SipServer():
43 65 mQuit(false),
44 66 mSipCtx(nullptr){
... ... @@ -83,7 +105,7 @@ int SipServer::sip_event_handle(eXosip_event_t *evtp) {
83 105  
84 106 switch(evtp->type) {
85 107 case EXOSIP_CALL_MESSAGE_NEW://14
86   - LOG_INFO("EXOSIP_CALL_MESSAGE_NEW type={}", evtp->type);
  108 + // LOG_INFO("EXOSIP_CALL_MESSAGE_NEW type={}", evtp->type);
87 109 this->dump_request(evtp);
88 110 this->dump_response(evtp);
89 111 break;
... ... @@ -111,7 +133,7 @@ int SipServer::sip_event_handle(eXosip_event_t *evtp) {
111 133 this->response_message(evtp);
112 134 }
113 135 else if(MSG_IS_BYE(evtp->request)){
114   - LOG_ERROR("unknown1");
  136 + LOG_ERROR("BYE");
115 137 }
116 138 else{
117 139 LOG_ERROR("unknown2");
... ... @@ -195,6 +217,18 @@ void SipServer::event_loop() {
195 217 }
196 218 }
197 219  
  220 +void SipServer::close() {
  221 + mQuit = true;
  222 +}
  223 +
  224 +void SipServer::timing_getcatlog() {
  225 + while(!mQuit) {
  226 + // 5分钟更新一次
  227 + std::this_thread::sleep_for(std::chrono::minutes(5));
  228 + cacheCatalog();
  229 + }
  230 +}
  231 +
198 232 void SipServer::response_message_answer(eXosip_event_t *evtp,int code){
199 233  
200 234 int returnCode = 0 ;
... ... @@ -217,7 +251,6 @@ void SipServer::response_message_answer(eXosip_event_t *evtp,int code){
217 251 }
218 252 void SipServer::response_register(eXosip_event_t *evtp) {
219 253  
220   -
221 254 osip_authorization_t * auth = nullptr;
222 255 osip_message_get_authorization(evtp->request, 0, &auth);
223 256  
... ... @@ -355,7 +388,22 @@ void SipServer::response_message(eXosip_event_t *evtp) {
355 388 }else{
356 389 this->response_message_answer(evtp,200);
357 390 }
  391 +}
358 392  
  393 +bool SipServer::check_device_status(string id) {
  394 + auto it_info = m_device_map.find(id);
  395 + if (it_info == m_device_map.end()) {
  396 + return false;
  397 + }
  398 +
  399 + string status = trim(it_info->second.status);
  400 + // status = std::tolower(status.c_str());
  401 + transform(status.begin(), status.end(), status.begin(),::tolower);
  402 + if (status == "on"){
  403 + return true;
  404 + }
  405 +
  406 + return false;
359 407 }
360 408  
361 409 Client* SipServer::get_parent_by_id(string id) {
... ... @@ -394,8 +442,9 @@ int SipServer::request_bye(eXosip_event_t* evtp) {
394 442  
395 443 int SipServer::RequestInvite_UDP(const char* dst_channel, int rtpPort) {
396 444  
397   - if (mClientMap.size() <= 0){
398   - return -1;
  445 + // 检查设备是否在线
  446 + if (!check_device_status(dst_channel)) {
  447 + return -2;
399 448 }
400 449  
401 450 Client* client = get_parent_by_id(dst_channel);
... ... @@ -453,8 +502,9 @@ int SipServer::RequestInvite_UDP(const char* dst_channel, int rtpPort) {
453 502 }
454 503  
455 504 int SipServer::RequestInvite_TCP_a(const char* dst_channel, int rtpPort) {
456   - if (mClientMap.size() <= 0){
457   - return -1;
  505 + // 检查设备是否在线
  506 + if (!check_device_status(dst_channel)) {
  507 + return -2;
458 508 }
459 509  
460 510 Client* client = get_parent_by_id(dst_channel);
... ...
src/decoder/gb28181/sip/SipServer.h
... ... @@ -134,16 +134,18 @@ public:
134 134  
135 135 int RequestInvite_TCP_a(const char* dst_channel, int rtpPort);
136 136  
137   - void cacheCatalog();
  137 + void close();
138 138  
139 139 public:
140 140 void event_loop();
  141 + void timing_getcatlog();
141 142  
142 143 private:
143 144 int init_sip_server();
144 145 int sip_event_handle(eXosip_event_t *evtp);
145 146  
146 147 void RequestCatalog(Client* client);
  148 + void cacheCatalog();
147 149  
148 150 void response_message_answer(eXosip_event_t *evtp,int code);
149 151 void response_register(eXosip_event_t *evtp);
... ... @@ -159,6 +161,7 @@ private:
159 161 void deleteClientByDevice(string device);
160 162  
161 163 Client* get_parent_by_id(string id);
  164 + bool check_device_status(string id);
162 165  
163 166 private:
164 167 bool mQuit{ false };
... ...