| 1429 | // toFile = Try to swap to this partfile only |
| 1430 | |
| 1431 | bool CUpDownClient::SwapToAnotherFile(bool bIgnoreNoNeeded, bool ignoreSuspensions, bool bRemoveCompletely, CPartFile* toFile) |
| 1432 | { |
| 1433 | // Fail if m_reqfile is invalid |
| 1434 | if ( m_reqfile == NULL ) { |
| 1435 | return false; |
| 1436 | } |
| 1437 | |
| 1438 | // It would be stupid to swap away a downloading source |
| 1439 | if (GetDownloadState() == DS_DOWNLOADING) { |
| 1440 | return false; |
| 1441 | } |
| 1442 | |
| 1443 | // The iterator of the final target |
| 1444 | A4AFList::iterator target = m_A4AF_list.end(); |
| 1445 | |
| 1446 | // Do we want to swap to a specific file? |
| 1447 | if ( toFile != NULL ) { |
| 1448 | A4AFList::iterator it = m_A4AF_list.find( toFile ); |
| 1449 | if ( it != m_A4AF_list.end() ) { |
| 1450 | |
| 1451 | // We force ignoring of timestamps |
| 1452 | if ( IsValidSwapTarget( it, bIgnoreNoNeeded, true ) ) { |
| 1453 | // Set the target |
| 1454 | target = it; |
| 1455 | } |
| 1456 | } |
| 1457 | } else { |
| 1458 | // We want highest priority possible, but need to start with |
| 1459 | // a value less than any other priority |
| 1460 | char priority = -1; |
| 1461 | |
| 1462 | A4AFList::iterator it = m_A4AF_list.begin(); |
| 1463 | for ( ; it != m_A4AF_list.end(); ++it ) { |
| 1464 | if ( IsValidSwapTarget( it, bIgnoreNoNeeded, ignoreSuspensions ) ) { |
| 1465 | char cur_priority = it->first->GetDownPriority(); |
| 1466 | |
| 1467 | // We would prefer to get files with needed parts, thus rate them higher. |
| 1468 | // However, this really only matters if bIgnoreNoNeeded is true. |
| 1469 | if ( it->second.NeededParts ) |
| 1470 | cur_priority += 10; |
| 1471 | |
| 1472 | // Change target if the current file has a higher rate than the previous |
| 1473 | if ( cur_priority > priority ) { |
| 1474 | priority = cur_priority; |
| 1475 | |
| 1476 | // Set the new target |
| 1477 | target = it; |
| 1478 | |
| 1479 | // Break on the first High-priority file with needed parts |
| 1480 | if ( priority == PR_HIGH + 10 ) { |
| 1481 | break; |
| 1482 | } |
| 1483 | } |
| 1484 | } |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | // Try to swap if we found a valid target |
no test coverage detected