본문 바로가기
  • fishing...
  • eating...
MISCELLANEOUSNESS

Updater 만들기.

by 회색뿔 2011. 4. 24.


Updater 만들기.

프로그램의 Update를 자동으로 수행하게 해주는 프로그램 만들기.
롤백이나 무결성을 체크해 주는 기능은 없지만, 간단한 프로그램에서 기존의 파일들을 Replace해주는 기능으로 사용할 수 있겠다.

기존의 있는 파일은 지워주고, 업데이트 갱신이 필요한 파일을 다운로드 받아 준다.

몇개의 사이트를 참고해서 작성했는데..
지금은 다 기억이 나질 않네..ㅋ

Flow.
1. Web에 있는 파일 내용에 업데이트 된 파일 리스트를 받는다.
2. 리스트에 있는 파일 중 로컷에도 존재하는 파일을 지운다.
3. 파일을 다운로드 받는다.
(롤벡이나 무결성 체크 기능은 존재하지 않음.


void CPatcherDlg::OnBnClickedUpdate()
{
	// TODO: Add your control notification handler code here
	CString strReceive = ""; 

	CInternetSession*		pSession = NULL;
	CHttpConnection*		pHttpConnect = NULL;
	CHttpFile*				pHttpFile = NULL;
	CString					url = _T("ocr/update.txt");

	try{
        // HTTP 메소드별로 사용 가능
	pSession = new CInternetSession;

        pHttpConnect = pSession->GetHttpConnection(_T("[IP or HOST DOMAIN]"));

        // 이런 식으로도 익셉션 처리 가능
        if(pHttpConnect == NULL)
            throw CString(_T("http connection failed!!!"));
       
        pHttpFile = pHttpConnect->OpenRequest(CHttpConnection::HTTP_VERB_GET, url);
		pHttpFile->SendRequest();

		/*
		// 요청 결과 수신
		pHttpFile->QueryInfoStatusCode(dwRet); 
		// 만약 요청이 정상이라면
		if(dwRet == HTTP_STATUS_OK) 
		{ 
			CString strLength; 
			int nTotal = 0; 
			UINT nRead; 
			BYTE szBuff[1024]; 
    				// 파일 크기 수신
			if(pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH, strLength, NULL)) 
				  nTotal = atoi((char*)(LPCTSTR)strLength); 
			else
				  nTotal = 0;
  			
			int nDown = 0;
			int nPacket = nTotal > 1024 ? 1024 : nTotal;
							// 패킷 단위로 데이터를 받아 파일에 저장
			while (nTotal - nDown > 0) 
			{ 
				// 데이터 수신
				nRead = pHttpFile->Read(szBuff, nPacket);
				if(nRead == 0)  break;
			  					  nDown += nRead;
				szBuff[nRead] = 0x00;
				strReceive += CString(szBuff);
				
				nPacket = (nTotal - nDown) > 1024 ? 1024 : (nTotal - nDown);
			} 

			bRet = TRUE;
		} 
		*/

		int idx = 0;
		CString FileList = "";

		strReceive = CGetterHttpFile::getFile(pHttpFile);

		int nSize = atoi((FileList = strReceive.Tokenize("\r\n", idx )));
		int nProgress = 0;
		CString strLog = "";
		ProgressBar.SetRange(0, nSize);

		FILE *fp;
		
		m_TextLog.SetWindowTextA("");
		while((FileList = strReceive.Tokenize("\r\n", idx )) != "")
		{
			if((fp = fopen(FileList, "r") )!= 0) {
				fclose(fp);
				CFile::Remove(FileList);
				strLog.Append((FileList + " deleted....\r\n"));
				m_TextLog.SetWindowTextA(strLog);
			}

			HRESULT hr = URLDownloadToFile(NULL, "[IP or HOST DOMAIN]" + FileList, FileList, 0, NULL);
			ProgressBar.SetPos(++nProgress);
			
			strLog.Append((FileList + " copied....\r\n"));
			m_TextLog.SetWindowTextA(strLog);
			m_TextLog.SetSel(strLog.GetLength()-1, strLog.GetLength() -1);
		}
	}
	catch(CInternetException *m_pEx)
	{
		m_pEx->ReportError();
		m_pEx->Delete();
	}
	// result값 확인
	DWORD m_dwStatusCode;
	pHttpFile->QueryInfoStatusCode(m_dwStatusCode);

	// 객체 delete 부분
	if(pHttpFile)
	{
		delete pHttpFile;
		pHttpFile = NULL;
	}
	if(pHttpConnect)
	{
		delete pHttpConnect;
		pHttpConnect = NULL;
	}
	if(pSession)
	{
		pSession->Close();
		delete pSession;
		pSession = NULL;
	}
}