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(bool attach) {
91 cerr << __FILE__ << "(" << __LINE__ << ")"
92 << ": finish(attach = " << attach << ");" << endl;
94 //socket_mutex.lock();
96 //socket.shutdown(sockbuf::shut_readwrite);
97 //socket_mutex.unlock();
98 Runnable::finish(attach);
101 void TCPServer::on_connection_finished(Connection* connection) {
103 cerr << __FILE__ << "(" << __LINE__ << ")"
104 << ": on_connection_finished(connection = "
105 << connection << ")" << endl;
107 Glib::Mutex::Lock lock(connections_mutex);
108 connections.remove(connection);
110 cerr << __FILE__ << "(" << __LINE__ << ")"
111 << ": lista de conexiones" << endl;
112 for (ConnectionList::const_iterator i = connections.begin();
113 i != connections.end(); i++) {
114 cerr << "\t " << *i << endl;
119 void TCPServer::real_run(void) throw() {
121 cerr << __FILE__ << "(" << __LINE__ << ")"
122 << ": real_run()" << endl;
124 Connection* connection;
126 // Forma grasa de salir del accept: crear conexion que salga al toque.
128 connection = new_connection(socket.accept());
129 } catch (const sockerr& e) { // No se si el accept() puede fallar.
130 signal_error().emit(e.serrno(), e.errstr());
131 continue; // Supongo que puede seguir aceptando conexiones.
134 cerr << __FILE__ << "(" << __LINE__ << ")"
135 << ": real_run(): connection = " << connection
138 Glib::Mutex::Lock lock(connections_mutex);
139 // XXX connections_mutex.lock();
140 connections.push_back(connection);
142 cerr << __FILE__ << "(" << __LINE__ << ")"
143 << ": real_run(): lista de conexiones" << endl;
144 for (ConnectionList::const_iterator i = connections.begin();
145 i != connections.end(); i++) {
146 cerr << "\t " << *i << endl;
149 // XXX connections_mutex.unlock(); // Si pongo el mutex antes del run(),
151 // Conecto la señal para cuando termina una conexión, borrarla.
152 connection->signal_finished().connect(
153 SigC::bind<Connection*>(
154 SigC::slot_class(*this,
155 &TCPServer::on_connection_finished),
161 bool TCPServer::disconnect(const std::string& host, const Connection::Port& port) {
163 cerr << __FILE__ << "(" << __LINE__ << ")"
164 << ": disconnect(host = " << host
165 << ", port = " << port << ")" << endl;
167 Glib::Mutex::Lock lock(connections_mutex);
168 for (ConnectionList::iterator con = connections.begin();
169 con != connections.end(); con++) {
170 if (((*con)->get_host() == host) && ((*con)->get_port() == port)) {
178 /// \todo TODO Hay que reemplazarlo por una lista generica.
179 TCPServer::ConnectionInfoList TCPServer::get_connected(void) {
181 cerr << __FILE__ << "(" << __LINE__ << ")"
182 << ": get_connected()" << endl;
184 TCPServer::ConnectionInfoList cl;
185 Glib::Mutex::Lock lock(connections_mutex);
186 for (ConnectionList::const_iterator con = connections.begin();
187 con != connections.end(); con++) {
188 TCPServer::ConnectionInfo ci =
189 { (*con)->get_host(), (*con)->get_port() };
195 } // namespace Server
197 } // namespace PlaQui