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

📄 remoteadmindoc.cpp

📁 vc++网络程序设计实例详解 人民邮电出版社1-2章源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// RemoteAdminDoc.cpp : implementation of the CRemoteAdminDoc class
//

#include "stdafx.h"
#include "RemoteAdminDoc.h"
#include "MachineInfo.h"
#include "GlobalMFCHelperFunc.h"
#include "GlobalHelperFunc.h"
#include "MachineView.h"
#include "resource.h"
#include "RemoteAdminView.h"
#include "MainFrame.h"
#include "RemoteAdmin.h"
#include "ProgressWndThread.h"
#include "ProcessUpdate.h"
#include "ConnectionThread.h"
#include <process.h>

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


extern CRITICAL_SECTION g_CriticalSection;
/////////////////////////////////////////////////////////////////////////////
// CRemoteAdminDoc

IMPLEMENT_DYNCREATE(CRemoteAdminDoc, CDocument)

BEGIN_MESSAGE_MAP(CRemoteAdminDoc, CDocument)
	//{{AFX_MSG_MAP(CRemoteAdminDoc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRemoteAdminDoc construction/destruction

CRemoteAdminDoc::CRemoteAdminDoc()
{
	m_hUpdateProcessList    = NULL;
	m_pVisualProgressThread = NULL;
}

CRemoteAdminDoc::~CRemoteAdminDoc()
{
}

BOOL CRemoteAdminDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;
	
    unsigned int threadID;

    /*
    m_threadForUpdateProcessList = ::AfxBeginThread(
                                        UpdateProcessListForAllMachines, 
                                        this, 
                                        THREAD_PRIORITY_NORMAL, 
                                        0,
                                        CREATE_SUSPENDED
    */                                       // );

	// Prepare the SDocView structure to pass wrapped pointers values of the
	// document, view(s), mainframe. They are being sent this way as a worker thread
	// can't call the functions UI (GetMachineView, GetMainFrame  etc)
	// involving windows, because of crashes, hence this workaround!
    SDocView* pDocView         = new SDocView;
    pDocView->pDoc             = this;
    pDocView->pMachineView     = MFC_DocView::GetMachineView();
    pDocView->pRemoteAdminView = MFC_DocView::GetRemoteAdminView();

    m_hUpdateProcessList = reinterpret_cast<HANDLE>(::_beginthreadex(
                                                        NULL, 
                                                        0, 
														ProcessUpdate::UpdateProcessListForAllMachines, 
                                                        pDocView, 
                                                        CREATE_SUSPENDED, 
                                                        &threadID)
                                                        );

	return TRUE;
}


void CRemoteAdminDoc::CreateVisualThread()
{
	if (m_pVisualProgressThread == NULL)
	{
		m_pVisualProgressThread = static_cast<CProgressWndThread*>(::AfxBeginThread(RUNTIME_CLASS(CProgressWndThread), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED));

		if (m_pVisualProgressThread != NULL)
		{
			m_pVisualProgressThread->SetDocument(this);
			m_pVisualProgressThread->ResumeThread();
		}
	}
}



/////////////////////////////////////////////////////////////////////////////
// CRemoteAdminDoc serialization

void CRemoteAdminDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
        CString* pstrConnctedMachinesIP = NULL;
        int iNumberOfConnectedMachines  = 0;

        GetConnectedMachinesIP(&pstrConnctedMachinesIP, &iNumberOfConnectedMachines);
		
        // Serialize the number of machines last beging monitored
        ar << iNumberOfConnectedMachines;

        // Serialize the Machine Informations
        for (int i = 0; i < iNumberOfConnectedMachines; ++i)
        {
            CMachineInfo* pMachineInfo = m_RemoteAdministrator.GetMachineInfo(pstrConnctedMachinesIP[i]);
            ar << pMachineInfo;
        }

		// Free memory
		if (pstrConnctedMachinesIP != NULL)
		{
			delete[] pstrConnctedMachinesIP;
		}
	}
	else
	{
        m_milInfoReadFromArchiveList   = new CMachineInfoList;
        int iNumberOfConnectedMachines = 0;
        CMachineInfo* pMachineInfo     = NULL;
		
        ar >> iNumberOfConnectedMachines;

        for (int i = 0; i < iNumberOfConnectedMachines; ++i)
        {
            ar >> pMachineInfo;
            m_milInfoReadFromArchiveList->AddTail(pMachineInfo);
        }
	}
}


/////////////////////////////////////////////////////////////////////////////
// CRemoteAdminDoc diagnostics

#ifdef _DEBUG
void CRemoteAdminDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CRemoteAdminDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG


