Index: source/ariba/utility/transport/transport_peer.cpp
===================================================================
--- source/ariba/utility/transport/transport_peer.cpp	(revision 7834)
+++ source/ariba/utility/transport/transport_peer.cpp	(revision 10653)
@@ -3,4 +3,14 @@
 #include "transport_peer.hpp"
 #include "transport.hpp"
+#include "ariba/utility/logging/Logging.h"
+#include <boost/asio/ip/tcp.hpp>
+#include <boost/asio/error.hpp>
+#include <boost/foreach.hpp>
+
+#ifdef ECLIPSE_PARSER
+    #define foreach(a, b) for(a : b)
+#else
+    #define foreach(a, b) BOOST_FOREACH(a, b)
+#endif
 
 // namespace ariba::transport
@@ -9,78 +19,153 @@
 
 using namespace ariba::addressing;
+using boost::asio::ip::tcp;
+
+#ifdef HAVE_LIBBLUETOOTH
+using boost::asio::bluetooth::rfcomm;
+#endif
+
+use_logging_cpp(transport_peer);
 
 transport_peer::transport_peer( endpoint_set& local_set ) : local(local_set) {
-	// setup tcp transports
-	tcp = NULL;
-	//cout << "#tcpip_transports = " << local.tcp.size() << endl;
-	if (local.tcp.size()==1) {
-		tcp = new tcpip(local.tcp.begin()->value());
-		//cout << "Started tcpip_transport on port "  << local.tcp.begin()->value() << endl;
-	}
-
+    
+    // setup tcp transports
+    foreach(tcp_port_address port, local.tcp) {
+        
+        if (local.ip.size() > 0) {
+            foreach(ip_address ip_addr, local.ip) {
+                
+                tcp::endpoint endp(ip_addr.asio(), port.asio());
+                create_service(endp);
+            }
+        } else {
+            tcp::endpoint endp_v6(tcp::v6(), port.asio());
+            tcp::endpoint endp_v4(tcp::v4(), port.asio());
+            
+            create_service(endp_v6);
+            create_service(endp_v4);
+        }
+        
+    }
+    
 	#ifdef HAVE_LIBBLUETOOTH
-	// setup rfcomm transports
-	rfc = NULL;
-	//cout << "#rfcomm_transports = " << local.rfcomm.size() << endl;
-	if ( local.rfcomm.size() == 1 ) {
-		rfc = new rfcomm( local.rfcomm.begin()->value() );
-		//cout << "Started rfcomm_transport on port "  << local.rfcomm.begin()->value() << endl;
-	}
+    foreach(rfcomm_channel_address channel, local.rfcomm) {
+    	if (local.bluetooth.size() > 0) {
+    		foreach(mac_address mac, local.bluetooth) {
+    			rfcomm::endpoint endp(mac.bluetooth(), channel.value());
+    			create_service(endp);
+    		}
+    	} else {
+    		rfcomm::endpoint endp(channel.value());
+    		create_service(endp);
+    	}
+    }
 	#endif
 }
 
