📄 database.cpp
字号:
/************************************************************************/
/* 功能:根据peerID从数据库peer表移除一个peer的信息
传入参数:类型为byte 的peerID
返回值:
TRUE: 正常返回
FALSE:异常返回
*/
/************************************************************************/
//根据peerID从数据库peer表移除一个peer的信息,成功TRUE,失败FALSE
BOOL CDataBase::DeletePeer(char *peerID)
{
CString sql;
InitDataBase();
try
{
sql.Format("delete from peer where pid='%s'",peerID);
BSTR bSql = sql.AllocSysString();
if(!Excute(bSql))
return FALSE;
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
}
// m_pConnection->Close();
return TRUE;
}
CString CDataBase::IDByteToStr(byte *bID)
{
byte temp;
CString strID;
for(int i=0;i<20;i++)
{
temp=bID[i];
int hNum=temp>>4;
int lNum=temp & 15;
strID+=IntToChar(hNum);
strID+=IntToChar(lNum);
}
return strID;
}
//将Int转化为Char
char CDataBase::IntToChar(int n)
{
CString str;
char num;
if(n<10){
str.Format("%d",n);
num=str.GetAt(0);
}
else
{
switch(n) {
case 10:
num='A';
break;
case 11:
num='B';
break;
case 12:
num='C';
break;
case 13:
num='D';
break;
case 14:
num='E';
break;
case 15:
num='F';
break;
default:
break;
}
}
return num;
}
void CDataBase::CopyArray(byte btobj[], byte btorigin[])
{
for (int i=0;i<20;i++)
{
btobj[i]=btorigin[i];
}
}
/************************************************************************/
/* 功能:根据一个peer的ID获取一个peer的ip和端口
传入参数:类型为byte 的peerID
返回节点信息: PeerItem结构体传递
struct
PeerItem{ //节点信息,返回给用户结点链表时用
char IP[20]; //节点IP
unsigned short Port; //节点端口
};
返回值:
TRUE: 正常返回
FALSE:异常返回
*/
/************************************************************************/
//根据一个peer的ID获取一个peer的ip和端口,成功TRUE,失败FALSE
BOOL CDataBase::GetPeerInfo(char *PeerID, PeerItem peer)
{
CString sql;
InitDataBase();
try
{
sql.Format("select ip,port from peer where pid='%s'",PeerID);
BSTR bSql = sql.AllocSysString();
_variant_t vIP,vPort;
m_pRecordset.CreateInstance("ADODB.Recordset");
HRESULT hr=m_pRecordset->Open(bSql,_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
if(FAILED(hr))
return FALSE;
vIP = m_pRecordset->GetCollect("ip");//取得ip字段的值
vPort = m_pRecordset->GetCollect("port");
strcpy(peer.IP,(LPCTSTR)(_bstr_t)vIP);
peer.Port=(unsigned short)vPort.lVal;
// TRACE("vip:%s,vport:%d\r\n",peer.IP,peer.Port);
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
}
m_pConnection->Close();
return TRUE;
}
//删除filepeer表项,根据pID删除fID;
BOOL CDataBase::DeletePeerFile(char *pID)
{
CString sql;
// InitDataBase();
try
{
sql.Format("delete from filepeer where pid='%s'",pID);
BSTR bSql = sql.AllocSysString();
if(!Excute(bSql))
return FALSE;
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
}
// m_pConnection->Close();
return TRUE;
}
//察看IP,分配peerID;//modify by xuhe 2.1
//查询IP是否存在,存在返回PeerID,不存在返回null;
char* CDataBase::SearchPeerIsExist(CString IP)
{
CString sql;
char *pID=new char[41];
if(pID==NULL)
AfxMessageBox("fenpei shibai");
InitDataBase();
try
{
sql.Format("select pid from peer where ip='%s'",IP);
BSTR bSql = sql.AllocSysString();
_variant_t vPID;
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open(bSql,_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
if(!m_pRecordset->adoEOF)//found
{
vPID = m_pRecordset->GetCollect("pid");//取得pid字段的值
memcpy(pID,(LPCTSTR)(_bstr_t)vPID,41);
}
else//not found;
{
// delete []pID;
pID = NULL;
}
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
}
// m_pRecordset->Close();
// m_pConnection->Close();
// CoUninitialize();
return pID;
}
//在filelist表中查询fileID是否存在,存在TRUE,不存在FALSE
BOOL CDataBase::SearchFileIDIsExist(char *fileID)
{
CString sql;
InitDataBase();
try
{
sql.Format("select * from filelist where fid='%s'",fileID);
BSTR bSql = sql.AllocSysString();
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open(bSql,_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
if(!m_pRecordset->adoEOF)
{
m_pRecordset->Close();
return TRUE;
}
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
}
// m_pConnection->Close();
return FALSE;
}
//在filepeer表查询pid,fid是否存在,存在TRUE,不存在FALSE
BOOL CDataBase::SearchFilePeerIsExist(CString pID)
{
CString sql;
// InitDataBase();
try
{
sql.Format("select * from filepeer where pid='%s'",pID);
BSTR bSql = sql.AllocSysString();
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open(bSql,_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
if(m_pRecordset->adoEOF)
{
m_pRecordset->Close();
return FALSE;
}
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
return FALSE;
}
// m_pConnection->Close();
return TRUE;
}
///* */
//插入filepeer表,文件ID与节点ID, // modify by xuhe.
BOOL CDataBase::AddFidPid(char *fID, char *pID)
{
CString sql;
// InitDataBase();
try
{
sql.Format("insert into filepeer (fid,pid) values('%s','%s')",fID,pID);
BSTR bSql = sql.AllocSysString();
if(!Excute(bSql))
return FALSE;
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
}
// m_pConnection->Close();
return FALSE;
}
BOOL CDataBase::SetStatus_1(char *pID)//设置状态值为1
{
CString sql;
InitDataBase();
try
{
sql.Format("update peer set status='1' where pid='%s'",pID);
BSTR bSql = sql.AllocSysString();
if(!Excute(bSql))
return FALSE;
// sql.Format("m_pRecordset->PutCollect(_variant_t("status"),_variant_t((long)1))");
// m_pRecordset->Update();
// BSTR bSql = sql.AllocSysString();
// if(!Excute(bSql))
// return FALSE;
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
}
// m_pConnection->Close();
return TRUE;
}
BOOL CDataBase::SearchPidIsExist(CString pID)//查询peer表中pid是否存在,若存在调用setstatus()设置在线状态为1
{
CString sql;
InitDataBase();
try
{
sql.Format("select * from peer where pid='%s'",pID);
BSTR bSql = sql.AllocSysString();
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open(bSql,_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
if(m_pRecordset->adoEOF)
{
// m_pRecordset->Close();
return FALSE;
}
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
return FALSE;
}
// m_pConnection->Close();
return TRUE;
}
int CDataBase::CountAllRegPeer()//统计所有注册节点个数
{
CString sql;
int sum_all=0;
// _variant_t vCount_all;
// InitDataBase();
try
{
sql.Format("select status from peer");
BSTR bSql = sql.AllocSysString();
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open(bSql,_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
while(!m_pRecordset->adoEOF)
{
sum_all++;
m_pRecordset->MoveNext();
}
// m_pRecordset->Close();
// m_pConnection->Close();
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
}
// m_pConnection->Close();
return sum_all;
}
int CDataBase::CountAllOnlinePeer()//统计所有在线节点个数
{
CString sql;
int sum_online=0;
// _variant_t vCount_online;
// InitDataBase();
try
{
sql.Format("select port from peer where status='1'");
BSTR bSql = sql.AllocSysString();
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open(bSql,_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
while(!m_pRecordset->adoEOF)
{
sum_online++;
m_pRecordset->MoveNext();
}
// m_pRecordset->Close();
// m_pConnection->Close();
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
}
// m_pConnection->Close();
return sum_online;
}
BOOL CDataBase::SetStatus_0(char *pID)
{
CString sql;
// InitDataBase();
try
{
sql.Format("update peer set status='0' where pid='%s'",pID);
BSTR bSql = sql.AllocSysString();
if(!Excute(bSql))
return FALSE;
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
}
// m_pConnection->Close();
return TRUE;
}
BOOL CDataBase::CloseDataBase()
{
m_pConnection->Close();
return TRUE;
}
void CDataBase::IDStrToByte(byte *id, CString strID)
{
for(int i=0,d=0;i<40;)
{
char hight=strID.GetAt(i++);
char low=strID.GetAt(i++);
int hNum=HexToInt(hight);
int lNum=HexToInt(low);
id[d++]= (byte)(hNum<<4) | lNum;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -