1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| UINT InstallComputerList( LPVOID pParam)
{
LPITEMIDLIST pMyIdList = NULL;
LPMALLOC pIMalloc;
CStdioFile myFile;
CObArray threadHandles; // List of running threads
CLauncherThreadParam* CLpParam = (CLauncherThreadParam *)pParam;
try
{
CAgentSettings *pSettings = ((CLauncherThreadParam *)pParam)->GetSettings();
CStringList *pComputers = ((CLauncherThreadParam *)pParam)->GetComputersList();
CStringList *pFailed = ((CLauncherThreadParam *)pParam)->GetFailedList();
CString csComputer, // Computer to setup
csSuccess, // String to format number of sucess or failure
csLocalDir, // Local directory where to find setup.sh
csFailure;
POSITION pos; // Position into string list
int nIndex, // Number of computer launched
nThread; // Number of threads
DWORD dwErr;
CWorkerThreadParam myParam; // Parameters to pass to working threads
CWinThread* pThread; // One working thread
// Launch setup for each computer
nIndex = 0;
pos = pComputers->GetHeadPosition();
while (pos)
{
// Ensure not too many threads already running
nThread = 10;
while (threadHandles.GetSize() >= nThread)
{
// Too many threads lanuched, wait a while
Sleep( 500);
// Cleaning dead threads if needed
nThread = 0;
while (nThread < threadHandles.GetSize())
{
// Get thread status
pThread = (CWinThread*)threadHandles.GetAt( nThread);
GetExitCodeThread( pThread->m_hThread, &dwErr );
if( dwErr != STILL_ACTIVE )
{
// Clean thread
threadHandles.RemoveAt( nThread);
delete pThread;
nThread --;
}
nThread ++;
}
nThread = 10;
}
// Launch new thread
csComputer = pComputers->GetNext( pos);
myParam.SetParam(pSettings, pFailed, csComputer, csLocalDir);
printf("Host <%s:%i> Debut du deploiment\n", (CStringA)csComputer.GetBuffer(),pSettings->GetServerPort());
pThread = AfxBeginThread((AFX_THREADPROC)InstallComputer, &myParam, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
threadHandles.Add( pThread);
pThread->m_bAutoDelete = FALSE;
pThread->ResumeThread();
// Set status text on dialogbox
nIndex ++;
csSuccess.Format( _T( "%d"), nIndex);
csFailure.Format( _T( "%d"), pComputers->GetCount());
/*csComputer.FormatMessage( IDS_STATUS_RUNNING_DEPLOYMENT, csSuccess, csFailure);
cout << csComputer << endl;*/
// Wait a while before launching next script
Sleep( 1000);
}
// Waiting for threads to terminate
//csComputer.LoadString( IDS_STATUS_STOP_DEPLOYMENT);
// ::SendMessage( hWnd, WM_SETTEXT, IDC_MESSAGE_HANDLER_STATUS, (LPARAM) LPCTSTR( csComputer));
while (threadHandles.GetSize() > 0)
{
Sleep( 500);
// Cleaning dead threads if needed
nThread = 0;
while (nThread < threadHandles.GetSize())
{
// Get thread status
pThread = (CWinThread*)threadHandles.GetAt( nThread);
GetExitCodeThread( pThread->m_hThread, &dwErr );
if( dwErr != STILL_ACTIVE )
{
// Clean thread
threadHandles.RemoveAt( nThread);
delete pThread;
nThread --;
//printf("Host <%s:%i> Fin du deploiment\n", (CStringA)csComputer.GetBuffer(),pSettings->GetServerPort());
}
nThread ++;
}
}
if (pFailed->GetCount() == 0)
{
// No error
return 0;
}
else {
//errors
return 1;
}
}
catch( CException *pEx)
{
return 1;
}
} |