+void transport_peer::create_service(tcp::endpoint endp) {
+    try {
+        TcpIpPtr tmp_ptr(new tcpip(endp));
+        tcps.push_back(tmp_ptr);
+        logging_info("Listening on IP/TCP " << endp);
+        
+    } catch (boost::system::system_error& e) {
+        if (e.code() == boost::asio::error::address_in_use) {
+            logging_warn("[WARN] Address already in use: "
+                    << endp << ". Endpoint will be ignored!");
+        } else {
+            // Rethrow
+            throw;
+        }
+    }
+}
+
+#ifdef HAVE_LIBBLUETOOTH
+void transport_peer::create_service(rfcomm::endpoint endp) {
+    try {
+        rfcomm_transport::sptr tmp_ptr(new rfcomm_transport(endp));
+        rfcomms.push_back(tmp_ptr);
+        logging_info("Listening on bluetooth/RFCOMM " << endp);
+        
+    } catch (boost::system::system_error& e) {
+        if (e.code() == boost::asio::error::address_in_use) {
+            logging_warn("[WARN] Address already in use: "
+                    << endp << ". Endpoint will be ignored!");
+        } else {
+            // Rethrow
+            throw;
+        }
+    }
+}
+#endif
+
 transport_peer::~transport_peer() {
-	if (tcp !=NULL ) delete tcp;
-#ifdef HAVE_LIBBLUETOOTH
-	if (rfc !=NULL ) delete rfc;
-#endif
 }
 
 void transport_peer::start() {
-	if (tcp!=NULL) tcp->start();
+    foreach(TcpIpPtr tcp, tcps) {
+        tcp->start();
+    }
+    
 #ifdef HAVE_LIBBLUETOOTH
-	if (rfc!=NULL) rfc->start();
+    foreach(rfcomm_transport::sptr x, rfcomms) {
+    	x->start();
+    }
 #endif
 }
 
 void transport_peer::stop() {
-	if (tcp!=NULL) tcp->stop();
+    foreach(TcpIpPtr tcp, tcps) {
+        tcp->stop();
+    }
+    
 #ifdef HAVE_LIBBLUETOOTH
-	if (rfc!=NULL) rfc->stop();
+	foreach(rfcomm_transport::sptr x, rfcomms) {
+		x->stop();
+	}
 #endif
 }
 
-void transport_peer::send( const address_v* remote, const uint8_t* data, size_t size ) {
-	if (remote->instanceof<tcpip_endpoint>() && tcp!=NULL) {
-		tcp->send(remote,data,size);
-	} else
+
+void transport_peer::send(
+        const endpoint_set& endpoints,
+        reboost::message_t message,
+        uint8_t priority)
+{
+    foreach(TcpIpPtr tcp, tcps) {
+        tcp->send(endpoints, message, priority);
+    }
+    
 #ifdef HAVE_LIBBLUETOOTH
-	if (remote->instanceof<rfcomm_endpoint>() && rfc!=NULL) {
-		rfc->send(remote,data,size);
-	} else
-#endif
-		cerr << "Could not send message to " << remote->to_string() << endl;
-}
-
-void transport_peer::send( const endpoint_set& endpoints, const uint8_t* data, size_t size ) {
-	if (tcp!=NULL) tcp->send(endpoints,data,size);
-#ifdef HAVE_LIBBLUETOOTH
-	if (rfc!=NULL) rfc->send(endpoints,data,size);
+    foreach(rfcomm_transport::sptr x, rfcomms) {
+		x->send(endpoints, message, priority);
+	}
 #endif
 }
 
 void transport_peer::terminate( const address_v* remote ) {
-	if (remote->instanceof<tcpip_endpoint>() && tcp!=NULL)
-		tcp->terminate(remote);
+	if (remote->instanceof<tcpip_endpoint>())// TODO direkt auf der richtigen verbindung
+	{
+	    foreach(TcpIpPtr tcp, tcps) {
+	        tcp->terminate(remote);
+	    }
+	}
 #ifdef HAVE_LIBBLUETOOTH
-	if (remote->instanceof<rfcomm_endpoint>() && rfc!=NULL)
-		rfc->terminate(remote);
+	if (remote->instanceof<rfcomm_endpoint>()) {
+		foreach(rfcomm_transport::sptr x, rfcomms) {
+			x->terminate(remote);
+		}
+	}
 #endif
 }
 
 void transport_peer::register_listener( transport_listener* listener ) {
-	if (tcp!=NULL) tcp->register_listener(listener);
+    foreach(TcpIpPtr tcp, tcps) {
+        tcp->register_listener(listener);
+    }
+    
 #ifdef HAVE_LIBBLUETOOTH
-	if (rfc!=NULL) rfc->register_listener(listener);
+    foreach(rfcomm_transport::sptr x, rfcomms) {
+    	x->register_listener(listener);
+    }
 #endif
 }
