Index: source/ariba/overlay/modules/chord/Chord.cpp
===================================================================
--- source/ariba/overlay/modules/chord/Chord.cpp	(revision 3718)
+++ source/ariba/overlay/modules/chord/Chord.cpp	(revision 5151)
@@ -71,9 +71,14 @@
 
 /// helper: sets up a link using the base overlay
-LinkID Chord::setup(const EndpointDescriptor& endp) {
+LinkID Chord::setup(const EndpointDescriptor& endp, const NodeID& node) {
 
 	logging_debug("request to setup link to " << endp.toString() );
+
+	for (size_t i=0; i<pending.size(); i++)
+		if (pending[i]==node) return LinkID::UNSPECIFIED;
+	pending.push_back(node);
+
 	// establish link via base overlay
-	return baseoverlay.establishLink(endp, OverlayInterface::OVERLAY_SERVICE_ID);
+	return baseoverlay.establishLink(endp, node, OverlayInterface::OVERLAY_SERVICE_ID);
 }
 
@@ -86,5 +91,5 @@
 /// sends a discovery message
 void Chord::send_discovery_to(const NodeID& destination, int ttl) {
-	logging_debug("Initiating discovery of " << destination.toString() );
+//	logging_debug("Initiating discovery of " << destination.toString() );
 	Message msg;
 	ChordMessage cmsg(ChordMessage::discovery, nodeid, destination);
@@ -111,5 +116,6 @@
 
 	// initiator? no->setup first link
-	if (!(boot == EndpointDescriptor::UNSPECIFIED)) bootstrapLink = setup(boot);
+	if (!(boot == EndpointDescriptor::UNSPECIFIED))
+		bootstrapLink = setup(boot);
 
 	// timer for stabilization management
@@ -138,5 +144,7 @@
 
 	// message for this node? yes-> delegate to base overlay
-	if (item->id == nodeid) baseoverlay.incomingRouteMessage(msg);
+	if (item->id == nodeid || destnode == nodeid)
+		baseoverlay.incomingRouteMessage( msg, LinkID::UNSPECIFIED, nodeid );
+
 	else { // no-> send to next hop
 		ChordMessage cmsg(ChordMessage::route, nodeid, destnode);
@@ -144,4 +152,26 @@
 		send(&cmsg, item->info);
 	}
+}
+
+/// @see OverlayInterface.h
+void Chord::routeMessage(const NodeID& node, const LinkID& link, Message* msg) {
+	logging_debug("Redirect over Chord to node id=" << node.toString()
+			<< " link id=" << link.toString() );
+	ChordMessage cmsg(ChordMessage::route, nodeid, node);
+	cmsg.encapsulate(msg);
+	send(&cmsg, link);
+}
+
+/// @see OverlayInterface.h
+const LinkID& Chord::getNextLinkId( const NodeID& id ) const {
+	// get next hop
+	const route_item* item = table->get_next_hop(id);
+
+	// returns a unspecified id when this is itself
+	if (item == NULL || item->id == nodeid)
+		return LinkID::UNSPECIFIED;
+
+	/// return routing info
+	return item->info;
 }
 
@@ -160,4 +190,9 @@
 	logging_debug("link_up: link=" << lnk.toString() << " remote=" <<
 			remote.toString() );
+	for (vector<NodeID>::iterator i=pending.begin(); i!=pending.end(); i++)
+		if (*i == remote) {
+			pending.erase(i);
+			break;
+		}
 	route_item* item = table->insert(remote);
 
@@ -207,5 +242,5 @@
 		break;
 
-		// route message with payload
+	// route message with payload
 	case M::route: {
 		// find next hop
@@ -214,10 +249,11 @@
 		// next hop == myself?
 		if (m->getDestination() == nodeid) { // yes-> route to base overlay
-			logging_debug("send message to baseoverlay");
-			baseoverlay.incomingRouteMessage(m);
+			logging_debug("Send message to baseoverlay");
+			baseoverlay.incomingRouteMessage( m, item->info, remote );
 		}
 		// no-> route to next hop
 		else {
-			logging_debug("route chord message to " << item->id.toString() );
+			logging_debug("Route chord message to "
+				<< item->id.toString() << " (destination=" << m->getDestination() << ")");
 			send(m, item->info);
 		}
@@ -236,6 +272,6 @@
 
 		// check if source node can be added to routing table and setup link
-		if (m->getSource() != nodeid && table->is_insertable(m->getSource())) setup(
-				*dmsg->getSourceEndpoint());
+		if (m->getSource() != nodeid && table->is_insertable(m->getSource()))
+			setup(*dmsg->getSourceEndpoint(), m->getSource() );
 
 		// delegate discovery message
@@ -302,5 +338,5 @@
 			}
 			if (item == NULL) break;
-			logging_debug("routing discovery message to succ/pred "
+				logging_debug("routing discovery message to succ/pred "
 					<< item->id.toString() );
 			ChordMessage cmsg(*m);
@@ -327,8 +363,7 @@
 
 void Chord::eventFunction() {
-	if (!LinkID::UNSPECIFIED.isUnspecified())
-		logging_error("LinkID::UNSPECIFIED not unspecified!!!!");
 	stabilize_counter++;
 	if (stabilize_counter == 3) {
+		pending.clear();
 		size_t numNeighbors = 0;
 		for (size_t i = 0; i < table->size(); i++) {
@@ -368,4 +403,3 @@
 }
 
-}
-} // namespace ariba, overlay
+}} // namespace ariba, overlay
