Index: source/ariba/overlay/BaseOverlay.cpp
===================================================================
--- source/ariba/overlay/BaseOverlay.cpp	(revision 10576)
+++ source/ariba/overlay/BaseOverlay.cpp	(revision 10653)
@@ -51,5 +51,4 @@
 
 #include "ariba/overlay/messages/OverlayMsg.h"
-#include "ariba/overlay/messages/DHTMessage.h"
 #include "ariba/overlay/messages/JoinRequest.h"
 #include "ariba/overlay/messages/JoinReply.h"
@@ -66,193 +65,4 @@
 #define visualIdBase		ariba::utility::ServerVis::NETWORK_ID_BASE_COMMUNICATION
 
-class ValueEntry {
-public:
-	ValueEntry( const Data& value ) : ttl(0), last_update(time(NULL)),
-		last_change(time(NULL)), value(value.clone()) {
-	}
-
-	ValueEntry( const ValueEntry& value ) :
-		ttl(value.ttl), last_update(value.last_update),
-		last_change(value.last_change), value(value.value.clone()) {
-	}
-
-	~ValueEntry()  {
-		value.release();
-	}
-
-	void refresh() {
-		last_update = time(NULL);
-	}
-
-	void set_value( const Data& value ) {
-		this->value.release();
-		this->value = value.clone();
-		this->last_change = time(NULL);
-		this->last_update = time(NULL);
-	}
-
-	Data get_value() const {
-		return value;
-	}
-
-	uint16_t get_ttl() const {
-		return ttl;
-	}
-
-	void set_ttl( uint16_t ttl ) {
-		this->ttl = ttl;
-	}
-
-	bool is_ttl_elapsed() const {
-		// is persistent? yes-> always return false
-		if (ttl==0) return false;
-		// return true, if ttl is elapsed
-		return ( difftime( time(NULL), this->last_update ) > ttl );
-	}
-
-private:
-	uint16_t ttl;
-	time_t last_update;
-	time_t last_change;
-	Data value;
-};
-
-class DHTEntry {
-public:
-	Data key;
-	vector<ValueEntry> values;
-
-	vector<Data> get_values() {
-		vector<Data> vect;
-		BOOST_FOREACH( ValueEntry& e, values )
-			vect.push_back( e.get_value() );
-		return vect;
-	}
-
-	void erase_expired_entries() {
-		for (vector<ValueEntry>::iterator i = values.begin();
-				i != values.end(); i++ )
-			if (i->is_ttl_elapsed())
-				i = values.erase(i)-1;
-	}
-};
-
-class DHT {
-public:
-	typedef vector<DHTEntry> Entries;
-	typedef vector<ValueEntry> Values;
-	Entries entries;
-	static const bool verbose = false;
-
-	static bool equals( const Data& lhs, const Data& rhs ) {
-		if (rhs.getLength()!=lhs.getLength()) return false;
-		for (size_t i=0; i<lhs.getLength()/8; i++)
-			if (lhs.getBuffer()[i] != rhs.getBuffer()[i]) return false;
-		return true;
-	}
-
-	void put( const Data& key, const Data& value, uint16_t ttl = 0 ) {
-		cleanup();
-
-		// find entry
-		for (size_t i=0; i<entries.size(); i++) {
-			DHTEntry& entry = entries.at(i);
-
-			// check if key is already known
-			if ( equals(entry.key, key) ) {
-
-				// check if value is already in values list
-				for (size_t j=0; j<entry.values.size(); j++) {
-					// found value already? yes-> refresh ttl
-					if ( equals(entry.values[j].get_value(), value) ) {
-						entry.values[j].refresh();
-						if (verbose)
-							std::cout << "DHT: Republished value. Refreshing value timestamp."
-								<< std::endl;
-						return;
-					}
-				}
-
-				// new value-> add to entry
-				if (verbose)
-					std::cout << "DHT: Added value to "
-						<< " key=" << key << " with value=" << value << std::endl;
-				entry.values.push_back( ValueEntry( value ) );
-				entry.values.back().set_ttl(ttl);
-				return;
-			}
-		}
-
-		// key is unknown-> add key value pair
-		if (verbose)
-			std::cout << "DHT: New key value pair "
-				<< " key=" << key << " with value=" << value << std::endl;
-
-		// add new entry
-		entries.push_back( DHTEntry() );
-		DHTEntry& entry = entries.back();
-		entry.key = key.clone();
-		entry.values.push_back( ValueEntry(value) );
-		entry.values.back().set_ttl(ttl);
-	}
-
-	vector<Data> get( const Data& key ) {
-		cleanup();
-		// find entry
-		for (size_t i=0; i<entries.size(); i++) {
-			DHTEntry& entry = entries.at(i);
-			if ( equals(entry.key,key) )
-				return entry.get_values();
-		}
-		return vector<Data>();
-	}
-
-	bool remove( const Data& key ) {
-		cleanup();
-
-		// find entry
-		for (Entries::iterator i = entries.begin(); i != entries.end(); i++) {
-			DHTEntry& entry = *i;
-
-			// found? yes-> delete entry
-			if ( equals(entry.key, key) ) {
-				entries.erase(i);
-				return true;
-			}
-		}
-		return false;
-	}
-
-	bool remove( const Data& key, const Data& value ) {
-		cleanup();
-		// find entry
-		for (Entries::iterator i = entries.begin(); i != entries.end(); i++) {
-			DHTEntry& entry = *i;
-
-			// found? yes-> try to find value
-			if ( equals(entry.key, key) ) {
-				for (Values::iterator j = entry.values.begin();
-						j != entry.values.end(); j++) {
-
-					// value found? yes-> delete
-					if (equals(j->get_value(), value)) {
-						j = entry.values.erase(j)-1;
-						return true;
-					}
-				}
-			}
-		}
-		return false;
-	}
-
-	void cleanup() {
-		for (Entries::iterator i = entries.begin(); i != entries.end(); i++) {
-			DHTEntry& entry = *i;
-			entry.erase_expired_entries();
-			if (entry.values.size()==0)
-				i = entries.erase(i)-1;
-		}
-	}
-};
 
 // ----------------------------------------------------------------------------
@@ -758,9 +568,7 @@
 			sideport(&SideportListener::DEFAULT), overlayInterface(NULL),
 			counter(0) {
-	initDHT();
 }
 
 BaseOverlay::~BaseOverlay() {
-	destroyDHT();
 }
 
@@ -1078,4 +886,5 @@
 }
 
+
 seqnum_t BaseOverlay::sendMessage(const Message* message,
 		const NodeID& node, const ServiceID& service) {
@@ -1114,4 +923,22 @@
 }
 
+
+NodeID BaseOverlay::sendMessageCloserToNodeID(const Message* message,
+        const NodeID& address, const ServiceID& service) {
+    
+    if ( overlayInterface->isClosestNodeTo(address) )
+    {
+        return NodeID::UNSPECIFIED;
+    }
+        
+    const NodeID& closest_node = overlayInterface->getNextNodeId(address); 
+    
+    if ( closest_node != NodeID::UNSPECIFIED )
+    {
+        seqnum_t seqnum = sendMessage(message, closest_node, service);
+    }
+    
+    return closest_node;  // XXX return seqnum ?? tuple? closest_node via (non const) reference?
+}
 // ----------------------------------------------------------------------------
 
@@ -1831,11 +1658,4 @@
 	overlayMsg->addRouteRecord(nodeId);
 
-	// handle dht messages (do not route)
-	if (overlayMsg->isDHTMessage()) {
-		bool ret = handleDHTMessage(overlayMsg);
-		delete overlayMsg;
-		return ret;
-	}
-
 	// handle signaling messages (do not route!)
 	if (overlayMsg->getType()>=OverlayMsg::typeSignalingStart &&
@@ -1856,11 +1676,4 @@
 		delete overlayMsg;
 		return true;
-	}
-
-	// handle DHT response messages
-	if (overlayMsg->hasTypeMask( OverlayMsg::maskDHTResponse )) {
-		bool ret = handleDHTMessage(overlayMsg);
-		delete overlayMsg;
-		return ret;
 	}
 
@@ -1964,5 +1777,4 @@
 	stabilizeRelays();
 	stabilizeLinks();
-	stabilizeDHT();
 	updateVisual();
 }
@@ -2110,221 +1922,4 @@
 // ----------------------------------------------------------------------------
 
-void BaseOverlay::initDHT() {
-	dht = new DHT();
-	localDHT = new DHT();
-	republishCounter = 0;
-}
-
-void BaseOverlay::destroyDHT() {
-	delete dht;
-	delete localDHT;
-}
-
-/// stabilize DHT state
-void BaseOverlay::stabilizeDHT() {
-
-	// do refresh every 2 seconds
-	if (republishCounter < 2) {
-		republishCounter++;
-		return;
-	}
-	republishCounter = 0;
-
-	// remove old values from DHT
-	BOOST_FOREACH( DHTEntry& entry, dht->entries ) {
-		// erase old entries
-		entry.erase_expired_entries();
-	}
-
-	// re-publish values-> do not refresh locally stored values
-	BOOST_FOREACH( DHTEntry& entry, localDHT->entries ) {
-		BOOST_FOREACH( ValueEntry& value, entry.values )
-			dhtPut(entry.key, value.get_value(), value.get_ttl(), false, true );
-	}
-}
-
-// handle DHT messages
-bool BaseOverlay::handleDHTMessage( OverlayMsg* msg ) {
-
-	// de-capsulate message
-	logging_debug("Received DHT message");
-	DHTMessage* dhtMsg = msg->decapsulate<DHTMessage>();
-
-	// handle DHT data message
-	if (msg->getType()==OverlayMsg::typeDHTData) {
-		const ServiceID& service = msg->getService();
-		logging_info( "Received DHT data for service " << service.toString() );
-
-		// delegate data message
-		CommunicationListener* lst = getListener(service);
-		if(lst != NULL) lst->onKeyValue(dhtMsg->getKey(), dhtMsg->getValues() );
-		delete dhtMsg;
-		return true;
-	}
-
-	// route message to closest node
-	if (!overlayInterface->isClosestNodeTo(dhtMsg->getHashedKey())) {
-		logging_debug("Routing DHT message to closest node "
-			<< " from " << msg->getSourceNode()
-			<< " to " << dhtMsg->getHashedKey()
-		);
-		dhtSend(msg, dhtMsg->getHashedKey());
-		delete dhtMsg;
-		return true;
-	}
-
-	// now, we are the closest node...
-	switch (msg->getType()) {
-
-	// ----------------------------------------------------------------- put ---
-	case OverlayMsg::typeDHTPut: {
-		logging_debug("DHT-Put: Attempt to store values for key "
-				<< dhtMsg->getKey());
-		if (dhtMsg->doReplace()) {
-			logging_debug("DHT-Put: Attempt to replace key: remove old values first!");
-			dht->remove(dhtMsg->getKey());
-		}
-		BOOST_FOREACH( Data value, dhtMsg->getValues() ) {
-			logging_debug("DHT-Put: Stored value: " << value );
-			dht->put(dhtMsg->getKey(), value, dhtMsg->getTTL() );
-		}
-		break;
-	}
-
-	// ----------------------------------------------------------------- get ---
-	case OverlayMsg::typeDHTGet: {
-		logging_info("DHT-Get: key=" << dhtMsg->getKey() );
-		vector<Data> vect = dht->get(dhtMsg->getKey());
-		BOOST_FOREACH(const Data& d, vect)
-			logging_info("DHT-Get: value=" << d);
-		OverlayMsg omsg(*msg);
-		omsg.swapRoles();
-		omsg.setType(OverlayMsg::typeDHTData);
-		DHTMessage dhtmsg(dhtMsg->getKey(), vect);
-		omsg.encapsulate(&dhtmsg);
-		
-		logging_info("Sending DHT response to " << omsg.getDestinationNode());
-		sendMessage(&omsg, omsg.getDestinationNode());
-		break;
-	}
-
-	// -------------------------------------------------------------- remove ---
-	case OverlayMsg::typeDHTRemove: {
-		if (dhtMsg->hasValues()) {
-			BOOST_FOREACH( Data value, dhtMsg->getValues() )
-							dht->remove(dhtMsg->getKey(), value );
-		} else
-			dht->remove( dhtMsg->getKey() );
-		break;
-	}
-
-	// -------------------------------------------------------------- default---
-	default:
-		logging_error("DHT Message type unknown.");
-		return false;
-	}
-	delete dhtMsg;
-	return true;
-}
-
-/// put a value to the DHT with a ttl given in seconds
-void BaseOverlay::dhtPut( const Data& key, const Data& value, int ttl, bool replace, bool no_local_refresh ) {
-
-	// log
-	logging_info("DHT-Put:"
-		<< " key=" << key << " value=" << value
-		<< " ttl=" << ttl << " replace=" << replace
-	);
-
-	if (!no_local_refresh) {
-
-		// put into local data store (for refreshes)
-		if (replace) localDHT->remove(key);
-		localDHT->put(key, value, ttl);
-	}
-
-	DHTMessage dhtmsg( key, value );
-	dhtmsg.setReplace( replace );
-	dhtmsg.setTTL(ttl);
-
-	OverlayMsg msg( OverlayMsg::typeDHTPut );
-	msg.encapsulate( &dhtmsg );
-	msg.setSourceNode(this->nodeId);
-	dhtSend(&msg, dhtmsg.getHashedKey());
-}
-
-/// removes a key value pair from the DHT
-void BaseOverlay::dhtRemove( const Data& key, const Data& value ) {
-	// remove from local data store
-	localDHT->remove(key,value);
-
-	DHTMessage dhtmsg(key,value);
-
-	// send message
-	OverlayMsg msg(OverlayMsg::typeDHTRemove);
-	msg.encapsulate( &dhtmsg );
-	msg.setSourceNode(this->nodeId);
-	dhtSend(&msg, dhtmsg.getHashedKey());
-}
-
-/// removes all data stored at the given key
-void BaseOverlay::dhtRemove( const Data& key ) {
-	// log: remove key
-	logging_info("DHT-Remove: Removing key=" << key );
-
-	DHTMessage dhtmsg(key);
-
-	// send message
-	OverlayMsg msg(OverlayMsg::typeDHTRemove);
-	msg.encapsulate( &dhtmsg );
-	msg.setSourceNode(this->nodeId);
-	dhtSend(&msg, dhtmsg.getHashedKey());
-}
-
-/// requests data stored using key
-void BaseOverlay::dhtGet( const Data& key, const ServiceID& service ) {
-	// log: get
-	logging_info("DHT-Get: Trying to resolve key=" <<
-			key << " for service=" << service.toString() );
-
-	DHTMessage dhtmsg(key);
-
-	// send message
-	OverlayMsg msg(OverlayMsg::typeDHTGet);
-	msg.setService(service);
-	msg.encapsulate( &dhtmsg );
-	msg.setSourceNode(this->nodeId);
-	dhtSend(&msg, dhtmsg.getHashedKey());
-}
-
-void BaseOverlay::dhtSend( OverlayMsg* msg, const NodeID& dest ) {
-	// log: dht send
-	logging_info("DHT-Send: Sending message with key=" << dest.toString() );
-
-	// local storage? yes-> put into DHT directly
-	if (overlayInterface->isClosestNodeTo(dest)) {
-		// be compatible with old code so set destination to hashed key
-		msg->setDestinationNode(dest);
-		
-		Data d = data_serialize(msg);
-		Message m2(d);
-		OverlayMsg* m3 = m2.decapsulate<OverlayMsg>();
-		
-		handleDHTMessage(m3);
-		
-		delete m3;
-		return;
-	} else {
-		// need to route
-		NodeID next_hop = overlayInterface->getNextNodeId(dest);
-		msg->setDestinationNode(next_hop);
-		logging_info("DHT-Send: sending via node " << next_hop.toString());
-		
-		send(msg, next_hop);
-		
-		return;
-	}
-}
-
 std::string BaseOverlay::debugInformation() {
 	std::stringstream s;
