download_thread.c

Go to the documentation of this file.
00001 /*
00002  Copyright (C) 2005-2007 Erik van Pienbroek
00003 
00004  This program is free software; you can redistribute it and/or modify
00005  it under the terms of the GNU General Public License as published by
00006  the Free Software Foundation; either version 2 of the License, or
00007  (at your option) any later version.
00008 
00009  This program is distributed in the hope that it will be useful,
00010  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00012  GNU General Public License for more details.
00013 
00014  You should have received a copy of the GNU General Public License
00015  along with this program; if not, write to the Free Software
00016  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00017 */
00018 
00019 #include 
00020 #include 
00021 #include 
00022 #include 
00023 #include 
00024 #include 
00025 #include 
00026 #include 
00027 #ifdef WIN32
00028 #include 
00029 #endif
00030 
00031 #include "nntpgrab_plugin_schedular.h"
00032 #include "schedular_plugin.h"
00033 
00034 void
00035 download_thread_func(gpointer data, gpointer user_data)
00036 {
00037     DownloadData *download_data = (DownloadData *) data;
00038     SchedularPlugin *schedular = SCHEDULAR_PLUGIN(user_data);
00039     NNTPPart *part;
00040     NNTPFile *file;
00041     NNTPCollection *collection;
00042     NNTPGrabErrCode err;
00043     gboolean abort_flag = FALSE;
00044     gpointer nntp_connection = NULL;
00045     gint idle_timer = 0;
00046     char *reason;
00047 
00048     do {
00049         g_static_mutex_lock(&schedular->mutex);
00050         abort_flag = schedular->abort_flag;
00051         g_static_mutex_unlock(&schedular->mutex);
00052 
00053         if (abort_flag) {
00054             break;
00055         }
00056 
00057         // Have we got anything to download ?
00058         if (!(get_next_part_to_download(schedular, download_data->server_id, &collection, &file, &part, download_data->poolDecoder))) {
00059             idle_timer++;
00060             if (idle_timer == 10) {
00061                 // We have waited for 10 seconds with no new tasks. Kill the connection
00062                 if (nntp_connection) {
00063                     imported_funcs_nntp_connection_destroy(nntp_connection);
00064                     nntp_connection = NULL;
00065                 }
00066             }
00067 
00068             g_usleep(G_USEC_PER_SEC * 1);
00069             continue;
00070         }
00071 
00072         idle_timer = 0;
00073 
00074         // Perform the download
00075         if (!nntp_connection) {
00076             err = NNTP_ERROR_NONE;
00077             nntp_connection = imported_funcs_nntp_connection_initialize(download_data->server->servername, &err);
00078         }
00079 
00080         g_static_mutex_lock(&schedular->mutex);
00081         abort_flag = schedular->abort_flag;
00082         g_static_mutex_unlock(&schedular->mutex);
00083 
00084         if (abort_flag) {
00085             break;
00086         }
00087 
00088         if (!nntp_connection) {
00089             if (err == NNTP_ERROR_HOST_NOT_FOUND) {
00090                 // Configuration error detected, kill this thread
00091                 update_part_download_status(collection, file, part, download_data->server_id, FALSE, FALSE, FALSE, FALSE, NULL);
00092                 reason = g_strdup_printf(_("Unable to detect the IP address belonging to '%s' (servername = %s)"), download_data->server->hostname, download_data->server->servername);
00093                 stop_schedular_from_seperate_thread(reason);
00094                 g_free(reason);
00095 
00096                 break;
00097             } else {
00098                 // Unable to connect, don't try to download this part from this server anymore
00099                 update_part_download_status(collection, file, part, download_data->server_id, FALSE, FALSE, FALSE, TRUE, NULL);
00100             }
00101 
00102             g_usleep(G_USEC_PER_SEC * 5);
00103 
00104             continue;
00105         }
00106 
00107         // Download the part.
00108         err = imported_funcs_nntp_connection_get_part(nntp_connection, collection->collection_name, file, part, &reason);
00109         switch (err) {
00110             case NNTP_ERROR_NONE:
00111                 update_part_download_status(collection, file, part, download_data->server_id, TRUE, FALSE, FALSE, TRUE, nntp_connection);
00112 
00113                 break;
00114 
00115             case NNTP_ERROR_PART_NOT_AVAILABLE:
00116                 update_part_download_status(collection, file, part, download_data->server_id, FALSE, FALSE, FALSE, TRUE, nntp_connection);
00117 
00118                 break;
00119 
00120             case NNTP_ERROR_UNABLE_TO_SAVE_PART:
00121                 update_part_download_status(collection, file, part, download_data->server_id, FALSE, FALSE, FALSE, FALSE, nntp_connection);
00122 
00123                 stop_schedular_from_seperate_thread(reason);
00124 
00125                 if (reason) {
00126                     g_free(reason);
00127                 }
00128 
00129                 // Prevent to start a new download task
00130                 g_usleep(G_USEC_PER_SEC / 10);
00131 
00132                 break;
00133 
00134             default:
00135                 // Connection error. Put the part back in the queue and
00136                 // sleep for a few seconds to avoid flooding
00137 
00138                 update_part_download_status(collection, file, part, download_data->server_id, FALSE, FALSE, FALSE, FALSE, nntp_connection);
00139 
00140                 // Kill the connection
00141                 imported_funcs_nntp_connection_destroy(nntp_connection);
00142                 nntp_connection = NULL;
00143 
00144                 g_usleep(G_USEC_PER_SEC * 5);
00145 
00146                 break;
00147         };
00148 
00149         g_static_mutex_lock(&schedular->mutex);
00150         abort_flag = schedular->abort_flag;
00151         g_static_mutex_unlock(&schedular->mutex);
00152     } while (!abort_flag);
00153 
00154     if (nntp_connection) {
00155         imported_funcs_nntp_connection_destroy(nntp_connection);
00156     }
00157 
00158     g_slice_free(DownloadData, download_data);
00159 }

Generated on Sun Oct 12 01:45:29 2008 for NNTPGrab by  1.5.4