← Back to team overview

linuxdcpp-team team mailing list archive

[Branch ~dcplusplus-team/dcplusplus/trunk] Rev 3194: Actions on search results happen on the correct hub

 

------------------------------------------------------------
revno: 3194
committer: poy <poy@xxxxxxxxxx>
branch nick: trunk
timestamp: Mon 2013-01-28 22:32:53 +0100
message:
  Actions on search results happen on the correct hub
modified:
  changelog.txt
  dcpp/AdcHub.cpp
  dcpp/ClientManager.cpp
  dcpp/QueueManager.cpp
  dcpp/SearchManager.cpp
  dcpp/SearchResult.cpp
  dcpp/SearchResult.h
  win32/SearchFrame.cpp


--
lp:dcplusplus
https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk

Your team Dcplusplus-team is subscribed to branch lp:dcplusplus.
To unsubscribe from this branch go to https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk/+edit-subscription
=== modified file 'changelog.txt'
--- changelog.txt	2013-01-28 19:41:11 +0000
+++ changelog.txt	2013-01-28 21:32:53 +0000
@@ -26,6 +26,7 @@
 * Satisfy some boost lockfree requirements, could fix bugs on heavy load
 * Clean up earlier after receiving zlib data (thanks irainman)
 * [ADC] Send passive search replies via the hub they were requested from (poy)
+* [ADC] Actions on search results happen on the correct hub (poy)
 
 Note: The hash registry will be upgraded when running this version for the
 first time. Make sure all your drives are connected to avoid re-hashing.

=== modified file 'dcpp/AdcHub.cpp'
--- dcpp/AdcHub.cpp	2013-01-28 19:41:11 +0000
+++ dcpp/AdcHub.cpp	2013-01-28 21:32:53 +0000
@@ -811,8 +811,10 @@
 
 	AdcCommand c(AdcCommand::CMD_SCH, AdcCommand::TYPE_BROADCAST);
 
