📄 globalmanager.cpp
字号:
#include "globalmanager.h"
#include ".\globalmanager.h"
CGlobalManager::CGlobalManager(CBinaryTree& tree,CKeyGenerator& kg,CEncryptor& encryptor)
:m_tree(tree),m_keyGenerator(kg),m_encryptor(encryptor),m_avgEncrypedTimes(0),m_respondCount(0)
{
}
CGlobalManager::~CGlobalManager(void)
{
}
CNode* CGlobalManager::InsertLeaf(void)
{
CNode* leafAdded=m_tree.AddNewLeaf();
//为新生成的节点分配一个密钥
if(leafAdded!=0)
{
leafAdded->m_key=m_keyGenerator.GenerateNewKey();
if(leafAdded->m_parent!=0)
leafAdded->m_parent->m_key=m_keyGenerator.GenerateNewKey();
UpdateAncestors(leafAdded);
}
return leafAdded;
}
bool CGlobalManager::DeleteLeaf(int nodeID,bool UpdateKeysRightNow)
{
CNode* leafToDelete=m_tree.GetNode(nodeID);
if(leafToDelete!=0 && leafToDelete->m_rightchild==0 && leafToDelete->m_leftchild==0)
{
//更新所有祖先节点的密钥
CNode* parent=leafToDelete->m_parent;
UpdateAncestors(parent);
//移除节点
return m_tree.RemoveExistedLeaf(leafToDelete);
}
return false;
}
void CGlobalManager::UpdateAncestors(CNode* node)
{
if(node!=0)
{
node=node->m_parent;
while(node)
{
node->m_key=m_keyGenerator.GenerateNewKey();
node=node->m_parent;
}
}
}
void CGlobalManager::UpdateDescendants(CNode* subRoot)
{
if(subRoot!=0)
{
if(subRoot->m_leftchild==0 && subRoot->m_rightchild==0)
UpdateLeafKeys(subRoot);
else
{
UpdateDescendants(subRoot->m_leftchild);
UpdateDescendants(subRoot->m_rightchild);
}
}
}
void CGlobalManager::UpdateLeafKeys(CNode* leaf)
{
if (leaf!=0)
{
leaf->m_keyList.clear();
CNode* prev=leaf;
CNode* now=leaf->m_parent;
if(now!=0)
{
int packetEncrypted=m_encryptor.EncryptIt(now->m_key,prev->m_key);
int packetDecrypted=m_encryptor.DecryptIt(packetEncrypted,prev->m_key);
leaf->m_keyList.push(packetDecrypted);
prev=now;
now=now->m_parent;
while(now)
{
//加密父亲节点的密钥
int packetEncrypted=m_encryptor.EncryptIt(now->m_key,prev->m_key);
//用叶子节点中keyList中栈顶元素解密
int packetDecrypted=m_encryptor.DecryptIt(packetEncrypted,leaf->m_keyList.top());
//将解密后的密钥压入keyList中
leaf->m_keyList.push(packetDecrypted);
prev=now;
now=now->m_parent;
}
}
}
}
void CGlobalManager::AddMember(unsigned int time)
{
m_statisticManager.AddInStatistic(time);
}
void CGlobalManager::RemoveMember(int nodeID,unsigned int time)
{
m_statisticManager.AddOutStatistic(nodeID,time);
}
void CGlobalManager::Respond(void)
{
while(m_statisticManager.GetInAmount()>0 && m_statisticManager.GetOutAmount()>0)
{
//取输入、输出两个队列首部的时间,先绘制小者
if(m_statisticManager.GetFirstInTime()<m_statisticManager.GetFirstOutTime())
{
m_statisticManager.RemoveInStatistic();
InsertLeaf();
double totalEncrypedTimes=(m_avgEncrypedTimes*m_respondCount);
m_avgEncrypedTimes=(totalEncrypedTimes+(m_tree.GetHeight()*2))/(++m_respondCount);
}
else
{
int nodeID=m_statisticManager.RemoveOutStatistic();
DeleteLeaf(nodeID);
double totalEncrypedTimes=(m_avgEncrypedTimes*m_respondCount);
m_avgEncrypedTimes=(totalEncrypedTimes+(m_tree.GetHeight()*2-1))/(++m_respondCount);
}
}
while(m_statisticManager.GetInAmount())
{
m_statisticManager.RemoveInStatistic();
InsertLeaf();
double totalEncrypedTimes=(m_avgEncrypedTimes*m_respondCount);
m_avgEncrypedTimes=(totalEncrypedTimes+(m_tree.GetHeight()*2))/(++m_respondCount);
}
while(m_statisticManager.GetOutAmount())
{
int nodeID=m_statisticManager.RemoveOutStatistic();
DeleteLeaf(nodeID);
double totalEncrypedTimes=(m_avgEncrypedTimes*m_respondCount);
m_avgEncrypedTimes=(totalEncrypedTimes+(m_tree.GetHeight()*2-1))/(++m_respondCount);
}
//在树构建完毕后,更新所有叶子节点
UpdateDescendants(m_tree.m_root);
}
double CGlobalManager::GetAverageWaitedTime(void)
{
return m_statisticManager.GetAverageWaitedTime();
}
void CGlobalManager::ResetStatist(void)
{
m_statisticManager.ResetTime();
}
double CGlobalManager::GetTotalAverageWaitedTime(void)
{
return m_statisticManager.GetTotalAverageWaitedTime();
}
void CGlobalManager::ResetEncrypedTimes()
{
this->m_avgEncrypedTimes=0;
this->m_respondCount=0;
}
double CGlobalManager::GetAverageEncrypedTimes()
{
return this->m_avgEncrypedTimes;
}
int CGlobalManager::GetTotalRespondCounts(void)
{
return m_statisticManager.GetTotalRespondCounts();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -