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;
76 cerr << __FILE__ << "(" << __LINE__ << ")"
77 << ": escuchando en " << socket.localhost()
78 << ":" << socket.localport() << "." << endl;
80 socket.listen(MAX_PENDING_CONNECTIONS);
82 cerr << __FILE__ << "(" << __LINE__ << ")"
83 << ": [despues de listen()] escuchando en "
84 << socket.localhost() << ":" << socket.localport() << "." << endl;
88 /*void TCPServer::finish(bool attach) {
90 cerr << __FILE__ << "(" << __LINE__ << ")"
91 << ": finish(attach = " << attach << ");" << endl;
93 //socket_mutex.lock();
95 //socket.shutdown(sockbuf::shut_readwrite);
96 //socket_mutex.unlock();
97 Runnable::finish(attach);
100 void TCPServer::on_connection_finished(Connection* connection) {
102 cerr << __FILE__ << "(" << __LINE__ << ")"
103 << ": on_connection_finished(connection = "
104 << connection << ")" << endl;
106 Glib::Mutex::Lock lock(connections_mutex);
107 connections.remove(connection);
109 cerr << __FILE__ << "(" << __LINE__ << ")"
110 << ": lista de conexiones" << endl;
111 for (ConnectionList::const_iterator i = connections.begin();
112 i != connections.end(); i++) {
113 cerr << "\t " << *i << endl;
118 void TCPServer::real_run(void) throw() {
120 cerr << __FILE__ << "(" << __LINE__ << ")"
121 << ": real_run()" << endl;
123 Connection* connection;
125 // Forma grasa de salir del accept: crear conexion que salga al toque.
127 connection = new_connection(socket.accept());
128 } catch (const sockerr& e) { // No se si el accept() puede fallar.
129 signal_error().emit(e.serrno(), e.errstr());
130 continue; // Supongo que puede seguir aceptando conexiones.
133 cerr << __FILE__ << "(" << __LINE__ << ")"
134 << ": real_run(): connection = " << connection
137 Glib::Mutex::Lock lock(connections_mutex);
138 // XXX connections_mutex.lock();
139 connections.push_back(connection);
141 cerr << __FILE__ << "(" << __LINE__ << ")"
142 << ": real_run(): lista de conexiones" << endl;
143 for (ConnectionList::const_iterator i = connections.begin();
144 i != connections.end(); i++) {
145 cerr << "\t " << *i << endl;
148 // XXX connections_mutex.unlock(); // Si pongo el mutex antes del run(),
150 // Conecto la señal para cuando termina una conexión, borrarla.
151 connection->signal_finished().connect(
152 SigC::bind<Connection*>(
153 SigC::slot_class(*this,
154 &TCPServer::on_connection_finished),
160 bool TCPServer::disconnect(const std::string& host, const Connection::Port& port) {
162 cerr << __FILE__ << "(" << __LINE__ << ")"
163 << ": disconnect(host = " << host
164 << ", port = " << port << ")" << endl;
166 Glib::Mutex::Lock lock(connections_mutex);
167 for (ConnectionList::iterator con = connections.begin();
168 con != connections.end(); con++) {
169 if (((*con)->get_host() == host) && ((*con)->get_port() == port)) {
177 /// \todo TODO Hay que reemplazarlo por una lista generica.
178 TCPServer::ConnectionInfoList TCPServer::get_connected(void) {
180 cerr << __FILE__ << "(" << __LINE__ << ")"
181 << ": get_connected()" << endl;
183 TCPServer::ConnectionInfoList cl;
184 Glib::Mutex::Lock lock(connections_mutex);
185 for (ConnectionList::const_iterator con = connections.begin();
186 con != connections.end(); con++) {
187 TCPServer::ConnectionInfo ci =
188 { (*con)->get_host(), (*con)->get_port() };
194 } // namespace Server
196 } // namespace PlaQui