ソースを参照

Add getDownloadHandle API

Tatsuhiro Tsujikawa 12 年 前
コミット
0ef5f4eea1
4 ファイル変更197 行追加0 行削除
  1. 137 0
      src/aria2api.cc
  2. 10 0
      src/aria2api.h
  3. 46 0
      src/includes/aria2/aria2.h
  4. 4 0
      src/option_processing.cc

+ 137 - 0
src/aria2api.cc

@@ -182,4 +182,141 @@ int addUri(Session* session,
   return 0;
 }
 
+namespace {
+struct RequestGroupDH : public DownloadHandle {
+  RequestGroupDH(const SharedHandle<RequestGroup>& 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<RequestGroup> group;
+  TransferStat ts;
+};
+} // namespace
+
+namespace {
+struct DownloadResultDH : public DownloadHandle {
+  DownloadResultDH(const SharedHandle<DownloadResult>& 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<DownloadResult> dr;
+};
+} // namespace
+
+DownloadHandle* getDownloadHandle(Session* session, const A2Gid& gid)
+{
+  const SharedHandle<DownloadEngine>& e =
+    session->context->reqinfo->getDownloadEngine();
+  const SharedHandle<RequestGroupMan>& rgman = e->getRequestGroupMan();
+  SharedHandle<RequestGroup> group = rgman->findGroup(gid);
+  if(group) {
+    return new RequestGroupDH(group);
+  } else {
+    SharedHandle<DownloadResult> 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

+ 10 - 0
src/aria2api.h

@@ -50,6 +50,16 @@ struct Session {
   SharedHandle<Context> 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

+ 46 - 0
src/includes/aria2/aria2.h

@@ -42,6 +42,15 @@
 #include <string>
 #include <vector>
 
+// 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<std::pair<std::string, std::string> > 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

+ 4 - 0
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();