diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2013-05-15 00:50:55 +0900 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2013-05-15 00:50:55 +0900 |
| commit | cef6db17ba08f40f39b879a9e1a7b71af1374df4 (patch) | |
| tree | f459c559da813c93064f833778cbf14c5e390427 /src | |
| parent | 4f5d26a0c72e8906afa4b30118b6b5414c429c38 (diff) | |
Add changePosition API
Diffstat (limited to 'src')
| -rw-r--r-- | src/IndexedList.h | 24 | ||||
| -rw-r--r-- | src/RequestGroupMan.cc | 2 | ||||
| -rw-r--r-- | src/RequestGroupMan.h | 2 | ||||
| -rw-r--r-- | src/RpcMethodImpl.cc | 8 | ||||
| -rw-r--r-- | src/aria2api.cc | 12 | ||||
| -rw-r--r-- | src/includes/aria2/aria2.h | 50 |
6 files changed, 78 insertions, 20 deletions
diff --git a/src/IndexedList.h b/src/IndexedList.h index abccf295..0a259225 100644 --- a/src/IndexedList.h +++ b/src/IndexedList.h @@ -42,13 +42,9 @@ #include <vector> #include <algorithm> -namespace aria2 { +#include <aria2/aria2.h> -enum A2_HOW { - A2_POS_SET, - A2_POS_CUR, - A2_POS_END -}; +namespace aria2 { template<typename SeqType, typename ValueType, typename ReferenceType, typename PointerType, typename SeqIteratorType> @@ -539,15 +535,15 @@ public: } // Moves element with |key| to the specified position. If |how| is - // A2_POS_CUR, the element is moved to the position |offset| - // relative to the current position. If |how| is A2_POS_SET, the - // element is moved to the position |offset|. If |how| is - // A2_POS_END, the element is moved to the position |offset| + // OFFSET_MODE_CUR, the element is moved to the position |offset| + // relative to the current position. If |how| is OFFSET_MODE_SET, + // the element is moved to the position |offset|. If |how| is + // OFFSET_MODE_END, the element is moved to the position |offset| // relative to the end of the list. This function returns the // position the elment is moved to if it succeeds, or -1 if no // element with |key| is found or |how| is invalid. Complexity: // O(N) - ssize_t move(KeyType key, ssize_t offset, A2_HOW how) + ssize_t move(KeyType key, ssize_t offset, OffsetMode how) { typename IndexType::iterator idxent = index_.find(key); if(idxent == index_.end()) { @@ -562,16 +558,16 @@ public: ssize_t xp = std::distance(seq_.begin(), x); ssize_t size = index_.size(); ssize_t dest; - if(how == A2_POS_CUR) { + if(how == OFFSET_MODE_CUR) { if(offset > 0) { dest = std::min(xp+offset, static_cast<ssize_t>(size-1)); } else { dest = std::max(xp+offset, static_cast<ssize_t>(0)); } } else { - if(how == A2_POS_END) { + if(how == OFFSET_MODE_END) { dest = std::min(size-1+offset, size-1); - } else if(how == A2_POS_SET) { + } else if(how == OFFSET_MODE_SET) { dest = std::min(offset, size-1); } else { return -1; diff --git a/src/RequestGroupMan.cc b/src/RequestGroupMan.cc index 3fdf064f..60ededbb 100644 --- a/src/RequestGroupMan.cc +++ b/src/RequestGroupMan.cc @@ -192,7 +192,7 @@ SharedHandle<RequestGroup> RequestGroupMan::findGroup(a2_gid_t gid) const } size_t RequestGroupMan::changeReservedGroupPosition -(a2_gid_t gid, int pos, A2_HOW how) +(a2_gid_t gid, int pos, OffsetMode how) { ssize_t dest = reservedGroups_.move(gid, pos, how); if(dest == -1) { diff --git a/src/RequestGroupMan.h b/src/RequestGroupMan.h index 186ed379..ab595372 100644 --- a/src/RequestGroupMan.h +++ b/src/RequestGroupMan.h @@ -180,7 +180,7 @@ public: // beginning or the end of the queue respectively. Returns the // destination position. size_t changeReservedGroupPosition(a2_gid_t gid, int pos, - A2_HOW how); + OffsetMode how); bool removeReservedGroup(a2_gid_t gid); diff --git a/src/RpcMethodImpl.cc b/src/RpcMethodImpl.cc index 3c7f386f..c88753f7 100644 --- a/src/RpcMethodImpl.cc +++ b/src/RpcMethodImpl.cc @@ -1301,13 +1301,13 @@ SharedHandle<ValueBase> ChangePositionRpcMethod::process a2_gid_t gid = str2Gid(gidParam); int pos = posParam->i(); const std::string& howStr = howParam->s(); - A2_HOW how; + OffsetMode how; if(howStr == "POS_SET") { - how = A2_POS_SET; + how = OFFSET_MODE_SET; } else if(howStr == "POS_CUR") { - how = A2_POS_CUR; + how = OFFSET_MODE_CUR; } else if(howStr == "POS_END") { - how = A2_POS_END; + how = OFFSET_MODE_END; } else { throw DL_ABORT_EX("Illegal argument."); } diff --git a/src/aria2api.cc b/src/aria2api.cc index 2e4702ff..cd4eec59 100644 --- a/src/aria2api.cc +++ b/src/aria2api.cc @@ -367,6 +367,18 @@ int unpauseDownload(Session* session, const A2Gid& gid) return 0; } +int changePosition(Session* session, const A2Gid& gid, int pos, OffsetMode how) +{ + const SharedHandle<DownloadEngine>& e = + session->context->reqinfo->getDownloadEngine(); + try { + return e->getRequestGroupMan()->changeReservedGroupPosition(gid, pos, how); + } catch(RecoverableException& e) { + A2_LOG_INFO_EX(EX_EXCEPTION_CAUGHT, e); + return -1; + } +} + std::vector<A2Gid> getActiveDownload(Session* session) { const SharedHandle<DownloadEngine>& e = diff --git a/src/includes/aria2/aria2.h b/src/includes/aria2/aria2.h index 94ebe427..46281a66 100644 --- a/src/includes/aria2/aria2.h +++ b/src/includes/aria2/aria2.h @@ -362,6 +362,56 @@ int pauseDownload(Session* session, const A2Gid& gid, bool force = false); int unpauseDownload(Session* session, const A2Gid& gid); /** + * @enum + * + * Constants how to re-position a download. + */ +enum OffsetMode { + /** + * Moves the download to a position relative to the beginning of the + * queue. + */ + OFFSET_MODE_SET, + /** + * Moves the download to a position relative to the current + * position. + */ + OFFSET_MODE_CUR, + /** + * Moves the download to a position relative to the end of the + * queue. + */ + OFFSET_MODE_END +}; + +/** + * @function + * + * Changes the position of the download denoted by the |gid|. if it is + * in :c:macro:`DOWNLOAD_WAITING` or :c:macro:`DOWNLOAD_PAUSED` state. + * If the |how| is :c:macro:`OFFSET_MODE_SET`, it moves the download + * to a position |pos| relative to the beginning of the queue. If the + * |how| is :c:macro:`OFFSET_MODE_CUR`, it moves the download to a + * position |pos| relative to the current position. If the |how| is + * :c:macro:`OFFSET_MODE_END`, it moves the download to a position + * |pos| relative to the end of the queue. If the destination position + * is less than 0 or beyond the end of the queue, it moves the + * download to the beginning or the end of the queue respectively. The + * response is the destination position on success. + * + * For example, if the download having GID gid is placed in position + * 3, ``changePosition(gid, -1, OFFSET_MODE_CUR)`` will change its + * position to 2. Additional call ``changePosition(gid, 0, + * OFFSET_MODE_SET)`` will change its position to 0 (the beginning of + * the queue). + * + * This function returns the final destination position of this + * download, or negative error code. + */ +int changePosition(Session* session, const A2Gid& gid, int pos, + OffsetMode how); + +/** * @function * * Schedules shutdown. If the |force| is true, shutdown will take |
