⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datatodb.cpp

📁 CCAMS系统是一种用于局域网下的C/S模式的软件管理和监测系统。它包括客户端和服务端
💻 CPP
字号:

#include "stdafx.h"
#include "DataToDB.h"

// 全局变量, 用来访问数据库
// 
CSetClient	gm_setClient;
CSetCPU		gm_setCpu;
CSetFile	gm_setFile;
CSetMail	gm_setMail;
CSetNet		gm_setNet;
CSetProc	gm_setProc;

// 临界区, 用来控制访问数据库冲突, 分别对应六张表
CRITICAL_SECTION g_criClientAccess;
CRITICAL_SECTION g_criCpuAccess;
CRITICAL_SECTION g_criFileAccess;
CRITICAL_SECTION g_criMailAccess;
CRITICAL_SECTION g_criNetAccess;
CRITICAL_SECTION g_criProcAccess;


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// * * * * * * * * * * * *
// * * * * * * * * * * * *
bool DataToDB::AddClientData( LPCTSTR  ip )
{
	CString strIP = ip;

	EnterCriticalSection( &g_criClientAccess );
try
{
	// throw( CDBException, CMemoryException );
	if ( !gm_setClient.IsOpen() )
		gm_setClient.Open();
	gm_setClient.Requery();

	while ( !gm_setClient.IsEOF() )
	{
		// 如果已经存在该主机的记录, 修改最后一次登陆时间, 退出
		if ( strIP == gm_setClient.m_ip )
		{
			gm_setClient.Edit();
//			gm_setClient.m_ip = strIP.GetBuffer(0);
			CTime t = CTime::GetCurrentTime();	//读取服务器当前时间
			gm_setClient.m_logtime = t.Format( "%H:%M:%S" );
			gm_setClient.Update(); // throw( CDBException );

			LeaveCriticalSection( &g_criClientAccess );
			return false;
		}
		gm_setClient.MoveNext();
	}
	gm_setClient.Close();

	// 重新打开, 插入主机记录
	// throw( CDBException, CMemoryException );
	if ( !gm_setClient.Open() || !gm_setClient.CanAppend() )
	{
		gm_setClient.Close();
		LeaveCriticalSection( &g_criClientAccess );
		return false;
	}

	gm_setClient.AddNew(); // throw( CDBException );

	CString str = ip;
	gm_setClient.m_ip = str.GetBuffer(0);
	CTime t = CTime::GetCurrentTime();	//读取服务器当前时间
	gm_setClient.m_logtime = t.Format( "%H:%M:%S" );

	gm_setClient.Update(); // throw( CDBException );
	gm_setClient.Close();
}
catch(...) 
{
	gm_setClient.Close();
	LeaveCriticalSection( &g_criClientAccess );
	return false;
}
	gm_setClient.Close();
	LeaveCriticalSection( &g_criClientAccess );
	return true;
}

// * * * * * * * * * * * *
// * * * * * * * * * * * *
bool DataToDB::AddCpuData( LPCTSTR  client, LPCTSTR  time, int usage )
{
	EnterCriticalSection( &g_criCpuAccess );
try
{
	if ( !gm_setCpu.IsOpen() )
		gm_setCpu.Open();

	if ( !gm_setCpu.CanAppend() )
	{
		LeaveCriticalSection( &g_criCpuAccess );
		return false;
	}

	gm_setCpu.AddNew(); // throw( CDBException );

	gm_setCpu.m_client = client;
	gm_setCpu.m_time = time;
	gm_setCpu.m_usage = usage;

	gm_setCpu.Update(); // throw( CDBException );
} // try
catch(...)
{
	gm_setCpu.Close();
	LeaveCriticalSection( &g_criCpuAccess );
	return false;
} // catch
	gm_setCpu.Close();
	LeaveCriticalSection( &g_criCpuAccess );
	return true;
}

