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 /// \todo TODO: ver tema de timeout o como salir de un accept().
119 void TCPServer::real_run(void) {
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