Index: source/ariba/overlay/BaseOverlay.h
===================================================================
--- source/ariba/overlay/BaseOverlay.h	(revision 5743)
+++ source/ariba/overlay/BaseOverlay.h	(revision 5870)
@@ -121,4 +121,5 @@
 
 class LinkDescriptor;
+class OverlayMsg;
 
 class BaseOverlay: public MessageReceiver,
@@ -161,8 +162,6 @@
 
 	/// Tries to establish a direct or overlay link
-	const LinkID establishLink(const EndpointDescriptor& ep,
-		const NodeID& node, const ServiceID& service,
-		const NodeID& remoteRelay = NodeID::UNSPECIFIED,
-		const LinkID& linkid = LinkID::UNSPECIFIED);
+	const LinkID establishLink(	const EndpointDescriptor& ep,
+		const NodeID& node, const ServiceID& service );
 
 	/**
@@ -174,7 +173,6 @@
 	 * @param linkid Link identifier to be used with this link
 	 */
-	const LinkID establishLink(const NodeID& node, const ServiceID& service,
-		const NodeID& remoteRelay = NodeID::UNSPECIFIED,
-		const LinkID& linkid = LinkID::UNSPECIFIED);
+	const LinkID establishLink(	const NodeID& remote,
+		const ServiceID& service = OverlayInterface::OVERLAY_SERVICE_ID );
 
 	/**
@@ -182,16 +180,16 @@
 	 * endpoint and to the specified service
 	 */
-	const LinkID establishDirectLink(const EndpointDescriptor& ep,
-		const ServiceID& service, const LinkID& linkid = LinkID::UNSPECIFIED);
+	const LinkID establishDirectLink( const EndpointDescriptor& endpoint,
+		const ServiceID& service = OverlayInterface::OVERLAY_SERVICE_ID );
 
 	/// drops a link
-	void dropLink(const LinkID& link);
+	void dropLink( const LinkID& link );
 
 	/// sends a message over an existing link
-	seqnum_t sendMessage(const Message* message, const LinkID& link);
+	seqnum_t sendMessage(const Message* message, const LinkID& link );
 
 	/// sends a message to a node and a specific service
-	seqnum_t sendMessage(const Message* message, const NodeID& node,
-		const ServiceID& service);
+	seqnum_t sendMessage(const Message* message, const NodeID& remote,
+		const ServiceID& service = OverlayInterface::OVERLAY_SERVICE_ID);
 
 	/**
@@ -207,6 +205,6 @@
 	 * @return The end-point descriptor of the link's end-point
 	 */
-	const EndpointDescriptor& getEndpointDescriptor(const LinkID& link =
-			LinkID::UNSPECIFIED) const;
+	const EndpointDescriptor& getEndpointDescriptor(
+		const LinkID& link = LinkID::UNSPECIFIED) const;
 
 	/**
@@ -332,37 +330,25 @@
 	 * the node the message came from!
 	 */
-	virtual bool receiveMessage(const Message* message, const LinkID& link,
-		const NodeID& source = NodeID::UNSPECIFIED);
+	virtual bool receiveMessage( const Message* message, const LinkID& link,
+		const NodeID& );
+
+	/**
+	 * This method is called, when a new node joined the network
+	 *
+	 * @see OverlayStructureEvents.h
+	 */
+	virtual void onNodeJoin(const NodeID& node);
+
+	/**
+	 * Timer Event method
+	 */
+	virtual void eventFunction();
 
 	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
-	/// handles an incoming message with link descriptor
-	bool handleMessage(const Message* message,
-		const LinkID& boLink, const LinkID& bcLink, const NodeID& remoteNode );
-
-	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-	/**
-	 * This method is called, when a routed message arrives from the
-	 * overlay.
-	 *
-	 * @see OverlayStructureEvents.h
-	 */
-	virtual void incomingRouteMessage(Message* msg,
-		const LinkID& link = LinkID::UNSPECIFIED,
-		const NodeID& source = NodeID::UNSPECIFIED);
-
-	/**
-	 * This method is called, when a new node joined the network
-	 *
-	 * @see OverlayStructureEvents.h
-	 */
-	virtual void onNodeJoin(const NodeID& node);
-
-	/**
-	 * Timer Event method
-	 */
-	virtual void eventFunction();
 
 private:
+	/// is the base overlay started yet
+	bool started;
 
 	/// The state of the BaseOverlay
@@ -372,13 +358,14 @@
 	} BaseOverlayState;
 
-	BaseOverlayState state;          ///< Current Base-Overlay state
-	BaseCommunication* bc;           ///< reference to the base communication
-	NodeID nodeId;                   ///< the node id of this node
-	SpoVNetID spovnetId;             ///< the spovnet id of the currently joined overlay
-	vector<LinkID> bootstrapLinks;   ///< the link id of the link to the initiator node
-	NodeID spovnetInitiator;         ///< The initiator node
+	BaseOverlayState state;         ///< Current Base-Overlay state
+	BaseCommunication* bc;          ///< reference to the base communication
+	NodeID nodeId;                  ///< the node id of this node
+	SpoVNetID spovnetId;            ///< the spovnet id of the currently joined overlay
+	vector<LinkID> bootstrapLinks;  ///< the link id of the link to the initiator node
+	NodeID spovnetInitiator;        ///< The initiator node
 
 	/// the service id communication listeners
 	Demultiplexer<CommunicationListener*, ServiceID> communicationListeners;