-	if(!aToken.empty())
-		c.addParam("TO", aToken);
+	/* token format: [pointer to this class] "/" [actual token]
+	this allows easily knowing which hub a search was sent on when parsing a search result,
+	whithout having to bother maintaining a list of sent tokens. */
+	c.addParam("TO", Util::toString(reinterpret_cast<uintptr_t>(this)) + "/" + aToken);
 
 	if(aFileType == SearchManager::TYPE_TTH) {
 		c.addParam("TR", aString);

=== modified file 'dcpp/ClientManager.cpp'
--- dcpp/ClientManager.cpp	2013-01-28 19:41:11 +0000
+++ dcpp/ClientManager.cpp	2013-01-28 21:32:53 +0000
@@ -413,12 +413,7 @@
 
 void ClientManager::userCommand(const HintedUser& user, const UserCommand& uc, ParamMap& params, bool compatibility) {
 	Lock l(cs);
-	/** @todo we allow wrong hints for now because users
-	 * extracted from search results don't always have a correct hint; see
-	 * SearchManager::onRES(const AdcCommand& cmd, ...). when that is done, and SearchResults are
-	 * switched to storing only reliable HintedUsers (found with the token of the ADC command),
-	 * change this call to findOnlineUserHint. */
-	OnlineUser* ou = findOnlineUser(user.user->getCID(), user.hint.empty() ? uc.getHub() : user.hint);
+	OnlineUser* ou = findOnlineUserHint(user.user->getCID(), user.hint.empty() ? uc.getHub() : user.hint);
 	if(!ou)
 		return;
 

=== modified file 'dcpp/QueueManager.cpp'
--- dcpp/QueueManager.cpp	2013-01-27 17:45:34 +0000
+++ dcpp/QueueManager.cpp	2013-01-28 21:32:53 +0000
@@ -1535,7 +1535,7 @@
 			if(qi->getSize() == sr->getSize() && !qi->isSource(sr->getUser()) && !qi->isBadSource(sr->getUser())) {
 				try {
 					if(!SETTING(AUTO_SEARCH_AUTO_MATCH))
-						wantConnection = addSource(qi, HintedUser(sr->getUser(), sr->getHubURL()), 0);
+						wantConnection = addSource(qi, sr->getUser(), 0);
 					added = true;
 				} catch(const Exception&) {
 					// ...
@@ -1547,15 +1547,14 @@
 
 	if(added && SETTING(AUTO_SEARCH_AUTO_MATCH)) {
 		try {
-			addList(HintedUser(sr->getUser(), sr->getHubURL()), QueueItem::FLAG_MATCH_QUEUE);
+			addList(sr->getUser(), QueueItem::FLAG_MATCH_QUEUE);
 		} catch(const Exception&) {
 			// ...
 		}
 	}
-	if(added && sr->getUser()->isOnline() && wantConnection) {
-		ConnectionManager::getInstance()->getDownloadConnection(HintedUser(sr->getUser(), sr->getHubURL()));
+	if(added && sr->getUser().user->isOnline() && wantConnection) {
+		ConnectionManager::getInstance()->getDownloadConnection(sr->getUser());
 	}
-
 }
 
 // ClientManagerListener

=== modified file 'dcpp/SearchManager.cpp'
--- dcpp/SearchManager.cpp	2013-01-28 19:41:11 +0000
+++ dcpp/SearchManager.cpp	2013-01-28 21:32:53 +0000
@@ -259,10 +259,9 @@
 			return;
 		}
 
-
-		SearchResultPtr sr(new SearchResult(user, type, slots, freeSlots, size,
-			file, hubName, url, remoteIp, TTHValue(tth), Util::emptyString));
-		fire(SearchManagerListener::SR(), sr);
+		fire(SearchManagerListener::SR(), SearchResultPtr(new SearchResult(HintedUser(user, url),
+			type, slots, freeSlots, size, file, hubName, remoteIp, TTHValue(tth),
+			Util::emptyString)));
 
 	} else if(x.compare(1, 4, "RES ") == 0 && x[x.length() - 1] == 0x0a) {
 		AdcCommand c(x.substr(0, x.length()-1));
@@ -310,22 +309,31 @@
 		}
 	}
 
-	if(!file.empty() && freeSlots != -1 && size != -1) {
-
-		/// @todo get the hub this was sent from, to be passed as a hint? (eg by using the token?)
-		StringList names = ClientManager::getInstance()->getHubNames(from->getCID());
-		string hubName = names.empty() ? _("Offline") : Util::toString(names);
-		StringList hubs = ClientManager::getInstance()->getHubUrls(from->getCID());
-		string hub = hubs.empty() ? _("Offline") : Util::toString(hubs);
-
-		SearchResult::Types type = (file[file.length() - 1] == '\\' ? SearchResult::TYPE_DIRECTORY : SearchResult::TYPE_FILE);
-		if(type == SearchResult::TYPE_FILE && tth.empty())
-			return;
-		/// @todo Something about the slots
-		SearchResultPtr sr(new SearchResult(from, type, 0, freeSlots, size,
-			file, hubName, hub, remoteIp, TTHValue(tth), token));
-		fire(SearchManagerListener::SR(), sr);
+	if(file.empty() || freeSlots == -1 || size == -1) { return; }
+
+	auto type = (*(file.end() - 1) == '\\' ? SearchResult::TYPE_DIRECTORY : SearchResult::TYPE_FILE);
+	if(type == SearchResult::TYPE_FILE && tth.empty()) { return; }
+
+	string hubUrl;
+
+	// token format: [pointer to the client class] "/" [actual token] (see AdcHub::search)
+	auto slash = token.find('/');
+	if(slash == string::npos) { return; }
+	{
+		auto lock = ClientManager::getInstance()->lock();
+		auto& clients = ClientManager::getInstance()->getClients();
+		auto i = clients.find(reinterpret_cast<Client*>(Util::toInt64(token.substr(0, slash))));
+		if(i == clients.end()) { return; }
+		hubUrl = (*i)->getHubUrl();
 	}
+	token.erase(0, slash + 1);
+
+	StringList names = ClientManager::getInstance()->getHubNames(from->getCID(), hubUrl);
+	string hubName = names.empty() ? _("Offline") : Util::toString(names);
+
+	/// @todo Something about the slots
+	fire(SearchManagerListener::SR(), SearchResultPtr(new SearchResult(HintedUser(from, hubUrl),
+		type, 0, freeSlots, size, file, hubName, remoteIp, TTHValue(tth), token)));
 }
 
 void SearchManager::respond(const AdcCommand& cmd, const OnlineUser& user) {

=== modified file 'dcpp/SearchResult.cpp'
--- dcpp/SearchResult.cpp	2013-01-18 21:28:38 +0000
+++ dcpp/SearchResult.cpp	2013-01-28 21:32:53 +0000
@@ -27,16 +27,16 @@
 
 namespace dcpp {
 
-SearchResult::SearchResult(const UserPtr& aUser, Types aType, int aSlots, int aFreeSlots,
+SearchResult::SearchResult(const HintedUser& aUser, Types aType, int aSlots, int aFreeSlots,
 	int64_t aSize, const string& aFile, const string& aHubName,
-	const string& aHubURL, const string& ip, TTHValue aTTH, const string& aToken) :
-file(aFile), hubName(aHubName), hubURL(aHubURL), user(aUser),
+	const string& ip, TTHValue aTTH, const string& aToken) :
+file(aFile), hubName(aHubName), user(aUser),
 	size(aSize), type(aType), slots(aSlots), freeSlots(aFreeSlots), IP(ip),
 	tth(aTTH), token(aToken) { }
 
 SearchResult::SearchResult(Types aType, int64_t aSize, const string& aFile, const TTHValue& aTTH) :
-	file(aFile), user(ClientManager::getInstance()->getMe()), size(aSize), type(aType), slots(SETTING(SLOTS)),
-	freeSlots(UploadManager::getInstance()->getFreeSlots()),
+	file(aFile), user(ClientManager::getInstance()->getMe(), Util::emptyString), size(aSize),
+	type(aType), slots(SETTING(SLOTS)), freeSlots(UploadManager::getInstance()->getFreeSlots()),
 	tth(aTTH) { }
 
 string SearchResult::toSR(const Client& c) const {
@@ -90,4 +90,8 @@
 	return getFile().substr(i + 1);
 }
 
+string SearchResult::getSlotString() const {
+	return Util::toString(getFreeSlots()) + '/' + Util::toString(getSlots());
+}
+
 }

=== modified file 'dcpp/SearchResult.h'
--- dcpp/SearchResult.h	2013-01-18 21:28:38 +0000
+++ dcpp/SearchResult.h	2013-01-28 21:32:53 +0000
@@ -20,11 +20,11 @@
 #define DCPLUSPLUS_DCPP_SEARCHRESULT_H
 
 #include "forward.h"
+#include "AdcCommand.h"
 #include "FastAlloc.h"
+#include "HintedUser.h"
 #include "MerkleTree.h"
-#include "AdcCommand.h"
 #include "Pointer.h"
-#include "Util.h"
 
 #include <boost/noncopyable.hpp>
 
@@ -41,24 +41,22 @@
 
 	SearchResult(Types aType, int64_t aSize, const string& name, const TTHValue& aTTH);
 
-	SearchResult(const UserPtr& aUser, Types aType, int aSlots, int aFreeSlots,
+	SearchResult(const HintedUser& aUser, Types aType, int aSlots, int aFreeSlots,
 		int64_t aSize, const string& aFile, const string& aHubName,
-		const string& aHubURL, const string& ip, TTHValue aTTH, const string& aToken);
+		const string& ip, TTHValue aTTH, const string& aToken);
 
-	string getFileName() const;
 	string toSR(const Client& client) const;
 	AdcCommand toRES(char type) const;
 
-	UserPtr& getUser() { return user; }
-	string getSlotString() const { return Util::toString(getFreeSlots()) + '/' + Util::toString(getSlots()); }
-
 	const string& getFile() const { return file; }
-	const string& getHubURL() const { return hubURL; }
+	string getFileName() const;
 	const string& getHubName() const { return hubName; }
+	HintedUser& getUser() { return user; }
 	int64_t getSize() const { return size; }
 	Types getType() const { return type; }
 	int getSlots() const { return slots; }
 	int getFreeSlots() const { return freeSlots; }
+	string getSlotString() const;
 	TTHValue getTTH() const { return tth; }
 	const string& getIP() const { return IP; }
 	const string& getToken() const { return token; }
@@ -70,8 +68,7 @@
 
 	string file;
 	string hubName;
-	string hubURL;
-	UserPtr user;
+	HintedUser user;
 	int64_t size;
 	Types type;
 	int slots;

=== modified file 'win32/SearchFrame.cpp'
--- win32/SearchFrame.cpp	2013-01-21 18:43:48 +0000
+++ win32/SearchFrame.cpp	2013-01-28 21:32:53 +0000
@@ -392,7 +392,7 @@
 void SearchFrame::SearchInfo::view() {
 	if(srs[0]->getType() == SearchResult::TYPE_FILE) {
 		QueueManager::getInstance()->add(Util::getTempPath() + srs[0]->getFileName(),
-			srs[0]->getSize(), srs[0]->getTTH(), HintedUser(srs[0]->getUser(), srs[0]->getHubURL()),
+			srs[0]->getSize(), srs[0]->getTTH(), srs[0]->getUser(),
 			QueueItem::FLAG_CLIENT_VIEW | QueueItem::FLAG_TEXT);
 	}
 }
@@ -409,7 +409,7 @@
 	for(const auto& sr: si->srs) {
 		total++;
 		try {
-			QueueManager::getInstance()->add(target, sr->getSize(), sr->getTTH(), HintedUser(sr->getUser(), sr->getHubURL()));
+			QueueManager::getInstance()->add(target, sr->getSize(), sr->getTTH(), sr->getUser());
 		} catch(const Exception& e) {
 			ignored++;
 			error = e.getError();
@@ -424,7 +424,7 @@
 	total++;
 	// TODO Add all users...
 	QueueManager::getInstance()->addDirectory(target.empty() ? si->srs[0]->getFile() : target,
-		HintedUser(si->srs[0]->getUser(), si->srs[0]->getHubURL()), Text::fromT(tgt),
+		si->srs[0]->getUser(), Text::fromT(tgt),
 		WinUtil::isShift() ? QueueItem::HIGHEST : QueueItem::DEFAULT);
 }
 
@@ -460,10 +460,10 @@
 	}
 
 	if(firstHubs && hubs.empty()) {
-		hubs = ClientManager::getInstance()->getHubUrls(sr->getUser()->getCID(), sr->getHubURL());
+		hubs = ClientManager::getInstance()->getHubUrls(sr->getUser());
 		firstHubs = false;
 	} else if(!hubs.empty()) {
-		Util::intersect(hubs, ClientManager::getInstance()->getHubUrls(sr->getUser()->getCID(), sr->getHubURL()));
+		Util::intersect(hubs, ClientManager::getInstance()->getHubUrls(sr->getUser()));
 	}
 }
 
@@ -510,14 +510,14 @@
 		}
 		columns[COLUMN_HUB] = Text::toT(Util::toString(StringList(hubs.begin(), hubs.end())));
 	} else {
-		columns[COLUMN_NICK] = WinUtil::getNicks(sr->getUser(), sr->getHubURL());
-		columns[COLUMN_CONNECTION] = Text::toT(ClientManager::getInstance()->getConnection(sr->getUser()->getCID()));
+		columns[COLUMN_NICK] = WinUtil::getNicks(sr->getUser());
+		columns[COLUMN_CONNECTION] = Text::toT(ClientManager::getInstance()->getConnection(sr->getUser().user->getCID()));
 		columns[COLUMN_SLOTS] = Text::toT(sr->getSlotString());
 		const auto& ip = sr->getIP();
 		const auto& country = GeoManager::getInstance()->getCountry(ip);
 		columns[COLUMN_IP] = Text::toT(country.empty() ? ip : str(F_("%1% (%2%)") % country % ip));
 		columns[COLUMN_HUB] = Text::toT(sr->getHubName());
-		columns[COLUMN_CID] = Text::toT(sr->getUser()->getCID().toBase32());
+		columns[COLUMN_CID] = Text::toT(sr->getUser().user->getCID().toBase32());
 	}
 }
 
