#include "StdAfx.h" #include "ImgUploader.h" #include "..\EvaluationUtil\HttpClient.h" #include "..\EvaluationUtil\IInputFiled.h" #include "..\EvaluationUtil\FileInputFiled.h" #include "basic_struct.h" #include "ServerConfig.h" CImgUploader::CImgUploader(void) { m_hwnd = NULL; } CImgUploader::~CImgUploader(void) { } bool DeleteDirectory(CString sDirName) { CFileFind tempFind; CString sTempFileFind=sDirName+_T("\\*.*"); BOOL IsFinded = tempFind.FindFile(sTempFileFind); while (IsFinded) { IsFinded = tempFind.FindNextFile(); CString sFoundFileName=tempFind.GetFilePath(); if(tempFind.IsDirectory()){ if(!tempFind.IsDots()){ DeleteDirectory(sFoundFileName); } }else{ if(tempFind.IsReadOnly()) { SetFileAttributes(sFoundFileName, FILE_ATTRIBUTE_NORMAL); } DeleteFile(sFoundFileName); } } tempFind.Close(); SetFileAttributes(sDirName, FILE_ATTRIBUTE_NORMAL); RemoveDirectory(sDirName); return true; } /* //�������̼�� ��������������״̬ //���ڴ���һ�����ε����ݿ� IMG_UPLOAD_STATE_OPENNING, //����׼�� IMG_UPLOAD_STATE_READYING, //�����ϴ� IMG_UPLOAD_STATE_UPLOADING, //���ڹرյ�ǰ�򿪵����ݿ� IMG_UPLOAD_STATE_CLOSING, //��ǰ���ݿ��Ѿ��ر� IMG_UPLOAD_STATE_CLOSED, */ ServiceState CImgUploader::OnRunning( void ) { ServiceState nextState= IService::OnRunning(); if(nextState != running){ if(m_current_database_state==IMG_UPLOAD_STATE_CLOSED)return nextState; else m_current_database_state = IMG_UPLOAD_STATE_CLOSING; } // Logger pTestLogger = Logger::getInstance(LOG4CPLUS_TEXT("LoggerName")); switch(m_current_database_state){ case IMG_UPLOAD_STATE_OPENNING://����һ��Ҫ�ϴ������� { char sql[256]; sprintf_s(sql,"select * from database where database_id>%d and database_state in(%d,%d,%d) limit 1",m_task_info.upload_task_id,DATABASE_STATE_RESULT_UPLOADED,DATABASE_STATE_IMG_UPLOADING,DATABASE_STATE_IMG_PAUSED); //LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("select * from database where database_id>%d and database_state in(%d,%d,%d) limit 1"), m_task_info.upload_task_id, DATABASE_STATE_RESULT_UPLOADED, DATABASE_STATE_IMG_UPLOADING, DATABASE_STATE_IMG_PAUSED); CppSQLite3Query query =database_db->execQuery(sql); if (query.eof()){ //LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("��ǰû���µ���������ϴ�")); if(m_uploading){ m_uploading =FALSE; if(m_hwnd!=NULL)::PostMessage(m_hwnd,WM_IDENTIFOR_IMG_UPLOAD_STATE,m_uploading,0); } Sleep(1000); } else{ if(!m_uploading){ m_uploading =TRUE; if(m_hwnd!=NULL)::PostMessage(m_hwnd,WM_IDENTIFOR_IMG_UPLOAD_STATE,m_uploading,0); } // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("��ʼ�ϴ��Ծ��µ�����")); // m_url.Format(_T("%s/upload/uploadImages"),CString(query.getStringField("server_address"))); m_bantch_dir = CString(query.getStringField("bantch_dir")); const char * db_file_name = query.getStringField("db_file_name"); m_task_info.upload_task_id = query.getIntField("database_id"); strcpy(m_task_info.task_name,query.getStringField("bantch_code")); m_task_info.upload_task_state = UPLOAD_TASK_STATE_NOTSTART; m_task_info.exam_id = query.getInt64Field("exam_id"); m_task_info.exam_course_id = query.getInt64Field("exam_course_id"); m_task_info.scan_type = query.getIntField("scan_type"); m_task_info.exam_course_id_from = query.getInt64Field("exam_course_id_from"); m_task_info.batch_code_from = query.getStringField("batch_code_from"); m_task_info.examinne_id_from = query.getStringField("examinne_id_from"); bantch_db.open(db_file_name); bantch_db.execDML("pragma journal_mode = MEMORY"); char sql[128]; sprintf_s(sql,"update database set database_state =%d where database_id =%d",DATABASE_STATE_IMG_UPLOADING,m_task_info.upload_task_id); EnterCriticalSection(database_db_lock); database_db->execDML("begin transaction"); database_db->execDML(sql); database_db->execDML("commit transaction"); LeaveCriticalSection(database_db_lock); // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("�Ѿ���ʼ�ϴ��Ծ��µ�����(%d)"), m_task_info.upload_task_id); m_current_has_more=TRUE; //if(m_hwnd!=NULL)::PostMessage(m_hwnd,WM_IDENTIFOR_IMG_UPLOAD_TASK_STATE,(WPARAM)&m_task_info,0); m_current_database_state = IMG_UPLOAD_STATE_READYING; } } break; case IMG_UPLOAD_STATE_READYING://ͳ�����������Ϣ(����׼��) { char sql[512]; sprintf_s(sql,"select count(0) from student_paper p left join student s on s.student_id = p.student_id where (p.paper_state&%d)=0",MEX_BUKESHANGCHUANG); m_task_info.total_count =bantch_db.execScalar(sql); sprintf_s(sql,"select count(0) from student_paper p left join student s on s.student_id = p.student_id where (p.paper_state&%d)=0 and not p.img_upload = 0 ",MEX_BUKESHANGCHUANG); m_task_info.upload_count =bantch_db.execScalar(sql); sprintf_s(sql,"select * from bantch_info"); CppSQLite3Query q=bantch_db.execQuery(sql); if(!q.eof()){m_school_id.Format(L"%d",(long)q.getInt64Field("school_id"));}else{m_school_id =L"-1";} m_task_info.upload_task_state = UPLOAD_TASK_STATE_UPLOADING; IMG_UPLOAD_TASK * msg=new IMG_UPLOAD_TASK; memcpy(msg, &m_task_info, sizeof(IMG_UPLOAD_TASK)); // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("�ϴ��Ծ��µ�����(%d)ͳ������׼�����"), m_task_info.upload_task_id); if(m_hwnd!=NULL)::PostMessage(m_hwnd,WM_IDENTIFOR_IMG_UPLOAD_TASK_STATE,(WPARAM)msg,0); m_last_paper_id =-1; m_current_database_state = IMG_UPLOAD_STATE_UPLOADING; } break; case IMG_UPLOAD_STATE_UPLOADING://ִ���ϴ� { char sql[512]; sprintf_s(sql,"select p.file_name,s.school_id,e.exam_id,e.exam_course_id,p.student_id,p.student_paper_id from student_paper p left join student s on s.student_id = p.student_id left join exam e on 1=1 where p.student_paper_id>%d and (p.paper_state&%d)=0 and p.img_upload = 0 limit 20",m_last_paper_id,MEX_BUKESHANGCHUANG); CppSQLite3Query query =bantch_db.execQuery(sql); if(query.eof()){ bool has_someone_uploading; do { handleCompleteTask(); has_someone_uploading =false; for (int index =0;index=0&&index_worker>=0){ CUploadTask& task = taskBuffer[index_task_buffer]; task.examCourseId.Format(_T("%d"),query.getInt64Field("exam_course_id")); task.examId.Format(_T("%d"),query.getInt64Field("exam_id")); task.file_name=CString(query.getStringField("file_name")); task.schoolId.Format(_T("%d"),query.getInt64Field("school_id")); task.student_id=CString(query.getStringField("student_id")); task.student_paper_id=query.getIntField("student_paper_id"); task.bantch=m_task_info.task_name; task.task_state =CUploadTask::TS_UPLOADING; task.upload_result = FALSE; task.url = m_url; BOOL success=worker[index_worker].upload(&taskBuffer[index_task_buffer]); m_last_paper_id=task.student_paper_id; // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("Ϊ�ϴ��߳�(%d)�����Ծ�idΪ(%d)������(%d)[0:ʧ�� 1:�ɹ� ���->%d]"), index_worker, task.student_paper_id, index_task_buffer, success ? 1 : 0); query.nextRow(); }else{ Sleep(10); } } } } break; case IMG_UPLOAD_STATE_CLOSING://�����ϴ����� { bantch_db.close(); char sql[256]; sprintf_s(sql,"update database set database_state =%d where database_id =%d",m_current_has_more?DATABASE_STATE_IMG_PAUSED:DATABASE_STATE_IMG_UPLOADED,m_task_info.upload_task_id); EnterCriticalSection(database_db_lock); database_db->execDML("begin transaction"); database_db->execDML(sql); database_db->execDML("commit transaction"); LeaveCriticalSection(database_db_lock); m_task_info.upload_task_state = m_current_has_more?UPLOAD_TASK_STATE_PAUSED:UPLOAD_TASK_STATE_COMPLETED; IMG_UPLOAD_TASK * msg=new IMG_UPLOAD_TASK; memcpy(msg, &m_task_info, sizeof(IMG_UPLOAD_TASK)); // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("�ϴ��Ծ��µ�����(%d)���"), m_task_info.upload_task_id); if(m_hwnd!=NULL)::PostMessage(m_hwnd,WM_IDENTIFOR_IMG_UPLOAD_TASK_STATE,(WPARAM)msg,0); m_current_database_state = IMG_UPLOAD_STATE_CLOSED; } break; case IMG_UPLOAD_STATE_CLOSED://�ϴ�����һ���� ������״̬��Ϊ����һ������ //DeleteDirectory(m_bantch_dir); m_current_database_state = IMG_UPLOAD_STATE_OPENNING; break; } return running; } //�ϴ��������׼�� ServiceState CImgUploader::OnStarting( void ) { // pTestLogger = Logger::getInstance(LOG4CPLUS_TEXT("LoggerName")); // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("�������ͼƬ�ϴ�����")); m_uploading = FALSE; for (int index =0;index& tasks) { EnterCriticalSection(database_db_lock); char sql[256]; sprintf_s(sql, "select * from database where database_state in(%d,%d,%d,%d) order by database_id desc", DATABASE_STATE_RESULT_UPLOADED, DATABASE_STATE_IMG_UPLOADING, DATABASE_STATE_IMG_PAUSED, DATABASE_STATE_IMG_UPLOADED); CppSQLite3Query query = database_db->execQuery(sql); while (!query.eof()){ IMG_UPLOAD_TASK task; task.upload_task_id = query.getIntField("database_id"); task.total_count = query.getIntField("total_upload_count"); //strcpy(task.task_name ,query.getStringField("bantch_code")); strcpy(task.time, query.getStringField("create_time")); strcpy(task.task_name, query.getStringField("exam_name")); sprintf_s(task.task_name, "%s(%s)", query.getStringField("exam_name"), query.getStringField("course_name")); int database_state = query.getIntField("database_state"); switch (database_state){ case DATABASE_STATE_RESULT_UPLOADED:task.upload_task_state = UPLOAD_TASK_STATE_NOTSTART; break; case DATABASE_STATE_IMG_UPLOADING:task.upload_task_state = UPLOAD_TASK_STATE_UPLOADING; break; case DATABASE_STATE_IMG_PAUSED:task.upload_task_state = UPLOAD_TASK_STATE_NOTSTART; break; case DATABASE_STATE_IMG_UPLOADED:task.upload_task_state = UPLOAD_TASK_STATE_COMPLETED; break; } //task.upload_task_state = UPLOAD_TASK_STATE_NOTSTART; task.upload_count = 0; tasks.push_back(task); query.nextRow(); } LeaveCriticalSection(database_db_lock); return TRUE; } int CImgUploader::ClearUploadedPaperImg() { EnterCriticalSection(database_db_lock); char sql[256]; sprintf_s(sql, "update database set database_state =%d where database_state =%d", DATABASE_STATE_DELETING, DATABASE_STATE_IMG_UPLOADED); database_db->execDML(sql); LeaveCriticalSection(database_db_lock); return TRUE; } int CImgUploader::SetHandlerWnd(HWND hwnd) { m_hwnd = hwnd; return 0; }