diff --git a/src/aria2api.cc b/src/aria2api.cc index 0d1e984a2..2943b5911 100644 --- a/src/aria2api.cc +++ b/src/aria2api.cc @@ -182,4 +182,141 @@ int addUri(Session* session, return 0; } +namespace { +struct RequestGroupDH : public DownloadHandle { + RequestGroupDH(const SharedHandle& group) + : group(group), + ts(group->calculateStat()) + {} + virtual ~RequestGroupDH() {} + virtual DOWNLOAD_STATUS getStatus() + { + if(group->getState() == RequestGroup::STATE_ACTIVE) { + return DOWNLOAD_ACTIVE; + } else { + if(group->isPauseRequested()) { + return DOWNLOAD_PAUSED; + } else { + return DOWNLOAD_WAITING; + } + } + } + virtual int64_t getTotalLength() + { + return group->getTotalLength(); + } + virtual int64_t getCompletedLength() + { + return group->getCompletedLength(); + } + virtual int64_t getUploadLength() + { + return ts.allTimeUploadLength; + } + virtual int getDownloadSpeed() + { + return ts.downloadSpeed; + } + virtual int getUploadSpeed() + { + return ts.uploadSpeed; + } + SharedHandle group; + TransferStat ts; +}; +} // namespace + +namespace { +struct DownloadResultDH : public DownloadHandle { + DownloadResultDH(const SharedHandle& dr) + : dr(dr) + {} + virtual ~DownloadResultDH() {} + virtual DOWNLOAD_STATUS getStatus() + { + switch(dr->result) { + case error_code::FINISHED: + return DOWNLOAD_COMPLETE; + case error_code::REMOVED: + return DOWNLOAD_REMOVED; + default: + return DOWNLOAD_ERROR; + } + } + virtual int64_t getTotalLength() + { + return dr->totalLength; + } + virtual int64_t getCompletedLength() + { + return dr->completedLength; + } + virtual int64_t getUploadLength() + { + return dr->uploadLength; + } + virtual int getDownloadSpeed() + { + return 0; + } + virtual int getUploadSpeed() + { + return 0; + } + SharedHandle dr; +}; +} // namespace + +DownloadHandle* getDownloadHandle(Session* session, const A2Gid& gid) +{ + const SharedHandle& e = + session->context->reqinfo->getDownloadEngine(); + const SharedHandle& rgman = e->getRequestGroupMan(); + SharedHandle group = rgman->findGroup(gid); + if(group) { + return new RequestGroupDH(group); + } else { + SharedHandle ds = rgman->findDownloadResult(gid); + if(ds) { + return new DownloadResultDH(ds); + } + } + return 0; +} + +void deleteDownloadHandle(DownloadHandle* dh) +{ + delete dh; +} + +DOWNLOAD_STATUS downloadGetStatus(DownloadHandle* dh) +{ + return dh->getStatus(); +} + +int64_t downloadGetTotalLength(DownloadHandle* dh) +{ + return dh->getTotalLength(); +} + +int64_t downloadGetCompletedLength(DownloadHandle* dh) +{ + return dh->getCompletedLength(); +} + +int64_t downloadGetUploadLength(DownloadHandle* dh) +{ + return dh->getUploadLength(); +} + +int downloadGetDownloadSpeed(DownloadHandle* dh) +{ + return dh->getDownloadSpeed(); +} + +int downloadGetUploadSpeed(DownloadHandle* dh) +{ + return dh->getUploadSpeed(); +} + } // namespace aria2 diff --git a/src/aria2api.h b/src/aria2api.h index 29fe529e0..82fb7436a 100644 --- a/src/aria2api.h +++ b/src/aria2api.h @@ -50,6 +50,16 @@ struct Session { SharedHandle context; }; +struct DownloadHandle { + virtual ~DownloadHandle() {} + virtual DOWNLOAD_STATUS getStatus() = 0; + virtual int64_t getTotalLength() = 0; + virtual int64_t getCompletedLength() = 0; + virtual int64_t getUploadLength() = 0; + virtual int getDownloadSpeed() = 0; + virtual int getUploadSpeed() = 0; +}; + } // namespace aria2 #endif // ARIA2_API_H diff --git a/src/includes/aria2/aria2.h b/src/includes/aria2/aria2.h index 625c1ec39..fb4a2c147 100644 --- a/src/includes/aria2/aria2.h +++ b/src/includes/aria2/aria2.h @@ -42,6 +42,15 @@ #include #include +// Libaria2: The aim of this library is provide same functionality +// available in RPC methods. The function signatures are not +// necessarily the same, because we can take advantage of the direct, +// no latency, access to the aria2 core. +// +// Therefore, this library is not meant to be the fine-grained, +// customizable, complete HTTP/FTP/BitTorrent library. If you are +// looking for such library for HTTP/FTP access, consider libcurl. + namespace aria2 { struct Session; @@ -49,10 +58,14 @@ struct Session; // Initializes the global data. It also initializes // underlying libraries libaria2 depends on. This function returns 0 // if it succeeds, or -1. +// +// Call this function only once before calling any other API functions. int libraryInit(); // Releases the global data. This function returns 0 if // it succeeds, or -1. +// +// Call this function only once at the end of the application. int libraryDeinit(); // type of GID @@ -65,6 +78,9 @@ typedef std::vector > KeyVals; // parameters. The |options| is treated as if they are specified in // command-line to aria2c(1). This function returns the pointer to the // newly created Session object if it succeeds, or NULL. +// +// Please note that only one Session object can be created per +// process. Session* sessionNew(const KeyVals& options); // Performs post-download action, including saving sessions etc and @@ -108,6 +124,36 @@ int addUri(Session* session, const KeyVals& options, int position = -1); +// Query download +enum DOWNLOAD_STATUS { + DOWNLOAD_ACTIVE, + DOWNLOAD_WAITING, + DOWNLOAD_PAUSED, + DOWNLOAD_COMPLETE, + DOWNLOAD_ERROR, + DOWNLOAD_REMOVED +}; + +struct DownloadHandle; + +// Returns handle for the download denoted by the |gid|. The caller +// can retrieve various information of the download via returned +// handle. The lifetime of the returned handle is before the next call +// of run() or sessionFinal(). This function returns NULL if no +// download denoted by the |gid| is present. The caller must call +// deleteDownloadHandle() to delete the acquired handle. +DownloadHandle* getDownloadHandle(Session* session, const A2Gid& gid); + +// Deallocates the |dh|. Calling this function with NULL is safe. +void deleteDownloadHandle(DownloadHandle* dh); + +DOWNLOAD_STATUS downloadGetStatus(DownloadHandle* dh); +int64_t downloadGetTotalLength(DownloadHandle* dh); +int64_t downloadGetCompletedLength(DownloadHandle* dh); +int64_t downloadGetUploadLength(DownloadHandle* dh); +int downloadGetDownloadSpeed(DownloadHandle* dh); +int downloadGetUploadSpeed(DownloadHandle* dh); + } // namespace aria2 #endif // ARIA2_H diff --git a/src/option_processing.cc b/src/option_processing.cc index 53689d13f..c84aa6e19 100644 --- a/src/option_processing.cc +++ b/src/option_processing.cc @@ -274,6 +274,10 @@ void option_processing(Option& op, bool standalone, overrideWithEnv(*confOption, oparser, PREF_FTP_PROXY, "ftp_proxy"); overrideWithEnv(*confOption, oparser, PREF_ALL_PROXY, "all_proxy"); overrideWithEnv(*confOption, oparser, PREF_NO_PROXY, "no_proxy"); + // For non-standalone mode, set PREF_QUIET to true to suppress + // output. The caller can override this by including PREF_QUIET in + // options argument. + confOption->put(PREF_QUIET, A2_V_TRUE); // we must clear eof bit and seek to the beginning of the buffer. cmdstream.clear();