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;
44 Glib::Mutex::Lock lock(connections_mutex);
45 for (ConnectionList::iterator con = connections.begin();
46 con != connections.end(); con++) {
51 TCPServer::TCPServer(const Connection::Port& port): socket(sockbuf::sock_stream) {
53 cerr << __FILE__ << ": port = " << port << endl;
56 //cerr << "recvtimeout = " << socket.recvtimeout(1) << endl;
57 //cerr << "sendtimeout = " << socket.sendtimeout(1) << endl;
58 //cerr << "recvtimeout = " << socket.recvtimeout(1) << endl;
59 //cerr << "sendtimeout = " << socket.sendtimeout(1) << endl;
62 cerr << __FILE__ << ": escuchando en " << socket.localhost()
63 << ":" << socket.localport() << "." << endl;
65 socket.listen(MAX_PENDING_CONNECTIONS);
67 cerr << __FILE__ << ": [despues de listen()] escuchando en "
68 << socket.localhost() << ":" << socket.localport() << "." << endl;
72 void TCPServer::finish(bool attach) {
73 //socket_mutex.lock();
74 socket.shutdown(sockbuf::shut_readwrite);
75 //socket_mutex.unlock();
76 Runnable::finish(attach);
79 void TCPServer::on_connection_finished(Connection* connection) {
81 cerr << __FILE__ << ": on_connection_finished(connection = "
82 << connection << ")" << endl;
84 Glib::Mutex::Lock lock(connections_mutex);
85 connections.remove(connection);
87 cerr << __FILE__ << ": lista de conexiones" << endl;
88 for (ConnectionList::const_iterator i = connections.begin();
89 i != connections.end(); i++) {
90 cerr << "\t " << *i << endl;
95 /// \todo TODO: ver tema de timeout o como salir de un accept().
96 void TCPServer::real_run(void) {
98 cerr << __FILE__ << ": real_run()" << endl;
100 Connection* connection;
102 // Forma grasa de salir del accept: crear conexion que salga al toque.
103 connection = new_connection(socket.accept());
105 cerr << __FILE__ << ": real_run(): connection = " << connection
108 Glib::Mutex::Lock lock(connections_mutex);
109 // XXX connections_mutex.lock();
110 connections.push_back(connection);
112 cerr << __FILE__ << ": real_run(): lista de conexiones" << endl;
113 for (ConnectionList::const_iterator i = connections.begin();
114 i != connections.end(); i++) {
115 cerr << "\t " << *i << endl;
118 // XXX connections_mutex.unlock(); // Si pongo el mutex antes del run(),
120 // Conecto la señal para cuando termina una conexión, borrarla.
121 connection->signal_finished().connect(
122 SigC::bind<Connection*>(
123 SigC::slot_class(*this,
124 &TCPServer::on_connection_finished),
130 bool TCPServer::disconnect(const std::string& host, const Connection::Port& port) {
132 cerr << __FILE__ << ": disconnect(host = " << host
133 << ", port = " << port << ")" << endl;
135 Glib::Mutex::Lock lock(connections_mutex);
136 for (ConnectionList::iterator con = connections.begin();
137 con != connections.end(); con++) {
138 if (((*con)->get_host() == host) && ((*con)->get_port() == port)) {
146 TCPServer::ConnectionInfoList TCPServer::get_connected(void) {
148 cerr << __FILE__ << ": get_connected()" << endl;
150 TCPServer::ConnectionInfoList cl;
151 Glib::Mutex::Lock lock(connections_mutex);
152 for (ConnectionList::const_iterator con = connections.begin();
153 con != connections.end(); con++) {
154 TCPServer::ConnectionInfo ci =
155 { (*con)->get_host(), (*con)->get_port() };
161 } // namespace Server
163 } // namespace PlaQui