📄 download.c
字号:
download_show_message(download, _ ("The file was not found in all the connections!")); /*Terminate the connections */ proz_download_stop_downloads(download); return DLREMOTEFATAL; } else { download_show_message(download, _("Relaunching download")); /* Make sure this thread has terminated */ pthread_join(download->threads[i], NULL); if (pthread_create (&download->threads[i], NULL, (void *) &ftp_loop, (void *) (download->pconnections[i])) != 0) proz_die(_("Error: Not enough system resources")); } } else /*Handle the file not being found on the server */ if (connection_err == FTPCWDFAIL) { if (proz_download_all_dls_ftpcwdfail(download) == TRUE) { download_show_message(download, _ ("Failed to change to the working directory on all the connections!")); /*Terminate the connections */ proz_download_stop_downloads(download); return DLREMOTEFATAL; } else { download_show_message(download, _("Relaunching download")); /* Make sure this thread has terminated */ pthread_join(download->threads[i], NULL); if (pthread_create (&download->threads[i], NULL, (void *) &ftp_loop, (void *) (download->pconnections[i])) != 0) proz_die(_("Error: Not enough system resources")); } } break; case LOCALFATAL: proz_download_stop_downloads(download); download_show_message(download, _ ("Connection %d, had a local fatal error: %s .Aborting download. "), i, proz_strerror(download->pconnections[i]->err)); return DLLOCALFATAL; break; case LOGINFAIL: /* * First check if the ftp server did not allow any thread * to login at all, then retry the curent thread */ if (proz_download_all_dls_status(download, LOGINFAIL) == TRUE) { download_show_message(download, _ ("All logins rejected! Retrying connection")); /* Make sure this thread has terminated */ pthread_join(download->threads[i], NULL); if (pthread_create (&download->threads[i], NULL, (void *) &ftp_loop, (void *) (download->pconnections[i])) != 0) proz_die(_("Error: Not enough system resources")); break; } else { /* * Ok so at least there is one download whos login has not been rejected, * so lets see if it has completed, if so we can relaunch this connection, * as the commonest reason for a ftp login being rejected is because, the * ftp server has a limit on the number of logins permitted from the same * IP address. */ /* * Query the number of threads that are downloading * if it is zero then relaunch this connection */ int dling_conns_count = download_query_conns_status_count(download, DOWNLOADING, NULL); if (dling_conns_count > download->max_simul_connections) { download->max_simul_connections = dling_conns_count; break; } if (dling_conns_count == 0 && (download_query_conns_status_count(download, CONNECTING, NULL) == 0) && (download_query_conns_status_count(download, LOGGININ, NULL) == 0)) { /* Make sure this thread has terminated */ pthread_join(download->threads[i], NULL); pthread_mutex_lock(&download->status_change_mutex); download_show_message(download, _("Relaunching download")); if (pthread_create(&download->threads[i], NULL, (void *) &ftp_loop, (void *) (download->pconnections[i])) != 0) proz_die(_("Error: Not enough system resources")); pthread_cond_wait(&download->pconnections[i]->connecting_cond, &download->status_change_mutex); pthread_mutex_unlock(&download->status_change_mutex); } else if (dling_conns_count < download->max_simul_connections && (download_query_conns_status_count (download, CONNECTING, NULL) == 0) && (download_query_conns_status_count (download, LOGGININ, NULL) == 0)) { /* Make sure this thread has terminated */ pthread_join(download->threads[i], NULL); pthread_mutex_lock(&download->status_change_mutex); download_show_message(download, _("Relaunching download")); if (pthread_create(&download->threads[i], NULL, (void *) &ftp_loop, (void *) (download->pconnections[i])) != 0) proz_die(_("Error: Not enough system resources")); pthread_cond_wait(&download->pconnections[i]->connecting_cond, &download->status_change_mutex); pthread_mutex_unlock(&download->status_change_mutex); } } break; case CONREJECT: /* * First check if the ftp server did not allow any thread * to login at all, then retry the curent thread */ if (proz_download_all_dls_status(download, CONREJECT) == TRUE) { download_show_message(download, _ ("All connections attempts have been rejected! Retrying connection")); /* Make sure this thread has terminated */ pthread_join(download->threads[i], NULL); if (pthread_create (&download->threads[i], NULL, (void *) &ftp_loop, (void *) (download->pconnections[i])) != 0) proz_die(_("Error: Not enough system resources")); break; } else { /* * Ok so at least there is one download whos connections attempt has not been rejected, * so lets see if it has completed, if so we can relaunch this connection, * as the commonest reason for a ftp login being rejected is because, the * ftp server has a limit on the number of logins permitted from the same * IP address. */ /* * Query the number of threads that are downloading * if it is zero then relaunch this connection */ int dling_conns_count = download_query_conns_status_count(download, DOWNLOADING, NULL); if (dling_conns_count > download->max_simul_connections) { download->max_simul_connections = dling_conns_count; break; } if (dling_conns_count == 0 && (download_query_conns_status_count(download, CONNECTING, NULL) == 0) && (download_query_conns_status_count(download, LOGGININ, NULL) == 0)) { /* Make sure this thread has terminated */ pthread_join(download->threads[i], NULL); pthread_mutex_lock(&download->status_change_mutex); download_show_message(download, _("Relaunching download")); if (pthread_create(&download->threads[i], NULL, (void *) &ftp_loop, (void *) (download->pconnections[i])) != 0) proz_die(_("Error: Not enough system resources")); pthread_cond_wait(&download->pconnections[i]->connecting_cond, &download->status_change_mutex); pthread_mutex_unlock(&download->status_change_mutex); } else if (dling_conns_count < download->max_simul_connections && (download_query_conns_status_count (download, CONNECTING, NULL) == 0) && (download_query_conns_status_count (download, LOGGININ, NULL) == 0)) { /* Make sure this thread has terminated */ pthread_join(download->threads[i], NULL); pthread_mutex_lock(&download->status_change_mutex); download_show_message(download, _("Relaunching download")); if (pthread_create(&download->threads[i], NULL, (void *) &ftp_loop, (void *) (download->pconnections[i])) != 0) proz_die(_("Error: Not enough system resources")); pthread_cond_wait(&download->pconnections[i]->connecting_cond, &download->status_change_mutex); pthread_mutex_unlock(&download->status_change_mutex); } } break; default: break; } } /*bandwith throttling */ download_calc_throttle_factor(download); return DLINPROGRESS;}pthread_mutex_t download_msg_mutex = PTHREAD_MUTEX_INITIALIZER;/*calls the msg_proc function if not null */void download_show_message(download_t * download, const char *format, ...){ va_list args; char message[MAX_MSG_SIZE + 1]; pthread_mutex_lock(&download_msg_mutex); va_start(args, format); vsnprintf(message, MAX_MSG_SIZE, format, args); va_end(args); if (download->msg_proc) download->msg_proc(message, download->cb_data); /*FIXME: Remove this later */ printf("%s\n", message); pthread_mutex_unlock(&download_msg_mutex);}/*This will return a pointer to the connection requested. */connection_t *proz_download_get_connection(download_t * download, int number){ assert(number >= 0 && number < download->num_connections); return (download->pconnections[number]);}/*Returns the total number of bytes got.*/long proz_download_get_total_bytes_got(download_t * download){ long total_bytes_got = 0; int i; for (i = 0; i < download->num_connections; i++) { total_bytes_got += proz_connection_get_total_bytes_got(download->pconnections[i]); } return total_bytes_got;}/*Returns 1 if a previous download exits, 0 if not, and -1 on error */int proz_download_prev_download_exists(download_t * download){ /* Currently if a logfile exists it assumes that a previous uncompleted download exists */ return proz_log_logfile_exists(download);}/*Returns the download speed in bytes per second */float proz_download_get_average_speed(download_t * download){ float speed; struct timeval cur_time; struct timeval diff_time; long total_remote_bytes_got = proz_download_get_total_remote_bytes_got(download); /*Has the download has been started.... */ if (download->start_time.tv_sec > 0 || download->start_time.tv_usec > 0) { gettimeofday(&cur_time, NULL); /*Get the diff. */ proz_timeval_subtract(&diff_time, &cur_time, &download->start_time); if (diff_time.tv_sec > 0 || diff_time.tv_usec > 0) { speed = (float) total_remote_bytes_got / ((float) diff_time.tv_sec + ((float) diff_time. tv_usec / 10e5)); } else speed = 0; } else speed = 0; /*The DL hasnt started yet */ return speed;}/* This can be called to erase the portions of the main file that have been got.*/int proz_download_delete_dl_file(download_t * download){ char *out_file; out_file=kmalloc(PATH_MAX); snprintf(out_file, PATH_MAX, "%s/%s.prozilla", download->dl_dir, download->u.file); if (unlink(out_file) == -1) { /* * if the file is not present no need for a error message ;-) */ if (errno == ENOENT) { return 1; } else { download_show_message(download, _ ("unable to delete the file %s. Reason-: %s"), out_file, strerror(errno)); return -1; } } return 1;}/*This will wait till all the downloaded threads are not running */void proz_download_wait_till_all_end(download_t * download){ int i; /*Wait till the end of all the threads */ for (i = 0; i < download->num_connections; i++) { pthread_join(download->threads[i], NULL); }}/*Creates the joining thread */void proz_download_join_downloads(download_t * download){ download->building = TRUE; pthread_create(&download->join_thread, NULL, (void *(*)(void *)) download_join_downloads, (void *) download);}/*This function will call download_join_downloads with a handler to join the downloaded files*/void download_join_downloads(download_t * download){ pthread_cleanup_push(cleanup_joining_thread, (void *) download); join_downloads(download); pthread_cleanup_pop(0);}/*This function will join the downloaded files*/void join_downloads(download_t * download){ pthread_mutex_lock(&download->access_mutex); download->building = 0; pthread_mutex_unlock(&download->access_mutex); return;}/***************************************************************************** Returns the total number of bytes that has being got from the server by the all the connections managed by this download******************************************************************************/long proz_download_get_total_remote_bytes_got(download_t * download){ long total_bytes_recv = 0; int i; for (i = 0; i < download->num_connections; i++) { total_bytes_recv += proz_connection_get_total_remote_bytes_got(download-> pconnections[i]); } return total_bytes_recv;}/*If all the downlaods status is equal to status ,returns TRUE */int proz_download_all_dls_status(download_t * download, dl_status status){ int i; pthread_mutex_lock(&download->status_change_mutex); for (i = 0; i < download->num_connections; i++) { if (download->pconnections[i]->status != status) { pthread_mutex_unlock(&download->status_change_mutex); return FALSE; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -