1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- #include "StdAfx.h"
- #include "ImageUploadWorker.h"
- #include "..\EvaluationUtil\IInputFiled.h"
- #include "..\EvaluationUtil\FileInputFiled.h"
- #include "..\EvaluationUtil\HttpClient.h"
- CImageUploadWorker::CImageUploadWorker(void) :m_uploadTask(0), m_Upload_Sate(US_WAITING)
- {
- }
- CImageUploadWorker::~CImageUploadWorker(void)
- {
- }
- ServiceState CImageUploadWorker::OnRunning( void )
- {
- if(m_Upload_Sate == US_WAITING){
- ServiceState nextState = IService::OnRunning();
- if(nextState != running) return nextState;
- Sleep(10);
- return running;
- }
- // Logger pTestLogger = Logger::getInstance(LOG4CPLUS_TEXT("LoggerName"));
- // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("工作线程开始上传试卷ID为(%d)的任务"), m_uploadTask->student_paper_id);
- int student_paper_id=m_uploadTask->student_paper_id;
- BOOL success =FALSE;
- if(PathFileExists(m_uploadTask->file_name)){
- CFileInputFiled filed(_T("file"),_T("img.zip"),m_uploadTask->file_name);
- CInputFiled filed1(_T("batch"),m_uploadTask->bantch,INPUT_FILED_VALUE);
- CInputFiled filed2(_T("schoolId"),m_uploadTask->schoolId,INPUT_FILED_VALUE);
- CInputFiled filed3(_T("examId"),m_uploadTask->examId,INPUT_FILED_VALUE);
- CInputFiled filed4(_T("examCourseId"),m_uploadTask->examCourseId,INPUT_FILED_VALUE);
- CInputFiled filed5(_T("examineeId"),m_uploadTask->student_id,INPUT_FILED_VALUE);
- vector<CInputFiled*> inputs;
- inputs.push_back(& filed);
- inputs.push_back(& filed1);
- inputs.push_back(& filed2);
- inputs.push_back(& filed3);
- inputs.push_back(& filed4);
- inputs.push_back(& filed5);
- CHttpClient httpClient;
- string response;
- // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("试卷ID为(%d)的任务开始发送请求"), m_uploadTask->student_paper_id);
- httpClient.HttpPostFile(m_uploadTask->url,inputs,response);
- // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("试卷ID为(%d)的任务收到返回%s"), m_uploadTask->student_paper_id, CString(response.c_str()));
- Json::Features features;
- Json::Reader re(features);
- Json::Value root;
- re.parse(response,root);
- if(root["success"].asInt())success =TRUE;
- }else{
- // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("试卷ID为(%d)的任务文件不存在"), m_uploadTask->student_paper_id);
- m_uploadTask->upload_result=FALSE;
- m_uploadTask->task_state =CUploadTask::TS_COMPLETE;
- m_uploadTask= NULL;
- m_Upload_Sate = US_WAITING;
- }
- // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("工作线程结束上传试卷ID为(%d)的任务[结果 %d]"), student_paper_id, success ? 1 : 0);
- if(success){
- m_uploadTask->upload_result=success;
- m_uploadTask->task_state =CUploadTask::TS_COMPLETE;
- m_uploadTask= NULL;
- m_Upload_Sate = US_WAITING;
- }else{
- //上传失败,20秒后重试
- Sleep(1000*20);
- }
- return ServiceState::running;
- }
- BOOL CImageUploadWorker::upload(CUploadTask * uploadTask)
- {
- if (m_Upload_Sate == US_UPLOADING)return FALSE;
- m_uploadTask = uploadTask;
- m_Upload_Sate = US_UPLOADING;
- return TRUE;
- }
|