ServerImgDownloadDispatcher.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "StdAfx.h"
  2. #include "ServerImgDownloadDispatcher.h"
  3. #ifdef _DEBUG
  4. #define SERVERIMGDOWNLOADWORKERCOUNT 1
  5. #else
  6. #define SERVERIMGDOWNLOADWORKERCOUNT 3
  7. #endif
  8. CServerImgDownloadDispatcher::CServerImgDownloadDispatcher(void):CMutilTaskDispatcher(SERVERIMGDOWNLOADWORKERCOUNT)
  9. {
  10. }
  11. CServerImgDownloadDispatcher::~CServerImgDownloadDispatcher(void)
  12. {
  13. }
  14. void CServerImgDownloadDispatcher::setImageInfo( Json::Value* imginfos )
  15. {
  16. m_imgInfos =imginfos;
  17. m_lastCreatedTaskIndex =-1;
  18. m_lastHandledIndex =-1;
  19. m_isError=false;
  20. if(imginfos->size()>0){
  21. tasks.resize(imginfos->size(),0);
  22. tasks_excuted.resize(imginfos->size(),TS_NOCREATE);
  23. }
  24. }
  25. CServerImgDownloadTask * CServerImgDownloadDispatcher::GetNextTask()
  26. {
  27. if(m_isError)return 0;//已经发生错误,结束下载
  28. int size =m_imgInfos->size();
  29. if(m_lastCreatedTaskIndex+1<size){
  30. int current_create_index =m_lastCreatedTaskIndex+1;
  31. Json::Value & img = (*m_imgInfos)[current_create_index];
  32. CString img_path ;
  33. img_path.Format(_T("%s\\%05d.jpg"),m_img_download_dir,current_create_index);
  34. CServerImgDownloadTask * task=new CServerImgDownloadTask(img_path,CString(img["httpImgPath"].asCString()));
  35. tasks[current_create_index]=task;
  36. tasks_excuted[current_create_index]=TS_EXCUTING;
  37. m_lastCreatedTaskIndex++;
  38. return task;
  39. }
  40. return NULL;
  41. }
  42. void CServerImgDownloadDispatcher::SaveTask( CServerImgDownloadTask * task )
  43. {
  44. for (int idx=m_lastCreatedTaskIndex;idx>=0;idx--)
  45. {
  46. if(tasks[idx]==task){
  47. if(task->GetResult()){
  48. tasks_excuted[idx] = TS_EXCUTED;
  49. }else{
  50. m_isError=true;
  51. tasks_excuted[idx] = TS_FIALURE;
  52. }
  53. break;
  54. }
  55. }
  56. }