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