Changeset 481
- Timestamp:
- 06/07/08 16:43:52 (12 years ago)
- Location:
- trunk/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/halSession.hpp
r480 r481 212 212 } 213 213 214 #define HAL_GENERIC_TORRENT_EXCEPTION_CATCH(TORRENT, FUNCTION) \ 215 catch (const libt::invalid_handle&) \ 216 {\ 217 event_log.post(shared_ptr<EventDetail>( \ 218 new EventInvalidTorrent(event_logger::critical, event_logger::invalidTorrent, TORRENT, std::string(FUNCTION)))); \ 219 }\ 220 catch (const invalidTorrent& t) \ 221 {\ 222 event_log.post(shared_ptr<EventDetail>( \ 223 new EventInvalidTorrent(event_logger::info, event_logger::invalidTorrent, t.who(), std::string(FUNCTION)))); \ 224 }\ 225 catch (const std::exception& e) \ 226 {\ 227 event_log.post(shared_ptr<EventDetail>( \ 228 new EventTorrentException(event_logger::critical, event_logger::torrentException, std::string(e.what()), TORRENT, std::string(FUNCTION)))); \ 229 } 230 214 231 class bit_impl 215 232 { … … 579 596 void stopAlertHandler(); 580 597 void alertHandler(); 598 599 void addTorrent(wpath file, wpath saveDirectory, bool startStopped, bool compactStorage, 600 boost::filesystem::wpath moveToDirectory, bool useMoveTo) 601 { 602 try 603 { 604 torrent_internal_ptr TIp; 605 606 std::pair<std::string, std::string> names = extract_names(file); 607 wstring xml_name = from_utf8(names.first) + L".xml"; 608 609 if (fs::exists(file.branch_path()/xml_name)) 610 { 611 torrent_standalone tsa; 612 613 if (tsa.load_standalone(file.branch_path()/xml_name)) 614 { 615 TIp = tsa.torrent; 616 617 TIp->set_save_directory(saveDirectory, true); 618 if (useMoveTo) 619 TIp->set_move_to_directory(moveToDirectory); 620 621 TIp->prepare(file); 622 } 623 } 624 625 if (!TIp) 626 { 627 if (useMoveTo) 628 TIp.reset(new torrent_internal(file, saveDirectory, compactStorage, moveToDirectory)); 629 else 630 TIp.reset(new torrent_internal(file, saveDirectory, compactStorage)); 631 632 TIp->setTransferSpeed(bittorrent().defTorrentDownload(), bittorrent().defTorrentUpload()); 633 TIp->setConnectionLimit(bittorrent().defTorrentMaxConn(), bittorrent().defTorrentMaxUpload()); 634 } 635 636 std::pair<TorrentManager::torrentByName::iterator, bool> p = 637 theTorrents.insert(TIp); 638 639 if (p.second) 640 { 641 torrent_internal_ptr me = theTorrents.get(TIp->name()); 642 643 if (!startStopped) 644 me->add_to_session(); 645 else 646 me->set_state_stopped(); 647 } 648 649 } 650 catch (const std::exception& e) 651 { 652 event_log.post(shared_ptr<EventDetail>( 653 new EventTorrentException(event_logger::critical, event_logger::torrentException, 654 std::string(e.what()), to_utf8(file.string()), std::string("addTorrent")))); 655 } 656 } 657 658 std::pair<libt::entry, libt::entry> prepTorrent(wpath filename, wpath saveDirectory) 659 { 660 libt::entry metadata = haldecode(filename); 661 libt::torrent_info info(metadata); 662 663 wstring torrentName = hal::from_utf8_safe(info.name()); 664 if (!boost::find_last(torrentName, L".torrent")) 665 torrentName += L".torrent"; 666 667 wpath torrentFilename = torrentName; 668 const wpath resumeFile = workingDirectory/L"resume"/torrentFilename.leaf(); 669 670 // vvv Handle old naming style! 671 const wpath oldResumeFile = workingDirectory/L"resume"/filename.leaf(); 672 673 if (filename.leaf() != torrentFilename.leaf() && exists(oldResumeFile)) 674 fs::rename(oldResumeFile, resumeFile); 675 // ^^^ Handle old naming style! 676 677 libt::entry resumeData; 678 679 if (fs::exists(resumeFile)) 680 { 681 try 682 { 683 resumeData = haldecode(resumeFile); 684 } 685 catch(std::exception &e) 686 { 687 hal::event_log.post(boost::shared_ptr<hal::EventDetail>( 688 new hal::EventStdException(event_logger::critical, e, L"prepTorrent, Resume"))); 689 690 fs::remove(resumeFile); 691 } 692 } 693 694 if (!fs::exists(workingDirectory/L"torrents")) 695 fs::create_directory(workingDirectory/L"torrents"); 696 697 if (!fs::exists(workingDirectory/L"torrents"/torrentFilename.leaf())) 698 fs::copy_file(filename.string(), workingDirectory/L"torrents"/torrentFilename.leaf()); 699 700 if (!fs::exists(saveDirectory)) 701 fs::create_directory(saveDirectory); 702 703 return std::make_pair(metadata, resumeData); 704 } 705 706 void resumeAll() 707 { 708 try { 709 710 event_log.post(shared_ptr<EventDetail>(new EventMsg(L"Resuming torrent."))); 711 712 for (TorrentManager::torrentByName::iterator i=theTorrents.begin(), e=theTorrents.end(); i != e;) 713 { 714 wpath file = wpath(workingDirectory)/L"torrents"/(*i).torrent->filename(); 715 716 if (exists(file)) 717 { 718 try 719 { 720 721 (*i).torrent->prepare(file); 722 723 switch ((*i).torrent->state()) 724 { 725 case TorrentDetail::torrent_stopped: 726 break; 727 case TorrentDetail::torrent_paused: 728 (*i).torrent->add_to_session(true); 729 break; 730 case TorrentDetail::torrent_active: 731 (*i).torrent->add_to_session(false); 732 break; 733 default: 734 assert(false); 735 }; 736 737 ++i; 738 739 } 740 catch(const libt::duplicate_torrent&) 741 { 742 hal::event_log.post(shared_ptr<hal::EventDetail>( 743 new hal::EventDebug(hal::event_logger::debug, L"Encountered duplicate torrent"))); 744 745 ++i; // Harmless, don't worry about it. 746 } 747 catch(const std::exception& e) 748 { 749 hal::event_log.post(shared_ptr<hal::EventDetail>( 750 new hal::EventStdException(hal::event_logger::warning, e, L"resumeAll"))); 751 752 theTorrents.erase(i++); 753 } 754 } 755 else 756 { 757 theTorrents.erase(i++); 758 } 759 } 760 761 } HAL_GENERIC_TORRENT_EXCEPTION_CATCH("Torrent Unknown!", "closeAll") 762 } 763 764 void closeAll(boost::optional<report_num_active> fn) 765 { 766 try 767 { 768 event_log.post(shared_ptr<EventDetail>(new EventInfo(L"Saving torrent data..."))); 769 770 saveTorrentData(); 771 772 event_log.post(shared_ptr<EventDetail>(new EventInfo(L"Stopping all torrents..."))); 773 774 for (TorrentManager::torrentByName::iterator i=theTorrents.begin(), e=theTorrents.end(); 775 i != e; ++i) 776 { 777 (*i).torrent->stop(); 778 } 779 780 // Ok this polling loop here is a bit curde, but a blocking wait is actually appropiate. 781 for (int num_active = -1; num_active != 0; ) 782 { 783 num_active = 0; 784 785 for (TorrentManager::torrentByName::iterator i=theTorrents.begin(), e=theTorrents.end(); 786 i != e; ++i) 787 { 788 if ((*i).torrent->state() != TorrentDetail::torrent_stopped) 789 ++num_active; 790 } 791 792 event_log.post(shared_ptr<EventDetail>(new EventInfo(wformat(L"%1% still active") % num_active))); 793 794 if (fn) (*fn)(num_active); 795 Sleep(200); 796 } 797 798 event_log.post(shared_ptr<EventDetail>(new EventInfo(L"All torrents stopped."))); 799 event_log.post(shared_ptr<EventDetail>(new EventInfo(L"Fast-resume data written."))); 800 801 } HAL_GENERIC_TORRENT_EXCEPTION_CATCH("Torrent Unknown!", "closeAll") 802 } 581 803 582 804 void saveTorrentData() … … 781 1003 } 782 1004 783 std::pair<libt::entry, libt::entry> prepTorrent(wpath filename, wpath saveDirectory);1005 // std::pair<libt::entry, libt::entry> prepTorrent(wpath filename, wpath saveDirectory); 784 1006 void removalThread(torrent_internal_ptr pIT, bool wipeFiles); 785 1007 -
trunk/src/halTorrent.cpp
r480 r481 122 122 } 123 123 124 #define HAL_GENERIC_TORRENT_EXCEPTION_CATCH(TORRENT, FUNCTION) \125 catch (const libt::invalid_handle&) \126 {\127 event_log.post(shared_ptr<EventDetail>( \128 new EventInvalidTorrent(event_logger::critical, event_logger::invalidTorrent, TORRENT, std::string(FUNCTION)))); \129 }\130 catch (const invalidTorrent& t) \131 {\132 event_log.post(shared_ptr<EventDetail>( \133 new EventInvalidTorrent(event_logger::info, event_logger::invalidTorrent, t.who(), std::string(FUNCTION)))); \134 }\135 catch (const std::exception& e) \136 {\137 event_log.post(shared_ptr<EventDetail>( \138 new EventTorrentException(event_logger::critical, event_logger::torrentException, std::string(e.what()), TORRENT, std::string(FUNCTION)))); \139 }140 141 124 void bit::shutDownSession() 142 125 { … … 303 286 } 304 287 305 std::pair<libt::entry, libt::entry> bit_impl::prepTorrent(wpath filename, wpath saveDirectory)306 {307 libt::entry metadata = haldecode(filename);308 libt::torrent_info info(metadata);309 310 wstring torrentName = hal::from_utf8_safe(info.name());311 if (!boost::find_last(torrentName, L".torrent"))312 torrentName += L".torrent";313 314 wpath torrentFilename = torrentName;315 const wpath resumeFile = workingDirectory/L"resume"/torrentFilename.leaf();316 317 // vvv Handle old naming style!318 const wpath oldResumeFile = workingDirectory/L"resume"/filename.leaf();319 320 if (filename.leaf() != torrentFilename.leaf() && exists(oldResumeFile))321 fs::rename(oldResumeFile, resumeFile);322 // ^^^ Handle old naming style!323 324 libt::entry resumeData;325 326 if (fs::exists(resumeFile))327 {328 try329 {330 resumeData = haldecode(resumeFile);331 }332 catch(std::exception &e)333 {334 hal::event_log.post(boost::shared_ptr<hal::EventDetail>(335 new hal::EventStdException(event_logger::critical, e, L"prepTorrent, Resume")));336 337 fs::remove(resumeFile);338 }339 }340 341 if (!fs::exists(workingDirectory/L"torrents"))342 fs::create_directory(workingDirectory/L"torrents");343 344 if (!fs::exists(workingDirectory/L"torrents"/torrentFilename.leaf()))345 fs::copy_file(filename.string(), workingDirectory/L"torrents"/torrentFilename.leaf());346 347 if (!fs::exists(saveDirectory))348 fs::create_directory(saveDirectory);349 350 return std::make_pair(metadata, resumeData);351 }352 353 288 void bit::addTorrent(wpath file, wpath saveDirectory, bool startStopped, bool compactStorage, 354 289 boost::filesystem::wpath moveToDirectory, bool useMoveTo) 355 290 { 356 try 357 { 358 torrent_internal_ptr TIp; 359 360 std::pair<std::string, std::string> names = extract_names(file); 361 wstring xml_name = from_utf8(names.first) + L".xml"; 362 363 if (fs::exists(file.branch_path()/xml_name)) 364 { 365 torrent_standalone tsa; 366 367 if (tsa.load_standalone(file.branch_path()/xml_name)) 368 { 369 TIp = tsa.torrent; 370 371 TIp->set_save_directory(saveDirectory, true); 372 if (useMoveTo) 373 TIp->set_move_to_directory(moveToDirectory); 374 375 TIp->prepare(file); 376 } 377 } 378 379 if (!TIp) 380 { 381 if (useMoveTo) 382 TIp.reset(new torrent_internal(file, saveDirectory, compactStorage, moveToDirectory)); 383 else 384 TIp.reset(new torrent_internal(file, saveDirectory, compactStorage)); 385 386 TIp->setTransferSpeed(bittorrent().defTorrentDownload(), bittorrent().defTorrentUpload()); 387 TIp->setConnectionLimit(bittorrent().defTorrentMaxConn(), bittorrent().defTorrentMaxUpload()); 388 } 389 390 std::pair<TorrentManager::torrentByName::iterator, bool> p = 391 pimpl->theTorrents.insert(TIp); 392 393 if (p.second) 394 { 395 torrent_internal_ptr me = pimpl->theTorrents.get(TIp->name()); 396 397 if (!startStopped) 398 me->add_to_session(); 399 else 400 me->set_state_stopped(); 401 } 402 403 } HAL_GENERIC_TORRENT_EXCEPTION_CATCH(to_utf8(file.string()), "addTorrent") 291 pimpl->addTorrent(file, saveDirectory, startStopped, compactStorage, moveToDirectory, useMoveTo); 404 292 } 405 293 … … 442 330 void bit::resumeAll() 443 331 { 444 try { 445 446 event_log.post(shared_ptr<EventDetail>(new EventMsg(L"Resuming torrent."))); 447 448 for (TorrentManager::torrentByName::iterator i=pimpl->theTorrents.begin(), e=pimpl->theTorrents.end(); i != e;) 449 { 450 wpath file = wpath(pimpl->workingDirectory)/L"torrents"/(*i).torrent->filename(); 451 452 if (exists(file)) 453 { 454 try 455 { 456 457 (*i).torrent->prepare(file); 458 459 switch ((*i).torrent->state()) 460 { 461 case TorrentDetail::torrent_stopped: 462 break; 463 case TorrentDetail::torrent_paused: 464 (*i).torrent->add_to_session(true); 465 break; 466 case TorrentDetail::torrent_active: 467 (*i).torrent->add_to_session(false); 468 break; 469 default: 470 assert(false); 471 }; 472 473 ++i; 474 475 } 476 catch(const libt::duplicate_torrent&) 477 { 478 hal::event_log.post(shared_ptr<hal::EventDetail>( 479 new hal::EventDebug(hal::event_logger::debug, L"Encountered duplicate torrent"))); 480 481 ++i; // Harmless, don't worry about it. 482 } 483 catch(const std::exception& e) 484 { 485 hal::event_log.post(shared_ptr<hal::EventDetail>( 486 new hal::EventStdException(hal::event_logger::warning, e, L"resumeAll"))); 487 488 pimpl->theTorrents.erase(i++); 489 } 490 } 491 else 492 { 493 pimpl->theTorrents.erase(i++); 494 } 495 } 496 497 } HAL_GENERIC_TORRENT_EXCEPTION_CATCH("Torrent Unknown!", "resumeAll") 332 pimpl->resumeAll(); 498 333 } 499 334 500 335 void bit::closeAll(boost::optional<report_num_active> fn) 501 336 { 502 try 503 { 504 event_log.post(shared_ptr<EventDetail>(new EventInfo(L"Saving torrent data..."))); 505 506 pimpl->saveTorrentData(); 507 508 event_log.post(shared_ptr<EventDetail>(new EventInfo(L"Stopping all torrents..."))); 509 510 for (TorrentManager::torrentByName::iterator i=pimpl->theTorrents.begin(), e=pimpl->theTorrents.end(); 511 i != e; ++i) 512 { 513 (*i).torrent->stop(); 514 } 515 516 // Ok this polling loop here is a bit curde, but a blocking wait is actually appropiate. 517 for (int num_active = -1; num_active != 0; ) 518 { 519 num_active = 0; 520 521 for (TorrentManager::torrentByName::iterator i=pimpl->theTorrents.begin(), e=pimpl->theTorrents.end(); 522 i != e; ++i) 523 { 524 if ((*i).torrent->state() != TorrentDetail::torrent_stopped) 525 ++num_active; 526 } 527 528 event_log.post(shared_ptr<EventDetail>(new EventInfo(wformat(L"%1% still active") % num_active))); 529 530 if (fn) (*fn)(num_active); 531 Sleep(200); 532 } 533 534 event_log.post(shared_ptr<EventDetail>(new EventInfo(L"All torrents stopped."))); 535 event_log.post(shared_ptr<EventDetail>(new EventInfo(L"Fast-resume data written."))); 536 537 } HAL_GENERIC_TORRENT_EXCEPTION_CATCH("Torrent Unknown!", "closeAll") 337 pimpl->closeAll(fn); 538 338 } 539 339
Note: See TracChangeset
for help on using the changeset viewer.