diff --git a/src/decoder/gb28181/sip/SipServer.cpp b/src/decoder/gb28181/sip/SipServer.cpp index 835f08f..8220292 100644 --- a/src/decoder/gb28181/sip/SipServer.cpp +++ b/src/decoder/gb28181/sip/SipServer.cpp @@ -17,6 +17,7 @@ #include "./Utils/HTTPDigest.h" #include +#include using namespace std; @@ -30,6 +31,16 @@ static void event_loop_thread(void* arg) { } } +static void timing_getcatlog_thread(void* arg) { + SipServer* _this = (SipServer*)arg; + if (_this != nullptr) { + _this->timing_getcatlog(); + } + else { + LOG_ERROR("event_loop线程启动失败 !"); + } +} + static void dt_printSipMsg(osip_message_t* msg) { osip_message_t* clone_event = NULL; size_t length = 0; @@ -39,6 +50,17 @@ static void dt_printSipMsg(osip_message_t* msg) { LOG_INFO("{}", message); } +static bool isntspace(const char &ch) { + return !isspace(ch); +} + +static std::string trim(const std::string &s) { + std::string::const_iterator iter1 = find_if(s.begin(), s.end(), isntspace); + std::string::const_iterator iter2 = find_if(s.rbegin(), s.rend(), isntspace).base(); + + return iter1 < iter2 ? string(iter1, iter2) : std::string(""); +} + SipServer::SipServer(): mQuit(false), mSipCtx(nullptr){ @@ -83,7 +105,7 @@ int SipServer::sip_event_handle(eXosip_event_t *evtp) { switch(evtp->type) { case EXOSIP_CALL_MESSAGE_NEW://14 - LOG_INFO("EXOSIP_CALL_MESSAGE_NEW type={}", evtp->type); + // LOG_INFO("EXOSIP_CALL_MESSAGE_NEW type={}", evtp->type); this->dump_request(evtp); this->dump_response(evtp); break; @@ -111,7 +133,7 @@ int SipServer::sip_event_handle(eXosip_event_t *evtp) { this->response_message(evtp); } else if(MSG_IS_BYE(evtp->request)){ - LOG_ERROR("unknown1"); + LOG_ERROR("BYE"); } else{ LOG_ERROR("unknown2"); @@ -195,6 +217,18 @@ void SipServer::event_loop() { } } +void SipServer::close() { + mQuit = true; +} + +void SipServer::timing_getcatlog() { + while(!mQuit) { + // 5分钟更新一次 + std::this_thread::sleep_for(std::chrono::minutes(5)); + cacheCatalog(); + } +} + void SipServer::response_message_answer(eXosip_event_t *evtp,int code){ int returnCode = 0 ; @@ -217,7 +251,6 @@ void SipServer::response_message_answer(eXosip_event_t *evtp,int code){ } void SipServer::response_register(eXosip_event_t *evtp) { - osip_authorization_t * auth = nullptr; osip_message_get_authorization(evtp->request, 0, &auth); @@ -355,7 +388,22 @@ void SipServer::response_message(eXosip_event_t *evtp) { }else{ this->response_message_answer(evtp,200); } +} +bool SipServer::check_device_status(string id) { + auto it_info = m_device_map.find(id); + if (it_info == m_device_map.end()) { + return false; + } + + string status = trim(it_info->second.status); + // status = std::tolower(status.c_str()); + transform(status.begin(), status.end(), status.begin(),::tolower); + if (status == "on"){ + return true; + } + + return false; } Client* SipServer::get_parent_by_id(string id) { @@ -394,8 +442,9 @@ int SipServer::request_bye(eXosip_event_t* evtp) { int SipServer::RequestInvite_UDP(const char* dst_channel, int rtpPort) { - if (mClientMap.size() <= 0){ - return -1; + // 检查设备是否在线 + if (!check_device_status(dst_channel)) { + return -2; } Client* client = get_parent_by_id(dst_channel); @@ -453,8 +502,9 @@ int SipServer::RequestInvite_UDP(const char* dst_channel, int rtpPort) { } int SipServer::RequestInvite_TCP_a(const char* dst_channel, int rtpPort) { - if (mClientMap.size() <= 0){ - return -1; + // 检查设备是否在线 + if (!check_device_status(dst_channel)) { + return -2; } Client* client = get_parent_by_id(dst_channel); diff --git a/src/decoder/gb28181/sip/SipServer.h b/src/decoder/gb28181/sip/SipServer.h index 72996d0..71a0daf 100644 --- a/src/decoder/gb28181/sip/SipServer.h +++ b/src/decoder/gb28181/sip/SipServer.h @@ -134,16 +134,18 @@ public: int RequestInvite_TCP_a(const char* dst_channel, int rtpPort); - void cacheCatalog(); + void close(); public: void event_loop(); + void timing_getcatlog(); private: int init_sip_server(); int sip_event_handle(eXosip_event_t *evtp); void RequestCatalog(Client* client); + void cacheCatalog(); void response_message_answer(eXosip_event_t *evtp,int code); void response_register(eXosip_event_t *evtp); @@ -159,6 +161,7 @@ private: void deleteClientByDevice(string device); Client* get_parent_by_id(string id); + bool check_device_status(string id); private: bool mQuit{ false };