123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- #include "StdAfx.h"
- #include "ServerImgDownloadDispatcher.h"
- #ifdef _DEBUG
- #define SERVERIMGDOWNLOADWORKERCOUNT 1
- #else
- #define SERVERIMGDOWNLOADWORKERCOUNT 3
- #endif
- CServerImgDownloadDispatcher::CServerImgDownloadDispatcher(void):CMutilTaskDispatcher(SERVERIMGDOWNLOADWORKERCOUNT)
- {
- }
- CServerImgDownloadDispatcher::~CServerImgDownloadDispatcher(void)
- {
- }
- void CServerImgDownloadDispatcher::setImageInfo( Json::Value* imginfos )
- {
- m_imgInfos =imginfos;
- m_lastCreatedTaskIndex =-1;
- m_lastHandledIndex =-1;
- m_isError=false;
- if(imginfos->size()>0){
- tasks.resize(imginfos->size(),0);
- tasks_excuted.resize(imginfos->size(),TS_NOCREATE);
- }
- }
- CServerImgDownloadTask * CServerImgDownloadDispatcher::GetNextTask()
- {
- if(m_isError)return 0;//已经发生错误,结束下载
- int size =m_imgInfos->size();
- if(m_lastCreatedTaskIndex+1<size){
- int current_create_index =m_lastCreatedTaskIndex+1;
- Json::Value & img = (*m_imgInfos)[current_create_index];
- CString img_path ;
- img_path.Format(_T("%s\\%05d.jpg"),m_img_download_dir,current_create_index);
- CServerImgDownloadTask * task=new CServerImgDownloadTask(img_path,CString(img["httpImgPath"].asCString()));
- tasks[current_create_index]=task;
- tasks_excuted[current_create_index]=TS_EXCUTING;
- m_lastCreatedTaskIndex++;
- return task;
- }
- return NULL;
- }
- void CServerImgDownloadDispatcher::SaveTask( CServerImgDownloadTask * task )
- {
- for (int idx=m_lastCreatedTaskIndex;idx>=0;idx--)
- {
- if(tasks[idx]==task){
- if(task->GetResult()){
- tasks_excuted[idx] = TS_EXCUTED;
- }else{
- m_isError=true;
- tasks_excuted[idx] = TS_FIALURE;
- }
- break;
- }
- }
- }
|