diff --git a/src/BitfieldManFactory.cc b/src/BitfieldManFactory.cc index 2ee68222..20db54e8 100644 --- a/src/BitfieldManFactory.cc +++ b/src/BitfieldManFactory.cc @@ -35,6 +35,6 @@ #include "BitfieldManFactory.h" #include "SimpleRandomizer.h" -RandomizerHandle BitfieldManFactory::defaultRandomizer = SimpleRandomizer::getInstance(); +BitfieldManFactoryHandle BitfieldManFactory::factory = 0; -BitfieldManFactory::BitfieldManFactory():randomizer(defaultRandomizer) {} +BitfieldManFactory::BitfieldManFactory():randomizer(0) {} diff --git a/src/BitfieldManFactory.h b/src/BitfieldManFactory.h index 34377a09..8b7d9ca8 100644 --- a/src/BitfieldManFactory.h +++ b/src/BitfieldManFactory.h @@ -45,15 +45,18 @@ typedef SharedHandle BitfieldManFactoryHandle; class BitfieldManFactory { private: - static RandomizerHandle defaultRandomizer; + static BitfieldManFactoryHandle factory; + RandomizerHandle randomizer; BitfieldManFactory(); public: ~BitfieldManFactory() {} - static BitfieldManFactoryHandle getNewFactory() { - BitfieldManFactoryHandle factory = new BitfieldManFactory(); + static BitfieldManFactoryHandle getFactoryInstance() { + if(factory.isNull()) { + factory = new BitfieldManFactory(); + } return factory; } @@ -64,11 +67,12 @@ public: } static void setDefaultRandomizer(const RandomizerHandle& randomizer) { - defaultRandomizer = randomizer; + BitfieldManFactoryHandle factory = getFactoryInstance(); + factory->setRandomizer(randomizer); } static RandomizerHandle getDefaultRandomizer() { - return defaultRandomizer; + return getFactoryInstance()->getRandomizer(); } void setRandomizer(const RandomizerHandle& randomizer) { diff --git a/src/DefaultPieceStorage.cc b/src/DefaultPieceStorage.cc index 20e4ffe0..c9f43299 100644 --- a/src/DefaultPieceStorage.cc +++ b/src/DefaultPieceStorage.cc @@ -52,7 +52,7 @@ DefaultPieceStorage::DefaultPieceStorage(BtContextHandle btContext, const Option option(option) { bitfieldMan = - BitfieldManFactory::getNewFactory()-> + BitfieldManFactory::getFactoryInstance()-> createBitfieldMan(btContext->getPieceLength(), btContext->getTotalLength()); logger = LogFactory::getInstance(); diff --git a/src/Peer.cc b/src/Peer.cc index b9591d23..7da4120d 100644 --- a/src/Peer.cc +++ b/src/Peer.cc @@ -46,7 +46,7 @@ Peer::Peer(string ipaddr, int port, int pieceLength, long long int totalLength): active(false) { resetStatus(); - this->bitfield = BitfieldManFactory::getNewFactory()-> + this->bitfield = BitfieldManFactory::getFactoryInstance()-> createBitfieldMan(pieceLength, totalLength); string idSeed = ipaddr+":"+Util::itos(port); id = Util::simpleMessageDigest(idSeed); diff --git a/src/Piece.cc b/src/Piece.cc index 1bba051b..f73cf83c 100644 --- a/src/Piece.cc +++ b/src/Piece.cc @@ -40,7 +40,7 @@ Piece::Piece():index(0), length(0), bitfield(0) {} Piece::Piece(int index, int length):index(index), length(length) { bitfield = - BitfieldManFactory::getNewFactory()->createBitfieldMan(BLOCK_LENGTH, length); + BitfieldManFactory::getFactoryInstance()->createBitfieldMan(BLOCK_LENGTH, length); } Piece::Piece(const Piece& piece) { diff --git a/src/SegmentMan.cc b/src/SegmentMan.cc index 12edb358..a09859de 100644 --- a/src/SegmentMan.cc +++ b/src/SegmentMan.cc @@ -238,7 +238,7 @@ void SegmentMan::init() { void SegmentMan::initBitfield(int segmentLength, long long int totalLength) { delete bitfield; - this->bitfield = BitfieldManFactory::getNewFactory()->createBitfieldMan(segmentLength, totalLength); + this->bitfield = BitfieldManFactory::getFactoryInstance()->createBitfieldMan(segmentLength, totalLength); } Segment SegmentMan::checkoutSegment(int cuid, int index) {