1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
/*
* Copyright (C) 2009-2010 Big Muscle, http://strongdc.sourceforge.net/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "KBucket.h"
#include "dcpp/CID.h"
#include "dcpp/FastAlloc.h"
#include "dcpp/MerkleTree.h"
#include "dcpp/Singleton.h"
#include "dcpp/TimerManager.h"
#include "dcpp/User.h"
namespace dht
{
struct Search :
public FastAlloc<Search>
{
Search() = default;
~Search();
enum class SearchType : uint8_t { // standard types should match ADC protocol
TYPE_FILE = 1,
TYPE_NODE = 3,
TYPE_STOREFILE = 4
};
Node::Map possibleNodes; // nodes where send search request soon to
Node::Map triedNodes; // nodes where search request has already been sent to
Node::Map respondedNodes; // nodes who responded to this search request
string token; // search identificator
string term; // search term (TTH/CID)
uint64_t lifeTime = 0; // time when this search has been started
int64_t filesize = 0; // file size
SearchType type = SearchType::TYPE_FILE; // search type
bool partial = false; // is this partial file search?
bool stopping = false; // search is being stopped
/** Processes this search request */
void process();
};
class SearchManager :
public Singleton<SearchManager>
{
public:
SearchManager(void);
~SearchManager(void);
/** Performs node lookup in the network */
void findNode(const CID& cid);
/** Performs value lookup in the network */
void findFile(const string& tth, const string& token);
/** Performs node lookup to store key/value pair in the network */
void findStore(const string& tth, int64_t size, bool partial);
/** Process incoming search request */
void processSearchRequest(const Node::Ptr& node, const AdcCommand& cmd);
/** Process incoming search result */
void processSearchResult(const Node::Ptr& node, const AdcCommand& cmd);
/** Processes all running searches and removes long-time ones */
void processSearches();
/** Processes incoming search results */
bool processSearchResults(const UserPtr& user, size_t slots);
private:
/** Running search operations */
typedef std::unordered_map<string*, Search*, CaseStringHash, CaseStringEq> SearchMap;
SearchMap searches;
/** Locks access to "searches" */
CriticalSection cs;
typedef std::unordered_multimap< CID, std::pair<uint64_t, SearchResultPtr> > ResultsMap;
ResultsMap searchResults;
/** Performs general search operation in the network */
void search(Search& s);
/** Sends publishing request */
void publishFile(const Node::Map& nodes, const string& tth, int64_t size, bool partial);
/** Checks whether we are alreading searching for a term */
bool isAlreadySearchingFor(const string& term);
uint64_t lastSearchFile;
};
}
|