Index: tcpserver.h
===================================================================
--- tcpserver.h	(revision 19861)
+++ tcpserver.h	(working copy)
@@ -39,30 +39,23 @@
 #ifndef TCPSERVER_H
 #define TCPSERVER_H
 
-#include "reactor.h"
 #include "noncopyable.h"
 
+#include <string>
+
 #ifdef __unix__
 	class PosixSocket;
 #else
 	#error unknown OS
 #endif
 
-
-struct sockaddr;
 class TcpSocket;
-class ReadHandler;
 class Logger;
 
 /**
- * TCP server using as Reactor handler.
+ * TCP server
  *
- * The server listens on an interface/port for incoming connection.
- * As soon a connection arrives, it is accepted on a different port,
- * so the server is free to accept new connections.
- *
- * The client callback client_cb is called whenever a new connection
- * is accepted.
+ * The server listens on an interface/port for incoming connections.
  */
 class TcpServer
 {
@@ -70,66 +63,27 @@
 
 public:
 	/**
-	 * The type of the client callback.
-	 *
-	 * It receives a pointer of a new instance of asynchronous socket.
-	 *
-	 * The callee is responsible to delete the socket when not needed anymore.
-	 */
-	typedef function<void(TcpSocket *)> client_cb_t;
-
-	/**
 	 * class constructor.
 	 *
 	 * \param host the address (in dotted format) where to listen;
 	 *		if an empty string, listen to all interfaces
 	 * \param port the port to listen on
-	 * \param client_cb the callback to call upon incoming connection.
-	 *        This parameter is optional to allow setting as callback a function
-	 *        bound to the server itself.
-	 * \param r the reactor to register to.
 	 */
 	TcpServer(
 		const std::string &host,
-		unsigned short port,
-		client_cb_t client_cb = NULL,
-		Reactor *r = NULL);
+		unsigned short port);
 
 	// vdtor
 	virtual ~TcpServer();
 
 	/**
-	 * Return the callback function to be called upon incoming connections.
-	 */
-	client_cb_t clientCallback() const;
-
-	/**
-	 * Set the callback function to be called upon incoming connections.
+	 * Receive the incoming connection event.
 	 *
-	 * The callback must be set before the reactor receives any connection.
+	 * Call the client callback if the connection has been successfully accepted.
 	 */
-	void setClientCallback(const client_cb_t cb);
+	TcpSocket *accept();
 
-	/// Return the reactor the TcpServer instance is registered to.
-	Reactor *reactor();
-
 	/**
-	 * Register the server to a Reactor.
-	 *
-	 * \note You can register the TcpServer to a single Reactor. 
-	 *       If you want to change reactor, use removeFromReactor() first.
-	 * \sa ReactorHandler::add()
-	 */
-	void addToReactor(Reactor *r);
-
-	/**
-	 * Remove the server from the reactor it has been registered to.
-	 *
-	 * \sa ReactorHandler::remove()
-	 */
-	void removeFromReactor();
-
-	/**
 	 *  Return the logger the server is logging into.
 	 */
 	Logger *logger();
@@ -142,12 +96,6 @@
 	void setLogger(Logger *logger);
 
 protected:
-	/// The callback to call upon incoming connection.
-	client_cb_t client_cb;
-
-	/// Return the handler internally used to monitor incoming connections.
-	ReadHandler &handler();
-
 	/// Socket to receive incoming connection.
 	PosixSocket *socket;
 
@@ -164,16 +112,6 @@
 	 * \param port the port to listen on
 	 */
 	void initSocket(const std::string &host, unsigned short port);
-
-	/// The handler to register the server into a Reactor.
-	ReadHandler _handler;
-
-	/**
-	 * Receive the incoming connection event.
-	 *
-	 * Call the client callback if the connection has been successfully accepted.
-	 */
-	void server_cb();
 };
 
 #endif // TCPSERVER_H