void CRemoteAdminDoc::AddMachine(CMachineInfo& miMachineInfo)
{
    m_RemoteAdministrator.AddMachine(miMachineInfo);
}

void CRemoteAdminDoc::DeleteMachine(CMachineInfo& miMachineInfo)
{
    m_RemoteAdministrator.DeleteMachine(miMachineInfo);
}

BOOL CRemoteAdminDoc::CheckIfMachinePresent(CString strIP)
{
    BOOL bSuccess = m_RemoteAdministrator.CheckIfMachinePresent(strIP);

    return bSuccess;
}

BOOL CRemoteAdminDoc::EstablishAllConnections(CString strRemoteMachineIP, CString strPwd, BOOL bEstablish, CMachineView* pMachineView, CRemoteAdminView* pRemoteAdminView)
{
    ASSERT(pRemoteAdminView);
    ASSERT(pMachineView);
    
    BOOL bConnectionSuccess      = FALSE;
    BOOL bCopyServiceExe         = FALSE;
    BOOL bStartAndInstallService = FALSE;
    BOOL bConnectToRemoteService = FALSE;

    // The service may be running already, so try to connect directly
    bConnectToRemoteService = ConnectToRemoteService(strRemoteMachineIP, strPwd, pMachineView, pRemoteAdminView);

    if (!bConnectToRemoteService)
    {
        bConnectionSuccess = m_RemoteAdministrator.EstablishAllConnections(strRemoteMachineIP, strPwd, bEstablish);
		if (!bConnectionSuccess)
		{
			CString strFormattedErrorMsg = ErrorHandling::ConvertStringTableIDToErrorMsg(strRemoteMachineIP, IDS_CONNECTION_NOT_ESTABLISHED);

			//CRemoteAdminApp* pApp = static_cast<CRemoteAdminApp*>(::AfxGetApp());
			(MFC_DocView::GetApp())->ShowBalloonMsgInTray(_T("Remote connection error !"), strFormattedErrorMsg);
		}


		bCopyServiceExe = m_RemoteAdministrator.CopyServiceExeToRemoteMachine(strRemoteMachineIP);
		if (!bCopyServiceExe)
		{
			CString strFormattedErrorMsg = ErrorHandling::ConvertStringTableIDToErrorMsg(strRemoteMachineIP, IDS_NOT_COPY_SERVICE_EXE);

			//CRemoteAdminApp* pApp = static_cast<CRemoteAdminApp*>(::AfxGetApp());
			(MFC_DocView::GetApp())->ShowBalloonMsgInTray(_T("Service executable copying error !"), strFormattedErrorMsg);
		}


        bStartAndInstallService = m_RemoteAdministrator.InstallAndStartRemoteService(strRemoteMachineIP);
		if (!bStartAndInstallService)
		{
			CString strFormattedErrorMsg = ErrorHandling::ConvertStringTableIDToErrorMsg(strRemoteMachineIP, IDS_NOT_START_SERVICE);

			//CRemoteAdminApp* pApp = static_cast<CRemoteAdminApp*>(::AfxGetApp());
			(MFC_DocView::GetApp())->ShowBalloonMsgInTray(_T("Remote service starting error !"), strFormattedErrorMsg);
		}
		
		// Do some time pass, for stabilization at the remote end
        ::Sleep(2000);

        bConnectToRemoteService = ConnectToRemoteService(strRemoteMachineIP, strPwd, pMachineView, pRemoteAdminView);
		if (!bConnectToRemoteService)
		{
			CString strFormattedErrorMsg = ErrorHandling::ConvertStringTableIDToErrorMsg(strRemoteMachineIP, IDS_NO_CONNECT_TO_REMOTE_SERVICE);

			//CRemoteAdminApp* pApp = static_cast<CRemoteAdminApp*>(::AfxGetApp());
			(MFC_DocView::GetApp())->ShowBalloonMsgInTray(_T("Connection error !"), strFormattedErrorMsg);
		}

        if (bConnectionSuccess && bCopyServiceExe && bStartAndInstallService && bConnectToRemoteService)
        {
            return TRUE;            
        }
    }

    return bConnectToRemoteService;
}

BOOL CRemoteAdminDoc::CopyServiceExeToRemoteMachine(CString strRemoteMachineIP)
{
    return m_RemoteAdministrator.CopyServiceExeToRemoteMachine(strRemoteMachineIP);
}

void CRemoteAdminDoc::RefreshProcessList(CString strRemoteMachineIP, CProcessInfoList& pilList)
{
    m_RemoteAdministrator.RefreshProcessList(strRemoteMachineIP, pilList);
}

CProcessInfoList* CRemoteAdminDoc::GetProcessInfoList(CString strRemoteMachineIP)

⌨️ 快捷键说明

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