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;
70 //cerr << "recvtimeout = " << socket.recvtimeout(1) << endl;
71 //cerr << "sendtimeout = " << socket.sendtimeout(1) << endl;
72 //cerr << "recvtimeout = " << socket.recvtimeout(1) << endl;
73 //cerr << "sendtimeout = " << socket.sendtimeout(1) << endl;
74 socket.reuseaddr(true);
77 cerr << __FILE__ << "(" << __LINE__ << ")"
78 << ": escuchando en " << socket.localhost()
79 << ":" << socket.localport() << "." << endl;
81 socket.listen(MAX_PENDING_CONNECTIONS);
83 cerr << __FILE__ << "(" << __LINE__ << ")"
84 << ": [despues de listen()] escuchando en "
85 << socket.localhost() << ":" << socket.localport() << "." << endl;
89 void TCPServer::finish(void) {
91 cerr << __FILE__ << "(" << __LINE__ << ")"
92 << ": finish(void);" << endl;
95 // Creo una conexión suicida para que el accept() del server retorne
96 // el control y el server pueda terminar realmente.
98 sockinetbuf suicida(sockbuf::sock_stream);
99 suicida.connect(socket.localhost(), socket.localport());
100 } catch (const sockerr& e) {
102 signal_error().emit(100000 + e.serrno(), string("No se pudo crear "
103 "conexión 'suicida' para terminar el servidor: ")
108 void TCPServer::on_connection_finished(Connection* connection) {
110 cerr << __FILE__ << "(" << __LINE__ << ")"
111 << ": on_connection_finished(connection = "
112 << connection << ")" << endl;
114 Glib::Mutex::Lock lock(connections_mutex);
115 connections.remove(connection);
117 cerr << __FILE__ << "(" << __LINE__ << ")"
118 << ": lista de conexiones" << endl;
119 for (ConnectionList::const_iterator i = connections.begin();
120 i != connections.end(); i++) {
121 cerr << "\t " << *i << endl;
126 void TCPServer::real_run(void) throw() {
128 cerr << __FILE__ << "(" << __LINE__ << ")"
129 << ": real_run()" << endl;
131 Connection* connection;
133 // Forma grasa de salir del accept: crear conexion que salga al toque.
135 connection = new_connection(socket.accept());
136 } catch (const sockerr& e) { // No se si el accept() puede fallar.
137 signal_error().emit(e.serrno(), e.errstr());
138 continue; // Supongo que puede seguir aceptando conexiones.
141 cerr << __FILE__ << "(" << __LINE__ << ")"
142 << ": real_run(): connection = " << connection
145 Glib::Mutex::Lock lock(connections_mutex);
146 // XXX connections_mutex.lock();
147 connections.push_back(connection);
149 cerr << __FILE__ << "(" << __LINE__ << ")"
150 << ": real_run(): lista de conexiones" << endl;
151 for (ConnectionList::const_iterator i = connections.begin();
152 i != connections.end(); i++) {
153 cerr << "\t " << *i << endl;
156 // XXX connections_mutex.unlock(); // Si pongo el mutex antes del run(),
158 // Conecto la señal para cuando termina una conexión, borrarla.
159 connection->signal_finished().connect(SigC::bind(
160 SigC::slot_class(*this, &TCPServer::on_connection_finished),
166 bool TCPServer::disconnect(const std::string& host, const Connection::Port& port) {
168 cerr << __FILE__ << "(" << __LINE__ << ")"
169 << ": disconnect(host = " << host
170 << ", port = " << port << ")" << endl;
172 Glib::Mutex::Lock lock(connections_mutex);
173 for (ConnectionList::iterator con = connections.begin();
174 con != connections.end(); con++) {
175 if (((*con)->get_host() == host) && ((*con)->get_port() == port)) {
183 /// \todo TODO Hay que reemplazarlo por una lista generica.
184 TCPServer::ConnectionInfoList TCPServer::get_connected(void) {
186 cerr << __FILE__ << "(" << __LINE__ << ")"
187 << ": get_connected()" << endl;
189 TCPServer::ConnectionInfoList cl;
190 Glib::Mutex::Lock lock(connections_mutex);
191 for (ConnectionList::const_iterator con = connections.begin();
192 con != connections.end(); con++) {
193 TCPServer::ConnectionInfo ci =
194 { (*con)->get_host(), (*con)->get_port() };
200 } // namespace Server
202 } // namespace PlaQui