+	CommunicationListener* getListener( const ServiceID& id );
 
 	/// the node listeners
@@ -392,6 +379,37 @@
 	OverlayInterface* overlayInterface;
 
+	/// Bootstrapper for our spovnet
+	OverlayBootstrap overlayBootstrap;
+
+	// message handlers --------------------------------------------------------
+
+	/// demultiplexes a incoming message with link descriptor
+	bool handleMessage( const Message* message, LinkDescriptor* ld,
+		const LinkID bcLink = LinkID::UNSPECIFIED );
+
+	// handle data and signalling messages
+	bool handleData( OverlayMsg* msg, LinkDescriptor* ld );
+	bool handleSignaling( OverlayMsg* msg, LinkDescriptor* ld );
+
+	// handle join request / reply messages
+	bool handleJoinRequest( OverlayMsg* msg, const LinkID& bcLink );
+	bool handleJoinReply( OverlayMsg* msg, const LinkID& bcLink );
+
+	// handle link messages
+	bool handleLinkRequest( OverlayMsg* msg, LinkDescriptor* ld );
+	bool handleLinkReply( OverlayMsg* msg, LinkDescriptor* ld );
+	bool handleLinkUpdate( OverlayMsg* msg, LinkDescriptor* ld );
+	bool handleLinkDirect( OverlayMsg* msg, LinkDescriptor* ld );
+	bool handleLinkAlive( OverlayMsg* msg, LinkDescriptor* ld );
+
+	// link state handling -----------------------------------------------------
+
+	/// link state information counter
+	int counter;
+
 	/// The link mapping of the node
 	vector<LinkDescriptor*> links;
+
+	/// erases a link descriptor
 	void eraseDescriptor(const LinkID& link, bool communication = false);
 
@@ -410,29 +428,60 @@
 	LinkDescriptor* addDescriptor(const LinkID& link = LinkID::UNSPECIFIED);
 
-	/// returns a direct link relay descriptor to the given relay node
-	LinkDescriptor* getRelayDescriptor( const NodeID& remoteNode );
-
-	/// returns the local relay node to a given remote node
-	NodeID getRelayNode( const NodeID& remoteNode );
-
-	/// returns the direct link the message to a neighbor is send to
-	LinkDescriptor* getSendDescriptor( const NodeID& nodeid, bool follow = true );
-
-	/// routes a message over the overlay or directly sends it when a link is open
-	seqnum_t sendOverlay( Message* message, const NodeID& nodeid,
-		const NodeID& remoteRelay = NodeID::UNSPECIFIED );
-
-	/// forwards a message over relays/overlay/directly using link descriptor
-	seqnum_t sendMessage( Message* message, const LinkDescriptor* ld );
-
-	/// creates a link descriptor, applys relay semantics if possible
-	LinkDescriptor* createLinkDescriptor(
-		const NodeID remoteNode, const ServiceID service, const LinkID link_id );
-
-	// map of a link request map a nonce to a LinkID
-	typedef map<const uint32_t, LinkID> PendingLinkMap;
-	PendingLinkMap pendingLinks;
-
-	void showLinkState();
+	/// stabilizes link information
+	void stabilizeLinks();
+
+	/// print the currently known links
+	void showLinks();
+
+	// relay route management --------------------------------------------------
+
+	/// relay route definitions
+	class relay_route {
+	public:
+		NodeID  node;
+		LinkID  link;
+		uint8_t hops;
+		time_t  used;
+	};
+	vector<relay_route> relay_routes;
+
+	/// stabilize relay information
+	void stabilizeRelays();
+
+	/// refreshes relay information
+	void refreshRelayInformation( const OverlayMsg* message, LinkDescriptor* ld );
+
+	/// returns a known relay link
+	LinkDescriptor* getRelayLinkTo( const NodeID& remote );
+
+	/// removes relay link information
+	void removeRelayLink( const LinkID& link );
+
+	/// removes relay node information
+	void removeRelayNode( const NodeID& link );
+
+	// internal message delivery -----------------------------------------------
+
+	/// routes a message to its destination node
+	void route( OverlayMsg* message, LinkDescriptor* incomingLink );
+
+	/// sends a raw message to another node, delivers it to the base overlay class
+	seqnum_t send( OverlayMsg* message, const NodeID& destination );
+
+	/// send a raw message using a link descriptor, delivers it to the base overlay class
+	seqnum_t send( OverlayMsg* message, LinkDescriptor* ld,
+		bool ignore_down = false );
+
+	/// send a message using a node id using overlay routing
+	/// sets necessary fields in the overlay message!
+	seqnum_t send_node( OverlayMsg* message, const NodeID& remote,
+		const ServiceID& service = OverlayInterface::OVERLAY_SERVICE_ID);
+
+	/// send a message using a node id using overlay routing using a link
+	/// sets necessary fields in the overlay message!
+	seqnum_t send_link( OverlayMsg* message, const LinkID& link,
+		bool ignore_down = false );
+
+	// misc --------------------------------------------------------------------
 
 	/**
@@ -442,14 +491,4 @@
 	typedef vector<NodeID> JoiningNodes;
 	JoiningNodes joiningNodes;
-
-	int counter;
-
-	/**
-	 * Bootstrapper for our spovnet
-	 */
-	OverlayBootstrap overlayBootstrap;
-
-	/// is the base overlay started yet
-	bool started;
 };
 
