📄 mpdupdatedlg.cpp
字号:
// MPDUpdateDlg.cpp : implementation file//#include "stdafx.h"#include "MPDUpdate.h"#include "MPDUpdateDlg.h"#include "mpd.h"#include "crypt.h"#include <tchar.h>#include "PwdDialog.h"#include <afxinet.h>#include "mpdutil.h"#include "launchprocess.h"#include "FindHostsDlg.h"#include "ConnectToHost.h"#include "Translate_Error.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endifbool GetLocalVersion(const char *filename, unsigned int &version);bool GetLocalMPICHVersion(const char *filename, unsigned int &version);/////////////////////////////////////////////////////////////////////////////// CMPDUpdateDlg dialogCMPDUpdateDlg::CMPDUpdateDlg(CWnd* pParent /*=NULL*/): CDialog(CMPDUpdateDlg::IDD, pParent){ m_bNeedPassword = false; //{{AFX_DATA_INIT(CMPDUpdateDlg) m_bShowHostConfig = FALSE; m_filename = _T(""); m_urlname = _T("ftp://ftp.mcs.anl.gov/pub/mpi/nt/binaries/mpd.exe"); m_hostname = _T(""); m_cred_account = _T(""); m_cred_password = _T(""); m_bForceUpdate = FALSE; m_results = _T(""); m_mpd_pwd = _T(""); m_mpd_port = MPD_DEFAULT_PORT; m_mpich_filename = _T(""); m_mpich_url = _T("ftp://ftp.mcs.anl.gov/pub/mpi/nt/binaries/mpich.dll"); m_bUpdateMPD = TRUE; m_bUpdateMPICH = FALSE; m_bMPDPassphraseChecked = FALSE; m_bMPDPortChecked = FALSE; m_mpd_version = _T(""); m_mpich_version = _T(""); m_config_host = _T(""); m_config_mpich_version = _T(""); m_config_mpd_version = _T(""); //}}AFX_DATA_INIT m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); m_hUpdateBtnThread = NULL; m_nMinWidth = -1; m_nMinHeight = -1;}void CMPDUpdateDlg::DoDataExchange(CDataExchange* pDX){ CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CMPDUpdateDlg) DDX_Control(pDX, IDC_MPICH_VERSION_BTN, m_mpich_version_btn); DDX_Control(pDX, IDC_MPD_VERSION_BTN, m_mpd_version_btn); DDX_Control(pDX, IDC_MPICH_SOURCE_STATIC, m_mpich_source_static); DDX_Control(pDX, IDC_CRED_ACCOUNT_EDIT, m_cred_account_edit); DDX_Control(pDX, IDC_SELECT_HOSTS_BTN, m_select_btn); DDX_Control(pDX, IDC_MPICH_URL_EDIT, m_mpich_url_edit); DDX_Control(pDX, IDC_MPICH_URL_RADIO, m_mpich_url_radio); DDX_Control(pDX, IDC_MPICH_FILE_RADIO, m_mpich_filename_radio); DDX_Control(pDX, IDC_MPICH_FILE_EDIT, m_mpich_filename_edit); DDX_Control(pDX, IDC_MPICH_FILE_BROWSE_BTN, m_mpich_file_browse_btn); DDX_Control(pDX, IDC_MPICH_ANL_BTN, m_mpich_anl_btn); DDX_Control(pDX, IDC_MPD_PORT_STATIC, m_mpd_port_static); DDX_Control(pDX, IDC_MPD_PORT_EDIT, m_mpd_port_edit); DDX_Control(pDX, IDC_MPD_PASSPHRASE_STATIC, m_mpd_pwd_static); DDX_Control(pDX, IDC_MPD_PASSPHRASE, m_mpd_pwd_edit); DDX_Control(pDX, IDC_RESULTS, m_results_edit); DDX_Control(pDX, IDC_UPDATE_STATIC, m_update_static); DDX_Control(pDX, IDC_UPDATE_ONE_STATIC, m_update_one_static); DDX_Control(pDX, IDC_UPDATE_ONE_BTN, m_update_one_btn); DDX_Control(pDX, IDC_UPDATE_BTN, m_update_btn); DDX_Control(pDX, IDC_SOURCE_STATIC, m_source_static); DDX_Control(pDX, IDC_URL_EDIT, m_url_edit); DDX_Control(pDX, IDC_FILE_EDIT, m_file_edit); DDX_Control(pDX, IDC_FILE_BROWSE_BTN, m_file_browse_btn); DDX_Control(pDX, IDC_ANL_BTN, m_anl_btn); DDX_Control(pDX, IDC_SHOW_HOST_CHK, m_show_host_chk); DDX_Control(pDX, IDOK, m_ok_btn); DDX_Control(pDX, IDCANCEL, m_cancel_btn); DDX_Control(pDX, IDC_EDIT_ADD_BTN, m_edit_add_btn); DDX_Control(pDX, IDC_HOST_LIST, m_host_list); DDX_Check(pDX, IDC_SHOW_HOST_CHK, m_bShowHostConfig); DDX_Text(pDX, IDC_FILE_EDIT, m_filename); DDX_Text(pDX, IDC_URL_EDIT, m_urlname); DDX_Text(pDX, IDC_HOSTNAME, m_hostname); DDX_Control(pDX, IDC_FILE_RADIO, m_file_radio); DDX_Control(pDX, IDC_URL_RADIO, m_url_radio); DDX_Text(pDX, IDC_CRED_ACCOUNT_EDIT, m_cred_account); DDX_Text(pDX, IDC_CRED_PWD_EDIT, m_cred_password); DDX_Check(pDX, IDC_FORCE_UPDATE_CHK, m_bForceUpdate); DDX_Text(pDX, IDC_RESULTS, m_results); DDX_Text(pDX, IDC_MPD_PASSPHRASE, m_mpd_pwd); DDX_Text(pDX, IDC_MPD_PORT_EDIT, m_mpd_port); DDV_MinMaxInt(pDX, m_mpd_port, 1, 65000); DDX_Text(pDX, IDC_MPICH_FILE_EDIT, m_mpich_filename); DDX_Text(pDX, IDC_MPICH_URL_EDIT, m_mpich_url); DDX_Check(pDX, IDC_UPDATE_MPD_CHECK, m_bUpdateMPD); DDX_Check(pDX, IDC_UPDATE_MPICH_CHECK, m_bUpdateMPICH); DDX_Check(pDX, IDC_MPD_PASSPHRASE_CHK, m_bMPDPassphraseChecked); DDX_Check(pDX, IDC_MPD_PORT_CHK, m_bMPDPortChecked); DDX_Text(pDX, IDC_MPD_VERSION_STATIC, m_mpd_version); DDX_Text(pDX, IDC_MPICH_VERSION_STATIC, m_mpich_version); DDX_Text(pDX, IDC_HOST_STATIC, m_config_host); DDX_Text(pDX, IDC_HOST_MPICH_VERSION_STATIC, m_config_mpich_version); DDX_Text(pDX, IDC_HOST_MPD_VERSION_STATIC, m_config_mpd_version); //}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CMPDUpdateDlg, CDialog)//{{AFX_MSG_MAP(CMPDUpdateDlg)ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_UPDATE_BTN, OnUpdateBtn)ON_BN_CLICKED(IDC_EDIT_ADD_BTN, OnEditAddBtn) ON_WM_VKEYTOITEM() ON_WM_CLOSE() ON_WM_SIZE() ON_BN_CLICKED(IDC_SHOW_HOST_CHK, OnShowHostChk) ON_LBN_SELCHANGE(IDC_HOST_LIST, OnSelchangeHostList) ON_BN_CLICKED(IDC_UPDATE_ONE_BTN, OnUpdateOneBtn) ON_BN_CLICKED(IDC_ANL_BTN, OnAnlBtn) ON_BN_CLICKED(IDC_FILE_BROWSE_BTN, OnFileBrowseBtn) ON_BN_CLICKED(IDC_URL_RADIO, OnURLRadio) ON_BN_CLICKED(IDC_FILE_RADIO, OnFileRadio) ON_BN_CLICKED(IDC_SELECT_HOSTS_BTN, OnSelectHostsBtn) ON_BN_CLICKED(IDC_UPDATE_MPICH_CHECK, OnUpdateMpichCheck) ON_BN_CLICKED(IDC_UPDATE_MPD_CHECK, OnUpdateMpdCheck) ON_BN_CLICKED(IDC_MPD_PORT_CHK, OnMpdPortChk) ON_BN_CLICKED(IDC_MPD_PASSPHRASE_CHK, OnMpdPassphraseChk) ON_BN_CLICKED(IDC_MPICH_URL_RADIO, OnMpichUrlRadio) ON_BN_CLICKED(IDC_MPICH_FILE_RADIO, OnMpichFileRadio) ON_BN_CLICKED(IDC_MPICH_FILE_BROWSE_BTN, OnMpichFileBrowseBtn) ON_BN_CLICKED(IDC_MPICH_ANL_BTN, OnMpichAnlBtn) ON_BN_CLICKED(IDC_MPICH_VERSION_BTN, OnMpichVersionBtn) ON_BN_CLICKED(IDC_MPD_VERSION_BTN, OnMpdVersionBtn) //}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CMPDUpdateDlg message handlersvoid CMPDUpdateDlg::ParseRegistry(){ HKEY tkey; DWORD result, len; char path[MAX_PATH]; char mpich_path[MAX_PATH], *filename; DWORD length; // Set the defaults. m_mpd_port = MPD_DEFAULT_PORT; gethostname(m_pszHost, 100); m_bNeedPassword = true; // Open the root key if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, MPD_REGISTRY_KEY, 0, KEY_ALL_ACCESS, &tkey) != ERROR_SUCCESS) { printf("Unable to open SOFTWARE\\MPICH\\MPD registry key, error %d\n", GetLastError()); return; } // Read the port len = sizeof(int); result = RegQueryValueEx(tkey, "port", 0, NULL, (unsigned char *)&m_mpd_port, &len); // Read the passphrase len = 100; result = RegQueryValueEx(tkey, "phrase", 0, NULL, (unsigned char *)m_pszPhrase, &len); if (result == ERROR_SUCCESS) m_bNeedPassword = false; // Read the port len = MAX_PATH; result = RegQueryValueEx(tkey, "path", 0, NULL, (unsigned char *)path, &len); if (result == ERROR_SUCCESS) { m_filename = path; } RegCloseKey(tkey); // Find the mpich.dll length = SearchPath(NULL, "mpich.dll", NULL, MAX_PATH, mpich_path, &filename); if (length > 0 && length < MAX_PATH) { // save the mpich.dll filename m_mpich_filename = mpich_path; // save the mpichd.dll filename m_mpich_filenamed = mpich_path; m_mpich_filenamed.TrimRight(".dll"); m_mpich_filenamed += "d.dll"; }}static bool bOnInitDialogFinished = false;BOOL CMPDUpdateDlg::OnInitDialog(){ CDialog::OnInitDialog(); SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon easy_socket_init(); ParseRegistry(); RECT r; GetClientRect(&r); m_nMinWidth = r.right; m_nMinHeight = r.bottom; rList.SetInitialPosition(m_host_list.m_hWnd, RSR_STRETCH_BOTTOM); rResults.SetInitialPosition(m_results_edit.m_hWnd, RSR_STRETCH); // mpd default - choose url m_source_static.EnableWindow(); m_bUpdateMPD = TRUE; m_file_radio.SetCheck(0); m_url_radio.SetCheck(1); m_url_edit.EnableWindow(); m_anl_btn.EnableWindow(); m_file_edit.EnableWindow(FALSE); m_file_browse_btn.EnableWindow(FALSE); // mpich defaults - choose url m_mpich_source_static.EnableWindow(); m_bUpdateMPICH = TRUE; m_mpich_filename_radio.SetCheck(0); m_mpich_url_radio.SetCheck(1); m_mpich_url_edit.EnableWindow(); m_mpich_anl_btn.EnableWindow(); m_mpich_filename_edit.EnableWindow(FALSE); m_mpich_file_browse_btn.EnableWindow(FALSE); // default - use default passphrase m_mpd_pwd_edit.EnableWindow(FALSE); m_mpd_port_edit.EnableWindow(FALSE); char host[100] = ""; gethostname(host, 100); m_hostname = host; UpdateData(FALSE); bOnInitDialogFinished = true; return TRUE; // return TRUE unless you set the focus to a control}// If you add a minimize button to your dialog, you will need the code below// to draw the icon. For MFC applications using the document/view model,// this is automatically done for you by the framework.void CMPDUpdateDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); }}HCURSOR CMPDUpdateDlg::OnQueryDragIcon(){ return (HCURSOR) m_hIcon;}void UpdateBtnThread(CMPDUpdateDlg *pDlg){ int i; int num_hosts; char host[100]; SOCKET sock; bool bDeleteTmpMpd = false; bool bDeleteTmpMPICH = false; bool bFailure = false; unsigned int version_new, version_old; CString results; num_hosts = pDlg->m_host_list.GetCount(); if (num_hosts == 0) { CloseHandle(pDlg->m_hUpdateBtnThread); pDlg->m_hUpdateBtnThread = NULL; return; } if (pDlg->m_bNeedPassword) { if (pDlg->m_bUseDefault) strcpy(pDlg->m_pszPhrase, MPD_DEFAULT_PASSPHRASE); else strcpy(pDlg->m_pszPhrase, pDlg->m_mpd_pwd); } if (pDlg->m_bUpdateMPD) { results = "Updating mpd\r\n"; pDlg->m_results += results; PostMessage(pDlg->m_hWnd, WM_USER + 5, 0, 0); if (pDlg->m_url_radio.GetCheck()) { if (!pDlg->GetTmpMpdFromURL()) { if (pDlg->m_bShowHostConfig) pDlg->GetHostConfig(NULL); CloseHandle(pDlg->m_hUpdateBtnThread); pDlg->m_hUpdateBtnThread = NULL; return; } bDeleteTmpMpd = true; } if (!pDlg->m_bForceUpdate) { if (!GetLocalVersion(pDlg->m_url_radio.GetCheck() ? pDlg->m_localfile : pDlg->m_filename, version_new)) { if (bDeleteTmpMpd) DeleteFile(pDlg->m_localfile); MessageBox(NULL, "Unable to get the version of the new mpd", "Update aborted", MB_OK); CloseHandle(pDlg->m_hUpdateBtnThread); pDlg->m_hUpdateBtnThread = NULL; return; } } // gray out the buttons PostMessage(pDlg->m_hWnd, WM_USER + 3, 0, 0); for (i=0; i<num_hosts; i++) { if (pDlg->m_host_list.GetText(i, host) == LB_ERR) continue; if (ConnectToHost(host, pDlg->m_mpd_port, pDlg->m_pszPhrase, &sock)) { char pszError[256]; char str[256], str2[256]; if (!pDlg->m_bForceUpdate) { WriteString(sock, "version"); ReadString(sock, str); } WriteString(sock, "done"); easy_closesocket(sock); if (!pDlg->m_bForceUpdate) { version_old = mpd_version_string_to_int(str); mpd_version_int_to_string(version_old, str); mpd_version_int_to_string(version_new, str2); } if (pDlg->m_bForceUpdate || version_old < version_new) { if (UpdateMPD(host, pDlg->m_cred_account, pDlg->m_cred_password, pDlg->m_mpd_port, pDlg->m_pszPhrase, bDeleteTmpMpd ? pDlg->m_localfile : pDlg->m_filename, pszError, 256)) { results.Format("%s: success\r\n", host); pDlg->m_results += results; PostMessage(pDlg->m_hWnd, WM_USER + 5, 0, 0); } else { bFailure = true; results.Format("%s: mpd failure, %s\r\n", host, pszError); pDlg->m_results += results; PostMessage(pDlg->m_hWnd, WM_USER + 5, 0, 0); //MessageBox(NULL, pszError, "Unable to update mpd", MB_OK); } } else { CString s; if (version_old == version_new) { s.Format("MPD version <%s> is already installed on %s", str, host); results.Format("%s: no action, <%s> is already installed\r\n", host, str); pDlg->m_results += results; PostMessage(pDlg->m_hWnd, WM_USER + 5, 0, 0); //MessageBox(NULL, s, "Update aborted", MB_OK); } else { s.Format("MPD version <%s> on %s is newer than version <%s>", str, host, str2); results.Format("%s: no action, <%s> is newer than <%s>\r\n", host, str, str2); pDlg->m_results += results; PostMessage(pDlg->m_hWnd, WM_USER + 5, 0, 0); //MessageBox(NULL, s, "Update aborted", MB_OK); } } } else { CString str; bFailure = true; str.Format("Connect to mpd on %s failed", host); results.Format("%s: failure, connect to mpd failed\r\n", host); pDlg->m_results += results; PostMessage(pDlg->m_hWnd, WM_USER + 5, 0, 0); //MessageBox(NULL, str, "Unable to update mpd", MB_OK); } } PostMessage(pDlg->m_hWnd, WM_USER + 4, 0, 0); if (bDeleteTmpMpd) DeleteFile(pDlg->m_localfile); if (pDlg->m_bShowHostConfig) { PostMessage(pDlg->m_hWnd, WM_USER+2, 0, 0); } } if (pDlg->m_bUpdateMPICH) { results = "Updating mpich dlls\r\n"; pDlg->m_results += results; PostMessage(pDlg->m_hWnd, WM_USER + 5, 0, 0); if (pDlg->m_mpich_url_radio.GetCheck()) { if (!pDlg->GetTmpMPICHFromURL()) { if (pDlg->m_bShowHostConfig) pDlg->GetHostConfig(NULL); CloseHandle(pDlg->m_hUpdateBtnThread); pDlg->m_hUpdateBtnThread = NULL; PostMessage(pDlg->m_hWnd, WM_USER + 4, 0, 0); // re-enable the buttons return; } bDeleteTmpMPICH = true; } if (!pDlg->m_bForceUpdate) { if (!GetLocalMPICHVersion(pDlg->m_mpich_url_radio.GetCheck() ? pDlg->m_mpich_localfile : pDlg->m_mpich_filename, version_new)) { if (bDeleteTmpMPICH) { DeleteFile(pDlg->m_mpich_localfile); DeleteFile(pDlg->m_mpich_localfiled); } MessageBox(NULL, "Unable to get the version of the new mpich dlls", "Update aborted", MB_OK); CloseHandle(pDlg->m_hUpdateBtnThread); pDlg->m_hUpdateBtnThread = NULL; PostMessage(pDlg->m_hWnd, WM_USER + 4, 0, 0); // re-enable the buttons return; } } // gray out the buttons PostMessage(pDlg->m_hWnd, WM_USER + 3, 0, 0); for (i=0; i<num_hosts; i++) { if (pDlg->m_host_list.GetText(i, host) == LB_ERR) continue; if (ConnectToHost(host, pDlg->m_mpd_port, pDlg->m_pszPhrase, &sock)) { char pszError[256]; char str[256], str2[256]; if (!pDlg->m_bForceUpdate) { WriteString(sock, "mpich version"); if (!ReadStringTimeout(sock, str, MPD_SHORT_TIMEOUT)) { MessageBox(NULL, "MPD is unable to update the mpich dlls, please update mpd before attempting to update the mpich dlls", "Error", MB_OK); WriteString(sock, "done"); easy_closesocket(sock); CloseHandle(pDlg->m_hUpdateBtnThread); pDlg->m_hUpdateBtnThread = NULL; PostMessage(pDlg->m_hWnd, WM_USER + 4, 0, 0); // re-enable the buttons return; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -