ImageUploadWorker.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "StdAfx.h"
  2. #include "ImageUploadWorker.h"
  3. #include "..\EvaluationUtil\IInputFiled.h"
  4. #include "..\EvaluationUtil\FileInputFiled.h"
  5. #include "..\EvaluationUtil\HttpClient.h"
  6. CImageUploadWorker::CImageUploadWorker(void) :m_uploadTask(0), m_Upload_Sate(US_WAITING)
  7. {
  8. }
  9. CImageUploadWorker::~CImageUploadWorker(void)
  10. {
  11. }
  12. ServiceState CImageUploadWorker::OnRunning( void )
  13. {
  14. if(m_Upload_Sate == US_WAITING){
  15. ServiceState nextState = IService::OnRunning();
  16. if(nextState != running) return nextState;
  17. Sleep(10);
  18. return running;
  19. }
  20. // Logger pTestLogger = Logger::getInstance(LOG4CPLUS_TEXT("LoggerName"));
  21. // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("工作线程开始上传试卷ID为(%d)的任务"), m_uploadTask->student_paper_id);
  22. int student_paper_id=m_uploadTask->student_paper_id;
  23. BOOL success =FALSE;
  24. if(PathFileExists(m_uploadTask->file_name)){
  25. CFileInputFiled filed(_T("file"),_T("img.zip"),m_uploadTask->file_name);
  26. CInputFiled filed1(_T("batch"),m_uploadTask->bantch,INPUT_FILED_VALUE);
  27. CInputFiled filed2(_T("schoolId"),m_uploadTask->schoolId,INPUT_FILED_VALUE);
  28. CInputFiled filed3(_T("examId"),m_uploadTask->examId,INPUT_FILED_VALUE);
  29. CInputFiled filed4(_T("examCourseId"),m_uploadTask->examCourseId,INPUT_FILED_VALUE);
  30. CInputFiled filed5(_T("examineeId"),m_uploadTask->student_id,INPUT_FILED_VALUE);
  31. vector<CInputFiled*> inputs;
  32. inputs.push_back(& filed);
  33. inputs.push_back(& filed1);
  34. inputs.push_back(& filed2);
  35. inputs.push_back(& filed3);
  36. inputs.push_back(& filed4);
  37. inputs.push_back(& filed5);
  38. CHttpClient httpClient;
  39. string response;
  40. // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("试卷ID为(%d)的任务开始发送请求"), m_uploadTask->student_paper_id);
  41. httpClient.HttpPostFile(m_uploadTask->url,inputs,response);
  42. // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("试卷ID为(%d)的任务收到返回%s"), m_uploadTask->student_paper_id, CString(response.c_str()));
  43. Json::Features features;
  44. Json::Reader re(features);
  45. Json::Value root;
  46. re.parse(response,root);
  47. if(root["success"].asInt())success =TRUE;
  48. }else{
  49. // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("试卷ID为(%d)的任务文件不存在"), m_uploadTask->student_paper_id);
  50. m_uploadTask->upload_result=FALSE;
  51. m_uploadTask->task_state =CUploadTask::TS_COMPLETE;
  52. m_uploadTask= NULL;
  53. m_Upload_Sate = US_WAITING;
  54. }
  55. // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("工作线程结束上传试卷ID为(%d)的任务[结果 %d]"), student_paper_id, success ? 1 : 0);
  56. if(success){
  57. m_uploadTask->upload_result=success;
  58. m_uploadTask->task_state =CUploadTask::TS_COMPLETE;
  59. m_uploadTask= NULL;
  60. m_Upload_Sate = US_WAITING;
  61. }else{
  62. //上传失败,20秒后重试
  63. Sleep(1000*20);
  64. }
  65. return ServiceState::running;
  66. }
  67. BOOL CImageUploadWorker::upload(CUploadTask * uploadTask)
  68. {
  69. if (m_Upload_Sate == US_UPLOADING)return FALSE;
  70. m_uploadTask = uploadTask;
  71. m_Upload_Sate = US_UPLOADING;
  72. return TRUE;
  73. }