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

📄 remoteadmindoc.cpp

📁 vc++网络程序设计实例详解 人民邮电出版社1-2章源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
{
    return m_RemoteAdministrator.GetProcessInfoList(strRemoteMachineIP);
}

void CRemoteAdminDoc::DeleteAndDisconnectAllMachines()
{
    m_RemoteAdministrator.DeleteAndDisconnectAllMachines();
}

void CRemoteAdminDoc::DeleteAndDisconnectMachine(CString strRemoteAdminMachine)
{
    m_RemoteAdministrator.DeleteAndDisconnectMachine(strRemoteAdminMachine);
}

void CRemoteAdminDoc::OnCloseDocument() 
{
	DeleteAndDisconnectAllMachines();
	
	CDocument::OnCloseDocument();
}

BOOL CRemoteAdminDoc::ConnectToRemoteService(CString strRemoteMachineIP, CString strPwd, CMachineView* pMachineView, CRemoteAdminView* pRemoteAdminView)
{
    CMachineInfo* pMachineInfo = new CMachineInfo;

    pMachineInfo->SetLogon(_T("Administrator"));
    pMachineInfo->SetIP(strRemoteMachineIP);
    pMachineInfo->SetPassword(strPwd);
    m_RemoteAdministrator.AddMachine(*pMachineInfo);

    BOOL bConnectToRemoteService = FALSE;
    bConnectToRemoteService = m_RemoteAdministrator.ConnectToRemoteService(strRemoteMachineIP, 1, 0);

    if (bConnectToRemoteService)
    {
        pMachineView->ShowNewMachine(strRemoteMachineIP, pMachineInfo);
        
        HTREEITEM h = pMachineView->GetTreeItemForText(strRemoteMachineIP);
        if (h != NULL)
        {
            pMachineView->GetTreeCtrl().Select(h, TVGN_CARET);
            pRemoteAdminView->GetListCtrl().DeleteAllItems();
            pRemoteAdminView->GetListCtrl().InsertItem(0, _T("Refreshing process list..."));
   
            ::Sleep(2000);   
			
			if (pMachineInfo)
			{
				delete pMachineInfo;
			}
				

            return TRUE;
        }
        else
        {
			if (pMachineInfo)
			{
				delete pMachineInfo;
			}
            return FALSE;
        }
    }
    else
    {
        m_RemoteAdministrator.DeleteMachine(*pMachineInfo);

		if (pMachineInfo)
		{
			delete pMachineInfo;
		}

        return FALSE;
    }
}

void CRemoteAdminDoc::GetConnectedMachinesIP(CString** pstrConnctedMachinesIP /*out*/, int* piNumberOfConnectedMachines/*out*/)
{
    m_RemoteAdministrator.GetConnectedMachinesIP(pstrConnctedMachinesIP, piNumberOfConnectedMachines);
}

int CRemoteAdminDoc::GetTotalMachinesMonitored()
{
    return m_RemoteAdministrator.GetTotalMachinesMonitored();
}

BOOL CRemoteAdminDoc::AddToConnectionPendingList(CString strIP)
{
    return m_RemoteAdministrator.AddToConnectionPendingList(strIP);
}

BOOL CRemoteAdminDoc::RemoveFromConnecionPendingList(CString strIP)
{
    return m_RemoteAdministrator.RemoveFromConnecionPendingList(strIP);
}

BOOL CRemoteAdminDoc::IsConnectionPending(CString strIP)
{
    return m_RemoteAdministrator.IsConnectionPending(strIP);
}

BOOL CRemoteAdminDoc::OnSaveDocument(LPCTSTR lpszPathName) 
{
	return CDocument::OnSaveDocument(lpszPathName);
}


