Browse Source

Use 64 bits random bytes as GID

This change replaces the current 64 bit sequential GID with 64 bits
random bytes GID in an attempt to support persistent GID. Internally,
the GID is stored as uint64_t. For human representation and RPC
interface, GID is represented as 16 bytes hex string. For console
readout, 16 bytes are too long, so it is abbreviated to first 6 bytes.
When querying GID in RPC calls, user can speicfy the prefix of GID as
long as the prefix is shared by more than 1 GID entries.
Tatsuhiro Tsujikawa 13 năm trước cách đây
mục cha
commit
b9da9d4ed3

+ 6 - 6
src/BtDependency.cc

@@ -155,20 +155,20 @@ bool BtDependency::resolve()
       }
     } catch(RecoverableException& e) {
       A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
-      A2_LOG_INFO(fmt("BtDependency for GID#%" PRId64 " failed. Go without Bt.",
-                      dependant_->getGID()));
+      A2_LOG_INFO(fmt("BtDependency for GID#%s failed. Go without Bt.",
+                      GroupId::toHex(dependant_->getGID()).c_str()));
       return true;
     }
-    A2_LOG_INFO(fmt("Dependency resolved for GID#%" PRId64 "",
-                    dependant_->getGID()));
+    A2_LOG_INFO(fmt("Dependency resolved for GID#%s",
+                    GroupId::toHex(dependant_->getGID()).c_str()));
     dependant_->setDownloadContext(context);
     return true;
   } else if(dependee_->getNumCommand() == 0) {
     // dependee_'s download failed.
     // cut reference here
     dependee_.reset();
-    A2_LOG_INFO(fmt("BtDependency for GID#%" PRId64 " failed. Go without Bt.",
-                    dependant_->getGID()));
+    A2_LOG_INFO(fmt("BtDependency for GID#%s failed. Go without Bt.",
+                    GroupId::toHex(dependant_->getGID()).c_str()));
     return true;
   } else {
     return false;

+ 2 - 2
src/BtStopDownloadCommand.cc

@@ -62,9 +62,9 @@ void BtStopDownloadCommand::preProcess()
     enableExit();
   }
   if(checkPoint_.difference(global::wallclock()) >= timeout_) {
-    A2_LOG_NOTICE(fmt(_("GID#%" PRId64 " Stop downloading torrent due to"
+    A2_LOG_NOTICE(fmt(_("GID#%s Stop downloading torrent due to"
                         " --bt-stop-timeout option."),
-                      requestGroup_->getGID()));
+                      GroupId::toHex(requestGroup_->getGID()).c_str()));
     requestGroup_->setForceHaltRequested(true);
     getDownloadEngine()->setRefreshInterval(0);
     enableExit();

+ 4 - 4
src/ConsoleStatCalc.cc

@@ -146,7 +146,7 @@ void printProgressCompact(std::ostream& o, const DownloadEngine* e,
         i = groups.begin(), eoi = groups.end(); i != eoi && cnt < MAX_ITEM;
       ++i, ++cnt) {
     TransferStat stat = (*i)->calculateStat();
-    o << "[#" << (*i)->getGID() << " ";
+    o << "[#" << GroupId::toAbbrevHex((*i)->getGID()) << " ";
     printSizeProgress(o, *i, stat, sizeFormatter);
     o << "]";
   }
@@ -166,7 +166,7 @@ void printProgress
   if(rg->getTotalLength() > 0 && stat.downloadSpeed > 0) {
     eta = (rg->getTotalLength()-rg->getCompletedLength())/stat.downloadSpeed;
   }
-  o << "[#" << rg->getGID() << " ";
+  o << "[#" << GroupId::toAbbrevHex(rg->getGID()) << " ";
   printSizeProgress(o, rg, stat, sizeFormatter);
   o << " CN:"
     << rg->getNumConnection();
@@ -330,7 +330,7 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e)
       e->getFileAllocationMan()->getPickedEntry();
     if(entry) {
       o << " [FileAlloc:#"
-        << entry->getRequestGroup()->getGID() << " "
+        << GroupId::toAbbrevHex(entry->getRequestGroup()->getGID()) << " "
         << sizeFormatter(entry->getCurrentLength()) << "B/"
         << sizeFormatter(entry->getTotalLength()) << "B(";
       if(entry->getTotalLength() > 0) {
@@ -350,7 +350,7 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e)
       e->getCheckIntegrityMan()->getPickedEntry();
     if(entry) {
       o << " [Checksum:#"
-        << entry->getRequestGroup()->getGID() << " "
+        << GroupId::toAbbrevHex(entry->getRequestGroup()->getGID()) << " "
         << sizeFormatter(entry->getCurrentLength()) << "B/"
         << sizeFormatter(entry->getTotalLength()) << "B(";
       if(entry->getTotalLength() > 0) {

+ 1 - 1
src/DownloadResult.h

@@ -54,7 +54,7 @@ class MetadataInfo;
 
 struct DownloadResult
 {
-  a2_gid_t gid;
+  SharedHandle<GroupId> gid;
 
   std::vector<SharedHandle<FileEntry> > fileEntries;
 

+ 1 - 1
src/FtpNegotiationCommand.cc

@@ -423,7 +423,7 @@ bool FtpNegotiationCommand::onFileSizeDetermined(int64_t totalLength)
           sequence_ = SEQ_DOWNLOAD_ALREADY_COMPLETED;
           A2_LOG_NOTICE
             (fmt(MSG_DOWNLOAD_ALREADY_COMPLETED,
-                 getRequestGroup()->getGID(),
+                 GroupId::toHex(getRequestGroup()->getGID()).c_str(),
                  getRequestGroup()->getFirstFilePath().c_str()));
         }
       poolConnection();

+ 158 - 0
src/GroupId.cc

@@ -0,0 +1,158 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * Copyright (C) 2012 Tatsuhiro Tsujikawa
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#include "GroupId.h"
+#include "util.h"
+
+namespace aria2 {
+
+std::set<a2_gid_t> GroupId::set_;
+
+SharedHandle<GroupId> GroupId::create()
+{
+  a2_gid_t n;
+  for(;;) {
+    util::generateRandomData(reinterpret_cast<unsigned char*>(&n), sizeof(n));
+    if(n != 0 && set_.count(n) == 0) {
+      break;
+    }
+  }
+  SharedHandle<GroupId> res(new GroupId(n));
+  return res;
+}
+
+SharedHandle<GroupId> GroupId::import(a2_gid_t n)
+{
+  SharedHandle<GroupId> res;
+  if(n == 0 || set_.count(n) != 0) {
+    return res;
+  }
+  res.reset(new GroupId(n));
+  return res;
+}
+
+void GroupId::clear()
+{
+  set_.clear();
+}
+
+int GroupId::expandUnique(a2_gid_t& n, const char* hex)
+{
+  a2_gid_t p = 0;
+  size_t i;
+  for(i = 0; hex[i]; ++i) {
+    unsigned int c = util::hexCharToUInt(hex[i]);
+    if(c == 255) {
+      return ERR_INVALID;
+    }
+    p <<= 4;
+    p |= c;
+  }
+  if(i == 0 || i > sizeof(a2_gid_t)*2) {
+    return ERR_INVALID;
+  }
+  p <<= 64-i*4;
+  a2_gid_t mask = UINT64_MAX-((1LL << (64-i*4))-1);
+  std::set<a2_gid_t>::const_iterator itr = set_.lower_bound(p);
+  if(itr == set_.end()) {
+    return ERR_NOT_FOUND;
+  }
+  if(p == ((*itr)&mask)) {
+    n = *itr;
+    ++itr;
+    if(itr == set_.end() || p != ((*itr)&mask)) {
+      return 0;
+    } else {
+      return ERR_NOT_UNIQUE;
+    }
+  } else {
+    return ERR_NOT_FOUND;
+  }
+}
+
+int GroupId::toNumericId(a2_gid_t& n, const char* hex)
+{
+  a2_gid_t p = 0;
+  size_t i;
+  for(i = 0; hex[i]; ++i) {
+    unsigned int c = util::hexCharToUInt(hex[i]);
+    if(c == 255) {
+      return ERR_INVALID;
+    }
+    p <<= 4;
+    p |= c;
+  }
+  if(p == 0 || i != sizeof(a2_gid_t)*2) {
+    return ERR_INVALID;
+  }
+  n = p;
+  return 0;
+}
+
+std::string GroupId::toHex(a2_gid_t gid)
+{
+  a2_gid_t n = hton64(gid);
+  return util::toHex(reinterpret_cast<unsigned char*>(&n), sizeof(n));
+}
+
+std::string GroupId::toAbbrevHex(a2_gid_t gid)
+{
+  const size_t abbrevSize = 6;
+  std::string h = toHex(gid);
+  assert(h.size() >= abbrevSize);
+  return toHex(gid).erase(abbrevSize);
+}
+
+std::string GroupId::toHex() const
+{
+  return toHex(gid_);
+}
+
+std::string GroupId::toAbbrevHex() const
+{
+  return toAbbrevHex(gid_);
+}
+
+GroupId::GroupId(a2_gid_t gid)
+  : gid_(gid)
+{
+  set_.insert(gid_);
+}
+
+GroupId::~GroupId()
+{
+  set_.erase(gid_);
+}
+
+} // namespace aria2

+ 81 - 0
src/GroupId.h

@@ -0,0 +1,81 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * Copyright (C) 2012 Tatsuhiro Tsujikawa
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef D_GROUP_ID_H
+#define D_GROUP_ID_H
+
+#include "common.h"
+
+#include <set>
+#include <string>
+
+#include "SharedHandle.h"
+
+namespace aria2 {
+
+typedef uint64_t a2_gid_t;
+
+class GroupId {
+public:
+  static SharedHandle<GroupId> create();
+  static SharedHandle<GroupId> import(a2_gid_t n);
+  static void clear();
+  enum {
+    ERR_NOT_UNIQUE = -1,
+    ERR_NOT_FOUND = -2,
+    ERR_INVALID = -3
+  };
+  static int expandUnique(a2_gid_t& n, const char* hex);
+  static int toNumericId(a2_gid_t& n, const char* hex);
+  static std::string toHex(a2_gid_t n);
+  static std::string toAbbrevHex(a2_gid_t n);
+
+  ~GroupId();
+  a2_gid_t getNumericId() const
+  {
+    return gid_;
+  }
+  std::string toHex() const;
+  std::string toAbbrevHex() const;
+private:
+  static std::set<a2_gid_t> set_;
+
+  GroupId(a2_gid_t gid);
+
+  a2_gid_t gid_;
+};
+
+} // namespace aria2
+
+#endif // D_GROUP_ID_H

+ 2 - 2
src/HttpResponseCommand.cc

@@ -189,7 +189,7 @@ bool HttpResponseCommand::executeInternal()
     getDownloadContext()->setChecksumVerified(true);
     A2_LOG_NOTICE
       (fmt(MSG_DOWNLOAD_ALREADY_COMPLETED,
-           getRequestGroup()->getGID(),
+           GroupId::toHex(getRequestGroup()->getGID()).c_str(),
            getRequestGroup()->getFirstFilePath().c_str()));
     poolConnection();
     getFileEntry()->poolRequest(getRequest());
@@ -453,7 +453,7 @@ bool HttpResponseCommand::handleOtherEncoding
         getDownloadContext()->setChecksumVerified(true);
         A2_LOG_NOTICE
           (fmt(MSG_DOWNLOAD_ALREADY_COMPLETED,
-               getRequestGroup()->getGID(),
+               GroupId::toHex(getRequestGroup()->getGID()).c_str(),
                getRequestGroup()->getFirstFilePath().c_str()));
       }
     poolConnection();

+ 2 - 1
src/Makefile.am

@@ -242,7 +242,8 @@ SRCS =  SocketCore.cc SocketCore.h\
 	AnonDiskWriterFactory.h\
 	XmlRpcRequestParserController.cc XmlRpcRequestParserController.h\
 	WrDiskCache.cc WrDiskCache.h\
-	WrDiskCacheEntry.cc WrDiskCacheEntry.h
+	WrDiskCacheEntry.cc WrDiskCacheEntry.h\
+	GroupId.cc GroupId.h
 
 if MINGW_BUILD
 SRCS += WinConsoleFile.cc WinConsoleFile.h

+ 1 - 1
src/Metalink2RequestGroup.cc

@@ -241,7 +241,7 @@ Metalink2RequestGroup::createRequestGroup
     }
 #endif // ENABLE_BITTORRENT
     SharedHandle<Option> option = util::copy(optionTemplate);
-    SharedHandle<RequestGroup> rg(new RequestGroup(option));
+    SharedHandle<RequestGroup> rg(new RequestGroup(GroupId::create(), option));
     SharedHandle<DownloadContext> dctx;
     int numSplit = option->getAsInt(PREF_SPLIT);
     int maxConn = option->getAsInt(PREF_MAX_CONNECTION_PER_SERVER);

+ 11 - 18
src/RequestGroup.cc

@@ -120,10 +120,9 @@
 
 namespace aria2 {
 
-a2_gid_t RequestGroup::gidCounter_ = 0;
-
-RequestGroup::RequestGroup(const SharedHandle<Option>& option)
-  : gid_(newGID()),
+RequestGroup::RequestGroup(const SharedHandle<GroupId>& gid,
+                           const SharedHandle<Option>& option)
+  : gid_(gid),
     state_(STATE_WAITING),
     option_(option),
     numConcurrentCommand_(option->getAsInt(PREF_SPLIT)),
@@ -244,7 +243,8 @@ SharedHandle<CheckIntegrityEntry> RequestGroup::createCheckIntegrityEntry()
         {
           downloadContext_->setChecksumVerified(true);
           A2_LOG_NOTICE(fmt(MSG_DOWNLOAD_ALREADY_COMPLETED,
-                            gid_, downloadContext_->getBasePath().c_str()));
+                            gid_->toHex().c_str(),
+                            downloadContext_->getBasePath().c_str()));
         }
     } else {
       checkEntry.reset(new StreamCheckIntegrityEntry(this));
@@ -346,9 +346,9 @@ void RequestGroup::createInitialCommand
       btAnnouncePtr->shuffleAnnounce();
       SharedHandle<BtAnnounce> btAnnounce(btAnnouncePtr);
 
-      assert(!btRegistry->get(gid_));
+      assert(!btRegistry->get(gid_->getNumericId()));
       btRegistry->put
-        (gid_, SharedHandle<BtObject>
+        (gid_->getNumericId(), SharedHandle<BtObject>
          (new BtObject
           (downloadContext_,
            pieceStorage_,
@@ -962,7 +962,7 @@ void RequestGroup::decreaseNumCommand()
 {
   --numCommand_;
   if(!numCommand_ && requestGroupMan_) {
-    A2_LOG_DEBUG(fmt("GID#%" PRId64 " - Request queue check", gid_));
+    A2_LOG_DEBUG(fmt("GID#%s - Request queue check", gid_->toHex().c_str()));
     requestGroupMan_->requestQueueCheck();
   }
 }
@@ -1007,7 +1007,7 @@ void RequestGroup::setPauseRequested(bool f)
 void RequestGroup::releaseRuntimeResource(DownloadEngine* e)
 {
 #ifdef ENABLE_BITTORRENT
-  e->getBtRegistry()->remove(gid_);
+  e->getBtRegistry()->remove(gid_->getNumericId());
   btRuntime_ = 0;
   peerStorage_ = 0;
 #endif // ENABLE_BITTORRENT
@@ -1152,7 +1152,8 @@ bool RequestGroup::needsFileAllocation() const
 
 SharedHandle<DownloadResult> RequestGroup::createDownloadResult() const
 {
-  A2_LOG_DEBUG(fmt("GID#%" PRId64 " - Creating DownloadResult.", gid_));
+  A2_LOG_DEBUG(fmt("GID#%s - Creating DownloadResult.",
+                   gid_->toHex().c_str()));
   TransferStat st = calculateStat();
   SharedHandle<DownloadResult> res(new DownloadResult());
   res->gid = gid_;
@@ -1298,12 +1299,4 @@ bool RequestGroup::p2pInvolved() const
 #endif // !ENABLE_BITTORRENT
 }
 
-a2_gid_t RequestGroup::newGID()
-{
-  if(gidCounter_ == INT64_MAX) {
-    gidCounter_ = 0;
-  }
-  return ++gidCounter_;
-}
-
 } // namespace aria2

+ 5 - 11
src/RequestGroup.h

@@ -47,6 +47,7 @@
 #include "Request.h"
 #include "error_code.h"
 #include "MetadataInfo.h"
+#include "GroupId.h"
 
 namespace aria2 {
 
@@ -73,8 +74,6 @@ class BtRuntime;
 class PeerStorage;
 #endif // ENABLE_BITTORRENT
 
-typedef int64_t a2_gid_t;
-
 class RequestGroup {
 public:
   enum HaltReason {
@@ -89,9 +88,7 @@ public:
     STATE_ACTIVE
   };
 private:
-  static a2_gid_t gidCounter_;
-
-  a2_gid_t gid_;
+  SharedHandle<GroupId> gid_;
 
   int state_;
 
@@ -201,7 +198,8 @@ private:
   (const SharedHandle<BtProgressInfoFile>& progressInfoFile);
 
 public:
-  RequestGroup(const SharedHandle<Option>& option);
+  RequestGroup(const SharedHandle<GroupId>& gid,
+               const SharedHandle<Option>& option);
 
   ~RequestGroup();
 
@@ -264,7 +262,7 @@ public:
 
   a2_gid_t getGID() const
   {
-    return gid_;
+    return gid_->getNumericId();
   }
 
   TransferStat calculateStat() const;
@@ -550,10 +548,6 @@ public:
   {
     state_ = state;
   }
-
-  static void resetGIDCounter() { gidCounter_ = 0; }
-
-  static a2_gid_t newGID();
 };
 
 } // namespace aria2

+ 15 - 16
src/RequestGroupMan.cc

@@ -261,7 +261,8 @@ size_t RequestGroupMan::changeReservedGroupPosition
   std::deque<SharedHandle<RequestGroup> >::iterator i =
     findByGID(reservedGroups_.begin(), reservedGroups_.end(), gid);
   if(i == reservedGroups_.end()) {
-    throw DL_ABORT_EX(fmt("GID#%" PRId64 " not found in the waiting queue.", gid));
+    throw DL_ABORT_EX(fmt("GID#%s not found in the waiting queue.",
+                          GroupId::toHex(gid).c_str()));
   }
   SharedHandle<RequestGroup> rg = *i;
   const size_t maxPos = reservedGroups_.size()-1;
@@ -401,7 +402,8 @@ public:
         group->closeFile();
         if(group->isPauseRequested()) {
           A2_LOG_NOTICE
-            (fmt(_("Download GID#%" PRId64 " paused"), group->getGID()));
+            (fmt(_("Download GID#%s paused"),
+                 GroupId::toHex(group->getGID()).c_str()));
           group->saveControlFile();
         } else if(group->downloadFinished() &&
            !group->getDownloadContext()->isChecksumVerificationNeeded()) {
@@ -429,7 +431,8 @@ public:
           if(group->getOption()->getAsBool(PREF_BT_REMOVE_UNSELECTED_FILE) &&
              !group->inMemoryDownload() &&
              dctx->hasAttribute(CTX_ATTR_BT)) {
-            A2_LOG_INFO(fmt(MSG_REMOVING_UNSELECTED_FILE, group->getGID()));
+            A2_LOG_INFO(fmt(MSG_REMOVING_UNSELECTED_FILE,
+                            GroupId::toHex(group->getGID()).c_str()));
             const std::vector<SharedHandle<FileEntry> >& files =
               dctx->getFileEntries();
             for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
@@ -447,8 +450,8 @@ public:
 #endif // ENABLE_BITTORRENT
         } else {
           A2_LOG_NOTICE
-            (fmt(_("Download GID#%" PRId64 " not complete: %s"),
-                 group->getGID(),
+            (fmt(_("Download GID#%s not complete: %s"),
+                 GroupId::toHex(group->getGID()).c_str(),
                  group->getDownloadContext()->getBasePath().c_str()));
           group->saveControlFile();
         }
@@ -760,24 +763,20 @@ const char* getStatusStr(DownloadStatus status, bool useColor)
 
 void RequestGroupMan::showDownloadResults(OutputFile& o, bool full) const
 {
-#ifdef __MINGW32__
-  int pathRowSize = 58;
-#else // !__MINGW32__
-  int pathRowSize = 59;
-#endif // !__MINGW32__
+  int pathRowSize = 55;
   // Download Results:
   // idx|stat|path/length
   // ===+====+=======================================================================
   o.printf("\n%s"
-           "\ngid|stat|avg speed  |",
+           "\ngid   |stat|avg speed  |",
            _("Download Results:"));
   if(full) {
     o.write("  %|path/URI"
-            "\n===+====+===========+===+");
+            "\n======+====+===========+===+");
     pathRowSize -= 4;
   } else {
     o.write("path/URI"
-            "\n===+====+===========+");
+            "\n======+====+===========+");
   }
   std::string line(pathRowSize, '=');
   o.printf("%s\n", line.c_str());
@@ -837,7 +836,7 @@ void formatDownloadResultCommon
  const char* status,
  const SharedHandle<DownloadResult>& downloadResult)
 {
-  o << std::setw(3) << downloadResult->gid << "|"
+  o << std::setw(3) << downloadResult->gid->toAbbrevHex() << "|"
     << std::setw(4) << status << "|"
     << std::setw(11);
   if(downloadResult->sessionTime > 0) {
@@ -975,7 +974,7 @@ RequestGroupMan::findDownloadResult(a2_gid_t gid) const
 {
   for(std::deque<SharedHandle<DownloadResult> >::const_iterator i =
         downloadResults_.begin(), eoi = downloadResults_.end(); i != eoi; ++i) {
-    if((*i)->gid == gid) {
+    if((*i)->gid->getNumericId() == gid) {
       return *i;
     }
   }
@@ -986,7 +985,7 @@ bool RequestGroupMan::removeDownloadResult(a2_gid_t gid)
 {
   for(std::deque<SharedHandle<DownloadResult> >::iterator i =
         downloadResults_.begin(), eoi = downloadResults_.end(); i != eoi; ++i) {
-    if((*i)->gid == gid) {
+    if((*i)->gid->getNumericId() == gid) {
       downloadResults_.erase(i);
       return true;
     }

+ 47 - 29
src/RpcMethodImpl.cc

@@ -148,7 +148,7 @@ const std::string KEY_NUM_ACTIVE = "numActive";
 namespace {
 SharedHandle<ValueBase> createGIDResponse(a2_gid_t gid)
 {
-  return String::g(util::itos(gid));
+  return String::g(GroupId::toHex(gid));
 }
 } // namespace
 
@@ -185,12 +185,19 @@ namespace {
 a2_gid_t str2Gid(const String* str)
 {
   assert(str);
-  int64_t gid;
-  if(util::parseLLIntNoThrow(gid, str->s())) {
-    return gid;
-  } else {
-    throw DL_ABORT_EX(fmt("Bad GID %s", str->s().c_str()));
-  }
+  if(str->s().size() > sizeof(a2_gid_t)*2) {
+    throw DL_ABORT_EX(fmt("Invalid GID %s", str->s().c_str()));
+  }
+  a2_gid_t n;
+  switch(GroupId::expandUnique(n, str->s().c_str())) {
+  case GroupId::ERR_NOT_UNIQUE:
+    throw DL_ABORT_EX(fmt("GID %s is not unique", str->s().c_str()));
+  case GroupId::ERR_NOT_FOUND:
+    throw DL_ABORT_EX(fmt("GID %s is not found", str->s().c_str()));
+  case GroupId::ERR_INVALID:
+    throw DL_ABORT_EX(fmt("Invalid GID %s", str->s().c_str()));
+  }
+  return n;
 }
 } // namespace
 
@@ -364,7 +371,7 @@ SharedHandle<ValueBase> AddMetalinkRpcMethod::process
     }
     for(std::vector<SharedHandle<RequestGroup> >::const_iterator i =
           result.begin(), eoi = result.end(); i != eoi; ++i) {
-      gids->append(util::itos((*i)->getGID()));
+      gids->append(GroupId::toHex((*i)->getGID()));
     }
   }
   return gids;
@@ -391,12 +398,13 @@ SharedHandle<ValueBase> removeDownload
       if(group->isDependencyResolved()) {
         e->getRequestGroupMan()->removeReservedGroup(gid);
       } else {
-        throw DL_ABORT_EX(fmt("GID#%" PRId64 " cannot be removed now", gid));
+        throw DL_ABORT_EX(fmt("GID#%s cannot be removed now",
+                              GroupId::toHex(gid).c_str()));
       }
     }
   } else {
-    throw DL_ABORT_EX(fmt("Active Download not found for GID#%" PRId64,
-                          gid));
+    throw DL_ABORT_EX(fmt("Active Download not found for GID#%s",
+                          GroupId::toHex(gid).c_str()));
   }
   return createGIDResponse(gid);
 }
@@ -455,7 +463,8 @@ SharedHandle<ValueBase> pauseDownload
       return createGIDResponse(gid);
     }
   }
-  throw DL_ABORT_EX(fmt("GID#%" PRId64 " cannot be paused now", gid));
+  throw DL_ABORT_EX(fmt("GID#%s cannot be paused now",
+                        GroupId::toHex(gid).c_str()));
 }
 } // namespace
 
@@ -519,7 +528,8 @@ SharedHandle<ValueBase> UnpauseRpcMethod::process
   if(!group ||
      group->getState() != RequestGroup::STATE_WAITING ||
      !group->isPauseRequested()) {
-    throw DL_ABORT_EX(fmt("GID#%" PRId64 " cannot be unpaused now", gid));
+    throw DL_ABORT_EX(fmt("GID#%s cannot be unpaused now",
+                          GroupId::toHex(gid).c_str()));
   } else {
     group->setPauseRequested(false);
     e->getRequestGroupMan()->requestQueueCheck();
@@ -644,7 +654,7 @@ void gatherProgressCommon
 {
   const SharedHandle<PieceStorage>& ps = group->getPieceStorage();
   if(requested_key(keys, KEY_GID)) {
-    entryDict->put(KEY_GID, util::itos(group->getGID()));
+    entryDict->put(KEY_GID, GroupId::toHex(group->getGID()).c_str());
   }
   if(requested_key(keys, KEY_TOTAL_LENGTH)) {
     // This is "filtered" total length if --select-file is used.
@@ -690,14 +700,14 @@ void gatherProgressCommon
       // The element is GID.
       for(std::vector<a2_gid_t>::const_iterator i = group->followedBy().begin(),
             eoi = group->followedBy().end(); i != eoi; ++i) {
-        list->append(util::itos(*i));
+        list->append(GroupId::toHex(*i));
       }
       entryDict->put(KEY_FOLLOWED_BY, list);
     }
   }
   if(requested_key(keys, KEY_BELONGS_TO)) {
     if(group->belongsTo()) {
-      entryDict->put(KEY_BELONGS_TO, util::itos(group->belongsTo()));
+      entryDict->put(KEY_BELONGS_TO, GroupId::toHex(group->belongsTo()));
     }
   }
   if(requested_key(keys, KEY_FILES)) {
@@ -832,7 +842,7 @@ void gatherStoppedDownload
  const std::vector<std::string>& keys)
 {
   if(requested_key(keys, KEY_GID)) {
-    entryDict->put(KEY_GID, util::itos(ds->gid));
+    entryDict->put(KEY_GID, ds->gid->toHex());
   }
   if(requested_key(keys, KEY_ERROR_CODE)) {
     entryDict->put(KEY_ERROR_CODE, util::itos(static_cast<int>(ds->result)));
@@ -852,14 +862,14 @@ void gatherStoppedDownload
       // The element is GID.
       for(std::vector<a2_gid_t>::const_iterator i = ds->followedBy.begin(),
             eoi = ds->followedBy.end(); i != eoi; ++i) {
-        list->append(util::itos(*i));
+        list->append(GroupId::toHex(*i));
       }
       entryDict->put(KEY_FOLLOWED_BY, list);
     }
   }
   if(requested_key(keys, KEY_BELONGS_TO)) {
     if(ds->belongsTo) {
-      entryDict->put(KEY_BELONGS_TO, util::itos(ds->belongsTo));
+      entryDict->put(KEY_BELONGS_TO, GroupId::toHex(ds->belongsTo));
     }
   }
   if(requested_key(keys, KEY_FILES)) {
@@ -922,8 +932,8 @@ SharedHandle<ValueBase> GetFilesRpcMethod::process
     SharedHandle<DownloadResult> dr =
       e->getRequestGroupMan()->findDownloadResult(gid);
     if(!dr) {
-      throw DL_ABORT_EX(fmt("No file data is available for GID#%" PRId64 "",
-                            gid));
+      throw DL_ABORT_EX(fmt("No file data is available for GID#%s",
+                            GroupId::toHex(gid).c_str()));
     } else {
       createFileEntry(files, dr->fileEntries.begin(), dr->fileEntries.end(),
                       dr->totalLength, dr->pieceLength, dr->bitfield);
@@ -949,7 +959,8 @@ SharedHandle<ValueBase> GetUrisRpcMethod::process
   a2_gid_t gid = str2Gid(gidParam);
   SharedHandle<RequestGroup> group = e->getRequestGroupMan()->findGroup(gid);
   if(!group) {
-    throw DL_ABORT_EX(fmt("No URI data is available for GID#%" PRId64, gid));
+    throw DL_ABORT_EX(fmt("No URI data is available for GID#%s",
+                          GroupId::toHex(gid).c_str()));
   }
   SharedHandle<List> uriList = List::g();
   // TODO Current implementation just returns first FileEntry's URIs.
@@ -968,7 +979,8 @@ SharedHandle<ValueBase> GetPeersRpcMethod::process
   a2_gid_t gid = str2Gid(gidParam);
   SharedHandle<RequestGroup> group = e->getRequestGroupMan()->findGroup(gid);
   if(!group) {
-    throw DL_ABORT_EX(fmt("No peer data is available for GID#%" PRId64, gid));
+    throw DL_ABORT_EX(fmt("No peer data is available for GID#%s",
+                          GroupId::toHex(gid).c_str()));
   }
   SharedHandle<List> peers = List::g();
   const SharedHandle<BtObject>& btObject =
@@ -998,7 +1010,8 @@ SharedHandle<ValueBase> TellStatusRpcMethod::process
     SharedHandle<DownloadResult> ds =
       e->getRequestGroupMan()->findDownloadResult(gid);
     if(!ds) {
-      throw DL_ABORT_EX(fmt("No such download for GID#%" PRId64 "", gid));
+      throw DL_ABORT_EX(fmt("No such download for GID#%s",
+                            GroupId::toHex(gid).c_str()));
     }
     gatherStoppedDownload(entryDict, ds, keys);
   } else {
@@ -1090,7 +1103,8 @@ SharedHandle<ValueBase> RemoveDownloadResultRpcMethod::process
 
   a2_gid_t gid = str2Gid(gidParam);
   if(!e->getRequestGroupMan()->removeDownloadResult(gid)) {
-    throw DL_ABORT_EX(fmt("Could not remove download result of GID#%" PRId64 "", gid));
+    throw DL_ABORT_EX(fmt("Could not remove download result of GID#%s",
+                          GroupId::toHex(gid).c_str()));
   }
   return VLB_OK;
 }
@@ -1191,7 +1205,8 @@ SharedHandle<ValueBase> ChangeOptionRpcMethod::process
     }
     changeOption(group, option, e);
   } else {
-    throw DL_ABORT_EX(fmt("Cannot change option for GID#%" PRId64, gid));
+    throw DL_ABORT_EX(fmt("Cannot change option for GID#%s",
+                          GroupId::toHex(gid).c_str()));
   }
   return VLB_OK;
 }
@@ -1277,7 +1292,8 @@ SharedHandle<ValueBase> GetOptionRpcMethod::process
   a2_gid_t gid = str2Gid(gidParam);
   SharedHandle<RequestGroup> group = e->getRequestGroupMan()->findGroup(gid);
   if(!group) {
-    throw DL_ABORT_EX(fmt("Cannot get option for GID#%" PRId64 "", gid));
+    throw DL_ABORT_EX(fmt("Cannot get option for GID#%s",
+                          GroupId::toHex(gid).c_str()));
   }
   SharedHandle<Dict> result = Dict::g();
   SharedHandle<Option> option = group->getOption();
@@ -1344,7 +1360,8 @@ SharedHandle<ValueBase> GetServersRpcMethod::process
   a2_gid_t gid = str2Gid(gidParam);
   SharedHandle<RequestGroup> group = e->getRequestGroupMan()->findGroup(gid);
   if(!group || group->getState() != RequestGroup::STATE_ACTIVE) {
-    throw DL_ABORT_EX(fmt("No active download for GID#%" PRId64, gid));
+    throw DL_ABORT_EX(fmt("No active download for GID#%s",
+                          GroupId::toHex(gid).c_str()));
   }
   const SharedHandle<DownloadContext>& dctx = group->getDownloadContext();
   const std::vector<SharedHandle<FileEntry> >& files = dctx->getFileEntries();
@@ -1390,7 +1407,8 @@ SharedHandle<ValueBase> ChangeUriRpcMethod::process
   size_t index = indexParam->i()-1;
   SharedHandle<RequestGroup> group = e->getRequestGroupMan()->findGroup(gid);
   if(!group) {
-    throw DL_ABORT_EX(fmt("Cannot remove URIs from GID#%" PRId64 "", gid));
+    throw DL_ABORT_EX(fmt("Cannot remove URIs from GID#%s",
+                          GroupId::toHex(gid).c_str()));
   }
   const SharedHandle<DownloadContext>& dctx = group->getDownloadContext();
   const std::vector<SharedHandle<FileEntry> >& files = dctx->getFileEntries();

+ 3 - 2
src/TrackerWatcherCommand.cc

@@ -221,7 +221,7 @@ TrackerWatcherCommand::createRequestGroup(const std::string& uri)
   std::vector<std::string> uris;
   uris.push_back(uri);
   SharedHandle<Option> option = util::copy(getOption());
-  SharedHandle<RequestGroup> rg(new RequestGroup(option));
+  SharedHandle<RequestGroup> rg(new RequestGroup(GroupId::create(), option));
   if(backupTrackerIsAvailable(requestGroup_->getDownloadContext())) {
     A2_LOG_DEBUG("This is multi-tracker announce.");
   } else {
@@ -255,7 +255,8 @@ TrackerWatcherCommand::createRequestGroup(const std::string& uri)
   rg->clearPreDownloadHandler();
   rg->clearPostDownloadHandler();
   dctx->setAcceptMetalink(false);
-  A2_LOG_INFO(fmt("Creating tracker request group GID#%" PRId64 "", rg->getGID()));
+  A2_LOG_INFO(fmt("Creating tracker request group GID#%s",
+                  GroupId::toHex(rg->getGID()).c_str()));
   return rg;
 }
 

+ 1 - 1
src/WebSocketSessionMan.cc

@@ -66,7 +66,7 @@ void WebSocketSessionMan::addNotification
   dict->put("jsonrpc", "2.0");
   dict->put("method", method);
   SharedHandle<Dict> eventSpec = Dict::g();
-  eventSpec->put("gid", util::itos(group->getGID()));
+  eventSpec->put("gid", GroupId::toHex((group->getGID())));
   SharedHandle<List> params = List::g();
   params->append(eventSpec);
   dict->put("params", params);

+ 3 - 3
src/download_helper.cc

@@ -112,7 +112,7 @@ SharedHandle<RequestGroup> createRequestGroup
  bool useOutOption = false)
 {
   SharedHandle<Option> option = util::copy(optionTemplate);
-  SharedHandle<RequestGroup> rg(new RequestGroup(option));
+  SharedHandle<RequestGroup> rg(new RequestGroup(GroupId::create(), option));
   SharedHandle<DownloadContext> dctx
     (new DownloadContext
      (option->getAsInt(PREF_PIECE_LENGTH),
@@ -168,7 +168,7 @@ createBtRequestGroup(const std::string& metaInfoUri,
                      bool adjustAnnounceUri = true)
 {
   SharedHandle<Option> option = util::copy(optionTemplate);
-  SharedHandle<RequestGroup> rg(new RequestGroup(option));
+  SharedHandle<RequestGroup> rg(new RequestGroup(GroupId::create(), option));
   SharedHandle<DownloadContext> dctx(new DownloadContext());
   // may throw exception
   bittorrent::loadFromMemory(torrent, dctx, option, auxUris,
@@ -210,7 +210,7 @@ createBtMagnetRequestGroup
  const SharedHandle<Option>& optionTemplate)
 {
   SharedHandle<Option> option = util::copy(optionTemplate);
-  SharedHandle<RequestGroup> rg(new RequestGroup(option));
+  SharedHandle<RequestGroup> rg(new RequestGroup(GroupId::create(), option));
   SharedHandle<DownloadContext> dctx
     (new DownloadContext(METADATA_PIECE_SIZE, 0,
                          A2STR::NIL));

+ 2 - 2
src/message.h

@@ -54,7 +54,7 @@
 #define MSG_GOT_NEW_PIECE "CUID#%" PRId64 " - we got new piece. index=%lu"
 #define MSG_GOT_WRONG_PIECE "CUID#%" PRId64 " - we got wrong piece. index=%lu"
 #define MSG_DOWNLOAD_NOT_COMPLETE "CUID#%" PRId64 " - Download not complete: %s"
-#define MSG_DOWNLOAD_ALREADY_COMPLETED _("GID#%" PRId64 " - Download has already completed: %s")
+#define MSG_DOWNLOAD_ALREADY_COMPLETED _("GID#%s - Download has already completed: %s")
 #define MSG_RESOLVING_HOSTNAME "CUID#%" PRId64 " - Resolving hostname %s"
 #define MSG_NAME_RESOLUTION_COMPLETE                    \
   "CUID#%" PRId64 " - Name resolution complete: %s -> %s"
@@ -197,7 +197,7 @@
 #define MSG_DIR_TRAVERSAL_DETECTED _("Detected directory traversal directive in %s")
 #define MSG_HASH_CHECK_NOT_DONE                                         \
   "File has already been downloaded but hash check has not been done yet."
-#define MSG_REMOVING_UNSELECTED_FILE _("GID#%" PRId64 " - Removing unselected file.")
+#define MSG_REMOVING_UNSELECTED_FILE _("GID#%s - Removing unselected file.")
 #define MSG_FILE_REMOVED _("File %s removed.")
 #define MSG_FILE_COULD_NOT_REMOVED _("File %s could not be removed.")
 

+ 1 - 1
src/util.cc

@@ -1691,7 +1691,7 @@ void executeHook
  size_t numFiles,
  const std::string& firstFilename)
 {
-  const std::string gidStr = util::itos(gid);
+  const std::string gidStr = GroupId::toHex(gid);
   const std::string numFilesStr = util::uitos(numFiles);
 #ifndef __MINGW32__
   A2_LOG_INFO(fmt("Executing user command: %s %s %s %s",

+ 4 - 2
test/BtDependencyTest.cc

@@ -38,7 +38,8 @@ class BtDependencyTest:public CppUnit::TestFixture {
 
   SharedHandle<RequestGroup> createDependant(const SharedHandle<Option>& option)
   {
-    SharedHandle<RequestGroup> dependant(new RequestGroup(util::copy(option)));
+    SharedHandle<RequestGroup> dependant(new RequestGroup(GroupId::create(),
+                                                          util::copy(option)));
     SharedHandle<DownloadContext> dctx
       (new DownloadContext(0, 0, "/tmp/outfile.path"));
     std::vector<std::string> uris;
@@ -56,7 +57,8 @@ class BtDependencyTest:public CppUnit::TestFixture {
    const std::string& torrentFile,
    int64_t length)
   {
-    SharedHandle<RequestGroup> dependee(new RequestGroup(util::copy(option)));
+    SharedHandle<RequestGroup> dependee(new RequestGroup(GroupId::create(),
+                                                         util::copy(option)));
     SharedHandle<DownloadContext> dctx
       (new DownloadContext(1024*1024, length, torrentFile));
     dependee->setDownloadContext(dctx);

+ 3 - 3
test/BtPostDownloadHandlerTest.cc

@@ -39,7 +39,7 @@ void BtPostDownloadHandlerTest::testCanHandle_extension()
 {
   SharedHandle<DownloadContext> dctx
     (new DownloadContext(0, 0, A2_TEST_DIR"/test.torrent"));
-  RequestGroup rg(option_);
+  RequestGroup rg(GroupId::create(), option_);
   rg.setDownloadContext(dctx);
 
   BtPostDownloadHandler handler;
@@ -54,7 +54,7 @@ void BtPostDownloadHandlerTest::testCanHandle_contentType()
 {
   SharedHandle<DownloadContext> dctx(new DownloadContext(0, 0, "test"));
   dctx->getFirstFileEntry()->setContentType("application/x-bittorrent");
-  RequestGroup rg(option_);
+  RequestGroup rg(GroupId::create(), option_);
   rg.setDownloadContext(dctx);
 
   BtPostDownloadHandler handler;
@@ -69,7 +69,7 @@ void BtPostDownloadHandlerTest::testGetNextRequestGroups()
 {
   SharedHandle<DownloadContext> dctx
     (new DownloadContext(1024, 0, A2_TEST_DIR"/test.torrent"));
-  RequestGroup rg(option_);
+  RequestGroup rg(GroupId::create(), option_);
   rg.setDownloadContext(dctx);
   rg.initPieceStorage();
   rg.getPieceStorage()->getDiskAdaptor()->enableReadOnly();

+ 1 - 1
test/DefaultBtMessageDispatcherTest.cc

@@ -138,7 +138,7 @@ public:
     option_.reset(new Option());
     option_->put(PREF_DIR, ".");
 
-    rg_.reset(new RequestGroup(option_));
+    rg_.reset(new RequestGroup(GroupId::create(), option_));
 
     dctx_.reset(new DownloadContext());
     bittorrent::load(A2_TEST_DIR"/test.torrent", dctx_, option_);

+ 1 - 1
test/DefaultExtensionMessageFactoryTest.cc

@@ -64,7 +64,7 @@ public:
     dctx_.reset(new DownloadContext());
 
     SharedHandle<Option> option(new Option());
-    requestGroup_.reset(new RequestGroup(option));
+    requestGroup_.reset(new RequestGroup(GroupId::create(), option));
     requestGroup_->setDownloadContext(dctx_);
 
     factory_.reset(new DefaultExtensionMessageFactory());

+ 4 - 4
test/DownloadHandlerFactoryTest.cc

@@ -51,7 +51,7 @@ CPPUNIT_TEST_SUITE_REGISTRATION( DownloadHandlerFactoryTest );
 void DownloadHandlerFactoryTest::testGetMetalinkPreDownloadHandler_extension()
 {
   SharedHandle<DownloadContext> dctx(new DownloadContext(0, 0, "test.metalink"));
-  RequestGroup rg(option_);
+  RequestGroup rg(GroupId::create(), option_);
   rg.setDownloadContext(dctx);
 
   SharedHandle<PreDownloadHandler> handler = DownloadHandlerFactory::getMetalinkPreDownloadHandler();
@@ -66,7 +66,7 @@ void DownloadHandlerFactoryTest::testGetMetalinkPreDownloadHandler_contentType()
 {
   SharedHandle<DownloadContext> dctx(new DownloadContext(0, 0, "test"));
   dctx->getFirstFileEntry()->setContentType("application/metalink+xml");
-  RequestGroup rg(option_);
+  RequestGroup rg(GroupId::create(), option_);
   rg.setDownloadContext(dctx);
 
   SharedHandle<PreDownloadHandler> handler = DownloadHandlerFactory::getMetalinkPreDownloadHandler();
@@ -85,7 +85,7 @@ void DownloadHandlerFactoryTest::testGetBtPreDownloadHandler_extension()
 {
   SharedHandle<DownloadContext> dctx
     (new DownloadContext(0, 0, A2_TEST_DIR"/test.torrent"));
-  RequestGroup rg(option_);
+  RequestGroup rg(GroupId::create(), option_);
   rg.setDownloadContext(dctx);
 
   SharedHandle<PreDownloadHandler> handler = DownloadHandlerFactory::getBtPreDownloadHandler();
@@ -100,7 +100,7 @@ void DownloadHandlerFactoryTest::testGetBtPreDownloadHandler_contentType()
 {
   SharedHandle<DownloadContext> dctx(new DownloadContext(0, 0, "test"));
   dctx->getFirstFileEntry()->setContentType("application/x-bittorrent");
-  RequestGroup rg(option_);
+  RequestGroup rg(GroupId::create(), option_);
   rg.setDownloadContext(dctx);
 
   SharedHandle<PreDownloadHandler> handler = DownloadHandlerFactory::getBtPreDownloadHandler();

+ 127 - 0
test/GroupIdTest.cc

@@ -0,0 +1,127 @@
+#include "GroupId.h"
+
+#include <cppunit/extensions/HelperMacros.h>
+
+#include "TestUtil.h"
+#include "array_fun.h"
+
+namespace aria2 {
+
+class GroupIdTest:public CppUnit::TestFixture {
+
+  CPPUNIT_TEST_SUITE(GroupIdTest);
+  CPPUNIT_TEST(testCreate);
+  CPPUNIT_TEST(testToNumericId);
+  CPPUNIT_TEST(testExpandUnique);
+  CPPUNIT_TEST(testToHex);
+  CPPUNIT_TEST(testToAbbrevHex);
+  CPPUNIT_TEST_SUITE_END();
+public:
+  void setUp()
+  {
+    GroupId::clear();
+  }
+
+  void testCreate();
+  void testToNumericId();
+  void testExpandUnique();
+  void testToHex();
+  void testToAbbrevHex();
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION( GroupIdTest );
+
+void GroupIdTest::testCreate()
+{
+  SharedHandle<GroupId> gid = GroupId::create();
+  CPPUNIT_ASSERT(gid);
+  CPPUNIT_ASSERT(0 != gid->getNumericId());
+  CPPUNIT_ASSERT(!GroupId::import(gid->getNumericId()));
+  CPPUNIT_ASSERT(!GroupId::import(0));
+}
+
+void GroupIdTest::testToNumericId()
+{
+  a2_gid_t gid;
+  std::string hex;
+  hex = std::string(16, '0');
+  CPPUNIT_ASSERT_EQUAL((int)GroupId::ERR_INVALID,
+                       GroupId::toNumericId(gid, hex.c_str()));
+
+  hex = std::string(16, 'f');
+  CPPUNIT_ASSERT_EQUAL(0, GroupId::toNumericId(gid, hex.c_str()));
+  CPPUNIT_ASSERT_EQUAL((a2_gid_t)UINT64_MAX, gid);
+
+  CPPUNIT_ASSERT_EQUAL(0, GroupId::toNumericId(gid, "1234567890abcdef"));
+  CPPUNIT_ASSERT_EQUAL((a2_gid_t)1311768467294899695LL, gid);
+
+  hex = std::string(15, 'f');
+  CPPUNIT_ASSERT_EQUAL((int)GroupId::ERR_INVALID,
+                       GroupId::toNumericId(gid, hex.c_str()));
+
+  CPPUNIT_ASSERT_EQUAL((int)GroupId::ERR_INVALID,
+                       GroupId::toNumericId(gid, ""));
+
+  CPPUNIT_ASSERT_EQUAL((int)GroupId::ERR_INVALID,
+                       GroupId::toNumericId(gid, "1234567890abcdeg"));
+}
+
+void GroupIdTest::testExpandUnique()
+{
+  a2_gid_t gid;
+  SharedHandle<GroupId> ids[] = {
+    GroupId::import(0xff80000000010000LL),
+    GroupId::import(0xff80000000020001LL),
+    GroupId::import(0xfff8000000030000LL)
+  };
+  for(SharedHandle<GroupId>* i = vbegin(ids), *eoi = vend(ids); i != eoi;
+      ++i) {
+    CPPUNIT_ASSERT(*i);
+  }
+
+  CPPUNIT_ASSERT_EQUAL((int)GroupId::ERR_NOT_UNIQUE,
+                       GroupId::expandUnique(gid, "ff8"));
+
+  CPPUNIT_ASSERT_EQUAL((int)GroupId::ERR_INVALID,
+                       GroupId::expandUnique(gid, "ffg"));
+
+  CPPUNIT_ASSERT_EQUAL((int)GroupId::ERR_INVALID,
+                       GroupId::expandUnique(gid,
+                                             std::string(17, 'a').c_str()));
+
+  CPPUNIT_ASSERT_EQUAL((int)GroupId::ERR_INVALID,
+                       GroupId::expandUnique(gid, ""));
+
+  CPPUNIT_ASSERT_EQUAL((int)GroupId::ERR_NOT_UNIQUE,
+                       GroupId::expandUnique(gid, "ff800000000"));
+
+  CPPUNIT_ASSERT_EQUAL(0, GroupId::expandUnique(gid, "ff8000000001"));
+  CPPUNIT_ASSERT_EQUAL(std::string("ff80000000010000"), GroupId::toHex(gid));
+
+  CPPUNIT_ASSERT_EQUAL(0, GroupId::expandUnique(gid, "ff8000000002"));
+  CPPUNIT_ASSERT_EQUAL(std::string("ff80000000020001"), GroupId::toHex(gid));
+
+  CPPUNIT_ASSERT_EQUAL(0, GroupId::expandUnique(gid, "fff800000003"));
+  CPPUNIT_ASSERT_EQUAL(std::string("fff8000000030000"), GroupId::toHex(gid));
+
+  CPPUNIT_ASSERT_EQUAL((int)GroupId::ERR_NOT_FOUND,
+                       GroupId::expandUnique(gid, "ff80000000031"));
+}
+
+void GroupIdTest::testToHex()
+{
+  CPPUNIT_ASSERT_EQUAL(std::string("1234567890abcdef"),
+                       GroupId::toHex(1311768467294899695LL));
+  CPPUNIT_ASSERT_EQUAL(std::string("0000000000000001"),
+                       GroupId::toHex(0000000000000000001LL));
+}
+
+void GroupIdTest::testToAbbrevHex()
+{
+  CPPUNIT_ASSERT_EQUAL(std::string("123456"),
+                       GroupId::toAbbrevHex(1311768467294899695LL));
+  CPPUNIT_ASSERT_EQUAL(std::string("000000"),
+                       GroupId::toAbbrevHex(0000000000000000001LL));
+}
+
+} // namespace aria2

+ 1 - 1
test/HandshakeExtensionMessageTest.cc

@@ -94,7 +94,7 @@ void HandshakeExtensionMessageTest::testDoReceivedAction()
   SharedHandle<DownloadContext> dctx
     (new DownloadContext(METADATA_PIECE_SIZE, 0));
   SharedHandle<Option> op(new Option());
-  RequestGroup rg(op);
+  RequestGroup rg(GroupId::create(), op);
   rg.setDownloadContext(dctx);
 
   SharedHandle<TorrentAttribute> attrs(new TorrentAttribute());

+ 2 - 1
test/Makefile.am

@@ -87,7 +87,8 @@ aria2c_SOURCES = AllTest.cc\
 	AbstractCommandTest.cc\
 	SinkStreamFilterTest.cc\
 	WrDiskCacheTest.cc\
-	WrDiskCacheEntryTest.cc
+	WrDiskCacheEntryTest.cc\
+	GroupIdTest.cc
 
 if ENABLE_XML_RPC
 aria2c_SOURCES += XmlRpcRequestParserControllerTest.cc

+ 4 - 4
test/MetalinkPostDownloadHandlerTest.cc

@@ -40,7 +40,7 @@ void MetalinkPostDownloadHandlerTest::testCanHandle_extension()
 {
   SharedHandle<DownloadContext> dctx
     (new DownloadContext(0, 0, "test.metalink"));
-  RequestGroup rg(option_);
+  RequestGroup rg(GroupId::create(), option_);
   rg.setDownloadContext(dctx);
 
   MetalinkPostDownloadHandler handler;
@@ -55,7 +55,7 @@ void MetalinkPostDownloadHandlerTest::testCanHandle_contentType()
 {
   SharedHandle<DownloadContext> dctx(new DownloadContext(0, 0, "test"));
   dctx->getFirstFileEntry()->setContentType("application/metalink+xml");
-  RequestGroup rg(option_);
+  RequestGroup rg(GroupId::create(), option_);
   rg.setDownloadContext(dctx);
 
   MetalinkPostDownloadHandler handler;
@@ -70,7 +70,7 @@ void MetalinkPostDownloadHandlerTest::testGetNextRequestGroups()
 {
   SharedHandle<DownloadContext> dctx
     (new DownloadContext(1024, 0, A2_TEST_DIR"/test.xml"));
-  RequestGroup rg(option_);
+  RequestGroup rg(GroupId::create(), option_);
   rg.setDownloadContext(dctx);
   rg.initPieceStorage();
   rg.getPieceStorage()->getDiskAdaptor()->enableReadOnly();
@@ -90,7 +90,7 @@ void MetalinkPostDownloadHandlerTest::testGetNextRequestGroups_withBaseUri()
   SharedHandle<DownloadContext> dctx
     (new DownloadContext(1024, 0, A2_TEST_DIR"/base_uri.xml"));
   dctx->getFirstFileEntry()->addUri("http://base/dir/base_uri.xml");
-  RequestGroup rg(option_);
+  RequestGroup rg(GroupId::create(), option_);
   rg.setDownloadContext(dctx);
   rg.initPieceStorage();
   rg.getPieceStorage()->getDiskAdaptor()->enableReadOnly();

+ 44 - 23
test/RequestGroupManTest.cc

@@ -33,7 +33,6 @@ private:
 public:
   void setUp()
   {
-    RequestGroup::resetGIDCounter();
     option_.reset(new Option());
   }
 
@@ -49,8 +48,10 @@ CPPUNIT_TEST_SUITE_REGISTRATION( RequestGroupManTest );
 
 void RequestGroupManTest::testIsSameFileBeingDownloaded()
 {
-  SharedHandle<RequestGroup> rg1(new RequestGroup(util::copy(option_)));
-  SharedHandle<RequestGroup> rg2(new RequestGroup(util::copy(option_)));
+  SharedHandle<RequestGroup> rg1(new RequestGroup(GroupId::create(),
+                                                  util::copy(option_)));
+  SharedHandle<RequestGroup> rg2(new RequestGroup(GroupId::create(),
+                                                  util::copy(option_)));
 
   SharedHandle<DownloadContext> dctx1
     (new DownloadContext(0, 0, "aria2.tar.bz2"));
@@ -118,51 +119,71 @@ void RequestGroupManTest::testLoadServerStat()
 void RequestGroupManTest::testChangeReservedGroupPosition()
 {
   SharedHandle<RequestGroup> gs[] = {
-    SharedHandle<RequestGroup>(new RequestGroup(util::copy(option_))),
-    SharedHandle<RequestGroup>(new RequestGroup(util::copy(option_))),
-    SharedHandle<RequestGroup>(new RequestGroup(util::copy(option_))),
-    SharedHandle<RequestGroup>(new RequestGroup(util::copy(option_)))
+    SharedHandle<RequestGroup>(new RequestGroup(GroupId::create(),
+                                                util::copy(option_))),
+    SharedHandle<RequestGroup>(new RequestGroup(GroupId::create(),
+                                                util::copy(option_))),
+    SharedHandle<RequestGroup>(new RequestGroup(GroupId::create(),
+                                                util::copy(option_))),
+    SharedHandle<RequestGroup>(new RequestGroup(GroupId::create(),
+                                                util::copy(option_)))
   };
   std::vector<SharedHandle<RequestGroup> > groups(vbegin(gs), vend(gs));
   RequestGroupMan rm(groups, 0, option_.get());
 
   CPPUNIT_ASSERT_EQUAL
-    ((size_t)0, rm.changeReservedGroupPosition(1, 0, RequestGroupMan::POS_SET));
+    ((size_t)0, rm.changeReservedGroupPosition(gs[0]->getGID(),
+                                               0, RequestGroupMan::POS_SET));
   CPPUNIT_ASSERT_EQUAL
-    ((size_t)1, rm.changeReservedGroupPosition(1, 1, RequestGroupMan::POS_SET));
+    ((size_t)1, rm.changeReservedGroupPosition(gs[0]->getGID(),
+                                               1, RequestGroupMan::POS_SET));
   CPPUNIT_ASSERT_EQUAL
-    ((size_t)3, rm.changeReservedGroupPosition(1, 10,RequestGroupMan::POS_SET));
+    ((size_t)3, rm.changeReservedGroupPosition(gs[0]->getGID(),
+                                               10,RequestGroupMan::POS_SET));
   CPPUNIT_ASSERT_EQUAL
-    ((size_t)0, rm.changeReservedGroupPosition(1,-10,RequestGroupMan::POS_SET));
+    ((size_t)0, rm.changeReservedGroupPosition(gs[0]->getGID(),
+                                               -10, RequestGroupMan::POS_SET));
 
   CPPUNIT_ASSERT_EQUAL
-    ((size_t)1, rm.changeReservedGroupPosition(2, 0, RequestGroupMan::POS_CUR));
+    ((size_t)1, rm.changeReservedGroupPosition(gs[1]->getGID(),
+                                               0, RequestGroupMan::POS_CUR));
   CPPUNIT_ASSERT_EQUAL
-    ((size_t)2, rm.changeReservedGroupPosition(2, 1, RequestGroupMan::POS_CUR));
+    ((size_t)2, rm.changeReservedGroupPosition(gs[1]->getGID(),
+                                               1, RequestGroupMan::POS_CUR));
   CPPUNIT_ASSERT_EQUAL
-    ((size_t)1, rm.changeReservedGroupPosition(2, -1,RequestGroupMan::POS_CUR));
+    ((size_t)1, rm.changeReservedGroupPosition(gs[1]->getGID(),
+                                               -1,RequestGroupMan::POS_CUR));
   CPPUNIT_ASSERT_EQUAL
-    ((size_t)0, rm.changeReservedGroupPosition(2,-10,RequestGroupMan::POS_CUR));
+    ((size_t)0, rm.changeReservedGroupPosition(gs[1]->getGID(),
+                                               -10, RequestGroupMan::POS_CUR));
   CPPUNIT_ASSERT_EQUAL
-    ((size_t)1, rm.changeReservedGroupPosition(2, 1, RequestGroupMan::POS_CUR));
+    ((size_t)1, rm.changeReservedGroupPosition(gs[1]->getGID(),
+                                               1, RequestGroupMan::POS_CUR));
   CPPUNIT_ASSERT_EQUAL
-    ((size_t)3, rm.changeReservedGroupPosition(2, 10,RequestGroupMan::POS_CUR));
+    ((size_t)3, rm.changeReservedGroupPosition(gs[1]->getGID(),
+                                               10, RequestGroupMan::POS_CUR));
   CPPUNIT_ASSERT_EQUAL
-    ((size_t)1, rm.changeReservedGroupPosition(2, -2,RequestGroupMan::POS_CUR));
+    ((size_t)1, rm.changeReservedGroupPosition(gs[1]->getGID(),
+                                               -2,RequestGroupMan::POS_CUR));
 
   CPPUNIT_ASSERT_EQUAL
-    ((size_t)3, rm.changeReservedGroupPosition(4, 0, RequestGroupMan::POS_END));
+    ((size_t)3, rm.changeReservedGroupPosition(gs[3]->getGID(),
+                                               0, RequestGroupMan::POS_END));
   CPPUNIT_ASSERT_EQUAL
-    ((size_t)2, rm.changeReservedGroupPosition(4, -1,RequestGroupMan::POS_END));
+    ((size_t)2, rm.changeReservedGroupPosition(gs[3]->getGID(),
+                                               -1,RequestGroupMan::POS_END));
   CPPUNIT_ASSERT_EQUAL
-    ((size_t)0, rm.changeReservedGroupPosition(4,-10,RequestGroupMan::POS_END));
+    ((size_t)0, rm.changeReservedGroupPosition(gs[3]->getGID(),
+                                               -10, RequestGroupMan::POS_END));
   CPPUNIT_ASSERT_EQUAL
-    ((size_t)3, rm.changeReservedGroupPosition(4, 10,RequestGroupMan::POS_END));
+    ((size_t)3, rm.changeReservedGroupPosition(gs[3]->getGID(),
+                                               10, RequestGroupMan::POS_END));
 
   CPPUNIT_ASSERT_EQUAL((size_t)4, rm.getReservedGroups().size());
 
   try {
-    rm.changeReservedGroupPosition(5, 0, RequestGroupMan::POS_CUR);
+    rm.changeReservedGroupPosition(GroupId::create()->getNumericId(),
+                                   0, RequestGroupMan::POS_CUR);
     CPPUNIT_FAIL("exception must be thrown.");
   } catch(RecoverableException& e) {
     // success

+ 2 - 2
test/RequestGroupTest.cc

@@ -36,7 +36,7 @@ void RequestGroupTest::testGetFirstFilePath()
   SharedHandle<DownloadContext> ctx
     (new DownloadContext(1024, 1024, "/tmp/myfile"));
 
-  RequestGroup group(option_);
+  RequestGroup group(GroupId::create(), option_);
   group.setDownloadContext(ctx);
 
   CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"), group.getFirstFilePath());
@@ -50,7 +50,7 @@ void RequestGroupTest::testCreateDownloadResult()
 {
   SharedHandle<DownloadContext> ctx
     (new DownloadContext(1024, 1024*1024, "/tmp/myfile"));
-  RequestGroup group(option_);
+  RequestGroup group(GroupId::create(), option_);
   group.setDownloadContext(ctx);
   group.initPieceStorage();
   {

+ 88 - 49
test/RpcMethodTest.cc

@@ -83,7 +83,6 @@ private:
 public:
   void setUp()
   {
-    RequestGroup::resetGIDCounter();
     option_.reset(new Option());
     option_->put(PREF_DIR, A2_TEST_OUT_DIR"/aria2_RpcMethodTest");
     option_->put(PREF_PIECE_LENGTH, "1048576");
@@ -176,8 +175,11 @@ void RpcMethodTest::testAddUri()
   {
     RpcResponse res = m.execute(req, e_.get());
     CPPUNIT_ASSERT_EQUAL(0, res.code);
+    a2_gid_t gid;
+    CPPUNIT_ASSERT_EQUAL(0, GroupId::toNumericId
+                         (gid, downcast<String>(res.param)->s().c_str()));
     CPPUNIT_ASSERT_EQUAL(std::string("/sink"),
-                         e_->getRequestGroupMan()->findReservedGroup(2)->
+                         e_->getRequestGroupMan()->findReservedGroup(gid)->
                          getOption()->get(PREF_DIR));
   }
 }
@@ -271,7 +273,8 @@ void RpcMethodTest::testAddTorrent()
       (!File(e_->getOption()->get(PREF_DIR)+
              "/0a3893293e27ac0490424c06de4d09242215f0a6.torrent").exists());
     CPPUNIT_ASSERT_EQUAL(0, res.code);
-    CPPUNIT_ASSERT_EQUAL(std::string("1"), downcast<String>(res.param)->s());
+    CPPUNIT_ASSERT_EQUAL(sizeof(a2_gid_t)*2,
+                         downcast<String>(res.param)->s().size());
   }
   e_->getOption()->put(PREF_RPC_SAVE_UPLOAD_METADATA, A2_V_TRUE);
   {
@@ -280,10 +283,12 @@ void RpcMethodTest::testAddTorrent()
       (File(e_->getOption()->get(PREF_DIR)+
             "/0a3893293e27ac0490424c06de4d09242215f0a6.torrent").exists());
     CPPUNIT_ASSERT_EQUAL(0, res.code);
-    CPPUNIT_ASSERT_EQUAL(std::string("2"), downcast<String>(res.param)->s());
+    a2_gid_t gid;
+    CPPUNIT_ASSERT_EQUAL(0, GroupId::toNumericId
+                         (gid, downcast<String>(res.param)->s().c_str()));
 
     SharedHandle<RequestGroup> group =
-      e_->getRequestGroupMan()->findReservedGroup(1);
+      e_->getRequestGroupMan()->findReservedGroup(gid);
     CPPUNIT_ASSERT(group);
     CPPUNIT_ASSERT_EQUAL(e_->getOption()->get(PREF_DIR)+"/aria2-0.8.2.tar.bz2",
                          group->getFirstFilePath());
@@ -304,9 +309,12 @@ void RpcMethodTest::testAddTorrent()
   {
     RpcResponse res = m.execute(req, e_.get());
     CPPUNIT_ASSERT_EQUAL(0, res.code);
+    a2_gid_t gid;
+    CPPUNIT_ASSERT_EQUAL(0, GroupId::toNumericId
+                         (gid, downcast<String>(res.param)->s().c_str()));
     CPPUNIT_ASSERT_EQUAL
       (dir+"/aria2-0.8.2.tar.bz2",
-       e_->getRequestGroupMan()->findReservedGroup(3)->getFirstFilePath());
+       e_->getRequestGroupMan()->findReservedGroup(gid)->getFirstFilePath());
     CPPUNIT_ASSERT
       (File(dir+"/0a3893293e27ac0490424c06de4d09242215f0a6.torrent").exists());
   }
@@ -367,8 +375,13 @@ void RpcMethodTest::testAddMetalink()
     CPPUNIT_ASSERT_EQUAL(0, res.code);
     const List* resParams = downcast<List>(res.param);
     CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
-    CPPUNIT_ASSERT_EQUAL(std::string("1"), downcast<String>(resParams->get(0))->s());
-    CPPUNIT_ASSERT_EQUAL(std::string("2"), downcast<String>(resParams->get(1))->s());
+    a2_gid_t gid1, gid2;
+    CPPUNIT_ASSERT_EQUAL
+      (0, GroupId::toNumericId
+       (gid1, downcast<String>(resParams->get(0))->s().c_str()));
+    CPPUNIT_ASSERT_EQUAL
+      (0, GroupId::toNumericId
+       (gid2, downcast<String>(resParams->get(1))->s().c_str()));
     CPPUNIT_ASSERT
       (!File(e_->getOption()->get(PREF_DIR)+
              "/c908634fbc257fd56f0114912c2772aeeb4064f4.meta4").exists());
@@ -379,8 +392,13 @@ void RpcMethodTest::testAddMetalink()
     CPPUNIT_ASSERT_EQUAL(0, res.code);
     const List* resParams = downcast<List>(res.param);
     CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
-    CPPUNIT_ASSERT_EQUAL(std::string("3"), downcast<String>(resParams->get(0))->s());
-    CPPUNIT_ASSERT_EQUAL(std::string("4"), downcast<String>(resParams->get(1))->s());
+    a2_gid_t gid3, gid4;
+    CPPUNIT_ASSERT_EQUAL
+      (0, GroupId::toNumericId
+       (gid3, downcast<String>(resParams->get(0))->s().c_str()));
+    CPPUNIT_ASSERT_EQUAL
+      (0, GroupId::toNumericId
+       (gid4, downcast<String>(resParams->get(1))->s().c_str()));
 #ifdef ENABLE_MESSAGE_DIGEST
     CPPUNIT_ASSERT
       (File(e_->getOption()->get(PREF_DIR)+
@@ -388,12 +406,12 @@ void RpcMethodTest::testAddMetalink()
 #endif // ENABLE_MESSAGE_DIGEST
 
     SharedHandle<RequestGroup> tar =
-      e_->getRequestGroupMan()->findReservedGroup(1);
+      e_->getRequestGroupMan()->findReservedGroup(gid3);
     CPPUNIT_ASSERT(tar);
     CPPUNIT_ASSERT_EQUAL(e_->getOption()->get(PREF_DIR)+"/aria2-5.0.0.tar.bz2",
                          tar->getFirstFilePath());
     SharedHandle<RequestGroup> deb =
-      e_->getRequestGroupMan()->findReservedGroup(2);
+      e_->getRequestGroupMan()->findReservedGroup(gid4);
     CPPUNIT_ASSERT(deb);
     CPPUNIT_ASSERT_EQUAL(e_->getOption()->get(PREF_DIR)+"/aria2-5.0.0.deb",
                          deb->getFirstFilePath());
@@ -408,8 +426,14 @@ void RpcMethodTest::testAddMetalink()
   {
     RpcResponse res = m.execute(req, e_.get());
     CPPUNIT_ASSERT_EQUAL(0, res.code);
+    const List* resParams = downcast<List>(res.param);
+    CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
+    a2_gid_t gid5;
+    CPPUNIT_ASSERT_EQUAL
+      (0, GroupId::toNumericId
+       (gid5, downcast<String>(resParams->get(0))->s().c_str()));
     CPPUNIT_ASSERT_EQUAL(dir+"/aria2-5.0.0.tar.bz2",
-                         e_->getRequestGroupMan()->findReservedGroup(5)->
+                         e_->getRequestGroupMan()->findReservedGroup(gid5)->
                          getFirstFilePath());
 #ifdef ENABLE_MESSAGE_DIGEST
     CPPUNIT_ASSERT
@@ -462,12 +486,13 @@ void RpcMethodTest::testAddMetalink_withPosition()
 
 void RpcMethodTest::testChangeOption()
 {
-  SharedHandle<RequestGroup> group(new RequestGroup(option_));
+  SharedHandle<RequestGroup> group(new RequestGroup(GroupId::create(),
+                                                    option_));
   e_->getRequestGroupMan()->addReservedGroup(group);
 
   ChangeOptionRpcMethod m;
   RpcRequest req(ChangeOptionRpcMethod::getMethodName(), List::g());
-  req.params->append("1");
+  req.params->append(GroupId::toHex(group->getGID()));
   SharedHandle<Dict> opt = Dict::g();
   opt->put(PREF_MAX_DOWNLOAD_LIMIT->k, "100K");
 #ifdef ENABLE_BITTORRENT
@@ -505,12 +530,13 @@ void RpcMethodTest::testChangeOption()
 
 void RpcMethodTest::testChangeOption_withBadOption()
 {
-  SharedHandle<RequestGroup> group(new RequestGroup(option_));
+  SharedHandle<RequestGroup> group(new RequestGroup(GroupId::create(),
+                                                    option_));
   e_->getRequestGroupMan()->addReservedGroup(group);
 
   ChangeOptionRpcMethod m;
   RpcRequest req(ChangeOptionRpcMethod::getMethodName(), List::g());
-  req.params->append("1");
+  req.params->append(GroupId::toHex(group->getGID()));
   SharedHandle<Dict> opt = Dict::g();
   opt->put(PREF_MAX_DOWNLOAD_LIMIT->k, "badvalue");
   req.params->append(opt);
@@ -520,12 +546,13 @@ void RpcMethodTest::testChangeOption_withBadOption()
 
 void RpcMethodTest::testChangeOption_withNotAllowedOption()
 {
-  SharedHandle<RequestGroup> group(new RequestGroup(option_));
+  SharedHandle<RequestGroup> group(new RequestGroup(GroupId::create(),
+                                                    option_));
   e_->getRequestGroupMan()->addReservedGroup(group);
 
   ChangeOptionRpcMethod m;
   RpcRequest req(ChangeOptionRpcMethod::getMethodName(), List::g());
-  req.params->append("1");
+  req.params->append(GroupId::toHex(group->getGID()));
   SharedHandle<Dict> opt = Dict::g();
   opt->put(PREF_MAX_OVERALL_DOWNLOAD_LIMIT->k, "100K");
   req.params->append(opt);
@@ -648,7 +675,7 @@ void RpcMethodTest::testTellWaiting()
 #else // !ENABLE_BITTORRENT
   addUri("http://4/", e_);
 #endif // !ENABLE_BITTORRENT
-
+  const SharedHandle<RequestGroupMan>& rgman = e_->getRequestGroupMan();
   TellWaitingRpcMethod m;
   RpcRequest req(TellWaitingRpcMethod::getMethodName(), List::g());
   req.params->append(Integer::g(1));
@@ -657,9 +684,9 @@ void RpcMethodTest::testTellWaiting()
   CPPUNIT_ASSERT_EQUAL(0, res.code);
   const List* resParams = downcast<List>(res.param);
   CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
-  CPPUNIT_ASSERT_EQUAL(std::string("2"),
+  CPPUNIT_ASSERT_EQUAL(GroupId::toHex(rgman->getReservedGroups()[1]->getGID()),
                        getString(downcast<Dict>(resParams->get(0)), "gid"));
-  CPPUNIT_ASSERT_EQUAL(std::string("3"),
+  CPPUNIT_ASSERT_EQUAL(GroupId::toHex(rgman->getReservedGroups()[2]->getGID()),
                        getString(downcast<Dict>(resParams->get(1)), "gid"));
   // waiting.size() == offset+num
   req = RpcRequest(TellWaitingRpcMethod::getMethodName(), List::g());
@@ -715,9 +742,9 @@ void RpcMethodTest::testTellWaiting()
   CPPUNIT_ASSERT_EQUAL(0, res.code);
   resParams = downcast<List>(res.param);
   CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
-  CPPUNIT_ASSERT_EQUAL(std::string("4"),
+  CPPUNIT_ASSERT_EQUAL(GroupId::toHex(rgman->getReservedGroups()[3]->getGID()),
                        getString(downcast<Dict>(resParams->get(0)), "gid"));
-  CPPUNIT_ASSERT_EQUAL(std::string("3"),
+  CPPUNIT_ASSERT_EQUAL(GroupId::toHex(rgman->getReservedGroups()[2]->getGID()),
                        getString(downcast<Dict>(resParams->get(1)), "gid"));
   // negative offset and size < num
   req.params->set(1, Integer::g(100));
@@ -774,7 +801,7 @@ void RpcMethodTest::testGatherStoppedDownload()
   followedBy.push_back(3);
   followedBy.push_back(4);
   SharedHandle<DownloadResult> d(new DownloadResult());
-  d->gid = 1;
+  d->gid = GroupId::create();
   d->fileEntries = fileEntries;
   d->inMemoryDownload = false;
   d->sessionDownloadLength = UINT64_MAX;
@@ -787,9 +814,11 @@ void RpcMethodTest::testGatherStoppedDownload()
   gatherStoppedDownload(entry, d, keys);
 
   const List* followedByRes = downcast<List>(entry->get("followedBy"));
-  CPPUNIT_ASSERT_EQUAL(std::string("3"), downcast<String>(followedByRes->get(0))->s());
-  CPPUNIT_ASSERT_EQUAL(std::string("4"), downcast<String>(followedByRes->get(1))->s());
-  CPPUNIT_ASSERT_EQUAL(std::string("2"),
+  CPPUNIT_ASSERT_EQUAL(GroupId::toHex(3),
+                       downcast<String>(followedByRes->get(0))->s());
+  CPPUNIT_ASSERT_EQUAL(GroupId::toHex(4),
+                       downcast<String>(followedByRes->get(1))->s());
+  CPPUNIT_ASSERT_EQUAL(GroupId::toHex(2),
                        downcast<String>(entry->get("belongsTo"))->s());
 
   keys.push_back("gid");
@@ -805,26 +834,30 @@ void RpcMethodTest::testGatherProgressCommon()
   SharedHandle<DownloadContext> dctx(new DownloadContext(0, 0,"aria2.tar.bz2"));
   std::string uris[] = { "http://localhost/aria2.tar.bz2" };
   dctx->getFirstFileEntry()->addUris(vbegin(uris), vend(uris));
-  SharedHandle<RequestGroup> group(new RequestGroup(util::copy(option_)));
+  SharedHandle<RequestGroup> group(new RequestGroup(GroupId::create(),
+                                                    util::copy(option_)));
   group->setDownloadContext(dctx);
   std::vector<SharedHandle<RequestGroup> > followedBy;
   for(int i = 0; i < 2; ++i) {
-    followedBy.push_back(SharedHandle<RequestGroup>(new RequestGroup(util::copy(option_))));
+    followedBy.push_back(SharedHandle<RequestGroup>
+                         (new RequestGroup(GroupId::create(),
+                                           util::copy(option_))));
   }
 
   group->followedBy(followedBy.begin(), followedBy.end());
-  group->belongsTo(2);
+  SharedHandle<GroupId> parent = GroupId::create();
+  group->belongsTo(parent->getNumericId());
 
   SharedHandle<Dict> entry = Dict::g();
   std::vector<std::string> keys;
   gatherProgressCommon(entry, group, keys);
 
   const List* followedByRes = downcast<List>(entry->get("followedBy"));
-  CPPUNIT_ASSERT_EQUAL(util::itos(followedBy[0]->getGID()),
+  CPPUNIT_ASSERT_EQUAL(GroupId::toHex(followedBy[0]->getGID()),
                        downcast<String>(followedByRes->get(0))->s());
-  CPPUNIT_ASSERT_EQUAL(util::itos(followedBy[1]->getGID()),
+  CPPUNIT_ASSERT_EQUAL(GroupId::toHex(followedBy[1]->getGID()),
                        downcast<String>(followedByRes->get(1))->s());
-  CPPUNIT_ASSERT_EQUAL(std::string("2"),
+  CPPUNIT_ASSERT_EQUAL(parent->toHex(),
                        downcast<String>(entry->get("belongsTo"))->s());
   const List* files = downcast<List>(entry->get("files"));
   CPPUNIT_ASSERT_EQUAL((size_t)1, files->size());
@@ -897,20 +930,23 @@ void RpcMethodTest::testGatherBitTorrentMetadata()
 void RpcMethodTest::testChangePosition()
 {
   e_->getRequestGroupMan()->addReservedGroup
-    (SharedHandle<RequestGroup>(new RequestGroup(util::copy(option_))));
+    (SharedHandle<RequestGroup>(new RequestGroup(GroupId::create(),
+                                                 util::copy(option_))));
   e_->getRequestGroupMan()->addReservedGroup
-    (SharedHandle<RequestGroup>(new RequestGroup(util::copy(option_))));
+    (SharedHandle<RequestGroup>(new RequestGroup(GroupId::create(),
+                                                 util::copy(option_))));
 
+  a2_gid_t gid = e_->getRequestGroupMan()->getReservedGroups()[0]->getGID();
   ChangePositionRpcMethod m;
   RpcRequest req(ChangePositionRpcMethod::getMethodName(), List::g());
-  req.params->append("1");
+  req.params->append(GroupId::toHex(gid));
   req.params->append(Integer::g(1));
   req.params->append("POS_SET");
   RpcResponse res = m.execute(req, e_.get());
   CPPUNIT_ASSERT_EQUAL(0, res.code);
   CPPUNIT_ASSERT_EQUAL((int64_t)1, downcast<Integer>(res.param)->i());
   CPPUNIT_ASSERT_EQUAL
-    ((a2_gid_t)1, e_->getRequestGroupMan()->getReservedGroups()[1]->getGID());
+    (gid, e_->getRequestGroupMan()->getReservedGroups()[1]->getGID());
 }
 
 void RpcMethodTest::testChangePosition_fail()
@@ -937,13 +973,14 @@ void RpcMethodTest::testChangeUri()
   files[1]->addUri("http://example.org/mustremove2");
   SharedHandle<DownloadContext> dctx(new DownloadContext());
   dctx->setFileEntries(&files[0], &files[3]);
-  SharedHandle<RequestGroup> group(new RequestGroup(option_));
+  SharedHandle<RequestGroup> group(new RequestGroup(GroupId::create(),
+                                                    option_));
   group->setDownloadContext(dctx);
   e_->getRequestGroupMan()->addReservedGroup(group);
 
   ChangeUriRpcMethod m;
   RpcRequest req(ChangeUriRpcMethod::getMethodName(), List::g());
-  req.params->append("1"); // GID
+  req.params->append(GroupId::toHex(group->getGID())); // GID
   req.params->append(Integer::g(2)); // index of FileEntry
   SharedHandle<List> removeuris = List::g();
   removeuris->append("http://example.org/mustremove1");
@@ -1007,13 +1044,14 @@ void RpcMethodTest::testChangeUri_fail()
   }
   SharedHandle<DownloadContext> dctx(new DownloadContext());
   dctx->setFileEntries(&files[0], &files[3]);
-  SharedHandle<RequestGroup> group(new RequestGroup(option_));
+  SharedHandle<RequestGroup> group(new RequestGroup(GroupId::create(),
+                                                    option_));
   group->setDownloadContext(dctx);
   e_->getRequestGroupMan()->addReservedGroup(group);
 
   ChangeUriRpcMethod m;
   RpcRequest req(ChangeUriRpcMethod::getMethodName(), List::g());
-  req.params->append("1"); // GID
+  req.params->append(GroupId::toHex(group->getGID())); // GID
   req.params->append(Integer::g(1)); // index of FileEntry
   SharedHandle<List> removeuris = List::g();
   req.params->append(removeuris);
@@ -1028,12 +1066,12 @@ void RpcMethodTest::testChangeUri_fail()
   CPPUNIT_ASSERT_EQUAL(1, res.code);
 
   req.params->set(1, Integer::g(1));
-  req.params->set(0, String::g("2"));
+  req.params->set(0, String::g(GroupId::create()->toHex()));
   res = m.execute(req, e_.get());
-  // RPC request fails because GID#2 does not exist.
+  // RPC request fails because the given GID does not exist.
   CPPUNIT_ASSERT_EQUAL(1, res.code);
 
-  req.params->set(0, String::g("1"));
+  req.params->set(0, String::g(GroupId::toHex(group->getGID())));
   req.params->set(1, Integer::g(4));
   res = m.execute(req, e_.get());
   // RPC request fails because FileEntry#3 does not exist.
@@ -1083,7 +1121,7 @@ void RpcMethodTest::testPause()
   {
     PauseRpcMethod m;
     RpcRequest req(PauseRpcMethod::getMethodName(), List::g());
-    req.params->append("1");
+    req.params->append(GroupId::toHex(groups[0]->getGID()));
     RpcResponse res = m.execute(req, e_.get());
     CPPUNIT_ASSERT_EQUAL(0, res.code);
   }
@@ -1091,7 +1129,7 @@ void RpcMethodTest::testPause()
   {
     UnpauseRpcMethod m;
     RpcRequest req(UnpauseRpcMethod::getMethodName(), List::g());
-    req.params->append("1");
+    req.params->append(GroupId::toHex(groups[0]->getGID()));
     RpcResponse res = m.execute(req, e_.get());
     CPPUNIT_ASSERT_EQUAL(0, res.code);
   }
@@ -1172,9 +1210,10 @@ void RpcMethodTest::testSystemMulticall()
   CPPUNIT_ASSERT_EQUAL(0, res.code);
   const List* resParams = downcast<List>(res.param);
   CPPUNIT_ASSERT_EQUAL((size_t)7, resParams->size());
-  CPPUNIT_ASSERT_EQUAL(std::string("1"),
+  SharedHandle<RequestGroupMan> rgman = e_->getRequestGroupMan();
+  CPPUNIT_ASSERT_EQUAL(GroupId::toHex(rgman->getReservedGroups()[0]->getGID()),
                        downcast<String>(downcast<List>(resParams->get(0))->get(0))->s());
-  CPPUNIT_ASSERT_EQUAL(std::string("2"),
+  CPPUNIT_ASSERT_EQUAL(GroupId::toHex(rgman->getReservedGroups()[1]->getGID()),
                        downcast<String>(downcast<List>(resParams->get(1))->get(0))->s());
   CPPUNIT_ASSERT_EQUAL((int64_t)1,
                        downcast<Integer>

+ 1 - 1
test/UTMetadataPostDownloadHandlerTest.cc

@@ -35,7 +35,7 @@ public:
     option_.reset(new Option());
     option_->put(PREF_DIR, A2_TEST_OUT_DIR);
     dctx_.reset(new DownloadContext(0, 0, A2_TEST_OUT_DIR"/something"));
-    requestGroup_.reset(new RequestGroup(option_));
+    requestGroup_.reset(new RequestGroup(GroupId::create(), option_));
     requestGroup_->setDownloadContext(dctx_);
   }