Index: source/ariba/overlay/modules/onehop/OneHop.cpp
===================================================================
--- source/ariba/overlay/modules/onehop/OneHop.cpp	(revision 12775)
+++ source/ariba/overlay/modules/onehop/OneHop.cpp	(revision 2378)
@@ -1,3 +1,3 @@
-// [License]
+// [Licence]
 // The Ariba-Underlay Copyright
 //
@@ -35,13 +35,9 @@
 // official policies, either expressed or implied, of the Institute of
 // Telematics.
-// [License]
+// [Licence]
 
 #include "OneHop.h"
 #include "ariba/overlay/BaseOverlay.h"
 
-#include "ariba/overlay/modules/onehop/messages/OneHopMessage.h"
-#include "ariba/overlay/modules/onehop/messages/NodeListingRequest.h"
-#include "ariba/overlay/modules/onehop/messages/NodeListingReply.h"
-
 namespace ariba {
 namespace overlay {
@@ -49,8 +45,9 @@
 use_logging_cpp( OneHop );
 
-OneHop::OneHop(BaseOverlay& _baseoverlay, const NodeID& _nodeid,
-		OverlayStructureEvents* _eventsReceiver, const OverlayParameterSet& param)
-	: 	OverlayInterface( _baseoverlay, _nodeid, _eventsReceiver, param ),
-		state( OneHopStateInvalid ) {
+OneHop::OneHop(BaseOverlay& _baseoverlay, const NodeID& _nodeid, OverlayStructureEvents* _eventsReceiver)
+	: 	OverlayInterface( _baseoverlay, _nodeid, _eventsReceiver ),
+		state		( OneHopStateInvalid ),
+		bootstrapLink	( LinkID::UNSPECIFIED ),
+		pendingLinks	( 0 ) {
 
 	//
@@ -58,11 +55,8 @@
 	//
 	overlayNodes.insert( make_pair(_nodeid, LinkID::UNSPECIFIED) );
-
-	Timer::setInterval(5000);
-	Timer::start();
 }
 
 OneHop::~OneHop(){
-	Timer::stop();
+
 	deleteOverlay();
 }
@@ -71,5 +65,5 @@
 
 	OverlayNodeMapping::const_iterator i = overlayNodes.find( node );
-	if (i == overlayNodes.end()) return EndpointDescriptor::UNSPECIFIED();
+	if (i == overlayNodes.end()) return EndpointDescriptor::UNSPECIFIED;
 
 	const EndpointDescriptor& ep = baseoverlay.getEndpointDescriptor( i->second );
@@ -79,11 +73,4 @@
 }
 
-
-/// @see OverlayInterface.h
-bool OneHop::isClosestNodeTo( const NodeID& node ) {
-	throw "NOT IMPLEMENTED!";
-	return false;
-}
-
 void OneHop::routeMessage(const NodeID& destnode, Message* msg){
 
@@ -93,9 +80,4 @@
 	logging_debug( "routing message to node " << destnode.toString() );
 
-	// msg for ourselfs
-	if(destnode == nodeid)
-		baseoverlay.incomingRouteMessage( msg, LinkID::UNSPECIFIED, nodeid );
-
-	// msg for other node
 	OverlayNodeMapping::const_iterator i = overlayNodes.find( destnode );
 	if (i == overlayNodes.end()) {
@@ -103,39 +85,15 @@
 		return;
 	}
-	OneHopMessage onehopRoute( OneHopMessage::OneHopMessageTypeRoute );
-	onehopRoute.encapsulate(msg);
-
-	baseoverlay.sendMessage( &onehopRoute, i->second );
-}
-
-void OneHop::routeMessage(const NodeID& node, const LinkID& link, Message* msg) {
-	OneHopMessage onehopRoute( OneHopMessage::OneHopMessageTypeRoute );
-	onehopRoute.encapsulate(msg);
-	baseoverlay.sendMessage( &onehopRoute, link );
-}
-
-/// @see OverlayInterface.h
-const LinkID& OneHop::getNextLinkId( const NodeID& id ) const {
-	OverlayNodeMapping::const_iterator i = overlayNodes.find( id );
-	if (i == overlayNodes.end()) return LinkID::UNSPECIFIED;
-	return i->second;
-}
-
-/// @see OverlayInterface.h
-const NodeID& OneHop::getNextNodeId( const NodeID& id ) const {
-	OverlayNodeMapping::const_iterator i = overlayNodes.find( id );
-	
-	// FIXME: in case the NodeID is not known we should return the nearest node
-	if (i == overlayNodes.end()) {
-		return NodeID::UNSPECIFIED;
-	}
-	
-	return i->first;
-}
-
-void OneHop::createOverlay() {
+
+	baseoverlay.sendMessage( msg, i->second );
+}
+
+void OneHop::createOverlay(){
+
 	// don't need to bootstrap against ourselfs.
 	// the create and join process is completed now.
+
 	logging_info( "creating onehop overlay structure" );
+	state = OneHopStateCompleted;
 }
 
@@ -144,7 +102,8 @@
 	logging_info( "deleting onehop overlay structure" );
 	state = OneHopStateInvalid;
-}
-
-OverlayInterface::NodeList OneHop::getKnownNodes(bool deep) const {
+	pendingLinks = 0;
+}
+
+OverlayInterface::NodeList OneHop::getKnownNodes() const {
 
 	OverlayInterface::NodeList retlist;
@@ -161,8 +120,12 @@
 void OneHop::joinOverlay(const EndpointDescriptor& bootstrapEp){
 
-	logging_info( "joining onehop overlay structure through end-point " <<
-			(bootstrapEp.isUnspecified() ? "local" : bootstrapEp.toString()) );
-
-	if( bootstrapEp.isUnspecified() ){
+	logging_info( "joining onehop overlay structure through endpoint " <<
+			(bootstrapEp == EndpointDescriptor::UNSPECIFIED ?
+					"local" : bootstrapEp.toString()) );
+
+	state = OneHopStateJoinInitiated;
+	pendingLinks = 0;
+
+	if( bootstrapEp == EndpointDescriptor::UNSPECIFIED ){
 
 		// we are the initiator and we are to bootstrap against
@@ -171,9 +134,10 @@
 
 		state = OneHopStateCompleted;
+
+
 	} else {
-		bootstrapLinks.push_back(
-				baseoverlay.establishDirectLink( bootstrapEp,
-					OverlayInterface::OVERLAY_SERVICE_ID )
-					);
+
+		bootstrapLink = baseoverlay.establishLink( bootstrapEp,
+					OverlayInterface::OVERLAY_SERVICE_ID );
 	}
 }
@@ -183,14 +147,6 @@
 	logging_info( "leaving onehop overlay structure" );
 
-	// set the state to invalid, this will prevent from
-	// handling onLinkDown events, as we are traversing the
-	// overlayNodes map and the onLinkDown function is called
-	// from the BaseOverlay and OneHop::onLinkDown will also
-	// try to access the overlayNodes structure.
-	state = OneHopStateInvalid;
-
-	//
-	// send leave messages to all nodes. the nodes
-	// will then drop the links
+	//
+	// close all links, this will indicate the other nodes that we left
 	//
 
@@ -199,21 +155,19 @@
 
 	for( ; i != iend; i++){
-		if( i->first != nodeid && i->second != LinkID::UNSPECIFIED ){
-
-			OneHopMessage msg (OneHopMessage::OneHopMessageTypeLeave);
-			baseoverlay.sendMessage( &msg, i->second );
-		}
-	}
-}
-
-
-void OneHop::onLinkDown(const LinkID& lnk, const NodeID& remote){
-
-	// don't handle when we are in state-invalid,
-	// see comment in OneHop::leaveOverlay
-	if( state == OneHopStateInvalid ) return;
-
+		if( i->first != nodeid && i->second != LinkID::UNSPECIFIED )
+			baseoverlay.dropLink( i->second );
+	}
+
+	state = OneHopStateInvalid;
+	pendingLinks = 0;
+}
+
+void OneHop::onLinkDown( const LinkID& link, const NodeID& local, const NodeID& remote ){
+
+	//
 	// node went down, remove from overlay mapping
-	logging_debug( "link " << lnk.toString() << " to node " << remote.toString() << " went down, removing node" );
+	//
+
+	logging_debug( "node " << remote.toString() << " left overlay structure" );
 
 	OverlayNodeMapping::iterator i = overlayNodes.begin();
@@ -221,15 +175,22 @@
 
 	for( ; i != iend; i++ ){
-		if( i->second == lnk ){
+		if( i->second == link ){
 			overlayNodes.erase( i );
 			break;
 		}
 	}
-
-	vector<LinkID>::iterator it = std::find( bootstrapLinks.begin(), bootstrapLinks.end(), lnk );
-	if( it != bootstrapLinks.end() ) bootstrapLinks.erase( it );
-}
-
-void OneHop::onLinkUp(const LinkID& lnk, const NodeID& remote){
+}
+
+void OneHop::onLinkUp(const LinkID& link, const NodeID& local, const NodeID& remote) {
+
+	//
+	// as soon as a link goes up, we always request the node listing.
+	// and try to get connections to as much nodes as possible in a greedy way.
+	//
+
+	if( link != bootstrapLink ){
+		if( pendingLinks > 0 )  pendingLinks--;
+		if( pendingLinks == 0 ) state = OneHopStateCompleted;
+	}
 
 	logging_debug( "link is up, sending out node listing request" );
@@ -239,11 +200,12 @@
 	onemsg.encapsulate( &requestmsg );
 
-	baseoverlay.sendMessage( &onemsg, lnk );
-}
-
-void OneHop::onMessage(const DataMessage& msg, const NodeID& remote, const LinkID& lnk){
-
-	OneHopMessage* onemsg = msg.getMessage()->convert<OneHopMessage>();
-	if( onemsg == NULL ) return;
+	state = OneHopStateJoinListingRequested;
+	baseoverlay.sendMessage( &onemsg, link );
+}
+
+bool OneHop::receiveMessage(const Message* message, const LinkID& link, const NodeID& node){
+
+	OneHopMessage* onemsg = const_cast<Message*>(message)->decapsulate<OneHopMessage>();
+	if( onemsg == NULL ) return false;
 
 	//
@@ -251,9 +213,9 @@
 	//
 
-	if( onemsg->isType( OneHopMessage::OneHopMessageTypeListingRequest ) ){
-
-		//NodeListingRequest* request = onemsg->decapsulate<NodeListingRequest>();
-
-		logging_info( "onehop received node listing request from node " << remote.toString() );
+	if( onemsg->isType( OneHopMessage::OneHopMessageTypeListingRequest) ){
+
+		NodeListingRequest* request = onemsg->decapsulate<NodeListingRequest>();
+
+		logging_info( "onehop received node listing request from node " << node.toString() );
 
 		//
@@ -261,5 +223,5 @@
 		//
 
-		overlayNodes.insert( make_pair(remote, lnk) );
+		overlayNodes.insert( make_pair(node, link) );
 
 		//
@@ -287,5 +249,5 @@
 
 		onehopReply.encapsulate( &listingReply );
-		baseoverlay.sendMessage( &onehopReply, lnk );
+		baseoverlay.sendMessage( &onehopReply, link );
 
 		//
@@ -294,7 +256,7 @@
 		//
 
-		eventsReceiver->onNodeJoin( remote );
-
-	} // OneHopMessageTypeListingRequest
+		eventsReceiver->onNodeJoin( node );
+
+	} // if( request != NULL )
 
 	//
@@ -306,5 +268,5 @@
 		NodeListingReply* reply = onemsg->decapsulate<NodeListingReply>();
 
-		logging_debug( "received node listing reply from node " << remote.toString()
+		logging_debug( "received node listing reply from node " << node.toString()
 					<< " with all overlay nodes. connecting to all of them" );
 
@@ -317,4 +279,5 @@
 		const NodeListingReply::NodeEndpointList& endpoints = reply->getList();
 		logging_debug( "received " << endpoints.size() << " nodes in listing" );
+		pendingLinks = 0;
 
 		NodeListingReply::NodeEndpointList::const_iterator i = endpoints.begin();
@@ -333,58 +296,13 @@
 
 			logging_debug( "building up link to node in overlay " << node.toString() );
-			const LinkID link = baseoverlay.establishDirectLink( *((*i).second),
+			const LinkID link = baseoverlay.establishLink( *((*i).second),
 							OverlayInterface::OVERLAY_SERVICE_ID );
 
 			overlayNodes.insert( make_pair(node, link) );
+			pendingLinks++;
 
 		} // for( ; i != iend; i++ )
 
-		delete reply;
-	} // OneHopMessageTypeListingReply
-
-	//
-	// handle node leaves
-	//
-
-	if( onemsg->isType(OneHopMessage::OneHopMessageTypeLeave) ){
-
-		logging_debug("received leave message from " <<
-				remote.toString() << " on link " << lnk.toString());
-
-		// drop the link to the node
-		baseoverlay.dropLink( lnk );
-
-	} // OneHopMessageTypeLeave
-
-	//
-	// handle kbr route messages
-	//
-
-	if( onemsg->isType( OneHopMessage::OneHopMessageTypeRoute) ){
-		logging_debug( "Route message arrived at destination node -> delegate to BaseOverlay" );
-		baseoverlay.incomingRouteMessage( onemsg, lnk, remote);
-	} // OneHopMessageTypeRoute
-
-	delete onemsg;
-}
-
-void OneHop::eventFunction(){
-
-	logging_debug("<<<<<<<<<<<<<<<<onehop-table<<<<<<<<<<<<<<<<<<<");
-
-		OverlayNodeMapping::iterator i = overlayNodes.begin();
-		OverlayNodeMapping::iterator iend = overlayNodes.end();
-
-		for( ; i != iend; i++ ){
-
-			const NodeID node = i->first;
-			const LinkID link = i->second;
-			const EndpointDescriptor& endpoint = baseoverlay.getEndpointDescriptor( link );
-
-			logging_debug( 	"node: " << node.toString() <<
-							", link_: " << link.toString() << ", endp: " << endpoint.toString());
-		}
-
-	logging_debug(">>>>>>>>>>>>>>>>>onehop-table>>>>>>>>>>>>>>>>>>>>>");
+	} // if( reply != NULL )
 
 }
