decoder_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 #include 
00028 
00029 #include "nntpgrab_plugin_schedular.h"
00030 #include "schedular_plugin.h"
00031 
00032 void
00033 decoder_thread_func(gpointer data, gpointer user_data)
00034 {
00035     DecoderData *decoder_data = (DecoderData *) data;
00036     int saved_errno = 0;
00037     static time_t last_timestamp = 0;
00038 
00039     // Do we still need to decode?
00040     g_static_mutex_lock(&decoder_data->schedular->mutex);
00041     if (decoder_data->schedular->abort_flag || decoder_data->file->stop_flag) {
00042         decoder_data->file->now_decoding = FALSE;
00043         g_static_mutex_unlock(&decoder_data->schedular->mutex);
00044         g_slice_free(DecoderData, decoder_data);
00045         return;
00046     }
00047     g_static_mutex_unlock(&decoder_data->schedular->mutex);
00048 
00049     imported_funcs_task_state_changed(decoder_data->collection_name, decoder_data->file->subject, decoder_data->file->status, TASK_STATE_DECODING);
00050 
00051     g_static_mutex_lock(&decoder_data->schedular->mutex);
00052     decoder_data->file->status = TASK_STATE_DECODING;
00053     g_static_mutex_unlock(&decoder_data->schedular->mutex);
00054 
00055     // Perform the actual decoding
00056     if (!imported_funcs_decode_file(decoder_data->collection_name, decoder_data->file, &saved_errno)) {
00057         if (saved_errno == ENOSPC) {
00058             // No free space available, stop the schedular
00059             stop_schedular_from_seperate_thread(strerror(saved_errno));
00060 
00061             // Sleep for 1 second to prevent other threads from retrying this file
00062             g_usleep(G_USEC_PER_SEC * 1);
00063 
00064             g_static_mutex_lock(&decoder_data->schedular->mutex);
00065             decoder_data->file->now_decoding = FALSE;
00066             imported_funcs_task_state_changed(decoder_data->collection_name, decoder_data->file->subject, decoder_data->file->status, TASK_STATE_WAITING_FOR_DECODE);
00067             decoder_data->file->status = TASK_STATE_WAITING_FOR_DECODE;
00068             g_static_mutex_unlock(&decoder_data->schedular->mutex);
00069 
00070             g_slice_free(DecoderData, decoder_data);
00071 
00072             return;
00073         } else if (saved_errno == -1) {
00074             // No parts available, ignore
00075         } else {
00076             // Unknown decode error occured, stop the schedular
00077             char *msg = g_strdup_printf(_("Unknown decode error occured, %s (%i)"), strerror(saved_errno), saved_errno);
00078             stop_schedular_from_seperate_thread(msg);
00079             g_free(msg);
00080 
00081             // Sleep for 1 second to prevent other threads from retrying this file
00082             g_usleep(G_USEC_PER_SEC * 1);
00083 
00084             g_static_mutex_lock(&decoder_data->schedular->mutex);
00085             decoder_data->file->now_decoding = FALSE;
00086             imported_funcs_task_state_changed(decoder_data->collection_name, decoder_data->file->subject, decoder_data->file->status, TASK_STATE_WAITING_FOR_DECODE);
00087             decoder_data->file->status = TASK_STATE_WAITING_FOR_DECODE;
00088             g_static_mutex_unlock(&decoder_data->schedular->mutex);
00089 
00090             g_slice_free(DecoderData, decoder_data);
00091 
00092             return;
00093         }
00094     }
00095 
00096     // Mark the file as completed
00097     g_static_mutex_lock(&decoder_data->schedular->mutex);
00098 
00099     // Was the task restarted while the decoding was busy?
00100     if (decoder_data->file->now_decoding) {
00101         // Nope, mark the file decode as completed
00102         decoder_data->file->file_is_downloaded = TRUE;
00103         decoder_data->file->now_decoding = FALSE;
00104 
00105         imported_funcs_task_state_changed(decoder_data->collection_name, decoder_data->file->subject, decoder_data->file->status, TASK_STATE_FINISHED);
00106         decoder_data->file->status = TASK_STATE_FINISHED;
00107     }
00108 
00109     g_static_mutex_unlock(&decoder_data->schedular->mutex);
00110 
00111     // Is this file a PAR2 file?
00112 
00113     g_slice_free(DecoderData, decoder_data);
00114 
00115     // Save the download queue only when there has been some time between now and the previous time we've decoded something
00116     if (time(NULL) > last_timestamp + 60) {
00117         schedular_plugin_save_queue(NULL);
00118         last_timestamp = time(NULL);
00119     }
00120 }
00121 

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