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>
30 #include <glibmm/timer.h>
41 TCPServer::~TCPServer(void) {
43 cerr << __FILE__ << "(" << __LINE__ << ")"
44 << ": destructor." << endl;
46 // Mando a terminar todas las conexiones.
47 connections_mutex.lock();
48 for (ConnectionList::iterator con = connections.begin();
49 con != connections.end(); con++) {
52 ConnectionList::size_type count = connections.size();
53 connections_mutex.unlock();
54 // Espero que terminen realmente.
56 Glib::usleep(10000); // 10 milisegundos
57 connections_mutex.lock();
58 count = connections.size();
59 connections_mutex.unlock();
63 TCPServer::TCPServer(const Connection::Port& port) throw(sockerr):
64 socket(sockbuf::sock_stream) {
66 cerr << __FILE__ << "(" << __LINE__ << ")"
67 << ": port = " << port << endl;
69 socket.reuseaddr(true);
72 cerr << __FILE__ << "(" << __LINE__ << ")"
73 << ": escuchando en " << socket.localhost()
74 << ":" << socket.localport() << "." << endl;
76 socket.listen(MAX_PENDING_CONNECTIONS);
78 cerr << __FILE__ << "(" << __LINE__ << ")"
79 << ": [despues de listen()] escuchando en "
80 << socket.localhost() << ":" << socket.localport() << "." << endl;
84 void TCPServer::finish(void) {
86 cerr << __FILE__ << "(" << __LINE__ << ")"
87 << ": finish(void);" << endl;
90 // Creo una conexión suicida para que el accept() del server retorne
91 // el control y el server pueda terminar realmente.
93 sockinetbuf suicida(sockbuf::sock_stream);
94 suicida.connect(socket.localhost(), socket.localport());
95 } catch (const sockerr& e) {
96 // FIXME mejorar codigos de error.
97 signal_error().emit(100000 + e.serrno(), string("No se pudo crear "
98 "conexión 'suicida' para terminar el servidor: ")
103 void TCPServer::on_connection_finished(Connection* connection) {
105 cerr << __FILE__ << "(" << __LINE__ << ")"
106 << ": on_connection_finished(connection = "
107 << connection << ")" << endl;
109 Glib::Mutex::Lock lock(connections_mutex);
110 connections.remove(connection);
112 cerr << __FILE__ << "(" << __LINE__ << ")"
113 << ": lista de conexiones" << endl;
114 for (ConnectionList::const_iterator i = connections.begin();
115 i != connections.end(); i++) {
116 cerr << "\t " << *i << endl;
121 void TCPServer::real_run(void) throw() {
123 cerr << __FILE__ << "(" << __LINE__ << ")"
124 << ": real_run()" << endl;
126 Connection* connection;
128 // Forma grasa de salir del accept: crear conexion que salga al toque.
130 connection = new_connection(socket.accept());
131 } catch (const sockerr& e) { // No se si el accept() puede fallar.
132 signal_error().emit(e.serrno(),
133 string("Error creando conexión nueva: ") + e.errstr());
134 continue; // Supongo que puede seguir aceptando conexiones.
137 // Solo avisamos si la conexión que viene no es para matarnos.
138 _connection_opened(connection->get_host(), connection->get_port());
141 cerr << __FILE__ << "(" << __LINE__ << ")"
142 << ": real_run(): connection = " << connection
145 Glib::Mutex::Lock lock(connections_mutex);
146 connections.push_back(connection);
148 cerr << __FILE__ << "(" << __LINE__ << ")"
149 << ": real_run(): lista de conexiones" << endl;
150 for (ConnectionList::const_iterator i = connections.begin();
151 i != connections.end(); i++) {
152 cerr << "\t " << *i << endl;
155 // Conecto la señal para cuando termina una conexión, borrarla.
156 connection->signal_finished().connect(SigC::bind(
157 SigC::slot_class(*this, &TCPServer::on_connection_finished),
163 bool TCPServer::disconnect(const std::string& host, const Connection::Port& port) {
165 cerr << __FILE__ << "(" << __LINE__ << ")"
166 << ": disconnect(host = " << host
167 << ", port = " << port << ")" << endl;
169 Glib::Mutex::Lock lock(connections_mutex);
170 for (ConnectionList::iterator con = connections.begin();
171 con != connections.end(); con++) {
172 if (((*con)->get_host() == host) && ((*con)->get_port() == port)) {
180 /// \todo TODO Hay que reemplazarlo por una lista generica.
181 TCPServer::ConnectionInfoList TCPServer::get_connected(void) {
183 cerr << __FILE__ << "(" << __LINE__ << ")"
184 << ": get_connected()" << endl;
186 TCPServer::ConnectionInfoList cl;
187 Glib::Mutex::Lock lock(connections_mutex);
188 for (ConnectionList::const_iterator con = connections.begin();
189 con != connections.end(); con++) {
190 TCPServer::ConnectionInfo ci =
191 { (*con)->get_host(), (*con)->get_port() };
197 TCPServer::SignalConnectionOpened& TCPServer::signal_connection_opened(void) {
198 return _connection_opened;
201 } // namespace Server
203 } // namespace PlaQui