Server.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:6k
- #include "stdafx.h"
- #include "Server.h"
- #include "ServerDlg.h"
- #include "adodataset.h"
- #include "netaddress.h"
- BEGIN_MESSAGE_MAP(CServerApp, CWinApp)
- //{{AFX_MSG_MAP(CServerApp)
- //}}AFX_MSG
- ON_COMMAND(ID_HELP, CWinApp::OnHelp)
- END_MESSAGE_MAP()
- CServerApp::CServerApp()
- {
- m_serverid = 1;
- }
- CServerApp theApp;
- BOOL CServerApp::InitInstance()
- {
- _unindex = 0;
- //////////////////////////////////////////////////////////////////////////
- if(this->m_oappinst.Exists())
- {
- m_serverid = -1;
- ::MessageBox(NULL, "系统中已经有一个trfServer实例在运行...", "trfServer v1.0", MB_OK | MB_ICONINFORMATION);
- return FALSE;
- }
- //////////////////////////////////////////////////////////////////////////
- //open the log file in current module file path
- char szfile[_MAX_PATH], szpath[_MAX_PATH];
- memset(szfile, 0x0, sizeof(szfile));
- GetModuleFileName(GetModuleHandle(NULL), szfile, _MAX_PATH-1);
- ExtractFilePath(szfile, szpath, _MAX_PATH);
- _snprintf(szfile, _MAX_PATH-1, "%s\%s", szpath, "trfServer.log");
- ::DeleteFile(szfile);
- m_syslog.SetFile(szfile, false);
- m_syslog.SetMode(VNCLog::ToFile);
- m_syslog.SetLevel((int)LL_ERROR);
- m_syslog.Print(LL_INFO, "CServerApp::InitInstance : 开始初始化trfServer系统...n");
- //////////////////////////////////////////////////////////////////////////
-
- //Initialize the socket library.
- if(!Initialize())
- {
- m_syslog.Print(LL_ERROR, "CServerApp::InitInstance : 初始化Win32 Socket库失败...n");
- return FALSE;
- }
- m_syslog.Print(LL_INFO, "CServerApp::InitInstance : 初始化Win32 Socket库成功.n");
- m_syslog.Print(LL_INFO, "CServerApp::InitInstance : 初始化trfServer数据库连接...n");
- //create the db connection.
- const char szConnEnv[] = "Provider=MSDASQL.1;Persist Security Info=True;User ID=eftp;Password=888888;Data Source=eFtp";
- try
- {
- if(!this->m_dbcont.Create(szConnEnv))
- {
- m_syslog.Print(LL_ERROR, "CServerApp::InitInstance : 建立trfServer数据库连接失败...n");
- return FALSE;
- }
- m_syslog.Print(LL_INFO, "CServerApp::InitInstance : 建立trfServer数据库连接成功.n");
- }
- catch (...)
- {
- m_syslog.Print(LL_ERROR, "CServerApp::InitInstance : 建立trfServer数据库连接失败...n");
- return FALSE;
- }
- //////////////////////////////////////////////////////////////////////////
- try
- {
- m_syslog.Print(LL_INFO, "CServerApp::InitInstance : 正在注册trfServer服务...n");
- //register the server information.
- char szsqltext[MAX_SQL_SIZE];
-
- CADODataSet rs;
- rs.SetConnection(this->m_dbcont);
- string localip = CXNetAddress::GetLocalHostIp();
-
- _snprintf(szsqltext, MAX_SQL_SIZE-1, SQL::SERVER_REG_EXISTS, localip.data());
- if(!rs.Open(szsqltext))
- {
- m_syslog.Print(LL_WARNING, "CServerApp::InitInstance : 本机已有一个trfServer服务被注册过.n");
- return FALSE;
- }
- //if the server isn't registered in the db server yet then register it.
- if(rs.Fields().FieldByName("SVRCOUNT").getValue().AsInteger() == 0)
- {
- rs.Close();
- if(!rs.Open(SQL::SERVER_REG_GETSVRCNT))
- {
- return FALSE;
- }
- if(rs.Fields().FieldByName("SVRCOUNT").getValue().AsInteger() > 0)
- {
- rs.Close();
-
- if(!rs.Open(SQL::SERVER_REG_GETMAXID))
- {
- return FALSE;
- }
- m_serverid = rs.Fields().FieldByName("MAXSVRID").getValue().AsInteger()+1;
-
- rs.Close();
- }
- else rs.Close();
-
- sprintf(szsqltext, SQL::SERVER_REG_REGISTER, m_serverid, localip.c_str(), 0);
-
- if(!rs.Open(szsqltext))
- {
- return FALSE;
- }
- rs.Close();
- m_syslog.Print(LL_INFO, "CServerApp::InitInstance : 本机服务注册成功,服务ID号=%dn", this->m_serverid);
- }
- }
- catch (_com_error e) {
- m_syslog.Print(LL_ERROR, "CServerApp::InitInstance : %sn", e.ErrorMessage());
- return FALSE;
- }
- m_syslog.Print(LL_INFO, "CServerApp::InitInstance : trfServer服务启动成功!!!n");
- //////////////////////////////////////////////////////////////////////////
- AfxEnableControlContainer();
- #ifdef _AFXDLL
- Enable3dControls(); // Call this when using MFC in a shared DLL
- #else
- Enable3dControlsStatic(); // Call this when linking to MFC statically
- #endif
- CServerDlg dlg;
- m_pMainWnd = &dlg;
- dlg.DoModal();
- return FALSE;
- }
- int CServerApp::ExitInstance()
- {
- if(-1 != m_serverid && this->m_dbcont.IsActived())
- {
- m_syslog.Print(LL_INFO, "CServerApp::ExitInstance: 准备结束服务...n");
-
- //////////////////////////////////////////////////////////////////////////
-
- //unregister current server information.
- char szsqltext[MAX_SQL_SIZE];
-
- CADODataSet rs;
- rs.SetConnection(this->m_dbcont);
-
- try
- {
- sprintf(szsqltext, SQL::SERVER_REG_UNREGISTER, this->m_serverid);
-
- if(!rs.Open(szsqltext))
- {
- m_syslog.Print(LL_ERROR, "CServerApp::ExitInstance: 反注销服务失败,原因是数据库访问失败...n");
- return 1;
- }
- rs.Close();
- }
- catch (_com_error e)
- {
- m_syslog.Print(LL_ERROR, "CServerApp::ExitInstance: %sn", e.ErrorMessage());
- return 1;
- }
-
- //////////////////////////////////////////////////////////////////////////
-
- //uninitialize the socket library.
- UnInitialize();
-
- //destroy the db connection.
- this->m_dbcont.Destroy();
-
- m_syslog.Print(LL_INFO, "CServerApp::ExitInstance: trfServer服务退出!!!n");
- }
- return CWinApp::ExitInstance();
- }
- void CServerApp::_GetTempFileName(LPCSTR lpcsPostfix, LPSTR lpsTempFile)
- {
- assert(NULL != lpcsPostfix && lpsTempFile != NULL);
-
- char lpcurdir[MAX_PATH];
- memset(lpcurdir, 0x0, sizeof(lpcurdir));
- GetCurrentDirectory(sizeof(lpcurdir), lpcurdir);
- do
- {
- InterlockedIncrement(&_unindex);
-
- sprintf(lpsTempFile, "%s\tmp%x.%s", lpcurdir, _unindex, lpcsPostfix);
- }
- while(FileExists(lpsTempFile));
- }