123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- #ifndef RVC_MOD_DOWNLOAD_H_
- #define RVC_MOD_DOWNLOAD_H_
- #include "stdafx.h"
- #include "SpBase.h"
- #include "SpIni.h"
- #include "SpTest.h"
- #include "modVer.h"
- #include "strutil.h"
- #include <list>
- #include "DownloadFSM.h"
- #include "Download_server_g.h"
- #include "libtoolkit/path.h"
- using namespace Download;
- class CDownloadEntity;
- class DownloadSession : public DownloadService_ServerSessionBase
- {
- public:
- DownloadSession(CDownloadEntity* pEntity) : m_pEntity(pEntity) {}
- virtual ~DownloadSession() {}
- virtual void Handle_DownloadFile(SpReqAnsContext<DownloadService_DownloadFile_Req, DownloadService_DownloadFile_Ans>::Pointer ctx);
- virtual void Handle_IsDownloading(SpReqAnsContext<DownloadService_IsDownloading_Req, DownloadService_IsDownloading_Ans>::Pointer ctx);
- virtual void Handle_CancelDownloadFile(SpReqAnsContext<DownloadService_CancelDownloadFile_Req, DownloadService_CancelDownloadFile_Ans>::Pointer ctx);
- virtual void Handle_QueryDownloadState(SpReqAnsContext<DownloadService_QueryDownloadState_Req, DownloadService_QueryDownloadState_Ans>::Pointer ctx);
- private:
- CDownloadEntity* m_pEntity;
- };
- class CDownloadEntity : public CEntityBase, public IFSMStateHooker
- {
- public:
- CDownloadEntity() :m_bSynchronized(false) {}
- virtual ~CDownloadEntity() {}
- virtual const char* GetEntityName() const { return "Download"; } // 0x105
- virtual bool IsService()const { return true; }
- const char* GetEntityVersion() const override
- {
- return MODULE_VERSION_FULL;
- }
- ON_ENTITYT_TEST();
- virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs, CSmartPointer<ITransactionContext> pTransactionContext)
- {
- ErrorCodeEnum Error = Error_Succeed;
- //初始化:加载状态hooker,初始化状态机
- m_fsm.AddStateHooker(this);
- Error = m_fsm.Init(this);
-
- pTransactionContext->SendAnswer(Error);
- }
- virtual void OnPreClose(EntityCloseCauseEnum eCloseCause, CSmartPointer<ITransactionContext> pTransactionContext)
- {
- pTransactionContext->SendAnswer(Error_Succeed);
- }
- virtual CServerSessionBase* OnNewSession(const char* /*pszRemoteEntityName*/, const char* /*pszClass*/)
- {
- LOG_FUNCTION();
- return new DownloadSession(this);
- }
- virtual void OnSelfTest(EntityTestEnum eTestType, CSmartPointer<ITransactionContext> pTransactionContext)
- {
- if (Test_ShakeHand == eTestType)
- {
- pTransactionContext->SendAnswer(Error_Succeed);
- }
- }
- virtual void OnStateTrans(int iSrcState, int iDstState)
- {
- if (iDstState == DownloadFSM::SYNC_STATE) { // Enter
- m_bSynchronized = true;
- }
- else if (iSrcState == DownloadFSM::SYNC_STATE) { // Leave
- m_bSynchronized = false;
- }
- }
- bool IsSynchronized()
- {
- return m_bSynchronized;
- }
- //接收新下载任务
- ErrorCodeEnum DownloadFile(SpReqAnsContext<DownloadService_DownloadFile_Req, DownloadService_DownloadFile_Ans>::Pointer ctx)
- {
- Dbg("DownloadFile req file name is : [%s] , expireTime is [%d]", (const char*)ctx->Req.strFileName, ctx->Req.dwExpireTime);
- ErrorCodeEnum Error = m_fsm.addDownLoadFileWork(ctx->Req.strFileName, ctx->Req.dwExpireTime);
- if (Error != Error_Succeed) {
- Dbg("DownloadFile req fail, file name is : [%s] ", (const char*)ctx->Req.strFileName);
- }
- return Error;
- }
- //整个流程是否正在下载任务
- ErrorCodeEnum IsDownloading(bool& bDownloading)
- {
- bDownloading = !m_bSynchronized;
- return Error_Succeed;
- }
- //取消下载任务
- ErrorCodeEnum CancelDownloadFile(SpReqAnsContext<DownloadService_CancelDownloadFile_Req, DownloadService_CancelDownloadFile_Ans>::Pointer ctx)
- {
- Dbg("CancelDownloadFile req file name is : [%s]", (const char*)ctx->Req.strFileName);
- //ErrorCodeEnum Error = m_fsm.deleteCurrentDownloadFile(ctx->Req.strFileName);
- ErrorCodeEnum Error = m_fsm.deleteDownLoadFileWork(ctx->Req.strFileName);
- if (Error != Error_Succeed) {
- Dbg("CancelDownloadFile req fail, file name is : [%s] ", (const char*)ctx->Req.strFileName);
- }
- m_fsm.PostEventFIFO(new FSMEvent(USER_EVT_JMP_DISABLE));//因下载任务发生变化,结束当前下载,重新开始新的一轮下载。
- return Error;
- }
- //查询下载任务状态
- ErrorCodeEnum QueryDownloadState(SpReqAnsContext<DownloadService_QueryDownloadState_Req, DownloadService_QueryDownloadState_Ans>::Pointer ctx)
- {
- //检查文件是否下载完毕,并检查本地文件是否存在
- const char* filename = (const char*)ctx->Req.strFileName;
- Dbg("QueryDownloadState req file name is : [%s]", filename);
- //当前正在下载,排队,不存在后则看是否下载完成,不存在
- //1:正在下载 2:排队中 3:下载完成 4:无下载任务并且没有文件
- if (strcmp(filename, m_fsm.m_currentFileName.GetData()) == 0) {
- ctx->Ans.iDownloadState = 1;
- return Error_Succeed;
- }
- if (m_fsm.isDownloadTask(filename)) {
- ctx->Ans.iDownloadState = 2;
- return Error_Succeed;
- }
- return Error_Succeed;
- CSimpleStringA str;
- GetFunction()->GetPath("Download", str);
- char tmp[MAX_PATH];
-
- strcpy(tmp, str.GetData());
- if (str_has_suffix(str.GetData(), SPLIT_SLASH_STR)) {
- strcat(tmp, filename);
- }
- else {
- strcat(tmp, SPLIT_SLASH_STR);
- strcat(tmp, filename);
- }
- //WIN32_FIND_DATAA fd;
- //HANDLE hFind = FindFirstFileA(tmp, &fd);
- //if (hFind != INVALID_HANDLE_VALUE) {
- // FindClose(hFind);
- // ctx->Ans.iDownloadState = 3;
- // return Error_Succeed;
- //}
- //else {
- // FindClose(hFind);
- // ctx->Ans.iDownloadState = 4;
- // return Error_Succeed;
- //}
- FILE* fp = fopen(tmp, "rb");
- if(fp!=NULL)
- {
- fclose(fp);
- ctx->Ans.iDownloadState = 3;
- return Error_Succeed;
- }
- else
- {
- ctx->Ans.iDownloadState = 4;
- return Error_Succeed;
- }
-
- }
- private:
- /*ErrorCodeEnum MakeFileList(std::list<CSimpleStringA>& fileList)
- {
- CSimpleStringA str;
- GetFunction()->GetPath("Download", str);
- return __MakeFileList(str, fileList);
- }*/
- /*ErrorCodeEnum __MakeFileList(const char* base_dir, std::list<CSimpleStringA>& fileList)
- {
- char tmp[MAX_PATH];
- WIN32_FIND_DATAA fd;
- strcpy(tmp, base_dir);
- if (str_has_suffix(base_dir, "\\")) {
- strcat(tmp, "*");
- }
- else {
- strcat(tmp, "\\*.*");
- }
- HANDLE hFind = FindFirstFileA(tmp, &fd);
- if (hFind != INVALID_HANDLE_VALUE) {
- do {
- if ((fd.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) && !(fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)) {
- fileList.push_back(CSimpleStringA(fd.cFileName));
- }
- } while (FindNextFileA(hFind, &fd));
- FindClose(hFind);
- }
- return Error_Succeed;
- }*/
- private:
- bool m_bSynchronized;
- DownloadFSM m_fsm;
- };
- #endif
|