// * * * * * * * * * * * *
// * * * * * * * * * * * *
bool DataToDB::AddFileData( LPCTSTR  client, LPCTSTR  time, LPCTSTR  action, LPCTSTR  path )
{
	EnterCriticalSection( &g_criFileAccess );
try
{
	if ( !gm_setFile.IsOpen() )
		gm_setFile.Open();

	if ( !gm_setFile.CanAppend() )
	{
		LeaveCriticalSection( &g_criFileAccess );
		return false;
	}

	gm_setFile.AddNew(); // throw( CDBException );

	gm_setFile.m_client = client;
	gm_setFile.m_time = time;
	gm_setFile.m_action = action;
	gm_setFile.m_path = path;

	gm_setFile.Update(); // throw( CDBException );
} // try
catch(...)
{
	gm_setFile.Close();
	LeaveCriticalSection( &g_criFileAccess );
	return false;
}
	gm_setFile.Close();
	LeaveCriticalSection( &g_criFileAccess );
	return true;
}


// * * * * * * * * * * * *
// * * * * * * * * * * * *
bool DataToDB::AddMailData( LPCTSTR  client, LPCTSTR  time, LPCTSTR  subject,
						   LPCTSTR  sender, LPCTSTR  recipient, LPCTSTR  attach)
{
	EnterCriticalSection( &g_criMailAccess );
try
{
	if ( !gm_setMail.IsOpen() )
		gm_setMail.Open();

	if ( !gm_setMail.CanAppend() )
	{
		LeaveCriticalSection( &g_criMailAccess );
		return false;
	}

	gm_setMail.AddNew(); // throw( CDBException );

	gm_setMail.m_client = client;
	gm_setMail.m_time = time;
	gm_setMail.m_subject = subject;
	gm_setMail.m_sender = sender;
	gm_setMail.m_recipient = recipient;
	gm_setMail.m_attach = attach;

	gm_setMail.Update(); // throw( CDBException );
} // try
catch(...)
{
	gm_setMail.Close();
	LeaveCriticalSection( &g_criMailAccess );
	return false;
} // catch
	gm_setMail.Close();
	LeaveCriticalSection( &g_criMailAccess );
	return true;
}

// * * * * * * * * * * * *
// * * * * * * * * * * * *
bool DataToDB::AddNetData( LPCTSTR  client, LPCTSTR  time, int repeat, LPCTSTR  direction,
						  LPCTSTR  protocol, int tcpflag, LPCTSTR  address, 
						  int port, int size )
{
	EnterCriticalSection( &g_criNetAccess );
try
{
	if ( !gm_setNet.IsOpen() )
		gm_setNet.Open();

	if ( !gm_setNet.CanAppend() )
	{
		LeaveCriticalSection( &g_criNetAccess );
		return false;
	}

	gm_setNet.AddNew(); // throw( CDBException );

	gm_setNet.m_client = client;
	gm_setNet.m_time = time;
	gm_setNet.m_repeat = repeat;
	gm_setNet.m_direction = direction;
	gm_setNet.m_protocol = protocol;
	gm_setNet.m_tcpflag = tcpflag;
	gm_setNet.m_address = address;
	gm_setNet.m_port = port;
	gm_setNet.m_size = size;

	gm_setNet.Update(); // throw( CDBException );
} // try
catch(...)
{
	gm_setNet.Close();
	LeaveCriticalSection( &g_criNetAccess );
	return false;
} // catch
	gm_setNet.Close();
	LeaveCriticalSection( &g_criNetAccess );
	return true;
}

// * * * * * * * * * * * *
// * * * * * * * * * * * *
bool DataToDB::AddNProcData( LPCTSTR  client, LPCTSTR  time, LPCTSTR  action, LPCTSTR  path )
{
	EnterCriticalSection( &g_criProcAccess );
try
{
	if ( !gm_setProc.IsOpen() )
		gm_setProc.Open();

	if ( !gm_setProc.CanAppend() )
	{
		LeaveCriticalSection( &g_criProcAccess );
		return false;
	}

	gm_setProc.AddNew(); // throw( CDBException );

	gm_setProc.m_client = client;
	gm_setProc.m_time = time;
	gm_setProc.m_action = action;
	gm_setProc.m_path = path;

	gm_setProc.Update(); // throw( CDBException );
} // try
catch(...)
{
	gm_setProc.Close();
	LeaveCriticalSection( &g_criProcAccess );
	return false;
} // catch
	gm_setProc.Close();
	LeaveCriticalSection( &g_criProcAccess );
	return true;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -