gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r2646 - GNUnet/src/applications/fs/fsui GNUnet/src/include


From: grothoff
Subject: [GNUnet-SVN] r2646 - GNUnet/src/applications/fs/fsui GNUnet/src/include gnunet-gtk/src/plugins/fs
Date: Sun, 23 Apr 2006 06:14:59 -0700 (PDT)

Author: grothoff
Date: 2006-04-23 06:14:55 -0700 (Sun, 23 Apr 2006)
New Revision: 2646

Modified:
   GNUnet/src/applications/fs/fsui/download.c
   GNUnet/src/applications/fs/fsui/fsui.c
   GNUnet/src/applications/fs/fsui/fsui.h
   GNUnet/src/include/gnunet_fsui_lib.h
   gnunet-gtk/src/plugins/fs/download.c
   gnunet-gtk/src/plugins/fs/fs.c
Log:
fixingn download resume code

Modified: GNUnet/src/applications/fs/fsui/download.c
===================================================================
--- GNUnet/src/applications/fs/fsui/download.c  2006-04-23 11:52:31 UTC (rev 
2645)
+++ GNUnet/src/applications/fs/fsui/download.c  2006-04-23 13:14:55 UTC (rev 
2646)
@@ -182,7 +182,7 @@
 testTerminate(void * cls) {
   FSUI_DownloadList * dl = cls;
 
-  if (dl->signalTerminate == YES) {
+  if (dl->state != FSUI_DOWNLOAD_ACTIVE) {
     return SYSERR;
   } else {
     return OK;
@@ -215,16 +215,20 @@
                          &testTerminate,
                          dl);  
   if (ret == OK) {
-    dl->finished = YES;
+    dl->state = FSUI_DOWNLOAD_COMPLETED;
     totalBytes = ECRS_fileSize(dl->uri);
   } else {
 #if DEBUG_DTM
-    LOG(LOG_ERROR,
+    LOG(LOG_DEBUG,
        "Download thread for `%s' failed (aborted or error)!\n",
        dl->filename);
 #endif
+    if (dl->state == FSUI_DOWNLOAD_ACTIVE)
+      dl->state = FSUI_DOWNLOAD_ERROR;
+    else if ( (dl->state != FSUI_DOWNLOAD_ABORTED) &&
+             (dl->state != FSUI_DOWNLOAD_SUSPENDING) )
+      BREAK();
     totalBytes = 0;
-    dl->total = 0;
   }
   root = dl;
   while (root->parent != &dl->ctx->activeDownloads) {
@@ -285,19 +289,29 @@
     FREE(fn);
   }
   if (ret != OK) {
-    if (dl->signalTerminate == YES) {
+    switch (dl->state) {
+    case FSUI_DOWNLOAD_ABORTED:
       event.type = FSUI_download_aborted;
       event.data.DownloadError.message = _("Download aborted.");
-    } else {
+      break;
+    case FSUI_DOWNLOAD_ERROR:
       event.type = FSUI_download_error;
       event.data.DownloadError.message = _("ECRS download failed (see logs).");
+      break;
+    case FSUI_DOWNLOAD_SUSPENDING:
+      event.type = FSUI_download_suspending;
+      event.data.DownloadError.message = _("ECRS download suspending.");
+      break;
+    default:
+      event.type = FSUI_download_error;
+      event.data.DownloadError.message = _("Unexpected download state.");
+      printf("State: %u\n", dl->state);
+      BREAK();
     }
     event.data.DownloadError.pos = dl;
     dl->ctx->ecb(dl->ctx->ecbClosure,
                 &event);
-    dl->signalTerminate = YES;
   } else {
-    dl->signalTerminate = YES;
     GNUNET_ASSERT(dl != &dl->ctx->activeDownloads);
     while ( (dl != NULL) &&
            (dl->ctx != NULL) &&
@@ -356,8 +370,7 @@
   dl = MALLOC(sizeof(FSUI_DownloadList));
   memset(dl, 0, sizeof(FSUI_DownloadList));
   cronTime(&dl->startTime); 
-  dl->signalTerminate = SYSERR;
-  dl->finished = NO;
+  dl->state = FSUI_DOWNLOAD_PENDING;
   dl->is_recursive = is_recursive;
   dl->parent = parent;
   dl->is_directory = SYSERR; /* don't know */
@@ -431,16 +444,15 @@
   /* should this one be started? */
   if ( (list->ctx->threadPoolSize
        > list->ctx->activeDownloadThreads) &&
-       (list->signalTerminate == SYSERR) &&
+       (list->state == FSUI_DOWNLOAD_PENDING) &&
        ( (list->total > list->completed) ||
-         (list->total == 0) ) &&
-       (list->finished == NO) ) {
+         (list->total == 0) ) ) {
 #if DEBUG_DTM
     LOG(LOG_DEBUG,
        "Download thread manager starts downlod of file `%s'\n",
        list->filename);
 #endif
-    list->signalTerminate = NO;
+    list->state = FSUI_DOWNLOAD_ACTIVE;
     if (0 == PTHREAD_CREATE(&list->handle,
                            &downloadThread,
                            list,
@@ -448,13 +460,14 @@
       list->ctx->activeDownloadThreads++;
     } else {
       LOG_STRERROR(LOG_WARNING, "pthread_create");     
+      list->state = FSUI_DOWNLOAD_ERROR_JOINED;
     }
   }
 
   /* should this one be stopped? */
   if ( (list->ctx->threadPoolSize
        < list->ctx->activeDownloadThreads) &&
-       (list->signalTerminate == NO) ) {
+       (list->state == FSUI_DOWNLOAD_ACTIVE) ) {
 #if DEBUG_DTM
     LOG(LOG_DEBUG,
        "Download thread manager aborts active download of file `%s' (%u/%u 
downloads)\n",
@@ -462,18 +475,20 @@
        list->ctx->activeDownloadThreads,
        list->ctx->threadPoolSize);
 #endif
-    list->signalTerminate = YES;
+    list->state = FSUI_DOWNLOAD_SUSPENDING;
     PTHREAD_KILL(&list->handle,
                 SIGALRM); /* terminate sleep */
     PTHREAD_JOIN(&list->handle,
                 &unused);
     list->ctx->activeDownloadThreads--;
-    list->signalTerminate = SYSERR;
+    list->state = FSUI_DOWNLOAD_PENDING;
     ret = YES;
   }
 
   /* has this one "died naturally"? */
-  if (list->signalTerminate == YES) {
+  if ( (list->state == FSUI_DOWNLOAD_COMPLETED) ||
+       (list->state == FSUI_DOWNLOAD_ABORTED) ||
+       (list->state == FSUI_DOWNLOAD_ERROR) ) {       
 #if DEBUG_DTM
     LOG(LOG_DEBUG,
        "Download thread manager collects inactive download of file `%s'\n",
@@ -482,7 +497,7 @@
     PTHREAD_JOIN(&list->handle,
                 &unused);
     list->ctx->activeDownloadThreads--;
-    list->signalTerminate = SYSERR;
+    list->state++; /* adds _JOINED */
     ret = YES;
   }
 
@@ -504,7 +519,7 @@
   FSUI_DownloadList * dpos;
   int i;
 
-  GNUNET_ASSERT(list->signalTerminate != NO);
+  GNUNET_ASSERT(list->state != FSUI_DOWNLOAD_ACTIVE);
 
   /* first, find our predecessor and
      unlink us from the tree! */
@@ -640,7 +655,9 @@
   while ( (dl != NULL) &&
          (stop == NO) ) {
     if ( (dl->completed == dl->total) &&
-        (dl->signalTerminate == SYSERR) ) {
+        ( (dl->state == FSUI_DOWNLOAD_COMPLETED_JOINED) ||
+          (dl->state == FSUI_DOWNLOAD_ABORTED_JOINED) ||
+          (dl->state == FSUI_DOWNLOAD_ERROR_JOINED) ) ) {
       if (iter != NULL)
        if (OK != iter(closure,
                       dl,

Modified: GNUnet/src/applications/fs/fsui/fsui.c
===================================================================
--- GNUnet/src/applications/fs/fsui/fsui.c      2006-04-23 11:52:31 UTC (rev 
2645)
+++ GNUnet/src/applications/fs/fsui/fsui.c      2006-04-23 13:14:55 UTC (rev 
2646)
@@ -96,14 +96,33 @@
         0,
         sizeof(FSUI_DownloadList));
   ret->ctx = ctx;
-
-  ret->signalTerminate
-    = SYSERR;
   READINT(ret->is_recursive);
   READINT(ret->is_directory);
   READINT(ret->anonymityLevel);
   READINT(ret->completedDownloadsCount);
-  READINT(ret->finished);
+  READINT(ret->state);
+  switch (ret->state) { /* try to correct errors */
+  case FSUI_DOWNLOAD_ACTIVE:
+    ret->state = FSUI_DOWNLOAD_PENDING;
+    break;
+  case FSUI_DOWNLOAD_PENDING:
+  case FSUI_DOWNLOAD_COMPLETED_JOINED:
+  case FSUI_DOWNLOAD_ABORTED_JOINED:
+  case FSUI_DOWNLOAD_ERROR_JOINED:
+    break;
+  case FSUI_DOWNLOAD_ERROR:
+    ret->state = FSUI_DOWNLOAD_ERROR_JOINED;
+    break;
+  case FSUI_DOWNLOAD_ABORTED:
+    ret->state = FSUI_DOWNLOAD_ABORTED_JOINED;
+    break;
+  case FSUI_DOWNLOAD_COMPLETED:
+    ret->state = FSUI_DOWNLOAD_COMPLETED_JOINED;
+    break;
+  default:
+    ret->state = FSUI_DOWNLOAD_PENDING;
+    break;
+  }
   READINT(big);
   if (big > 1024 * 1024) {
     BREAK();
@@ -141,7 +160,6 @@
     goto ERR;
   }
   ret->parent = parent;
-  ret->signalTerminate = SYSERR;
   ret->next = readDownloadList(fd,
                               ctx,
                               parent);
@@ -227,7 +245,7 @@
   WRITEINT(fd, list->is_directory);
   WRITEINT(fd, list->anonymityLevel);
   WRITEINT(fd, list->completedDownloadsCount);
-  WRITEINT(fd, list->finished);
+  WRITEINT(fd, list->state);
   WRITEINT(fd, strlen(list->filename));
   WRITE(fd,
        list->filename,
@@ -356,8 +374,8 @@
 
   ret = MALLOC(sizeof(FSUI_Context));
   memset(ret, 0, sizeof(FSUI_Context));
-  ret->activeDownloads.signalTerminate
-    = SYSERR;
+  ret->activeDownloads.state
+    = FSUI_DOWNLOAD_PENDING; /* !? */
   ret->activeDownloads.ctx
     = ret;
   gh = getFileName("GNUNET",

Modified: GNUnet/src/applications/fs/fsui/fsui.h
===================================================================
--- GNUnet/src/applications/fs/fsui/fsui.h      2006-04-23 11:52:31 UTC (rev 
2645)
+++ GNUnet/src/applications/fs/fsui/fsui.h      2006-04-23 13:14:55 UTC (rev 
2646)
@@ -140,6 +140,63 @@
 } FSUI_SearchList;
 
 /**
+ * Current state of a download.
+ *
+ * PENDING means that the download is waiting for a thread
+ * to be assigned to run it.  Downloads start in this state,
+ * and during shutdown are serialized in this state.<br>
+ *
+ * ACTIVE means that there is currently a thread running
+ * the download (and that thread is allowed to continue).<br>
+ *
+ * COMPLETED means that the download is finished (but the
+ * thread has not been joined yet).  The download thread
+ * makes the transition from PENDING to COMPLETED when it
+ * is about to terminate.<br>
+ *
+ * COMPLETED_JOINED means that the download is finished and
+ * the thread has been joined.<br>
+ *
+ * ABORTED means that the user is causing the download to be
+ * terminated early (but the thread has not been joined yet).  The
+ * controller or the download thread make this transition; the
+ * download thread is supposed to terminate shortly after the state is
+ * moved to ABORTED.<br>
+ *
+ * ABORTED_JOINED means that the download did not complete
+ * successfully, should not be restarted and that the thread
+ * has been joined.<br>
+ *
+ * ERROR means that some fatal error is causing the download to be
+ * terminated early (but the thread has not been joined yet).  The
+ * controller or the download thread make this transition; the
+ * download thread is supposed to terminate shortly after the state is
+ * moved to ERROR.<br>
+ *
+ * ERROR_JOINED means that the download did not complete successfully,
+ * should not be restarted and that the thread has been joined.<br>
+ *
+ * SUSPENDING is used to notify the download thread that it
+ * should terminate because of an FSUI shutdown.  After this
+ * termination the code that joins the thread should move
+ * the state into PENDING (a new thread would not be started
+ * immediately because "threadPoolSize" will be 0 until FSUI
+ * resumes).
+ */
+typedef enum {
+  FSUI_DOWNLOAD_PENDING = 0,
+  FSUI_DOWNLOAD_ACTIVE = 1,
+  FSUI_DOWNLOAD_COMPLETED = 2,
+  FSUI_DOWNLOAD_COMPLETED_JOINED = 3,
+  FSUI_DOWNLOAD_ABORTED = 4,
+  FSUI_DOWNLOAD_ABORTED_JOINED = 5,
+  FSUI_DOWNLOAD_ERROR = 6,
+  FSUI_DOWNLOAD_ERROR_JOINED = 7,
+  FSUI_DOWNLOAD_SUSPENDING = 8,
+} FSUI_DownloadState;
+
+
+/**
  * @brief list of active downloads
  */
 typedef struct FSUI_DownloadList {
@@ -168,22 +225,11 @@
   struct FSUI_Context * ctx;
 
   /**
-   * Set this to YES to signal the download thread that
-   * termination is desired.  Then join on handle.
-   * Set to NO if thread is running.  Set to SYSERR
-   * if no thread has been assigned to this download
-   * at the moment.
+   * State of the download.
    */
-  int signalTerminate;
+  FSUI_DownloadState state;
 
   /**
-   * Is the download of this file finished (not necessarily
-   * transitively for directories, just the directory/file
-   * itself).
-   */
-  int finished;
-
-  /**
    * Currently assigned thread (if any).
    */
   PTHREAD_T handle;

Modified: GNUnet/src/include/gnunet_fsui_lib.h
===================================================================
--- GNUnet/src/include/gnunet_fsui_lib.h        2006-04-23 11:52:31 UTC (rev 
2645)
+++ GNUnet/src/include/gnunet_fsui_lib.h        2006-04-23 13:14:55 UTC (rev 
2646)
@@ -105,6 +105,7 @@
    * Connection status with gnunetd changed.
    */
   FSUI_gnunetd_disconnected,
+  FSUI_download_suspending,
 };
 
 /**

Modified: gnunet-gtk/src/plugins/fs/download.c
===================================================================
--- gnunet-gtk/src/plugins/fs/download.c        2006-04-23 11:52:31 UTC (rev 
2645)
+++ gnunet-gtk/src/plugins/fs/download.c        2006-04-23 13:14:55 UTC (rev 
2646)
@@ -51,6 +51,10 @@
    void * closure) {
   struct ECRS_URI * uri = closure;
   DownloadList * pos;
+  GtkTreeIter iter;
+  GtkTreeIter child;
+  int i;
+  GtkTreePath * path;
 
   if (isRoot == YES)
     return OK;
@@ -62,14 +66,7 @@
       break;
     pos = pos->next;
   }
-  if (pos == NULL) {
-    BREAK(); /* odd */
-  } else {
-    GtkTreeIter iter;
-    GtkTreeIter child;
-    int i;
-    GtkTreePath * path;
-
+  if (pos != NULL) {
     if (! gtk_tree_row_reference_valid(pos->rr))
       return SYSERR;
     path = gtk_tree_row_reference_get_path(pos->rr);

Modified: gnunet-gtk/src/plugins/fs/fs.c
===================================================================
--- gnunet-gtk/src/plugins/fs/fs.c      2006-04-23 11:52:31 UTC (rev 2645)
+++ gnunet-gtk/src/plugins/fs/fs.c      2006-04-23 13:14:55 UTC (rev 2646)
@@ -51,6 +51,7 @@
        event->data.message);
     break;
   case FSUI_download_aborted:
+  case FSUI_download_suspending:
     /* ignore for now */
     break;
   case FSUI_download_progress:





reply via email to

[Prev in Thread] Current Thread [Next in Thread]