JD2022-TU1/main/extern/Camcam/Inc/download_task.h

135 lines
No EOL
3.7 KiB
C++

#ifndef DOWNLOAD_TASK_H
#define DOWNLOAD_TASK_H
#include "http_client_curl.h"
#include "camcam_httpclient.h"
#include "card.h"
#include <list>
class CDownloadQueueTask;
class CardElement;
/**
*
*/
class DownloadAction
{
public:
DownloadAction(CardElement *card) : _RelatedCard(card), _Status(false) {}
const CardElement *relatedCard() const { return _RelatedCard; }
virtual void onEnqueue();
virtual void bgDownload(CDownloadQueueTask *task) = 0;
virtual void onDownloaded(CDownloadQueueTask *task, lua_State * L);
virtual void setIdMedia(u32 idMedia) {}
virtual u32 getIdMedia() const { return ~0; }
virtual ~DownloadAction() {}
protected:
CardElement *_RelatedCard;
bool _Status;
};
/**
* Download action for thumbnails
*/
class DownloadThumbnailAction : public DownloadAction
{
public:
DownloadThumbnailAction(CardElement *card, u32 idMedia, const std::string& filename) : DownloadAction(card), _IdMedia(idMedia), _ActualPath(filename) {}
void bgDownload(CDownloadQueueTask *task);
void onDownloaded(CDownloadQueueTask *task, lua_State * L);
void setIdMedia(u32 idMedia) { _IdMedia = idMedia; }
u32 getIdMedia() const { return _IdMedia; }
static void clearCDNBaseURL() { _CDNBaseURL.clear(); } // call from download thread only
private:
u32 _IdMedia;
std::string _ActualPath;
static std::string _CDNBaseURL; // must be accessed from a single thread (the download thread)
};
/**
* Download action on ZCC files
*/
class DownloadZccAction : public DownloadAction
{
public:
DownloadZccAction(CardElement *card) : DownloadAction(card) {}
void bgDownload(CDownloadQueueTask *task);
void onDownloaded(CDownloadQueueTask *task, lua_State * L);
static bool checkFileIntegrity(const char *zccFilename, const char *serverHashCode);
protected:
/// Make sur that zccCard is not NULL
bool ensureValidZccPresent(CDownloadQueueTask *task, ZccCard *zccCard);
};
/**
* Request to know if there are new cards
*/
class RefreshCardStatusAction : public DownloadAction
{
public:
RefreshCardStatusAction() : DownloadAction(NULL) {}
void bgDownload(CDownloadQueueTask *task);
void onDownloaded(CDownloadQueueTask *task, lua_State * L);
static bool requestAssets(CamCamHttpClient& ccHttpClient); // set the response page in ccHttpClient.getHttpClient()
private:
HttpPage _Buffer;
};
/**
* Queuing and downloading assets from the CamCam server (singleton)
*/
class CDownloadQueueTask : public CBackgroundTask, public CamCamHttpClient
{
public:
CDownloadQueueTask() : _CurrentDownloadingAction(NULL), _CardsLocked(false) {}
~CDownloadQueueTask() {}
bool init(const SimpleString<16> *pIdAccount);
void enqueue(DownloadAction *action, bool topPriority=false);
void setPriority(CardElement *card);
void setDestinationMedia(CardElement *card, u32 idMedia);
bool isAlreadyDownloaded(CardElement *card);
const CardElement *currentDownloadCardElement();
bool isEmpty();
void clear();
/// Get Memory usage (not user mode)
#ifndef USER_MODE
unsigned int getMemoryUsage() const;
#endif
/// Return the first completed download of the output queue (NULL if empty). Caller must delete it after usage.
DownloadAction *getNextCompletedDownload();
/// Wait until the task does not hold references to cards (call from main thread), this will usually be shorter if you call clear() before.
void waitUntilNoCardsNeeded();
private:
/// The code of the background task.
void run();
friend class DownloadThumbnailAction;
friend class RefreshCardStatusAction;
static CDownloadQueueTask *_Instance;
typedef std::list<DownloadAction*> Fifo;
Fifo _FifoToDownload;
MUTEX_TYPE _MutexToDownload;
Fifo _FifoDownloaded;
MUTEX_TYPE _MutexDownloaded;
DownloadAction *_CurrentDownloadingAction;
volatile bool _CardsLocked;
SimpleString<256> _ErrorString;
};
#endif // DOWNLOAD_TASK_H