Exemple #1
0
int main(int argc, char const *[])
{
	std::string str="123456";
	std::cout<<str<<std::endl;
	typedef std::map<std::string, CacheServer> ServerMap;
	ServerMap servers;
	Consistent::HashRing<std::string,std::string,SdbmHash> ring(4,SdbmHash());

	//Create some cache servers
	servers["cach1.example.com"]=CacheServer();
	servers["cach2.example.com"]=CacheServer();
	servers["cach3.example.com"]=CacheServer();

	//Add their host names to the hash ring
	for (ServerMap::const_iterator it = servers.begin();it != servers.end(); ++it) {
		std::cout << "Adding " << it->first << " with hash " << ring.AddNode(it->first) <<std::endl;
	}

	//Store some data
	const char* fruits[] = {"apple","pear","banana","orange","cherry","apricot"};
	const char* colours[] = {"red","green","yellow","orange","black","pink"};
	const unsigned int numfruits = sizeof(fruits)/sizeof(char*);
	//std::cout<< "sizeof(fruits):" <<sizeof(fruits) << " sizeof (char*):" <<sizeof(char*) <<std::endl; 
	for (unsigned int f = 0; f<numfruits;f++) {
		std::string host = ring.GetNode(fruits[f]);
		std::cout << "Storing " <<fruits[f] << " on server " <<host <<std::endl;
		servers[host].Put(fruits[f],colours[f]);
	}

	//Read it back
	for (unsigned int f = 0; f< numfruits; f++) {
		std::string host = ring.GetNode(fruits[f]);
		std::string colour =servers[host].Get(fruits[f]);
		std::cout << "Found " << fruits[f] << " on server "<< host << " (" <<colour << ")" <<std::endl;
	}
	return 0;
}
Exemple #2
0
	/// Dtor.
	virtual ~ServerManager()
		{ std::for_each (_servermap.begin(), _servermap.end(), [](decltype(_servermap)::value_type& pp){ delete pp.second; }); }