00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
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
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
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
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
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
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
00130 g_usleep(G_USEC_PER_SEC / 10);
00131
00132 break;
00133
00134 default:
00135
00136
00137
00138 update_part_download_status(collection, file, part, download_data->server_id, FALSE, FALSE, FALSE, FALSE, nntp_connection);
00139
00140
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 }