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__ << ": destructor." << endl;
46 TCPServer::TCPServer(const Connection::Port& port): socket(sockbuf::sock_stream) {
48 cerr << __FILE__ << ": port = " << port << endl;
51 //cerr << "recvtimeout = " << socket.recvtimeout(1) << endl;
52 //cerr << "sendtimeout = " << socket.sendtimeout(1) << endl;
53 //cerr << "recvtimeout = " << socket.recvtimeout(1) << endl;
54 //cerr << "sendtimeout = " << socket.sendtimeout(1) << endl;
57 cerr << __FILE__ << ": escuchando en " << socket.localhost()
58 << ":" << socket.localport() << "." << endl;
60 socket.listen(MAX_PENDING_CONNECTIONS);
62 cerr << __FILE__ << ": [despues de listen()] escuchando en "
63 << socket.localhost() << ":" << socket.localport() << "." << endl;
67 void TCPServer::finish(bool attach) {
68 //socket_mutex.lock();
69 socket.shutdown(sockbuf::shut_readwrite);
70 //socket_mutex.unlock();
71 Runnable::finish(attach);
74 void TCPServer::on_connection_finished(Connection* connection) {
76 cerr << __FILE__ << ": on_connection_finished(connection = "
77 << connection << ")" << endl;
79 Glib::Mutex::Lock lock(connections_mutex);
80 connections.remove(connection);
82 cerr << __FILE__ << ": lista de conexiones" << endl;
83 for (ConnectionList::const_iterator i = connections.begin();
84 i != connections.end(); i++) {
85 cerr << "\t " << *i << endl;
90 /// \todo TODO: ver tema de timeout o como salir de un accept().
91 void TCPServer::real_run(void) {
93 cerr << __FILE__ << ": real_run()" << endl;
95 Connection* connection;
97 // Forma grasa de salir del accept: crear conexion que salga al toque.
98 connection = new_connection(socket.accept());
100 cerr << __FILE__ << ": real_run(): connection = " << connection
103 Glib::Mutex::Lock lock(connections_mutex);
104 // XXX connections_mutex.lock();
105 connections.push_back(connection);
107 cerr << __FILE__ << ": real_run(): lista de conexiones" << endl;
108 for (ConnectionList::const_iterator i = connections.begin();
109 i != connections.end(); i++) {
110 cerr << "\t " << *i << endl;
113 // XXX connections_mutex.unlock(); // Si pongo el mutex antes del run(),
115 // Conecto la señal para cuando termina una conexión, borrarla.
116 connection->signal_finished().connect(
117 SigC::bind<Connection*>(
118 SigC::slot_class(*this,
119 &TCPServer::on_connection_finished),
125 bool TCPServer::disconnect(const std::string& host, const Connection::Port& port) {
127 cerr << __FILE__ << ": disconnect(host = " << host
128 << ", port = " << port << ")" << endl;
130 Glib::Mutex::Lock lock(connections_mutex);
131 for (ConnectionList::iterator con = connections.begin();
132 con != connections.end(); con++) {
133 if (((*con)->get_host() == host) && ((*con)->get_port() == port)) {
141 TCPServer::ConnectionInfoList TCPServer::get_connected(void) {
143 cerr << __FILE__ << ": get_connected()" << endl;
145 TCPServer::ConnectionInfoList cl;
146 Glib::Mutex::Lock lock(connections_mutex);
147 for (ConnectionList::const_iterator con = connections.begin();
148 con != connections.end(); con++) {
149 TCPServer::ConnectionInfo ci =
150 { (*con)->get_host(), (*con)->get_port() };
156 } // namespace Server
158 } // namespace PlaQui