NetworkListener::SendStatus NetworkListener::send(Common::MessageHeader::MessageType type, const google::protobuf::Message& message, const Common::Hash& peerID) { if (peerID.isNull()) return this->uDPListener.send(type, message); else return this->uDPListener.send(type, message, peerID); }
void InternalCoreConnection::download(const Common::Hash& peerID, const Protos::Common::Entry& entry, const Common::Hash& sharedFolderID, const QString& path) { // We cannot download our entries. if (peerID == this->getLocalID()) return; Protos::GUI::Download downloadMessage; downloadMessage.mutable_peer_id()->set_hash(peerID.getData(), Common::Hash::HASH_SIZE); downloadMessage.mutable_entry()->CopyFrom(entry); if (!sharedFolderID.isNull()) downloadMessage.mutable_destination_directory_id()->set_hash(sharedFolderID.getData(), Common::Hash::HASH_SIZE); Common::ProtoHelper::setStr(downloadMessage, &Protos::GUI::Download::set_destination_path, path); this->send(Common::MessageHeader::GUI_DOWNLOAD, downloadMessage); }
ChunkDownloader::ChunkDownloader(LinkedPeers& linkedPeers, OccupiedPeers& occupiedPeersDownloadingChunk, Common::TransferRateCalculator& transferRateCalculator, Common::ThreadPool& threadPool, Common::Hash chunkHash) : linkedPeers(linkedPeers), occupiedPeersDownloadingChunk(occupiedPeersDownloadingChunk), transferRateCalculator(transferRateCalculator), threadPool(threadPool), chunkHash(chunkHash), socket(0), downloading(false), closeTheSocket(false), lastTransferStatus(QUEUED), mainThread(QThread::currentThread()), mutex(QMutex::Recursive) { Q_ASSERT(!chunkHash.isNull()); L_DEBU(QString("New ChunkDownloader : %1").arg(this->chunkHash.toStr())); }
void File::populateEntry(Protos::Common::Entry* entry, bool setSharedDir, int maxHashes) const { QMutexLocker locker(&this->mutex); Entry::populateEntry(entry, setSharedDir); entry->set_type(Protos::Common::Entry_Type_FILE); entry->clear_chunk(); int nb = 0; for (QVectorIterator<QSharedPointer<Chunk>> i(this->chunks); i.hasNext();) { Protos::Common::Hash* protoHash = entry->add_chunk(); Common::Hash hash = i.next()->getHash(); if (!hash.isNull() && ++nb <= maxHashes) protoHash->set_hash(hash.getData(), Common::Hash::HASH_SIZE); } }
void PeerMessageSocket::onNewMessage(const Common::Message& message) { switch (message.getHeader().getType()) { case Common::MessageHeader::CORE_GET_ENTRIES: { if (!this->entriesResultsToReceive.isEmpty()) return; const Protos::Core::GetEntries& getEntries = message.getMessage<Protos::Core::GetEntries>(); for (int i = 0; i < getEntries.dirs().entry_size(); i++) { QSharedPointer<FM::IGetEntriesResult> entriesResult = this->fileManager->getScannedEntries(getEntries.dirs().entry(i)); connect(entriesResult.data(), SIGNAL(result(const Protos::Core::GetEntriesResult::EntryResult&)), this, SLOT(entriesResult(const Protos::Core::GetEntriesResult::EntryResult&)), Qt::DirectConnection); connect(entriesResult.data(), SIGNAL(timeout()), this, SLOT(entriesResultTimeout()), Qt::DirectConnection); this->entriesResultsToReceive << entriesResult; this->entriesResultMessage.add_result(); } // Add the root directories if asked. if (getEntries.dirs().entry_size() == 0 || getEntries.get_roots()) this->entriesResultMessage.add_result()->mutable_entries()->CopyFrom(this->fileManager->getEntries()); if (this->entriesResultsToReceive.isEmpty()) this->sendEntriesResultMessage(); else foreach (QSharedPointer<FM::IGetEntriesResult> entriesResult, this->entriesResultsToReceive) entriesResult->start(); } break; case Common::MessageHeader::CORE_GET_ENTRIES_RESULT: this->finished(); break; case Common::MessageHeader::CORE_GET_HASHES: { const Protos::Core::GetHashes& getHashes = message.getMessage<Protos::Core::GetHashes>(); this->currentHashesResult = this->fileManager->getHashes(getHashes.file()); connect(this->currentHashesResult.data(), SIGNAL(nextHash(Protos::Core::HashResult)), this, SLOT(nextAskedHash(Protos::Core::HashResult)), Qt::QueuedConnection); Protos::Core::GetHashesResult res = this->currentHashesResult->start(); this->nbHash = res.nb_hash(); this->send(Common::MessageHeader::CORE_GET_HASHES_RESULT, res); if (res.status() != Protos::Core::GetHashesResult_Status_OK) { this->currentHashesResult.clear(); this->finished(); } } break; case Common::MessageHeader::CORE_GET_HASHES_RESULT: { const Protos::Core::GetHashesResult& getHashesResult = message.getMessage<Protos::Core::GetHashesResult>(); this->nbHash = getHashesResult.nb_hash(); } break; case Common::MessageHeader::CORE_HASH_RESULT: { if (--this->nbHash == 0) this->finished(); } break; case Common::MessageHeader::CORE_GET_CHUNK: { const Protos::Core::GetChunk& getChunkMessage = message.getMessage<Protos::Core::GetChunk>(); const Common::Hash hash(getChunkMessage.chunk().hash()); if (hash.isNull()) { L_WARN("GET_CHUNK: Chunk null"); this->finished(true); break; } // TODO: implements 'GetChunkResult.ALREADY_DOWNLOADING', 'GetChunkResult.TOO_MANY_CONNECTIONS' and 'GetChunkResult.DONT_HAVE_DATA_FROM_OFFSET' QSharedPointer<FM::IChunk> chunk = this->fileManager->getChunk(hash); if (chunk.isNull()) { Protos::Core::GetChunkResult result; result.set_status(Protos::Core::GetChunkResult::DONT_HAVE); this->send(Common::MessageHeader::CORE_GET_CHUNK_RESULT, result); this->finished(); L_WARN(QString("GET_CHUNK: Chunk unknown : %1").arg(hash.toStr())); } else { Protos::Core::GetChunkResult result; result.set_status(Protos::Core::GetChunkResult::OK); result.set_chunk_size(chunk->getKnownBytes()); this->send(Common::MessageHeader::CORE_GET_CHUNK_RESULT, result); this->stopListening(); emit getChunk(chunk, getChunkMessage.offset(), this); } } break; default:; // Do nothing. } }
void Socket::onNewMessage(Common::MessageHeader::MessageType type, const google::protobuf::Message& message) { switch (type) { case Common::MessageHeader::CORE_GET_ENTRIES: { const Protos::Core::GetEntries& getEntries = static_cast<const Protos::Core::GetEntries&>(message); Protos::Core::GetEntriesResult result; for (int i = 0; i < getEntries.dirs().entry_size(); i++) result.add_entries()->CopyFrom(this->fileManager->getEntries(getEntries.dirs().entry(i))); // Add the root directories if asked. if (getEntries.dirs().entry_size() == 0 || getEntries.get_roots()) result.add_entries()->CopyFrom(this->fileManager->getEntries()); this->send(Common::MessageHeader::CORE_GET_ENTRIES_RESULT, result); this->finished(); } break; case Common::MessageHeader::CORE_GET_ENTRIES_RESULT: this->finished(); break; case Common::MessageHeader::CORE_GET_HASHES: { const Protos::Core::GetHashes& getHashes = static_cast<const Protos::Core::GetHashes&>(message); this->currentHashesResult = this->fileManager->getHashes(getHashes.file()); connect(this->currentHashesResult.data(), SIGNAL(nextHash(Common::Hash)), this, SLOT(nextAskedHash(Common::Hash)), Qt::QueuedConnection); Protos::Core::GetHashesResult res = this->currentHashesResult->start(); this->nbHash = res.nb_hash(); this->send(Common::MessageHeader::CORE_GET_HASHES_RESULT, res); if (res.status() != Protos::Core::GetHashesResult_Status_OK) { this->currentHashesResult.clear(); this->finished(); } } break; case Common::MessageHeader::CORE_GET_HASHES_RESULT: { const Protos::Core::GetHashesResult& getHashesResult = static_cast<const Protos::Core::GetHashesResult&>(message); this->nbHash = getHashesResult.nb_hash(); } break; case Common::MessageHeader::CORE_HASH: { if (--this->nbHash == 0) this->finished(); } break; case Common::MessageHeader::CORE_GET_CHUNK: { const Protos::Core::GetChunk& getChunkMessage = static_cast<const Protos::Core::GetChunk&>(message); const Common::Hash hash(getChunkMessage.chunk().hash()); if (hash.isNull()) { L_WARN("GET_CHUNK: Chunk null"); this->finished(ISocket::SFS_ERROR); break; } QSharedPointer<FM::IChunk> chunk = this->fileManager->getChunk(hash); if (chunk.isNull()) { Protos::Core::GetChunkResult result; result.set_status(Protos::Core::GetChunkResult_Status_DONT_HAVE); this->send(Common::MessageHeader::CORE_GET_CHUNK_RESULT, result); this->finished(); L_WARN(QString("GET_CHUNK: Chunk unknown : %1").arg(hash.toStr())); } else { Protos::Core::GetChunkResult result; result.set_status(Protos::Core::GetChunkResult_Status_OK); result.set_chunk_size(chunk->getKnownBytes()); this->send(Common::MessageHeader::CORE_GET_CHUNK_RESULT, result); this->stopListening(); emit getChunk(chunk, getChunkMessage.offset(), this); } } break; default:; // Do nothing. } }