BOOL CRemoteAdminDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	// << BLOCK start
	// << Commented by Prateek Kaul on July 28, 2003.
	// It is better to make a clean exit (making the thread return), rather than terminating
	// the thread forcefully.
    // Kill the thread that is updating the process list
    /*if (m_hUpdateProcessList != NULL)
    {
        ::TerminateThread(m_hUpdateProcessList, 0);
        m_hUpdateProcessList = NULL;
    }*/
	// << BLOCK end

	// Make the ProcessUpdate::UpdateProcessListForAllMachines(), i.e the thread function
	// that updates the process list for all machines end (return gracefully)
	// by setting the flag, rather terminate it forcefully 
	extern BOOL g_bUpdateProcessList;
	g_bUpdateProcessList = FALSE;

	// Wait for few secs to allow the thread terminate, it may be in the middle of a
	// calculation
	::Sleep(3000);

	// No need to keep a track of the ProcessUpdate::UpdateProcessListForAllMachines() thread handle
	if (m_hUpdateProcessList != NULL)
    {
		m_hUpdateProcessList = NULL;
    }

	// Close all connections and clean up the data structures for all
	// connected machines
	DeleteAndDisconnectAllMachines();

	// Allow the new ProcessUpdate::UpdateProcessListForAllMachines() thread function, which will
	// be triggered below, to proceed gracefully.
	g_bUpdateProcessList = TRUE;

    if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;

	// Since the MachineInfo List has been serailzed from
    // the archive, we need to make connections for the machines
    // in the list.
    CMachineInfo* pMachineInfo = NULL;
    POSITION pos = m_milInfoReadFromArchiveList->GetHeadPosition();

    // Start connections to the Machines whose CMachineInfo class have
    // been read from the archive. They have been read in the m_milInfoReadFromArchiveList.
    while (pos != NULL)
    {
        pMachineInfo = m_milInfoReadFromArchiveList->GetNext(pos);
       
        SConnectInfo* pConnectInfo     = new SConnectInfo;
        pConnectInfo->pDoc             = MFC_DocView::GetAppDocument();
        pConnectInfo->pMachineView     = MFC_DocView::GetMachineView();
        pConnectInfo->pRemoteAdminView = MFC_DocView::GetRemoteAdminView();
        pConnectInfo->strIP            = pMachineInfo->GetIP();
        pConnectInfo->strPwd           = pMachineInfo->GetPassword();

        // Start the thread that will process the connection
        unsigned threadID = 0;
        ::_beginthreadex(NULL, 0, ThreadConnection::ConnectToMachine, pConnectInfo, 0, &threadID);
    }
	
    // Since all machines have been read from the archive, dispose off the list.
    if (m_milInfoReadFromArchiveList != NULL)
    {
		CMachineInfo* pMachineInfo = NULL;
		POSITION pos = m_milInfoReadFromArchiveList->GetHeadPosition();
		while (pos != NULL)
		{
			pMachineInfo = m_milInfoReadFromArchiveList->GetNext(pos);

			ASSERT(pMachineInfo != NULL);

			if (pMachineInfo != NULL)
			{
				delete pMachineInfo;
			}
		}

        delete m_milInfoReadFromArchiveList;
        m_milInfoReadFromArchiveList = NULL;
    }
    
    // Now start the thread that retrieves the processes running on the remotely 
    // connected machines, in suspended state
    unsigned int threadID;
    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)
                                                        );
	CreateVisualThread();

	return TRUE;
}

void CRemoteAdminDoc::DeleteContents() 
{
    DeleteAndDisconnectAllMachines();
    	
	CDocument::DeleteContents();
}

BOOL CRemoteAdminDoc::CanCloseFrame(CFrameWnd* pFrame) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CDocument::CanCloseFrame(pFrame);
}

CString CRemoteAdminDoc::GetPasswordForMachine(CString strIP)
{
	return m_RemoteAdministrator.GetPassword(strIP);
}

BOOL CRemoteAdminDoc::AreConnectionsStillPending()
{
	UINT iMachines = m_RemoteAdministrator.GetNumberOfMachineConnectionsStillInProgress();

	if (iMachines == 0) // No connections pending...
	{
		return FALSE;
	}
	
	return TRUE;
}

UINT CRemoteAdminDoc::GetNumberOfMachineConnectionsStillInProgress()
{
	return m_RemoteAdministrator.GetNumberOfMachineConnectionsStillInProgress();
}

CString CRemoteAdminDoc::GetComputerNameFromIP(CString& strIP)
{
	return m_RemoteAdministrator.GetComputerNameFromIP(strIP);
}

CString CRemoteAdminDoc::GetComputerIPFromName(CString& strComputerName)
{
	return m_RemoteAdministrator.GetComputerIPFromName(strComputerName);
}

⌨️ 快捷键说明

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