/* */ #ifndef _D_DOWNLOAD_ENGINE_H_ #define _D_DOWNLOAD_ENGINE_H_ #include "Command.h" #include "Socket.h" #include "SegmentMan.h" #include "common.h" #include "Logger.h" #include "Option.h" typedef deque Sockets; typedef deque Commands; typedef deque CommandUuids; class SocketEntry { public: enum TYPE { TYPE_RD, TYPE_WR, }; SocketHandle socket; CommandUuid commandUuid; TYPE type; public: SocketEntry(const SocketHandle& socket, const CommandUuid& commandUuid, TYPE type): socket(socket), commandUuid(commandUuid), type(type) {} ~SocketEntry() {} bool operator==(const SocketEntry& entry) { return socket == entry.socket && commandUuid == entry.commandUuid && type == entry.type; } }; typedef deque SocketEntries; class DownloadEngine { private: void waitData(CommandUuids& activeUuids); SocketEntries socketEntries; fd_set rfdset; fd_set wfdset; int fdmax; void shortSleep() const; bool addSocket(const SocketEntry& socketEntry); bool deleteSocket(const SocketEntry& socketEntry); protected: const Logger* logger; virtual void initStatistics() = 0; virtual void calculateStatistics() = 0; virtual void onEndOfRun() = 0; virtual void afterEachIteration() {} public: bool noWait; Commands commands; SegmentMan* segmentMan; const Option* option; DownloadEngine(); virtual ~DownloadEngine(); void run(); void cleanQueue(); void updateFdSet(); bool addSocketForReadCheck(const SocketHandle& socket, const CommandUuid& commandUuid); bool deleteSocketForReadCheck(const SocketHandle& socket, const CommandUuid& commandUuid); bool addSocketForWriteCheck(const SocketHandle& socket, const CommandUuid& commandUuid); bool deleteSocketForWriteCheck(const SocketHandle& socket, const CommandUuid& command); }; #endif // _D_DOWNLOAD_ENGINE_H_