/* */ #include "DefaultExtensionMessageFactory.h" #include "Peer.h" #include "DlAbortEx.h" #include "HandshakeExtensionMessage.h" #include "UTPexExtensionMessage.h" #include "LogFactory.h" #include "Logger.h" #include "StringFormat.h" #include "PeerStorage.h" #include "ExtensionMessageRegistry.h" namespace aria2 { DefaultExtensionMessageFactory::DefaultExtensionMessageFactory(): _logger(LogFactory::getInstance()) {} DefaultExtensionMessageFactory::DefaultExtensionMessageFactory (const PeerHandle& peer, const SharedHandle& registry): _peer(peer), _registry(registry), _logger(LogFactory::getInstance()) {} DefaultExtensionMessageFactory::~DefaultExtensionMessageFactory() {} ExtensionMessageHandle DefaultExtensionMessageFactory::createMessage(const unsigned char* data, size_t length) { uint8_t extensionMessageID = *data; if(extensionMessageID == 0) { // handshake HandshakeExtensionMessageHandle m = HandshakeExtensionMessage::create(data, length); m->setPeer(_peer); return m; } else { std::string extensionName = _registry->getExtensionName(extensionMessageID); if(extensionName.empty()) { throw DL_ABORT_EX (StringFormat("No extension registered for extended message ID %u", extensionMessageID).str()); } if(extensionName == "ut_pex") { // uTorrent compatible Peer-Exchange UTPexExtensionMessageHandle m = UTPexExtensionMessage::create(data, length); m->setPeerStorage(_peerStorage); return m; } else { throw DL_ABORT_EX (StringFormat("Unsupported extension message received. extensionMessageID=%u, extensionName=%s", extensionMessageID, extensionName.c_str()).str()); } } } void DefaultExtensionMessageFactory::setPeerStorage (const SharedHandle& peerStorage) { _peerStorage = peerStorage; } void DefaultExtensionMessageFactory::setPeer(const PeerHandle& peer) { _peer = peer; } } // namespace aria2