Index: tcpserver_posix.cpp
===================================================================
--- tcpserver_posix.cpp	(revision 19861)
+++ tcpserver_posix.cpp	(working copy)
@@ -52,48 +52,28 @@
 
 TcpServer::TcpServer(
 	const std::string &host,
-	unsigned short port,
-	client_cb_t client_cb,
-	Reactor *r) :
-		client_cb(client_cb),
+	unsigned short port) :
 		socket(NULL),
 		_logger(NULL)
 {
 	initSocket(host, port);
-	_handler.setFd(socket->fd);
-	_handler.setCallback(bind(&TcpServer::server_cb, this));
-	if (r) {
-		_handler.add(r);
-	}
 }
 
 TcpServer::~TcpServer()
 {
-	if (socket) {
-		delete socket;
-	}
+	delete socket;
 }
 
-void TcpServer::server_cb()
+TcpSocket *TcpServer::accept()
 {
-	assert (client_cb);
-	assert (socket && handler().fd() == socket->fd);
-
-	if (logger()) {
+	if (logger())
 		LOG_INFO(*logger()) << "receiving connection";
-	}
 
 	std::auto_ptr<PosixSocket> s(socket->accept());
-	if (!s.get()) {
-		if (logger()) {
-			LOG_WARNING(*logger()) << "accept failed";
-		}
-		return;
-	}
-
-	// Create the TCP socket and pass ownership to the client.
-	TcpSocket *cs = new TcpSocket(*s.get());
-	client_cb(cs);
+	if (s.get())
+		return new TcpSocket(*s.get());
+	else
+		return NULL;
 }
 
 void TcpServer::initSocket(const std::string &host, unsigned short port)
@@ -105,55 +85,22 @@
 	s->setsockopt(SOL_SOCKET, SO_REUSEADDR, 1);
 
 	// Give the socket a name
-	if (!s->bind(host, port)) {
+	if (!s->bind(host, port))
 		throw std::runtime_error(std::string("socket bind failed: ")
 			+ std::string(::strerror(errno)));
-    }
 
 	// Start listening
-	if (!s->listen()) {
+	if (!s->listen())
 		throw std::runtime_error(std::string("listen failed: ")
 			+ std::string(::strerror(errno)));
-	}
 
 	// steal the fd
-	if (logger()) {
+	if (logger())
 		LOG_INFO(*logger()) << "listening on port " << port;
-	}
+
 	socket = s.release();
 }
 
