/* */ #include "DefaultBtMessageFactory.h" #include "DlAbortEx.h" #include "PeerMessageUtil.h" #include "BtKeepAliveMessage.h" #include "BtChokeMessage.h" #include "BtUnchokeMessage.h" #include "BtInterestedMessage.h" #include "BtNotInterestedMessage.h" #include "BtHaveMessage.h" #include "BtHaveMessageValidator.h" #include "BtBitfieldMessage.h" #include "BtBitfieldMessageValidator.h" #include "BtRequestMessage.h" #include "BtRequestMessageValidator.h" #include "BtCancelMessage.h" #include "BtCancelMessageValidator.h" #include "BtPieceMessage.h" #include "BtPieceMessageValidator.h" #include "BtPortMessage.h" #include "BtHaveAllMessage.h" #include "BtHaveNoneMessage.h" #include "BtRejectMessage.h" #include "BtRejectMessageValidator.h" #include "BtSuggestPieceMessage.h" #include "BtSuggestPieceMessageValidator.h" #include "BtAllowedFastMessage.h" #include "BtAllowedFastMessageValidator.h" #include "BtHandshakeMessage.h" #include "BtHandshakeMessageValidator.h" #include "BtExtendedMessage.h" #include "ExtensionMessage.h" #include "Peer.h" #include "Piece.h" #include "BtContext.h" #include "PieceStorage.h" #include "PeerStorage.h" #include "StringFormat.h" #include "ExtensionMessageFactory.h" namespace aria2 { DefaultBtMessageFactory::DefaultBtMessageFactory():cuid(0), _dhtEnabled(false) {} DefaultBtMessageFactory::~DefaultBtMessageFactory() {} BtMessageHandle DefaultBtMessageFactory::createBtMessage(const unsigned char* data, size_t dataLength) { AbstractBtMessageHandle msg; if(dataLength == 0) { // keep-alive msg.reset(new BtKeepAliveMessage()); } else { uint8_t id = PeerMessageUtil::getId(data); switch(id) { case BtChokeMessage::ID: msg = BtChokeMessage::create(data, dataLength); break; case BtUnchokeMessage::ID: msg = BtUnchokeMessage::create(data, dataLength); break; case BtInterestedMessage::ID: msg = BtInterestedMessage::create(data, dataLength); break; case BtNotInterestedMessage::ID: { SharedHandle m = BtNotInterestedMessage::create(data, dataLength); m->setPeerStorage(_peerStorage); msg = m; } break; case BtHaveMessage::ID: msg = BtHaveMessage::create(data, dataLength); { SharedHandle v (new BtHaveMessageValidator((BtHaveMessage*)msg.get(), btContext->getNumPieces())); msg->setBtMessageValidator(v); } break; case BtBitfieldMessage::ID: msg = BtBitfieldMessage::create(data, dataLength); { SharedHandle v (new BtBitfieldMessageValidator((BtBitfieldMessage*)msg.get(), btContext->getNumPieces())); msg->setBtMessageValidator(v); } break; case BtRequestMessage::ID: { BtRequestMessageHandle temp = BtRequestMessage::create(data, dataLength); BtMessageValidatorHandle validator (new BtRequestMessageValidator(temp.get(), btContext->getNumPieces(), _pieceStorage->getPieceLength(temp->getIndex()))); temp->setBtMessageValidator(validator); msg = temp; break; } case BtCancelMessage::ID: { BtCancelMessageHandle temp = BtCancelMessage::create(data, dataLength); BtMessageValidatorHandle validator (new BtCancelMessageValidator(temp.get(), btContext->getNumPieces(), _pieceStorage->getPieceLength(temp->getIndex()))); temp->setBtMessageValidator(validator); msg = temp; break; } case BtPieceMessage::ID: { BtPieceMessageHandle temp = BtPieceMessage::create(data, dataLength); BtMessageValidatorHandle validator (new BtPieceMessageValidator(temp.get(), btContext->getNumPieces(), _pieceStorage->getPieceLength(temp->getIndex()))); temp->setBtMessageValidator(validator); msg = temp; break; } case BtHaveAllMessage::ID: msg = BtHaveAllMessage::create(data, dataLength); break; case BtHaveNoneMessage::ID: msg = BtHaveNoneMessage::create(data, dataLength); break; case BtRejectMessage::ID: { BtRejectMessageHandle temp = BtRejectMessage::create(data, dataLength); BtMessageValidatorHandle validator (new BtRejectMessageValidator(temp.get(), btContext->getNumPieces(), _pieceStorage->getPieceLength(temp->getIndex()))); temp->setBtMessageValidator(validator); msg = temp; break; } case BtSuggestPieceMessage::ID: { BtSuggestPieceMessageHandle temp = BtSuggestPieceMessage::create(data, dataLength); BtMessageValidatorHandle validator (new BtSuggestPieceMessageValidator(temp.get(), btContext->getNumPieces())); temp->setBtMessageValidator(validator); msg = temp; break; } case BtAllowedFastMessage::ID: { BtAllowedFastMessageHandle temp = BtAllowedFastMessage::create(data, dataLength); BtMessageValidatorHandle validator (new BtAllowedFastMessageValidator(temp.get(), btContext->getNumPieces())); temp->setBtMessageValidator(validator); msg = temp; break; } case BtPortMessage::ID: { SharedHandle temp = BtPortMessage::create(data, dataLength); temp->setLocalNode(_localNode); temp->setRoutingTable(_routingTable); temp->setTaskQueue(_taskQueue); temp->setTaskFactory(_taskFactory); msg = temp; break; } case BtExtendedMessage::ID: { if(peer->isExtendedMessagingEnabled()) { msg = BtExtendedMessage::create(_extensionMessageFactory, peer, data, dataLength); } else { throw DlAbortEx("Received extended message from peer during a session with extended messaging disabled."); } break; } default: throw DlAbortEx(StringFormat("Invalid message ID. id=%u", id).str()); } } setCommonProperty(msg); return msg; } void DefaultBtMessageFactory::setCommonProperty(const AbstractBtMessageHandle& msg) { msg->setCuid(cuid); msg->setPeer(peer); msg->setBtContext(btContext); msg->setPieceStorage(_pieceStorage); msg->setBtMessageDispatcher(dispatcher); msg->setBtRequestFactory(requestFactory); msg->setBtMessageFactory(WeakHandle(this)); msg->setPeerConnection(peerConnection); } SharedHandle DefaultBtMessageFactory::createHandshakeMessage(const unsigned char* data, size_t dataLength) { SharedHandle msg = BtHandshakeMessage::create(data, dataLength); BtMessageValidatorHandle validator (new BtHandshakeMessageValidator(msg.get(), btContext->getInfoHash())); msg->setBtMessageValidator(validator); setCommonProperty(msg); return msg; } SharedHandle DefaultBtMessageFactory::createHandshakeMessage(const unsigned char* infoHash, const unsigned char* peerId) { SharedHandle msg(new BtHandshakeMessage(infoHash, peerId)); BtMessageValidatorHandle validator (new BtHandshakeMessageValidator(msg.get(), btContext->getInfoHash())); msg->setBtMessageValidator(validator); msg->setDHTEnabled(_dhtEnabled); setCommonProperty(msg); return msg; } BtMessageHandle DefaultBtMessageFactory::createRequestMessage(const PieceHandle& piece, size_t blockIndex) { BtRequestMessageHandle msg (new BtRequestMessage(piece->getIndex(), blockIndex*piece->getBlockLength(), piece->getBlockLength(blockIndex), blockIndex)); BtMessageValidatorHandle validator (new BtRequestMessageValidator(msg.get(), btContext->getNumPieces(), _pieceStorage->getPieceLength(msg->getIndex()))); msg->setBtMessageValidator(validator); setCommonProperty(msg); return msg; } BtMessageHandle DefaultBtMessageFactory::createCancelMessage(size_t index, uint32_t begin, size_t length) { BtCancelMessageHandle msg(new BtCancelMessage(index, begin, length)); BtMessageValidatorHandle validator (new BtCancelMessageValidator(msg.get(), btContext->getNumPieces(), _pieceStorage->getPieceLength(index))); msg->setBtMessageValidator(validator); setCommonProperty(msg); return msg; } BtMessageHandle DefaultBtMessageFactory::createPieceMessage(size_t index, uint32_t begin, size_t length) { BtPieceMessageHandle msg(new BtPieceMessage(index, begin, length)); BtMessageValidatorHandle validator (new BtPieceMessageValidator(msg.get(), btContext->getNumPieces(), _pieceStorage->getPieceLength(index))); msg->setBtMessageValidator(validator); setCommonProperty(msg); return msg; } BtMessageHandle DefaultBtMessageFactory::createHaveMessage(size_t index) { BtHaveMessageHandle msg(new BtHaveMessage(index)); SharedHandle v (new BtHaveMessageValidator(msg.get(), btContext->getNumPieces())); msg->setBtMessageValidator(v); setCommonProperty(msg); return msg; } BtMessageHandle DefaultBtMessageFactory::createChokeMessage() { BtChokeMessageHandle msg(new BtChokeMessage()); setCommonProperty(msg); return msg; } BtMessageHandle DefaultBtMessageFactory::createUnchokeMessage() { BtUnchokeMessageHandle msg(new BtUnchokeMessage()); setCommonProperty(msg); return msg; } BtMessageHandle DefaultBtMessageFactory::createInterestedMessage() { BtInterestedMessageHandle msg(new BtInterestedMessage()); setCommonProperty(msg); return msg; } BtMessageHandle DefaultBtMessageFactory::createNotInterestedMessage() { BtNotInterestedMessageHandle msg(new BtNotInterestedMessage()); setCommonProperty(msg); return msg; } BtMessageHandle DefaultBtMessageFactory::createBitfieldMessage() { BtBitfieldMessageHandle msg (new BtBitfieldMessage(_pieceStorage->getBitfield(), _pieceStorage->getBitfieldLength())); SharedHandle v (new BtBitfieldMessageValidator(msg.get(), btContext->getNumPieces())); msg->setBtMessageValidator(v); setCommonProperty(msg); return msg; } BtMessageHandle DefaultBtMessageFactory::createKeepAliveMessage() { BtKeepAliveMessageHandle msg(new BtKeepAliveMessage()); setCommonProperty(msg); return msg; } BtMessageHandle DefaultBtMessageFactory::createHaveAllMessage() { BtHaveAllMessageHandle msg(new BtHaveAllMessage()); setCommonProperty(msg); return msg; } BtMessageHandle DefaultBtMessageFactory::createHaveNoneMessage() { BtHaveNoneMessageHandle msg(new BtHaveNoneMessage()); setCommonProperty(msg); return msg; } BtMessageHandle DefaultBtMessageFactory::createRejectMessage(size_t index, uint32_t begin, size_t length) { BtRejectMessageHandle msg(new BtRejectMessage(index, begin, length)); BtMessageValidatorHandle validator (new BtRejectMessageValidator(msg.get(), btContext->getNumPieces(), _pieceStorage->getPieceLength(index))); msg->setBtMessageValidator(validator); setCommonProperty(msg); return msg; } BtMessageHandle DefaultBtMessageFactory::createAllowedFastMessage(size_t index) { BtAllowedFastMessageHandle msg(new BtAllowedFastMessage(index)); BtMessageValidatorHandle validator (new BtAllowedFastMessageValidator(msg.get(), btContext->getNumPieces())); msg->setBtMessageValidator(validator); setCommonProperty(msg); return msg; } BtMessageHandle DefaultBtMessageFactory::createPortMessage(uint16_t port) { SharedHandle msg(new BtPortMessage(port)); setCommonProperty(msg); return msg; } BtMessageHandle DefaultBtMessageFactory::createBtExtendedMessage(const ExtensionMessageHandle& msg) { BtExtendedMessageHandle m(new BtExtendedMessage(msg)); setCommonProperty(m); return m; } void DefaultBtMessageFactory::setTaskQueue(const WeakHandle& taskQueue) { _taskQueue = taskQueue; } void DefaultBtMessageFactory::setTaskFactory(const WeakHandle& taskFactory) { _taskFactory = taskFactory; } void DefaultBtMessageFactory::setPeer(const SharedHandle& peer) { this->peer = peer; } void DefaultBtMessageFactory::setBtContext(const SharedHandle& btContext) { this->btContext = btContext; } void DefaultBtMessageFactory::setPieceStorage (const SharedHandle& pieceStorage) { _pieceStorage = pieceStorage; } void DefaultBtMessageFactory::setPeerStorage (const SharedHandle& peerStorage) { _peerStorage = peerStorage; } void DefaultBtMessageFactory::setBtMessageDispatcher(const WeakHandle& dispatcher) { this->dispatcher = dispatcher; } void DefaultBtMessageFactory::setExtensionMessageFactory (const SharedHandle& factory) { _extensionMessageFactory = factory; } void DefaultBtMessageFactory::setLocalNode(const WeakHandle& localNode) { _localNode = localNode; } void DefaultBtMessageFactory::setRoutingTable(const WeakHandle& routingTable) { _routingTable = routingTable; } void DefaultBtMessageFactory::setBtRequestFactory(const WeakHandle& factory) { this->requestFactory = factory; } void DefaultBtMessageFactory::setPeerConnection(const WeakHandle& connection) { this->peerConnection = connection; } } // namespace aria2