1 // vim: set noexpandtab tabstop=4 shiftwidth=4:
2 //----------------------------------------------------------------------------
4 //----------------------------------------------------------------------------
5 // This file is part of PlaQui.
7 // PlaQui is free software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the Free Software
9 // Foundation; either version 2 of the License, or (at your option) any later
12 // PlaQui is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 // You should have received a copy of the GNU General Public License along
18 // with PlaQui; if not, write to the Free Software Foundation, Inc., 59 Temple
19 // Place, Suite 330, Boston, MA 02111-1307 USA
20 //----------------------------------------------------------------------------
21 // Creado: Sat Oct 18 18:18:36 2003
22 // Autores: Leandro Lucarella <llucare@fi.uba.ar>
23 //----------------------------------------------------------------------------
28 #include "plaqui/server/tcpserver.h"
29 #include <sigc++/class_slot.h>
40 const unsigned TCPServer::MAX_PENDING_CONNECTIONS = 10;
42 TCPServer::~TCPServer(void) {
44 cerr << __FILE__ << ": destructor." << endl;
48 TCPServer::TCPServer(int port): socket(sockbuf::sock_stream) {
50 cerr << __FILE__ << ": port = " << port << endl;
54 cerr << __FILE__ << ": escuchando en " << socket.localhost()
55 << ":" << socket.localport() << "." << endl;
57 socket.listen(MAX_PENDING_CONNECTIONS);
59 cerr << __FILE__ << ": [despues de listen()] escuchando en "
60 << socket.localhost() << ":" << socket.localport() << "." << endl;
64 void TCPServer::on_connection_finished(
65 Connection* connection) {
67 cerr << __FILE__ << ": on_connection_finished(connection = "
68 << connection << ")" << endl;
70 Glib::Mutex::Lock lock(connections_mutex);
71 connections.remove(connection);
73 cerr << __FILE__ << ": lista de conexiones" << endl;
74 for (ConnectionList::const_iterator i = connections.begin();
75 i != connections.end(); i++) {
76 cerr << "\t " << *i << endl;
81 /// \todo TODO: ver tema de timeout o como salir de un accept().
82 void TCPServer::real_run(void) {
84 cerr << __FILE__ << ": real_run()" << endl;
86 Connection* connection;
88 // Forma grasa de salir del accept: crear conexion que salga al toque.
89 connection = new_connection(socket.accept());
91 cerr << __FILE__ << ": real_run(): connection = " << connection
94 Glib::Mutex::Lock lock(connections_mutex);
95 // XXX connections_mutex.lock();
96 connections.push_back(connection);
98 cerr << __FILE__ << ": real_run(): lista de conexiones" << endl;
99 for (ConnectionList::const_iterator i = connections.begin();
100 i != connections.end(); i++) {
101 cerr << "\t " << *i << endl;
104 // XXX connections_mutex.unlock(); // Si pongo el mutex antes del run(),
106 // Conecto la señal para cuando termina una conexión, borrarla.
107 connection->signal_finished().connect(
108 SigC::bind<Connection*>(
109 SigC::slot_class(*this,
110 &TCPServer::on_connection_finished),
116 } // namespace Server
118 } // namespace PlaQui