]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Server/src/tcpserver.cpp
d15f34ddb31bcae01ff02c840975c04ad55da495
[z.facultad/75.42/plaqui.git] / Server / src / tcpserver.cpp
1 // vim: set noexpandtab tabstop=4 shiftwidth=4:
2 //----------------------------------------------------------------------------
3 //                                  PlaQui
4 //----------------------------------------------------------------------------
5 // This file is part of PlaQui.
6 //
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
10 // version.
11 //
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
15 // details.
16 //
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 //----------------------------------------------------------------------------
24 //
25 // $Id$
26 //
27
28 #include "plaqui/server/tcpserver.h"
29 #include <sigc++/class_slot.h>
30 #ifdef DEBUG
31 #       include <iostream>
32 #endif // DEBUG
33
34 using namespace std;
35
36 namespace PlaQui {
37
38 namespace Server {
39
40 TCPServer::~TCPServer(void) {
41 #ifdef DEBUG
42         cerr << __FILE__ <<  ": destructor." << endl;
43 #endif // DEBUG
44 }
45
46 TCPServer::TCPServer(const Connection::Port& port): socket(sockbuf::sock_stream) {
47 #ifdef DEBUG
48         cerr << __FILE__ <<  ": port = " << port << endl;
49 #endif // DEBUG
50         // FIXME
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;
55         socket.bind(port);
56 #ifdef DEBUG
57         cerr << __FILE__ <<  ": escuchando en " << socket.localhost()
58                 << ":" << socket.localport() << "." << endl;
59 #endif // DEBUG
60         socket.listen(MAX_PENDING_CONNECTIONS);
61 #ifdef DEBUG
62         cerr << __FILE__ <<  ": [despues de listen()] escuchando en "
63                 << socket.localhost() << ":" << socket.localport() << "." << endl;
64 #endif // DEBUG
65 }
66
67 void TCPServer::finish(bool attach) {
68         //socket_mutex.lock();
69         socket.shutdown(sockbuf::shut_readwrite);
70         //socket_mutex.unlock();
71         Runnable::finish(attach);
72 }
73
74 void TCPServer::on_connection_finished(Connection* connection) {
75 #ifdef DEBUG
76         cerr << __FILE__ <<  ": on_connection_finished(connection = "
77                 << connection << ")" << endl;
78 #endif // DEBUG
79         Glib::Mutex::Lock lock(connections_mutex);
80         connections.remove(connection);
81 #ifdef DEBUG
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;
86         }
87 #endif // DEBUG
88 }
89
90 /// \todo TODO: ver tema de timeout o como salir de un accept().
91 void TCPServer::real_run(void) {
92 #ifdef DEBUG
93         cerr << __FILE__ <<  ": real_run()" << endl;
94 #endif // DEBUG
95         Connection* connection;
96         while (!stop) {
97                 // Forma grasa de salir del accept: crear conexion que salga al toque.
98                 connection = new_connection(socket.accept());
99 #ifdef DEBUG
100                 cerr << __FILE__ <<  ": real_run(): connection = " << connection
101                         << endl;
102 #endif // DEBUG
103                 Glib::Mutex::Lock lock(connections_mutex);
104                 // XXX connections_mutex.lock();
105                 connections.push_back(connection);
106 #ifdef DEBUG
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;
111                 }
112 #endif // DEBUG
113                 // XXX connections_mutex.unlock(); // Si pongo el mutex antes del run(),
114                 //                                    muere.
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),
120                                         connection));
121                 connection->run();
122         }
123 }
124
125 bool TCPServer::disconnect(const std::string& host, const Connection::Port& port) {
126 #ifdef DEBUG
127         cerr << __FILE__ <<  ": disconnect(host = " << host
128                 << ", port = " << port << ")" << endl;
129 #endif // DEBUG
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)) {
134                         (*con)->finish();
135                         return true;
136                 }
137         }
138         return false;
139 }
140
141 TCPServer::ConnectionInfoList TCPServer::get_connected(void) {
142 #ifdef DEBUG
143         cerr << __FILE__ <<  ": get_connected()" << endl;
144 #endif // DEBUG
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() };
151                 cl.push_back(ci);
152         }
153         return cl;
154 }
155
156 } // namespace Server
157
158 } // namespace PlaQui
159