123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- #include "stdafx.h"
- #include"SyncManager.h"
- #include <deque>
- #include <Eigen/Dense>
- #include <fstream>
- #include <iostream>
- #include <direct.h>
- #include <io.h>
- #include "../ProcessRemodule.h"
- #include "../log_process_module.h"
- #include "./../system_basic_info/SystemAnalysis.h"
- #include "Functions/Functions.h"
- #pragma warning(disable: 4244)
- HostServer::SyncManager::SyncManager()
- {
- init();
- }
- void HostServer::SyncManager::init()
- {
- InitializeCriticalSectionAndSpinCount(&m_csSyncTime, 4000);
- _anchors.swap(unordered_map<unsigned long long, Position>());
- _distance.swap(unordered_map<unsigned long long,unordered_map<unsigned long long, double>>());
- std::string dir = "";
- TCHAR chpath[MAX_PATH];
- ::GetModuleFileName(NULL,chpath, MAX_PATH);
- (_tcsrchr(chpath, _T('\\')))[1] = 0;
- dir = CFunctions::TCHAR2string(chpath);
- logDir = dir + "\\synclog\\";
- if(0 != _access(logDir.c_str(), 0)){
- _mkdir(logDir.c_str());
- }
- isOutputLog = false;
- }
- void HostServer::SyncManager::analyzeSyncMsg(SyncTimeMsg &msg)
- {
- LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_10);
- EnterCriticalSection(&m_csSyncTime);
- //查找root节点的时间同步序号
- int idx = FindSyncTimeMsg(msg.RootIdCode(), msg.SyncNum());
- if( -1 == idx){
- // 没找到
- LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_72);
- // 如果时间同步消息的版本数量超过最大限制,则删除最早添加的消息
- if(_syncTimeMsgs[msg.RootIdCode()].size() >= MAX_SYNCTIME_NUM){
- // 删除第一个
- // 可能有泄露
- _syncTimeMsgs[msg.RootIdCode()].pop_front();
- LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_12);
- }
- //构造此时间同步数据保存到队列中
- SyncTimeMsgItem it;
- it.SyncNum = msg.SyncNum();
- it.SyncTimeMsgs[msg.LocalIdCode()] = msg;
- _syncTimeMsgs[msg.RootIdCode()].push_back(it);
- idx = _syncTimeMsgs[msg.RootIdCode()].size() - 1;
- }else{
- LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_73);
- _syncTimeMsgs[msg.RootIdCode()][idx].SyncTimeMsgs[msg.LocalIdCode()] = msg;
- }
- // 如果历史同步队列中的时间同步数据大于指定容量,清除数据直到满足指定容量
- while(_historySync[msg.RootIdCode()].size() > MAX_SYNCTIME_NUM){
- _historySync[msg.RootIdCode()].pop_front();
- }
- // 更新时间同步并计算
- for(auto it :_syncTimeMsgs[msg.RootIdCode()][idx].SyncTimeMsgs)
- {
- updateSync(msg.RootIdCode(), idx, msg.SyncNum(), it.first);
- }
- LeaveCriticalSection(&m_csSyncTime);
- }
- bool HostServer::SyncManager::updateSync(unsigned long long rootIdCode, int idx, unsigned short SyncNum, unsigned long long localIdCode)
- {
- if(-1 == idx){
- LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_74);
- return false;
- }
- // 如果当前版本的时间同步消息未收到则返回false
- unordered_map<unsigned long long, SyncTimeMsg>::iterator itSyncTime = _syncTimeMsgs[rootIdCode][idx].SyncTimeMsgs.find(localIdCode);
- if(itSyncTime == _syncTimeMsgs[rootIdCode][idx].SyncTimeMsgs.end()){
- LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_75);
- return false;
- }
- SyncTimeMsg &msg = _syncTimeMsgs[rootIdCode][idx].SyncTimeMsgs[localIdCode];
- // 如果当前的为root,则返回true
- if(msg.SyncLevel() == 0){
- LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_76);
- return true;
- }
- int idx_synctime = FindHisSyncTime(rootIdCode, SyncNum);
- // 如果时间同步已经计算过,则返回true
- if(-1 != idx_synctime){
- unordered_map<unsigned long long, SyncTime>::iterator itHistSync = _historySync[rootIdCode][idx_synctime].HistSync.find(localIdCode);
- if(itHistSync != _historySync[rootIdCode][idx_synctime].HistSync.end()){
- if(itHistSync->second.TimeDelay())
- return true;
- }
- }
- // 如果已经收到了上一级的同步消息
- if(_syncTimeMsgs[rootIdCode][idx].SyncTimeMsgs.count(msg.UpperIdCode())){
- LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_77);
- if(!updateSync(rootIdCode, idx, SyncNum, msg.UpperIdCode())){
- LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_78);
- return false;
- }
- SyncTimeMsg &upperMsg = _syncTimeMsgs[rootIdCode][idx].SyncTimeMsgs[msg.UpperIdCode()];
- SyncTime* s = NULL;
- for(auto it(_historySync[rootIdCode].rbegin()); it != _historySync[rootIdCode].rend(); ++it)
- {
- if(it->SyncNum != msg.SyncNum() && it->HistSync.count(localIdCode))
- {
- s = &(it->HistSync.find(localIdCode)->second);
- break;
- }
- }
- idx_synctime = FindHisSyncTime(rootIdCode, SyncNum);
- if(-1 == idx_synctime){
- LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_79);
- SyncTimeItem it;
- it.SyncNum = msg.SyncNum();
- it.HistSync[localIdCode] = SyncTime(msg, upperMsg, s);
- _historySync[rootIdCode].push_back(it);
- idx_synctime = _historySync[rootIdCode].size() - 1;
- }else{
- LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_80);
- _historySync[rootIdCode][idx_synctime].HistSync[localIdCode] = SyncTime(msg, upperMsg, s);
- }
- // 计算时间同步
- long long upperTimeDelay = 0;
- if(_syncTimeMsgs[rootIdCode][idx].SyncTimeMsgs[msg.UpperIdCode()].SyncLevel() != 0)
- {
- LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_81);
- upperTimeDelay = _historySync[rootIdCode][idx_synctime].HistSync[msg.UpperIdCode()].TimeDelay();
- }
- long long sendTime = _historySync[rootIdCode][idx_synctime].HistSync[localIdCode].SendTime();
- long long receiveTime = _historySync[rootIdCode][idx_synctime].HistSync[localIdCode].ReceiveTime();
- long long timeDelay = receiveTime - sendTime - _distance[localIdCode][msg.UpperIdCode()];
- timeDelay += upperTimeDelay;
-
- // 从不同的Upper来的同步数据,跨周期判断可能不正确,将时间差控制在一个周期内
- while(timeDelay > TIME_MAX){
- timeDelay -= TIME_MAX;
- }
- while(timeDelay + TIME_MAX < 0 ){
- timeDelay += TIME_MAX;
- }
- _historySync[rootIdCode][idx_synctime].HistSync[localIdCode].TimeDelay(timeDelay);
- if(isOutputLog){
- char filename[100];
- char temp[200];
- long long aa = timeDelay;
- if(aa < 0){
- aa += TIME_MAX;
- }
- int bb = (receiveTime > TIME_MAX) ? 1 : 0;
- int cc = (sendTime > TIME_MAX) ? 1 : 0;
- int dd = msg.UpperIdCode() >> 8;
- sprintf_s(filename, "%s\\%d.log", logDir.c_str(), localIdCode>>8);
- ofstream outfile(filename, ofstream::app);
- sprintf_s(temp, "sn:%d, up:%d, rec:%I64d, snd:%I64d, del:%I64d, crt:%I64d, r_crs:%d, s_crs:%d\r", SyncNum, dd, receiveTime, sendTime, timeDelay, aa, bb, cc);
- outfile << temp;
- }
- LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_69);
- return true;
- }else{
- LOCATION_SYSTEM_BRANCH(LOCATION_SYSTEM_BRANCH_110);
- }
- return false;
- }
- unsigned long long HostServer::SyncManager::calTimeByLinar(TagMsg &tag)
- {
- EnterCriticalSection(&m_csSyncTime);
- // 获取历史记录中与此tag最近的两条
- deque<SyncTime> hisSync;
- unsigned long long rootIdCode = tag.SyncRootIdCode;
- int i = 0;
- int idx = FindSyncTimeMsg(rootIdCode, tag.SyncNum);
- if(-1 != idx){
- unordered_map<unsigned long long, SyncTimeMsg>::iterator it = _syncTimeMsgs[rootIdCode][idx].SyncTimeMsgs.find(tag.StationIdCode);
- if(it != _syncTimeMsgs[rootIdCode][idx].SyncTimeMsgs.end()){
- if(it->second.SyncLevel() == 0){
- LeaveCriticalSection(&m_csSyncTime);
- return tag.ReceiveTime;
- }
- }
- }
-
- int idx_sync = -1;
- while(hisSync.size() < 2 && i < MAX_CALCLINER_NUM)
- {
- auto syncNum = tag.SyncNum - i;
- idx_sync = FindHisSyncTime(rootIdCode, syncNum);
- if(-1 != idx_sync){
- if(_historySync[rootIdCode][idx_sync].HistSync.count(tag.StationIdCode)){
- hisSync.push_front(_historySync[rootIdCode][idx_sync].HistSync[tag.StationIdCode]);
- }
- }
- i++;
- }
- // 如果满足条件的历史记录不足两个则返回
- if(hisSync.size() < 2){
- LeaveCriticalSection(&m_csSyncTime);
- return LLONG_MAX;
- }
- // 计算预估值
- long long y1(hisSync.at(0).ReceiveTime() - hisSync.at(0).TimeDelay()),y2(hisSync.at(1).ReceiveTime() - hisSync.at(1).TimeDelay());
- long long x1(hisSync.at(0).RealReceiveTime()), x2(hisSync.at(1).RealReceiveTime()), x3(tag.ReceiveTime);
- LeaveCriticalSection(&m_csSyncTime);
- unsigned long long res;
- if(x1 > x2)
- {
- x2 += TIME_MAX;
- }
- if(x2 > x3)
- {
- x3 += TIME_MAX;
- }
- if(y1 < 0){ // 理论y值不能小于0
- y1 += TIME_MAX;
- }
- if(y2 < 0 ){
- y2 += TIME_MAX;
- }
- if(y1 > y2){
- y2 += TIME_MAX;
- }else if(y2-y1 > TIME_MAX){
- y2 -=TIME_MAX;
- }
- Eigen::Matrix3d a;
- a << x1 ,1 , 0,
- x2 ,1 , 0,
- x3, 1, -1;
- Eigen::Vector3d b(y1, y2, 0);
- Eigen::Vector3d X = a.colPivHouseholderQr().solve(b);
-
- res = X(2);
- res &= TIME_MAX;
- return res;
- }
- void HostServer::SyncManager::updateDistance(unsigned int localId, unsigned char localAntNum, unsigned int upperId, unsigned char uppderAntNum, double d)
- {
- unsigned long long lId = SyncHelper::parseId(localId, localAntNum);
- unsigned long long uId = SyncHelper::parseId(upperId, uppderAntNum);
- if(_anchors.count(lId) == 0)
- {
- _anchors[lId] = Position();
- }
- if(_anchors.count(uId) == 0)
- {
- _anchors[uId] = Position();
- }
- _distance[lId][uId] = d;
- _distance[uId][lId] = d;
- // 删除所有版本的消息记录
- _historySync.clear();
- _syncTimeMsgs.clear();
- }
- void HostServer::SyncManager::updateAnchor(unsigned int localId, unsigned char localAntNum, double x, double y, double z)
- {
- deleteAnchor(localId, localAntNum);
- unsigned long long lId = SyncHelper::parseId(localId, localAntNum);
- _anchors[lId] = Position( x, y, z);
- }
- void HostServer::SyncManager::deleteAnchor(unsigned int localId, unsigned char localAntNum)
- {
- unsigned long long lId = SyncHelper::parseId(localId, localAntNum);
- auto it = _anchors.find(lId);
- if(it == _anchors.end()) return;
- // 删除anchor
- _anchors.erase(it);
- // 删除与此anchor相关的距离信息
- _distance.erase(_distance.find(lId));
- for(auto it(_distance.begin()); it != _distance.end(); it++)
- {
- it->second.erase(it->second.find(lId));
- if(it->second.size() == 0)
- {
- it = _distance.erase(it);
- }
- }
- // 删除所有版本的消息记录
- _historySync.clear();
- _syncTimeMsgs.clear();
- }
- HostServer::SyncManager::~SyncManager()
- {
- DeleteCriticalSection(&m_csSyncTime);
- }
- int HostServer::SyncManager::FindSyncTimeMsg(unsigned long long rootIdCode, unsigned short SyncNum )
- {
- int idx = -1;
- for(int i = _syncTimeMsgs[rootIdCode].size() - 1; i >= 0; i--){
- if(_syncTimeMsgs[rootIdCode][i].SyncNum == SyncNum ){
- return i;
- }
- }
- return idx;
- }
- int HostServer::SyncManager::FindHisSyncTime(unsigned long long rootIdCode, unsigned short SyncNum )
- {
- int idx = -1;
- for(int i = _historySync[rootIdCode].size() - 1; i >= 0; i--){
- if(_historySync[rootIdCode][i].SyncNum == SyncNum ){
- return i;
- }
- }
- return idx;
- }
|