@@ -528,7 +528,7 @@
 	if(currentSearch.empty()) {
 		return;
 	}
-	if(!sr.getToken().empty() && sr.getToken() != token) {
+	if(sr.getToken() != token) {
 		addDropped();
 		return;
 	}
@@ -569,7 +569,7 @@
 		for(auto& j: si2.srs) {
 			auto& sr2 = *j;
 
-			bool sameUser = sr.getUser()->getCID() == sr2.getUser()->getCID();
+			bool sameUser = sr.getUser() == sr2.getUser();
 			if(sameUser && sr.getFile() == sr2.getFile()) {
 				return; // dupe
 			}
@@ -765,7 +765,7 @@
 	void operator()(T* si) {
 		for(const auto& sr: si->srs) {
 			if(std::find(users.begin(), users.end(), sr->getUser()) == users.end()) {
-				users.emplace_back(sr->getUser(), sr->getHubURL());
+				users.emplace_back(sr->getUser());
 				dirs.push_back(Util::getFilePath(sr->getFile()));
 			}
 		}
@@ -1066,13 +1066,13 @@
 		auto si = results->getData(sel);
 		for(auto sr: si->srs) {
 
-			if(!sr->getUser()->isOnline())
+			if(!sr->getUser().user->isOnline())
 				continue;
 
 			if(uc.once()) {
-				if(users.find(sr->getUser()->getCID()) != users.end())
+				if(users.find(sr->getUser().user->getCID()) != users.end())
 					continue;
-				users.insert(sr->getUser()->getCID());
+				users.insert(sr->getUser().user->getCID());
 			}
 
 			ucParams["fileFN"] = [sr] { return sr->getFile(); };
@@ -1090,7 +1090,7 @@
 			ucParams["tth"] = ucParams["fileTR"];
 
 			auto tmp = ucParams;
-			ClientManager::getInstance()->userCommand(HintedUser(sr->getUser(), sr->getHubURL()), uc, tmp, true);
+			ClientManager::getInstance()->userCommand(sr->getUser(), uc, tmp, true);
 		}
 	}
 }