/* */ #include "DownloadEngineFactory.h" #include "prefs.h" #include "DefaultDiskWriter.h" #include "InitiateConnectionCommandFactory.h" #include "ByteArrayDiskWriter.h" #include "Util.h" #include "FileAllocator.h" #include "FileAllocationMonitor.h" #ifdef ENABLE_BITTORRENT # include "PeerListenCommand.h" # include "TrackerWatcherCommand.h" # include "TrackerUpdateCommand.h" # include "TorrentAutoSaveCommand.h" # include "SeedCheckCommand.h" # include "PeerChokeCommand.h" # include "HaveEraseCommand.h" # include "ActivePeerConnectionCommand.h" # include "UnionSeedCriteria.h" # include "TimeSeedCriteria.h" # include "ShareRatioSeedCriteria.h" # include "DefaultPieceStorage.h" # include "DefaultPeerStorage.h" # include "DefaultBtAnnounce.h" # include "DefaultBtProgressInfoFile.h" #endif // ENABLE_BITTORRENT ConsoleDownloadEngine* DownloadEngineFactory::newConsoleEngine(const Option* op, const Requests& requests, const Requests& reserved) { ConsoleDownloadEngine* e = new ConsoleDownloadEngine(); e->option = op; e->segmentMan = new SegmentMan(); e->segmentMan->diskWriter = DefaultDiskWriter::createNewDiskWriter(op); e->segmentMan->dir = op->get(PREF_DIR); e->segmentMan->ufilename = op->get(PREF_OUT); e->segmentMan->option = op; e->segmentMan->reserved = reserved; int cuidCounter = 1; for(Requests::const_iterator itr = requests.begin(); itr != requests.end(); itr++, cuidCounter++) { e->commands.push_back(InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuidCounter, *itr, e)); } return e; } #ifdef ENABLE_BITTORRENT TorrentConsoleDownloadEngine* DownloadEngineFactory::newTorrentConsoleEngine(const BtContextHandle& btContext, const Option* op, const Strings& targetFiles) { TorrentConsoleDownloadEngine* te = new TorrentConsoleDownloadEngine(); te->option = op; ByteArrayDiskWriter* byteArrayDiskWriter = new ByteArrayDiskWriter(); te->segmentMan = new SegmentMan(); te->segmentMan->diskWriter = byteArrayDiskWriter; te->segmentMan->option = op; BtRuntimeHandle btRuntime(new BtRuntime()); BtRegistry::registerBtRuntime(btContext->getInfoHashAsString(), btRuntime); PieceStorageHandle pieceStorage(new DefaultPieceStorage(btContext, op)); BtRegistry::registerPieceStorage(btContext->getInfoHashAsString(), pieceStorage); PeerStorageHandle peerStorage(new DefaultPeerStorage(btContext, op)); BtRegistry::registerPeerStorage(btContext->getInfoHashAsString(), peerStorage); BtAnnounceHandle btAnnounce(new DefaultBtAnnounce(btContext, op)); BtRegistry::registerBtAnnounce(btContext->getInfoHashAsString(), btAnnounce); btAnnounce->shuffleAnnounce(); BtProgressInfoFileHandle btProgressInfoFile(new DefaultBtProgressInfoFile(btContext, op)); BtRegistry::registerBtProgressInfoFile(btContext->getInfoHashAsString(), btProgressInfoFile); BtRegistry::registerPeerObjectCluster(btContext->getInfoHashAsString(), new PeerObjectCluster()); /* DefaultBtMessageFactoryAdaptorHandle factoryAdaptor = new DefaultBtMessageFactoryAdaptor(); BtRegistry::registerBtMessageFactoryAdaptor(btContext->getInfoHashAsString(), factoryAdaptor); BtMessageFactoryClusterHandle factoryCluster = new BtMessageFactoryCluster(); BtRegistry::registerBtMessageFactoryCluster(btContext->getInfoHashAsString(), factoryCluster); BtMessageDispatcherClusterHandle dispatcherCluster = new BtMessageDispatcherCluster(); BtRegistry::registerBtMessageDispatcherCluster(btContext->getInfoHashAsString(), dispatcherCluster); */ te->setBtContext(btContext); // initialize file storage pieceStorage->initStorage(); Integers selectIndexes; Util::unfoldRange(op->get(PREF_SELECT_FILE), selectIndexes); if(selectIndexes.size()) { pieceStorage->setFileFilter(selectIndexes); } else { pieceStorage->setFileFilter(targetFiles); } PeerListenCommand* listenCommand = new PeerListenCommand(btRuntime->getNewCuid(), te, btContext); int port; int listenPort = op->getAsInt(PREF_LISTEN_PORT); if(listenPort == -1) { port = listenCommand->bindPort(6881, 6999); } else { port = listenCommand->bindPort(listenPort, listenPort); } if(port == -1) { printf(_("Errors occurred while binding port.\n")); exit(EXIT_FAILURE); } btRuntime->setListenPort(port); te->commands.push_back(listenCommand); te->commands.push_back(new TrackerWatcherCommand(btRuntime->getNewCuid(), te, btContext)); te->commands.push_back(new TrackerUpdateCommand(btRuntime->getNewCuid(), te, btContext)); te->commands.push_back(new TorrentAutoSaveCommand(btRuntime->getNewCuid(), te, btContext, op->getAsInt(PREF_AUTO_SAVE_INTERVAL))); te->commands.push_back(new PeerChokeCommand(btRuntime->getNewCuid(), te, btContext, 10)); te->commands.push_back(new HaveEraseCommand(btRuntime->getNewCuid(), te, btContext, 10)); te->commands.push_back(new ActivePeerConnectionCommand(btRuntime->getNewCuid(), te, btContext, 30)); SharedHandle unionCri = new UnionSeedCriteria(); if(op->defined(PREF_SEED_TIME)) { unionCri->addSeedCriteria(new TimeSeedCriteria(op->getAsInt(PREF_SEED_TIME)*60)); } if(op->defined(PREF_SEED_RATIO)) { unionCri->addSeedCriteria(new ShareRatioSeedCriteria(op->getAsDouble(PREF_SEED_RATIO), btContext)); } if(unionCri->getSeedCriterion().size() > 0) { te->commands.push_back(new SeedCheckCommand(btRuntime->getNewCuid(), te, btContext, unionCri)); } return te; } #endif // ENABLE_BITTORRENT