From: <arn...@us...> - 2007-11-18 19:04:50
|
Revision: 93 http://adchpp.svn.sourceforge.net/adchpp/?rev=93&view=rev Author: arnetheduck Date: 2007-11-18 11:04:42 -0800 (Sun, 18 Nov 2007) Log Message: ----------- Experimental Bloom filter plugin Added Paths: ----------- adchpp/trunk/plugins/Bloom/ adchpp/trunk/plugins/Bloom/SConscript adchpp/trunk/plugins/Bloom/src/ adchpp/trunk/plugins/Bloom/src/BloomManager.cpp adchpp/trunk/plugins/Bloom/src/BloomManager.h adchpp/trunk/plugins/Bloom/src/BloomPlugin.cpp adchpp/trunk/plugins/Bloom/src/HashBloom.h adchpp/trunk/plugins/Bloom/src/HashValue.h adchpp/trunk/plugins/Bloom/src/SConscript adchpp/trunk/plugins/Bloom/src/stdinc.cpp adchpp/trunk/plugins/Bloom/src/stdinc.h Added: adchpp/trunk/plugins/Bloom/SConscript =================================================================== --- adchpp/trunk/plugins/Bloom/SConscript (rev 0) +++ adchpp/trunk/plugins/Bloom/SConscript 2007-11-18 19:04:42 UTC (rev 93) @@ -0,0 +1,5 @@ +Import('dev source_path') + +ret = dev.build('src/') + +Return('ret') Added: adchpp/trunk/plugins/Bloom/src/BloomManager.cpp =================================================================== --- adchpp/trunk/plugins/Bloom/src/BloomManager.cpp (rev 0) +++ adchpp/trunk/plugins/Bloom/src/BloomManager.cpp 2007-11-18 19:04:42 UTC (rev 93) @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2006-2007 Jacek Sieka, arnetheduck on gmail point com + * + * 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "stdinc.h" +#include "BloomManager.h" + +#include <adchpp/LogManager.h> + +using namespace std; +using namespace std::tr1::placeholders; + +BloomManager* BloomManager::instance = 0; +const string BloomManager::className = "BloomManager"; + +BloomManager::BloomManager() { + LOGDT(className, "Starting"); + ClientManager* cm = ClientManager::getInstance(); + receiveConn = manage(&cm->signalReceive(), std::tr1::bind(&BloomManager::onReceive, this, _1, _2, _3)); + disconnectConn = manage(&cm->signalDisconnected(), std::tr1::bind(&BloomManager::onDisconnected, this, _1)); +} + +BloomManager::~BloomManager() { + LOGDT(className, "Shutting down"); +} + +static const std::string FEATURE = "BLOM"; + +void BloomManager::onReceive(Client& c, AdcCommand& cmd, int& override) { + std::string tth; + + if(cmd.getCommand() == AdcCommand::CMD_INF && c.supports(FEATURE)) { + AdcCommand get(AdcCommand::CMD_GET); + get.addParam("blom"); + get.addParam("/"); + get.addParam("0"); + get.addParam("-1"); + c.send(get); + } else if(cmd.getCommand() == AdcCommand::CMD_SND) { + if(cmd.getParameters().size() < 4) { + return; + } + if(cmd.getParam(0) != "blom") { + return; + } + + int64_t bytes = Util::toInt(cmd.getParam(4)); + + c.setDataMode(std::tr1::bind(&BloomManager::onData, this, _1, _2, _3), bytes); + } else if(cmd.getCommand() == AdcCommand::CMD_SCH && cmd.getParam("TH", 0, tth)) { + + BloomMap::const_iterator i = blooms.find(c.getCID()); + if(i != blooms.end() && i->second.match(TTHValue(tth))) { + // Stop it + override |= ClientManager::DONT_DISPATCH | ClientManager::DONT_SEND; + } + } +} + +void BloomManager::onData(Client& c, const uint8_t* data, size_t len) { + HashBloom& bloom = blooms[c.getCID()]; + for(size_t i = 0; i < len; ++i) { + for(size_t j = 0; j < 8; ++j) { + bloom.push_back(data[i] & 1 << j); + } + } +} + +void BloomManager::onDisconnected(Client& c) { + blooms.erase(c.getCID()); +} + Added: adchpp/trunk/plugins/Bloom/src/BloomManager.h =================================================================== --- adchpp/trunk/plugins/Bloom/src/BloomManager.h (rev 0) +++ adchpp/trunk/plugins/Bloom/src/BloomManager.h 2007-11-18 19:04:42 UTC (rev 93) @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2006-2007 Jacek Sieka, arnetheduck on gmail point com + * + * 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef BLOOM_MANAGER_H +#define BLOOM_MANAGER_H + +#if _MSC_VER > 1000 +#pragma once +#endif // _MSC_VER > 1000 + +#include <adchpp/Exception.h> +#include <adchpp/Singleton.h> +#include <adchpp/ClientManager.h> + +#include "HashBloom.h" + +#ifdef _WIN32 +# ifdef BLOOM_EXPORT +# define BLOOM_DLL __declspec(dllexport) +# else +# define BLOOM_DLL __declspec(dllimport) +# endif +#else +# define BLOOM_DLL +#endif + +STANDARD_EXCEPTION(BloomException); +class Engine; + +namespace adchpp { +class SimpleXML; +class Client; +class AdcCommand; +} + +class BloomManager : public Singleton<BloomManager> { +public: + BloomManager(); + virtual ~BloomManager(); + + virtual int getVersion() { return 0; } + + void onReceive(Client& c, AdcCommand& cmd, int&); + void onData(Client& c, const uint8_t* data, size_t len); + void onDisconnected(Client& c); + + static const std::string className; +private: + friend class Singleton<BloomManager>; + static BloomManager* instance; + + typedef std::tr1::unordered_map<CID, HashBloom, CID::Hash> BloomMap; + BloomMap blooms; + + ClientManager::SignalReceive::ManagedConnection receiveConn; + ClientManager::SignalDisconnected::ManagedConnection disconnectConn; + +}; + +#endif //ACCESSMANAGER_H Added: adchpp/trunk/plugins/Bloom/src/BloomPlugin.cpp =================================================================== --- adchpp/trunk/plugins/Bloom/src/BloomPlugin.cpp (rev 0) +++ adchpp/trunk/plugins/Bloom/src/BloomPlugin.cpp 2007-11-18 19:04:42 UTC (rev 93) @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2006-2007 Jacek Sieka, arnetheduck on gmail point com + * + * 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "stdinc.h" +#include "BloomManager.h" + +#include <adchpp/PluginManager.h> + +#ifdef _WIN32 + +BOOL APIENTRY DllMain(HANDLE /*hModule */, DWORD /* reason*/, LPVOID /*lpReserved*/) { + return TRUE; +} + +#endif + +extern "C" { +int PLUGIN_API pluginGetVersion() { return PLUGINVERSION; } + +int PLUGIN_API pluginLoad() { + BloomManager::newInstance(); + return 0; +} + +void PLUGIN_API pluginUnload() { + BloomManager::deleteInstance(); +} +} + Added: adchpp/trunk/plugins/Bloom/src/HashBloom.h =================================================================== --- adchpp/trunk/plugins/Bloom/src/HashBloom.h (rev 0) +++ adchpp/trunk/plugins/Bloom/src/HashBloom.h 2007-11-18 19:04:42 UTC (rev 93) @@ -0,0 +1,21 @@ +#ifndef HASHBLOOM_H_ +#define HASHBLOOM_H_ + +#include "HashValue.h" + +class HashBloom { +public: + void add(const TTHValue& tth) { bloom[pos(tth)] = true; } + bool match(const TTHValue& tth) const { return bloom[pos(tth)]; } + void resize(size_t hashes) { bloom.resize(hashes); std::fill(bloom.begin(), bloom.end(), false); } + void push_back(bool v) { bloom.push_back(v); } +private: + + size_t pos(const TTHValue& tth) const { + return (*(size_t*)tth.data) % bloom.size(); + } + + std::vector<bool> bloom; +}; + +#endif /*HASHBLOOM_H_*/ Added: adchpp/trunk/plugins/Bloom/src/HashValue.h =================================================================== --- adchpp/trunk/plugins/Bloom/src/HashValue.h (rev 0) +++ adchpp/trunk/plugins/Bloom/src/HashValue.h 2007-11-18 19:04:42 UTC (rev 93) @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2001-2006 Jacek Sieka, arnetheduck on gmail point com + * + * 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#if !defined(HASH_VALUE_H) +#define HASH_VALUE_H + +#include <adchpp/TigerHash.h> + +template<class Hasher> +struct HashValue { + static const size_t SIZE = Hasher::HASH_SIZE; + + struct Hash { +#ifdef _MSC_VER + static const size_t bucket_size = 4; + static const size_t min_buckets = 8; +#endif + size_t operator()(const HashValue& rhs) const { return *(size_t*)&rhs; } + bool operator()(const HashValue& a, const HashValue& b) const { return a < b; } + }; + + HashValue() { } + explicit HashValue(uint8_t* aData) { memcpy(data, aData, SIZE); } + explicit HashValue(const std::string& base32) { Encoder::fromBase32(base32.c_str(), data, SIZE); } + HashValue(const HashValue& rhs) { memcpy(data, rhs.data, SIZE); } + HashValue& operator=(const HashValue& rhs) { memcpy(data, rhs.data, SIZE); return *this; } + bool operator!=(const HashValue& rhs) const { return !(*this == rhs); } + bool operator==(const HashValue& rhs) const { return memcmp(data, rhs.data, SIZE) == 0; } + bool operator<(const HashValue& rhs) const { return memcmp(data, rhs.data, SIZE) < 0; } + + std::string toBase32() const { return Encoder::toBase32(data, SIZE); } + std::string& toBase32(std::string& tmp) const { return Encoder::toBase32(data, SIZE, tmp); } + + uint8_t data[SIZE]; +}; + +typedef HashValue<TigerHash> TTHValue; + +#endif // !defined(HASH_VALUE_H) Added: adchpp/trunk/plugins/Bloom/src/SConscript =================================================================== --- adchpp/trunk/plugins/Bloom/src/SConscript (rev 0) +++ adchpp/trunk/plugins/Bloom/src/SConscript 2007-11-18 19:04:42 UTC (rev 93) @@ -0,0 +1,14 @@ +Import('dev source_path') + +env, target, sources = dev.prepare_build(source_path, 'Bloom') +env['SHLIBPREFIX'] = '' + +if 'gcc' in env['TOOLS']: + env['GchSh'] = env.GchSh('stdinc.h')[0] + +env.Append(CPPPATH = ['#']) +env.Append(LIBS = ['adchpp']) + +ret = env.SharedLibrary(target, sources) + +Return('ret') Added: adchpp/trunk/plugins/Bloom/src/stdinc.cpp =================================================================== --- adchpp/trunk/plugins/Bloom/src/stdinc.cpp (rev 0) +++ adchpp/trunk/plugins/Bloom/src/stdinc.cpp 2007-11-18 19:04:42 UTC (rev 93) @@ -0,0 +1,19 @@ +/* + * Copyright (C) 2006-2007 Jacek Sieka, arnetheduck on gmail point com + * + * 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "stdinc.h" Added: adchpp/trunk/plugins/Bloom/src/stdinc.h =================================================================== --- adchpp/trunk/plugins/Bloom/src/stdinc.h (rev 0) +++ adchpp/trunk/plugins/Bloom/src/stdinc.h 2007-11-18 19:04:42 UTC (rev 93) @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2006-2007 Jacek Sieka, arnetheduck on gmail point com + * + * 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef SCRIPT_STDINC_H +#define SCRIPT_STDINC_H + +#include <adchpp/adchpp.h> +#include <adchpp/common.h> + +using namespace adchpp; + +#endif //ACCESS_STDINC_H This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |