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 TCPServer::~TCPServer(void) {
42 cerr << __FILE__ << "(" << __LINE__ << ")"
43 << ": destructor." << endl;
45 Glib::Mutex::Lock lock(connections_mutex);
46 for (ConnectionList::iterator con = connections.begin();
47 con != connections.end(); con++) {
52 TCPServer::TCPServer(const Connection::Port& port) throw(sockerr):
53 socket(sockbuf::sock_stream) {
55 cerr << __FILE__ << "(" << __LINE__ << ")"
56 << ": port = " << port << endl;
59 //cerr << "recvtimeout = " << socket.recvtimeout(1) << endl;
60 //cerr << "sendtimeout = " << socket.sendtimeout(1) << endl;
61 //cerr << "recvtimeout = " << socket.recvtimeout(1) << endl;
62 //cerr << "sendtimeout = " << socket.sendtimeout(1) << endl;
65 cerr << __FILE__ << "(" << __LINE__ << ")"
66 << ": escuchando en " << socket.localhost()
67 << ":" << socket.localport() << "." << endl;
69 socket.listen(MAX_PENDING_CONNECTIONS);
71 cerr << __FILE__ << "(" << __LINE__ << ")"
72 << ": [despues de listen()] escuchando en "
73 << socket.localhost() << ":" << socket.localport() << "." << endl;
77 /*void TCPServer::finish(bool attach) {
78 //socket_mutex.lock();
79 socket.shutdown(sockbuf::shut_readwrite);
80 //socket_mutex.unlock();
81 Runnable::finish(attach);
84 void TCPServer::on_connection_finished(Connection* connection) {
86 cerr << __FILE__ << "(" << __LINE__ << ")"
87 << ": on_connection_finished(connection = "
88 << connection << ")" << endl;
90 Glib::Mutex::Lock lock(connections_mutex);
91 connections.remove(connection);
93 cerr << __FILE__ << "(" << __LINE__ << ")"
94 << ": lista de conexiones" << endl;
95 for (ConnectionList::const_iterator i = connections.begin();
96 i != connections.end(); i++) {
97 cerr << "\t " << *i << endl;
102 /// \todo TODO: ver tema de timeout o como salir de un accept().
103 void TCPServer::real_run(void) {
105 cerr << __FILE__ << "(" << __LINE__ << ")"
106 << ": real_run()" << endl;
108 Connection* connection;
110 // Forma grasa de salir del accept: crear conexion que salga al toque.
112 connection = new_connection(socket.accept());
113 } catch (const sockerr& e) { // No se si el accept() puede fallar.
114 error(e.serrno(), e.errstr());
115 continue; // Supongo que puede seguir aceptando conexiones.
118 cerr << __FILE__ << "(" << __LINE__ << ")"
119 << ": real_run(): connection = " << connection
122 Glib::Mutex::Lock lock(connections_mutex);
123 // XXX connections_mutex.lock();
124 connections.push_back(connection);
126 cerr << __FILE__ << "(" << __LINE__ << ")"
127 << ": real_run(): lista de conexiones" << endl;
128 for (ConnectionList::const_iterator i = connections.begin();
129 i != connections.end(); i++) {
130 cerr << "\t " << *i << endl;
133 // XXX connections_mutex.unlock(); // Si pongo el mutex antes del run(),
135 // Conecto la señal para cuando termina una conexión, borrarla.
136 connection->signal_finished().connect(
137 SigC::bind<Connection*>(
138 SigC::slot_class(*this,
139 &TCPServer::on_connection_finished),
145 bool TCPServer::disconnect(const std::string& host, const Connection::Port& port) {
147 cerr << __FILE__ << "(" << __LINE__ << ")"
148 << ": disconnect(host = " << host
149 << ", port = " << port << ")" << endl;
151 Glib::Mutex::Lock lock(connections_mutex);
152 for (ConnectionList::iterator con = connections.begin();
153 con != connections.end(); con++) {
154 if (((*con)->get_host() == host) && ((*con)->get_port() == port)) {
162 /// \todo TODO Hay que reemplazarlo por una lista generica.
163 TCPServer::ConnectionInfoList TCPServer::get_connected(void) {
165 cerr << __FILE__ << "(" << __LINE__ << ")"
166 << ": get_connected()" << endl;
168 TCPServer::ConnectionInfoList cl;
169 Glib::Mutex::Lock lock(connections_mutex);
170 for (ConnectionList::const_iterator con = connections.begin();
171 con != connections.end(); con++) {
172 TCPServer::ConnectionInfo ci =
173 { (*con)->get_host(), (*con)->get_port() };
179 } // namespace Server
181 } // namespace PlaQui