-ReadHandler &TcpServer::handler()
-{
-	return _handler;
-}
-
-TcpServer::client_cb_t TcpServer::clientCallback() const
-{
-	return client_cb;
-}
-
-void TcpServer::setClientCallback(const client_cb_t cb)
-{
-	client_cb = cb;
-}
-
-Reactor *TcpServer::reactor()
-{
-	return handler().reactor();
-}
-
-void TcpServer::addToReactor(Reactor *r)
-{
-	assert("Rector required" && r);
-	handler().add(r);
-}
-
-void TcpServer::removeFromReactor()
-{
-	handler().remove();
-}
-
 Logger *TcpServer::logger()
 {
 	return _logger;
Index: reactivetcpserver.h
===================================================================
--- reactivetcpserver.h	(revision 0)
+++ reactivetcpserver.h	(revision 0)
@@ -0,0 +1,143 @@
+/**
+ * \file
+ * <!--
+ * This file is part of BeRTOS.
+ *
+ * Bertos is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * As a special exception, you may use this file as part of a free software
+ * library without restriction.  Specifically, if other files instantiate
+ * templates or use macros or inline functions from this file, or you compile
+ * this file and link it with other files to produce an executable, this
+ * file does not by itself cause the resulting executable to be covered by
+ * the GNU General Public License.  This exception does not however
+ * invalidate any other reasons why the executable file might be covered by
+ * the GNU General Public License.
+ *
+ * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
+ *
+ * -->
+ *
+ * \version $Id: tcpserver.h 19846 2007-11-16 01:17:55Z piro $
+ *
+ * \author Daniele Varrazzo <piro@develer.com>
+ * \author Bernardo Innocenti <bernie@develer.com>
+ *
+ * A TCP server dispatching through a reactor.
+ */
+#ifndef REACTIVETCPSERVER_H
+#define REACTIVETCPSERVER_H
+
+#include "tcpserver.h"
+#include "reactor.h"
+
+struct sockaddr;
+class TcpSocket;
+class ReadHandler;
+
+/**
+ * TCP server using as Reactor handler.
+ *
+ * The server listens on an interface/port for incoming connection.
+ * As soon a connection arrives, it is accepted on a different port,
+ * so the server is free to accept new connections.
+ *
+ * The client callback client_cb is called whenever a new connection
+ * is accepted.
+ */
+class ReactiveTcpServer : public TcpServer
+{
+	DISABLE_COPY(ReactiveTcpServer)
+
+// public definitions
+public:
+	/**
+	 * The type of the client callback.
+	 *
+	 * It receives a pointer of a new instance of asynchronous socket.
+	 *
+	 * The callee is responsible to delete the socket when not needed anymore.
+	 */
+	typedef function<void(TcpSocket *)> client_cb_t;
+
+public:
+	/**
+	 * class constructor.
+	 *
+	 * \param host the address (in dotted format) where to listen;
+	 *		if an empty string, listen to all interfaces
+	 * \param port the port to listen on
+	 * \param client_cb the callback to call upon incoming connection.
+	 *        This parameter is optional to allow setting as callback a function
+	 *        bound to the server itself.
+	 * \param r the reactor to register to.
+	 */
+	ReactiveTcpServer(
+		const std::string &host,
+		unsigned short port,
+		client_cb_t client_cb = NULL,
+		Reactor *r = NULL);
+
+	/**
+	 * Return the callback function to be called upon incoming connections.
+	 */
+	client_cb_t clientCallback() const;
+
+	/**
+	 * Set the callback function to be called upon incoming connections.
+	 *
+	 * The callback must be set before the reactor receives any connection.
+	 */
+	void setClientCallback(const client_cb_t cb);
+
+	/// Return the reactor the TcpServer instance is registered to.
+	Reactor *reactor();
+
+	/**
+	 * Register the server to a Reactor.
+	 *
+	 * \note You can register the TcpServer to a single Reactor.
+	 *       If you want to change reactor, use removeFromReactor() first.
+	 * \sa ReactorHandler::add()
+	 */
+	void addToReactor(Reactor *r);
+
+	/**
+	 * Remove the server from the reactor it has been registered to.
+	 *
+	 * \sa ReactorHandler::remove()
+	 */
+	void removeFromReactor();
+
+protected:
+	/// The callback to call upon incoming connection.
+	client_cb_t client_cb;
+
+	/// Return the handler internally used to monitor incoming connections.
+	ReadHandler &handler();
+
+private:
+	/// The handler to register the server into a Reactor.
+	ReadHandler _handler;
+
+	/**
+	 * Receive the incoming connection event.
+	 *
+	 * Call the client callback if the connection has been successfully accepted.
+	 */
+	void server_cb();
+};
+
+#endif // REACTIVETCPSERVER_H

Property changes on: reactivetcpserver.h
___________________________________________________________________
Name: svn:eol-style
   + native

Index: echo.cpp
===================================================================
--- echo.cpp	(revision 19861)
+++ echo.cpp	(working copy)
@@ -41,9 +41,8 @@
 
 #include "reactor.h"
 #include "appcore.h"
-#include "logger.h"
 
-#include "tcpserver.h"
+#include "reactivetcpserver.h"
 #include "tcpsocket.h"
 
 /**
@@ -57,7 +56,7 @@
 public:
 	TcpSocket *socket;
 	ReadHandler rh;
-	EchoProtocol(TcpSocket *socket, TcpServer *server) :
+	EchoProtocol(TcpSocket *socket, ReactiveTcpServer *server) :
 		socket(socket), // take ownership
 		rh(socket->fd(), bind(&EchoProtocol::data, this), server->reactor())
 	{ }
@@ -88,7 +87,7 @@
 		delete this;
 	}
 
-	static void server_cb(TcpSocket *socket, TcpServer *server)
+	static void server_cb(TcpSocket *socket, ReactiveTcpServer *server)
 	{
 		EchoProtocol *e = new EchoProtocol(socket, server);
 	}
@@ -101,7 +100,7 @@
 	namespace ph = std::tr1::placeholders;
 
 	Reactor *r = &app.reactor();
-	TcpServer server("127.0.0.1", 12345);
+	ReactiveTcpServer server("127.0.0.1", 12345);
 	server.setLogger(&app.log());
 	server.setClientCallback(bind(&EchoProtocol::server_cb, ph::_1, &server));
 	server.addToReactor(r);
Index: reactivetcpserver_posix.cpp
===================================================================
--- reactivetcpserver_posix.cpp	(revision 0)
+++ reactivetcpserver_posix.cpp	(revision 0)
@@ -0,0 +1,111 @@
+/**
+ * \file
+ * <!--
+ * This file is part of BeRTOS.
+ *
+ * Bertos is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * As a special exception, you may use this file as part of a free software
+ * library without restriction.  Specifically, if other files instantiate
+ * templates or use macros or inline functions from this file, or you compile
+ * this file and link it with other files to produce an executable, this
+ * file does not by itself cause the resulting executable to be covered by
+ * the GNU General Public License.  This exception does not however
+ * invalidate any other reasons why the executable file might be covered by
+ * the GNU General Public License.
+ *
+ * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
+ *
+ * -->
+ *
+ * \version $Id: tcpserver_posix.cpp 19845 2007-11-16 00:46:44Z piro $
+ *
+ * \author Daniele Varrazzo <piro@develer.com>
+ * \author Bernardo Innocenti <bernie@develer.com>
+ *
+ * A TCP server dispatching through a reactor
+ */
+
+#include "reactivetcpserver.h"
+#include "reactor.h"
+#include "logger.h"
+
+#ifdef __unix__
+#include "netutils_posix.h"
+#endif
+
+
+ReactiveTcpServer::ReactiveTcpServer(
+	const std::string &host,
+	unsigned short port,
+	client_cb_t client_cb,
+	Reactor *r) :
+		TcpServer(host, port),
+		client_cb(client_cb)
+{
+	_handler.setFd(socket->fd);
+	_handler.setCallback(bind(&ReactiveTcpServer::server_cb, this));
+	if (r)
+		_handler.add(r);
+}
+
+ReadHandler &ReactiveTcpServer::handler()
+{
+	return _handler;
+}
+
+ReactiveTcpServer::client_cb_t ReactiveTcpServer::clientCallback() const
+{
+	return client_cb;
+}
+
+void ReactiveTcpServer::setClientCallback(const client_cb_t cb)
+{
+	client_cb = cb;
+}
+
+void ReactiveTcpServer::server_cb()
+{
+	assert (client_cb);
+	assert (socket && handler().fd() == socket->fd);
+
+	TcpSocket *s;
+	if ((s = accept()))
+	{
+		// Pass ownership of the socket to the client.
+		client_cb(s);
+	}
+	else
+	{
+		if (logger())
+			LOG_WARNING(*logger()) << "accept failed";
+	}
+}
+
+Reactor *ReactiveTcpServer::reactor()
+{
+	return handler().reactor();
+}
+
+void ReactiveTcpServer::addToReactor(Reactor *r)
+{
+	assert("Rector required" && r);
+	handler().add(r);
+}
+
+void ReactiveTcpServer::removeFromReactor()
+{
+	handler().remove();
+}

Property changes on: reactivetcpserver_posix.cpp
___________________________________________________________________
Name: svn:eol-style
   + native

