Index: source/ariba/utility/transport/asio/asio_io_service.cpp
===================================================================
--- source/ariba/utility/transport/asio/asio_io_service.cpp	(revision 7464)
+++ 	(revision )
@@ -1,103 +1,0 @@
-// Internal version: Please do not publish!
-// (... until released under FreeBSD-like license *g*)
-// Code: Sebastian Mies <mies@tm.uka.de>
-
-#include "asio_io_service.h"
-
-#include <boost/asio.hpp>
-#include <boost/thread.hpp>
-
-namespace ariba {
-namespace transport {
-namespace detail {
-
-using namespace boost::asio;
-using namespace std;
-
-asio_io_service* asio_io_service::singleton = NULL;
-
-//#define DBG(x) cout << x << endl;
-#define DBG(x)
-
-void asio_io_service::operator ()() {
-	running = true;
-	DBG("io_service started");
-	boost::asio::io_service::work work(*service);
-	service->run();
-	DBG("io_service stopped");
-	if (destroy) {
-		delete singleton;
-		singleton = NULL;
-		DBG(cout << "asio io_service singleton destroyed" << endl);
-	}
-	running = false;
-}
-
-asio_io_service::asio_io_service() :
-	references(1), running(false), destroy(false), thread(NULL), service(NULL) {
-	service = new io_service();
-}
-
-asio_io_service::~asio_io_service() {
-	if (running) {
-		service->stop();
-		thread->join();
-	}
-	if (thread != NULL) delete thread;
-	if (service != NULL) delete service;
-	thread = NULL;
-	service = NULL;
-}
-
-void asio_io_service::internal_start() {
-	if (!running) {
-		if (thread != NULL) delete thread;
-		thread = new boost::thread(boost::ref(*this));
-	}
-}
-
-void asio_io_service::internal_stop() {
-	service->stop();
-	singleton->running = false;
-}
-
-io_service& asio_io_service::alloc() {
-	if (singleton != NULL) {
-		DBG("new asio io_service reference");
-		singleton->references++;
-		return *singleton->service;
-	} else {
-		DBG("creating new asio io_service singleton");
-		singleton = new asio_io_service();
-		return *singleton->service;
-	}
-}
-
-void asio_io_service::free() {
-	if (singleton != NULL) {
-		DBG("decreasing asio io_service reference");
-		singleton->references--;
-		if (singleton->references == 0) {
-			DBG("request asio io_service destruction");
-			if (singleton->running == false) {
-				delete singleton;
-				singleton = NULL;
-				DBG("asio io_service singleton destroyed");
-			} else {
-				singleton->destroy = true;
-				singleton->service->stop();
-			}
-		}
-	}
-}
-
-void asio_io_service::start() {
-	singleton->internal_start();
-}
-
-void asio_io_service::stop() {
-	singleton->internal_stop();
-}
-
-
-}}} // namespace ariba::transport::detail
Index: source/ariba/utility/transport/asio/asio_io_service.h
===================================================================
--- source/ariba/utility/transport/asio/asio_io_service.h	(revision 7464)
+++ 	(revision )
@@ -1,48 +1,0 @@
-// Internal version: Please do not publish!
-// (... until released under FreeBSD-like license *g*)
-// Code: Sebastian Mies <mies@tm.uka.de>
-
-#ifndef ASIO_IO_SERVICE_H_
-#define ASIO_IO_SERVICE_H_
-
-#include<boost/thread.hpp>
-#include<boost/asio.hpp>
-
-namespace ariba {
-namespace transport {
-namespace detail {
-
-/**
- * TODO: Doc
- *
- * @author Sebastian Mies <mies@tm.uka.de>
- */
-class asio_io_service {
-private:
-	int references;
-	volatile bool running;
-	volatile bool destroy;
-	boost::thread* thread;
-	boost::asio::io_service* service;
-
-	static asio_io_service* singleton;
-
-	friend class boost::thread;
-	friend class boost::detail::thread_data<boost::reference_wrapper<asio_io_service> >;
-
-protected:
-	void operator ()();
-	asio_io_service();
-	~asio_io_service();
-	void internal_start();
-	void internal_stop();
-public:
-	static boost::asio::io_service& alloc();
-	static void free();
-	static void start();
-	static void stop();
-};
-
-}}} // namespace ariba::transport::detail
-
-#endif /* ASIO_IO_SERVICE_H_ */
Index: source/ariba/utility/transport/asio/bluetooth_endpoint.hpp
===================================================================
--- source/ariba/utility/transport/asio/bluetooth_endpoint.hpp	(revision 7464)
+++ 	(revision )
@@ -1,186 +1,0 @@
-#include "ariba/config.h"
-
-#ifdef HAVE_LIBBLUETOOTH
-
-#ifndef BOOST_ASIO_BLUETOOTH_BLUETOOTH_ENDPOINT_HPP__
-#define BOOST_ASIO_BLUETOOTH_BLUETOOTH_ENDPOINT_HPP__
-
-#include <bluetooth/bluetooth.h>
-#include <bluetooth/rfcomm.h>
-
-#include <boost/asio/basic_stream_socket.hpp>
-
-namespace boost {
-namespace asio {
-namespace bluetooth {
-
-/**
- * Describes an endpoint for a RFCOMM Bluetooth socket.
- *
- * @author Martin Florian <mflorian@lafka.net>
- */
-template<typename BluetoothProtocol>
-class bluetooth_endpoint {
-private:
-	static bdaddr_t addr_any;
-
-public:
-	/// The protocol type associated with the endpoint.
-	typedef BluetoothProtocol protocol_type;
-
-	/// The type of the endpoint structure. This type is dependent on the
-	/// underlying implementation of the socket layer.
-	typedef boost::asio::detail::socket_addr_type data_type; // <-- Do I need this?
-	//typedef sockaddr_rc data_type;
-
-	/// Default constructor.
-	bluetooth_endpoint() :
-		data_() {
-		data_.rc_family = AF_BLUETOOTH;
-		data_.rc_bdaddr = addr_any;
-		data_.rc_channel = (uint8_t) 0;
-	}
-
-	bluetooth_endpoint(const BluetoothProtocol& protocol,
-			unsigned short channel) :
-		data_() {
-		data_.rc_family = AF_BLUETOOTH;
-		data_.rc_bdaddr = addr_any;
-		data_.rc_channel = channel;
-	}
-
-	/// Construct an endpoint using a port number, specified in the host's byte
-	/// order. The IP address will be the any address (i.e. INADDR_ANY or
-	/// in6addr_any). This constructor would typically be used for accepting new
-	/// connections.
-	bluetooth_endpoint(unsigned short channel) :
-		data_() {
-		data_.rc_family = AF_BLUETOOTH;
-		data_.rc_bdaddr = *BDADDR_ANY;
-		data_.rc_channel = channel;
-	}
-
-	/// Construct an endpoint using a port number and an BT address.
-	/// The address is in human readable form as a string.
-	bluetooth_endpoint(const char *addr, unsigned short channel) :
-		data_() {
-		data_.rc_family = AF_BLUETOOTH;
-		data_.rc_channel = channel;
-		str2ba(addr, &data_.rc_bdaddr);
-	}
-
-	/// Construct an endpoint using a port number and an BT address.
-	/// The address is given in the bluetooth-internal format.
-	bluetooth_endpoint(bdaddr_t addr, unsigned short channel) :
-		data_() {
-		data_.rc_family = AF_BLUETOOTH;
-		data_.rc_channel = channel;
-		data_.rc_bdaddr = addr;
-	}
-
-	/// Copy constructor.
-	bluetooth_endpoint(const bluetooth_endpoint& other) :
-		data_(other.data_) {
-	}
-
-	/// Assign from another endpoint.
-	bluetooth_endpoint& operator=(const bluetooth_endpoint& other) {
-		data_ = other.data_;
-		return *this;
-	}
-
-	/// The protocol associated with the endpoint.
-	protocol_type protocol() const {
-		return protocol_type::get();
-	}
-
-	/// Get the underlying endpoint in the native type.
-	/// TODO: make this nice and generic -> union like in tcp
-	data_type* data() {
-		return (boost::asio::detail::socket_addr_type*) &data_;
-	}
-
-	/// Get the underlying endpoint in the native type.
-	const data_type* data() const {
-		return (boost::asio::detail::socket_addr_type*) &data_;
-	}
-
-	/// Get the underlying size of the endpoint in the native type.
-	std::size_t size() const {
-		return sizeof(data_type);
-	}
-
-	/// Set the underlying size of the endpoint in the native type.
-	void resize(std::size_t size) {
-		if (size > sizeof(data_type)) {
-			boost::system::system_error e(boost::asio::error::invalid_argument);
-			boost::throw_exception(e);
-		}
-	}
-
-	/// Get the capacity of the endpoint in the native type.
-	std::size_t capacity() const {
-		return sizeof(data_type);
-	}
-
-	/// Get the channel associated with the endpoint. The port number is always in
-	/// the host's byte order.
-	unsigned short channel() const {
-		return data_.rc_channel;
-	}
-
-	/// Set the channel associated with the endpoint. The port number is always in
-	/// the host's byte order.
-	void channel(unsigned short channel_num) {
-		data_.rc_channel = channel_num;
-	}
-
-	/// Get the Bluetooth address associated with the endpoint.
-	bdaddr_t address() const {
-		return data_.rc_bdaddr;
-	}
-
-	/// Set the Bluetooth address associated with the endpoint.
-	void address(const boost::asio::ip::address& addr) {
-		bluetooth_endpoint<BluetoothProtocol> tmp_endpoint(addr, channel());
-		data_ = tmp_endpoint.data_;
-	}
-
-	/// Get the Bluetooth address in human readable form and write it to buf.
-	void address_hr(char &buf) {
-		ba2str(&data_.rc_bdaddr, buf);
-	}
-
-	/// Compare two endpoints for equality.
-	friend bool operator==(const bluetooth_endpoint& e1,
-			const bluetooth_endpoint& e2) {
-		return e1.address() == e2.address() && e1.channel() == e2.channel();
-	}
-
-	/// Compare two endpoints for inequality.
-	friend bool operator!=(const bluetooth_endpoint& e1,
-			const bluetooth_endpoint& e2) {
-		return e1.address() != e2.address() || e1.channel() != e2.channel();
-	}
-
-	/// Compare endpoints for ordering.
-	friend bool operator<(const bluetooth_endpoint<BluetoothProtocol>& e1,
-			const bluetooth_endpoint<BluetoothProtocol>& e2) {
-		if (e1.address() < e2.address()) return true;
-		if (e1.address() != e2.address()) return false;
-		return e1.channel() < e2.channel();
-	}
-
-private:
-	// The underlying rfcomm socket address structure thingy.
-	//struct data_type data_;
-	struct sockaddr_rc data_;
-};
-
-template<typename X>
-bdaddr_t bluetooth_endpoint<X>::addr_any = { {0u, 0u, 0u, 0u, 0u, 0u} };
-
-}}} // namespace boost::asio::bluetooth
-
-#endif
-#endif
Index: source/ariba/utility/transport/asio/rfcomm.hpp
===================================================================
--- source/ariba/utility/transport/asio/rfcomm.hpp	(revision 7464)
+++ 	(revision )
@@ -1,54 +1,0 @@
-#ifndef BOOST_ASIO_BLUETOOTH_RFCOMM_HPP__
-#define BOOST_ASIO_BLUETOOTH_RFCOMM_HPP__
-
-#include "bluetooth_endpoint.hpp"
-
-#include <bluetooth/bluetooth.h>
-#include <bluetooth/rfcomm.h>
-
-namespace boost {
-namespace asio {
-namespace bluetooth {
-
-/**
- * The rfcomm class contains flags necessary for RFCOMM sockets.
- *
- * @author Martin Florian <mflorian@lafka.net>
- */
-class rfcomm {
-public:
-	/// The type of endpoint.
-	typedef bluetooth_endpoint<rfcomm> endpoint;
-
-	/// Get an Instance.
-	/// We need this to fulfill the asio Endpoint requirements, I think.
-	static rfcomm get() {
-		return rfcomm();
-	}
-
-	/// Obtain an identifier for the type of the protocol.
-	int type() const {
-		return SOCK_STREAM;
-	}
-
-	/// Obtain an identifier for the protocol.
-	int protocol() const {
-		return BTPROTO_RFCOMM;
-	}
-
-	/// Obtain an identifier for the protocol family.
-	int family() const {
-		return AF_BLUETOOTH;
-	}
-
-	/// The RFCOMM socket type, lets pray that this will work.
-	typedef basic_stream_socket<rfcomm> socket;
-
-	/// The RFCOMM acceptor type.
-	typedef basic_socket_acceptor<rfcomm> acceptor;
-
-};
-
-}}} // namespace boost::asio::bluetooth
-
-#endif /* BOOST_ASIO_BLUETOOTH_RFCOMM_HPP__ */
Index: source/ariba/utility/transport/asio/unique_io_service.cpp
===================================================================
--- source/ariba/utility/transport/asio/unique_io_service.cpp	(revision 10653)
+++ source/ariba/utility/transport/asio/unique_io_service.cpp	(revision 10653)
@@ -0,0 +1,152 @@
+// [License]
+// The Ariba-Underlay Copyright
+//
+// Copyright (c) 2008-2012, Institute of Telematics, UniversitÃ€t Karlsruhe (TH)
+//
+// Institute of Telematics
+// UniversitÃ€t Karlsruhe (TH)
+// Zirkel 2, 76128 Karlsruhe
+// Germany
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// 1. Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright
+// notice, this list of conditions and the following disclaimer in the
+// documentation and/or other materials provided with the distribution.
+//
+// THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ARIBA PROJECT OR
+// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+// The views and conclusions contained in the software and documentation
+// are those of the authors and should not be interpreted as representing
+// official policies, either expressed or implied, of the Institute of
+// Telematics.
+// [License]
+
+/**
+ * @author Mario Hock <mario@omnifile.org>, Michael TÃ€nzer <neo@nhng.de>
+ */
+
+#include "unique_io_service.h"
+
+#include <boost/asio.hpp>
+#include <boost/thread.hpp>
+
+namespace ariba {
+namespace transport {
+namespace detail {
+
+using namespace std;
+
+
+/* init static members */
+boost::asio::io_service* unique_io_service::asio_io_service = NULL;
+int unique_io_service::asio_io_service_ref_count = 0;
+boost::thread* unique_io_service::thread = NULL;
+int unique_io_service::run_count = 0;
+boost::mutex unique_io_service::thread_stopped_mutex;
+boost::condition_variable unique_io_service::thread_stopped_cond;
+bool unique_io_service::thread_stopped = true;
+
+
+unique_io_service::unique_io_service():
+        running(false)
+{
+    if ( asio_io_service_ref_count == 0 )
+    {
+        asio_io_service = new boost::asio::io_service();
+    }
+    
+    asio_io_service_ref_count++;
+}
+
+
+
+unique_io_service::~unique_io_service()
+{
+    assert( !running );
+    
+    asio_io_service_ref_count--;
+    
+    if ( asio_io_service_ref_count <= 0 )
+    {
+        delete asio_io_service;
+        asio_io_service = NULL;
+    }
+}
+
+
+
+boost::asio::io_service & unique_io_service::get_asio_io_service()
+{
+    assert(asio_io_service != NULL );
+    
+    return *asio_io_service;
+}
+
+
+
+void unique_io_service::start()
+{
+    assert(!running);
+    
+    boost::mutex::scoped_lock lock(thread_stopped_mutex);
+    if (thread_stopped)
+    {
+    	thread_stopped = false;
+        thread = new boost::thread(&unique_io_service::thread_function);
+    }
+    
+    run_count++;
+    running = true;
+}
+
+
+
+void unique_io_service::stop()
+{
+    assert( running );
+    
+    running = false;
+    run_count--;
+    
+    if ( run_count == 0 )
+    {
+        asio_io_service->stop();
+        
+        boost::mutex::scoped_lock lock(thread_stopped_mutex);
+        while (!thread_stopped)
+        {
+            thread_stopped_cond.wait(lock);
+        }
+        delete thread;
+    }
+}
+
+
+void unique_io_service::thread_function()
+{
+    asio_io_service->run();
+    
+    {
+        boost::mutex::scoped_lock lock(thread_stopped_mutex);
+        thread_stopped = true;
+    }
+    thread_stopped_cond.notify_one();
+}
+
+
+}}} // namespace ariba::transport::detail
Index: source/ariba/utility/transport/asio/unique_io_service.h
===================================================================
--- source/ariba/utility/transport/asio/unique_io_service.h	(revision 10653)
+++ source/ariba/utility/transport/asio/unique_io_service.h	(revision 10653)
@@ -0,0 +1,101 @@
+// [License]
+// The Ariba-Underlay Copyright
+//
+// Copyright (c) 2008-2012, Institute of Telematics, UniversitÃ€t Karlsruhe (TH)
+//
+// Institute of Telematics
+// UniversitÃ€t Karlsruhe (TH)
+// Zirkel 2, 76128 Karlsruhe
+// Germany
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// 1. Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright
+// notice, this list of conditions and the following disclaimer in the
+// documentation and/or other materials provided with the distribution.
+//
+// THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ARIBA PROJECT OR
+// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+// The views and conclusions contained in the software and documentation
+// are those of the authors and should not be interpreted as representing
+// official policies, either expressed or implied, of the Institute of
+// Telematics.
+// [License]
+
+
+#ifndef UNIQUE_IO_SERVICE_H_
+#define UNIQUE_IO_SERVICE_H_
+
+#include <boost/thread.hpp>
+#include <boost/asio.hpp>
+#include <boost/noncopyable.hpp>
+
+namespace ariba {
+namespace transport {
+namespace detail {
+
+/**
+ * Multiplexes an asio_io_service so it can be used by multiple components.
+ * This class is _not_ threadsafe (use only from ariba thread/system queue)
+ *
+ * @author Mario Hock <mario@omnifile.org>, Michael TÃ€nzer <neo@nhng.de>
+ */
+class unique_io_service : public boost::noncopyable
+{
+public:
+    unique_io_service();
+    ~unique_io_service();
+    
+    /**
+     * Returned io_service is only valid during the lifetime of the
+     * unique_io_service object. 
+     */
+    boost::asio::io_service& get_asio_io_service();
+    
+    /**
+     * Executes the io_service::run() method in a parallel thread.
+     * Multiple calls don't lead to multiple threads.
+     */
+    void start();
+    
+    /**
+     * Make the io_service::run() method return and stop thread if this is the
+     * last stop() call (number of start() calls == number of stop() calls)
+     */
+    void stop();
+
+private:
+    static void thread_function();
+    
+    
+private:
+    static boost::asio::io_service* asio_io_service;
+    static int asio_io_service_ref_count;
+    
+    static boost::thread* thread;
+    static int run_count;
+    
+    static boost::mutex thread_stopped_mutex;
+    static boost::condition_variable thread_stopped_cond;
+    static bool thread_stopped;
+    
+    bool running;
+};
+
+}}} // namespace ariba::transport::detail
+
+#endif /* UNIQUE_IO_SERVICE_H_ */
