#include "stdafx.h" #include "resource.h" #include "Mediator.h" //#include "Information.h" CMediator :: CMediator(): m_pBasicSettings(NULL), m_currDirectory(_T("")),m_pAdvancedSettings(NULL) { } CMediator :: CMediator(CString str):m_pBasicSettings(NULL),m_currDirectory(str), m_pAdvancedSettings(NULL) { } void CMediator :: SetControls(const int nIndex) { FillDlgControls(STR_RUN_TYPE_COMBO[nIndex]); } void CMediator :: FillDlgControls(const CString& choice) { SAFE_DELETE(m_pBasicSettings); // This is to read/write a config file from/to the current directory //CFileHandling* fileHandle = CFileHandling::GetInstance(); if(choice.CompareNoCase(_T("SIMPLE")) == 0) { m_pBasicSettings = new BASIC_SETTINGS(2,1,FALSE,FALSE,FALSE,m_currDirectory,_T("")); } else if(choice.CompareNoCase(_T("INTERMEDIATE")) == 0) { m_pBasicSettings = new BASIC_SETTINGS(5,10,FALSE,FALSE,FALSE,m_currDirectory,_T("")); } else if(choice.CompareNoCase(_T("HIGH")) == 0) { m_pBasicSettings = new BASIC_SETTINGS(4,100,FALSE,FALSE,FALSE,m_currDirectory,_T("")); } else if(choice.CompareNoCase(_T("CUSTOMIZED")) == 0) { m_pBasicSettings = new BASIC_SETTINGS(1,1,FALSE,FALSE,FALSE,m_currDirectory,_T("")); } else { } SetDlgItems(); } BASIC_SETTINGS* CMediator :: GetBasicSettings() { return m_pBasicSettings; } void CMediator :: SetHWND(HWND handle) { m_handle = handle; } void CMediator :: SetDlgItems() { CString str; str.Format(_T("%d"),m_pBasicSettings->m_nTime); SetDlgItemText(m_handle, IDC_EDIT_TIME, str); str.Format(_T("%d"),m_pBasicSettings->m_nIterations); SetDlgItemText(m_handle, IDC_EDIT_ITERATIONS, str); str.Format(m_pBasicSettings->m_strOutputFile); SetDlgItemText(m_handle, IDC_MFCEDITBROWSE_OUTPUT_FILE, str); if((m_pBasicSettings->m_strPresetFile).CompareNoCase(_T("")) == 0) { SetDlgItemText(m_handle, IDC_MFCEDITBROWSE_PRESET_FILE, _T("DEFAULT")); } else { SetDlgItemText(m_handle, IDC_MFCEDITBROWSE_PRESET_FILE, m_pBasicSettings->m_strPresetFile); } } void CMediator :: Run() { //CCommunicateWithApp comm; m_comminicateWithApp.Run(m_currDirectory); //CString str = m_currDirectory; //str += _T("\\perfstat8.exe"); //ExecuteExternalFile(str, _T("--verbose --nodes=10.2.1.1")); /* CString str = m_currDirectory; str += _T("\\perfstat8.exe"); //ShellExecute(NULL, _T("open"), str, _T("--help"), NULL, SW_SHOW); CString strArguments = _T("--verbose --nodes=10.2.1.1"); CString strExe = str; CString csExecute; csExecute = strExe + " " + strArguments; HINSTANCE h = ShellExecute(NULL, _T("open"), str.AllocSysString(), strArguments.AllocSysString(), NULL, SW_HIDE); */ /* CString str = m_currDirectory; str += _T("\\perfstat8.exe"); //ShellExecute(NULL, _T("open"), str, _T("--help"), NULL, SW_SHOW); CString strArguments = _T("--verbose --nodes=10.2.1.1"); CString strExe = str; CString csExecute; csExecute = strExe + " " + strArguments; SECURITY_ATTRIBUTES secattr; ZeroMemory(&secattr,sizeof(secattr)); secattr.nLength = sizeof(secattr); secattr.bInheritHandle = TRUE; HANDLE rPipe; HANDLE wPipe; HANDLE rOutPipe; HANDLE wOutPipe; //Create pipes to write and read data CreatePipe(&rPipe,&wPipe,&secattr,0); CreatePipe(&rOutPipe,&wOutPipe,&secattr,0); // STARTUPINFO sInfo; ZeroMemory(&sInfo,sizeof(sInfo)); PROCESS_INFORMATION pInfo; ZeroMemory(&pInfo,sizeof(pInfo)); sInfo.cb=sizeof(sInfo); sInfo.dwFlags=STARTF_USESTDHANDLES; sInfo.hStdInput=NULL; sInfo.hStdOutput=wPipe; sInfo.hStdError=wPipe; //sInfo.hStdOutput=wOutPipe; //sInfo.hStdError=wOutPipe; char command[1024]; CString s = csExecute; //strcpy(command, c); //Create the process here. //CString s =(CString) command; //CreateProcess(0, command,0,0,TRUE, NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW,0,0,&sInfo,&pInfo); CreateProcess(0, s.AllocSysString(),0,0,TRUE, NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW,0,0,&sInfo,&pInfo); //CloseHandle(wPipe); Sleep(100); //now read the output pipe here. char buf[1024]; DWORD reDword; CString m_csOutput,csTemp; BOOL res; int nFilePos = 0; HANDLE hParentStdOut = GetStdHandle(STD_OUTPUT_HANDLE); do { res=::ReadFile(rPipe,buf,1024,&reDword,0); //::WriteFile(wPipe, csTemp = buf; m_csOutput += csTemp.Left(reDword); int nTmp = 0; CString strBuffer; strBuffer = buf; PROMPT_MESSAGES prmpMsg; bool bCheck = CheckForCommandInteraction(strBuffer, nTmp, prmpMsg); nFilePos += nTmp; if(bCheck) { //int retCode = WaitForSingleObject(pInfo.hProcess, 100); CInformation i; i.SetPromptMsg(prmpMsg); if(i.DoModal() == IDOK) { CString info = i.GetText(); CString strAppend = _T("\r\n\r"); info += strAppend; DWORD wrDword; for(int i = 0; i < info.GetLength(); ++i) { CString tmp; tmp = info.GetAt(i); ::WriteFile(hParentStdOut,tmp,1, &wrDword, NULL); } //::WaitForSingleObject(rPipe, 5000); } } }while(res); CloseHandle(wPipe); //return m_csOutput; */ } CString CMediator :: ExecuteExternalFile(CString csExeName, CString csArguments) { CString csExecute; csExecute=csExeName + " " + csArguments; SECURITY_ATTRIBUTES secattr; ZeroMemory(&secattr,sizeof(secattr)); secattr.nLength = sizeof(secattr); secattr.bInheritHandle = TRUE; HANDLE rPipe, wPipe; //Create pipes to write and read data CreatePipe(&rPipe,&wPipe,&secattr,0); // STARTUPINFO sInfo; ZeroMemory(&sInfo,sizeof(sInfo)); PROCESS_INFORMATION pInfo; ZeroMemory(&pInfo,sizeof(pInfo)); sInfo.cb=sizeof(sInfo); sInfo.dwFlags=STARTF_USESTDHANDLES; sInfo.hStdInput=wPipe; sInfo.hStdOutput=rPipe; sInfo.hStdError=rPipe; char command[1024]; /*strcpy(command, csExecute.GetBuffer(csExecute.GetLength()));*/ //Create the process here. CreateProcess(0, csExecute.AllocSysString(),0,0,TRUE, NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW,0,0,&sInfo,&pInfo); //CloseHandle(rPipe); //now read the output pipe here. char buf[100]; char buf1[1024] = {"HIDAYATH"}; DWORD reDword; DWORD wrWord; CString m_csOutput,csTemp; BOOL res; ::WriteFile(wPipe, buf1, 100, &wrWord, NULL); do { res=::ReadFile(rPipe,buf,100,&reDword,0); csTemp=buf; m_csOutput+=csTemp.Left(reDword); }while(res); return m_csOutput; } void CMediator :: Stop() { // This is going to kill the perfstat run m_comminicateWithApp.stop(); } bool CMediator :: CheckForCommandInteraction(const CString& str, int& nPosToMove, PROMPT_MESSAGES& prmpMsg) { if(str.Find(STR_PROMPT_MESSAGES[0]) != -1) { nPosToMove = (STR_PROMPT_MESSAGES[0]).GetLength(); prmpMsg = PROMPT_SSH_PASSPHRASE; } else if(str.Find(STR_PROMPT_MESSAGES[1]) != -1) { nPosToMove = (STR_PROMPT_MESSAGES[1]).GetLength(); prmpMsg = PROMPT_SSH_PASSPHRASE_REPEAT; } else { } return (nPosToMove != 0); } ADVANCED_SETTINGS* CMediator :: GetAdvancedSettings() const { return m_pAdvancedSettings; }