Browse Source

Corrections in RealProgressBarThread

- Fixed a bug related to the size of the array
- Removed the much hated goto statements with loops :)
adaptive-webui-19844
Ishan Arora 17 years ago
parent
commit
8a9a23866f
  1. 96
      src/realprogressbarthread.cpp

96
src/realprogressbarthread.cpp

@ -53,84 +53,72 @@ void RealProgressBarThread::refresh()
condition.wakeOne(); condition.wakeOne();
} }
//this is called inside run()
#define CHECK_INTERRUPT \
switch(ifInterrupted())\
{\
case stop:\
qDebug("RealProgressBarThread stop");\
return;\
case restart:\
qDebug("RealProgressBarThread restart");\
goto start;\
case ignore:\
break;\
}
void RealProgressBarThread::run(){ void RealProgressBarThread::run(){
//wait for refresh or rezise slot forever
wait: {
QMutexLocker locker(&mutex);
condition.wait(&mutex);
locker.unlock();
//start checking the torrent information //start checking the torrent information
start: if(ifInterrupted() == stop)
CHECK_INTERRUPT {
qDebug("RealProgressBarThread stop");
return;
}
size_type total_size = thandle.total_size(); size_type total_size = thandle.total_size();
size_type piece_length = thandle.piece_length(); size_type piece_length = thandle.piece_length();
int num_pieces = thandle.num_pieces(); int num_pieces = thandle.num_pieces();
const std::vector<bool>* pieces = thandle.pieces(); const std::vector<bool>* pieces = thandle.pieces();
//no vector returned //pieces not returned
if (pieces == 0) if (pieces == 0)
{
qDebug("pieces vector not returned");
return; return;
}
//empty the array //empty the array
locker.relock(); mutex.lock();
for(int i=0; i<size; i++) for(int i=0; i<array.size(); i++)
array[i] = 0.; array[i] = 0.;
locker.unlock(); mutex.unlock();
qreal subfraction = size / (qreal) total_size; qreal subfraction = array.size() / (qreal) total_size;
qreal fraction = subfraction * piece_length; qreal fraction = subfraction * piece_length;
bool success = true;
//fill the array with complete pieces //fill the array with complete pieces
for(int i=0; i<num_pieces; i++) for(int i=0; i<num_pieces; i++)
{ {
CHECK_INTERRUPT Interrupt temp = ifInterrupted();
if (temp == stop)
{
qDebug("RealProgressBarThread stop");
return;
}
if (temp == restart)
{
qDebug("RealProgressBarThread restart");
success = false;
break;
}
qreal start = i * fraction; qreal start = i * fraction;
qreal end = start + fraction; qreal end = start + fraction;
if((*pieces)[i]) if((*pieces)[i])
mark(start, end); mark(start, end);
} }
/* if (success)
//fill the array with incomplete pieces (from download queue)
std::vector<partial_piece_info> queue;
thandle.get_download_queue(queue);
for(unsigned int i=0; i<queue.size(); i++)
{
partial_piece_info ppi = queue[i];
qreal start = ppi.piece_index * fraction;
qreal end = start;
for(int j=0; j<ppi.blocks_in_piece; j++)
{ {
CHECK_INTERRUPT /*
block_info bi = ppi.blocks[j];
end += bi.block_size * subfraction;
qreal progress = bi.bytes_progress / (qreal) bi.block_size;
mark(start, end, progress);
start = end;
}
}
qreal sum = 0.; qreal sum = 0.;
locker.relock(); mutex.lock();
for(int i=0; i<size; i++) for(int i=0; i<array.size(); i++)
sum += array[i]; sum += array[i];
qDebug()<<"progress:"<<sum*100./size; qDebug()<<"progress:"<<sum*100./array.size();
locker.unlock();*/ mutex.unlock();*/
qDebug("refreshed emmitted"); qDebug("refreshed emmitted");
emit refreshed(array); emit refreshed(array);
goto wait; //wait for refresh or rezise slot
mutex.lock();
condition.wait(&mutex);
mutex.unlock();
}
}
} }
//this is called by CHECK_INTERRUPT
Interrupt RealProgressBarThread::ifInterrupted() Interrupt RealProgressBarThread::ifInterrupted()
{ {
QMutexLocker locker(&mutex); QMutexLocker locker(&mutex);
@ -150,8 +138,8 @@ Interrupt RealProgressBarThread::ifInterrupted()
void RealProgressBarThread::mark(qreal start, qreal end, qreal progress){ void RealProgressBarThread::mark(qreal start, qreal end, qreal progress){
QMutexLocker locker(&mutex); QMutexLocker locker(&mutex);
if (end > size) if (end > array.size())
end = size; end = array.size();
int start_int, end_int; int start_int, end_int;
qreal temp, start_frac, end_frac; qreal temp, start_frac, end_frac;
start_frac = modf(start, &temp); start_frac = modf(start, &temp);

Loading…
Cancel
Save