]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Server/src/tcpserver.cpp
Se pasa a autoconf + automake.
[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         Glib::Mutex::Lock lock(connections_mutex);
45         for (ConnectionList::iterator con = connections.begin();
46                         con != connections.end(); con++) {
47                 (*con)->finish(true);
48         }
49 }
50
51 TCPServer::TCPServer(const Connection::Port& port): socket(sockbuf::sock_stream) {
52 #ifdef DEBUG
53         cerr << __FILE__ <<  ": port = " << port << endl;
54 #endif // DEBUG
55         // FIXME
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;
60         socket.bind(port);
61 #ifdef DEBUG
62         cerr << __FILE__ <<  ": escuchando en " << socket.localhost()
63                 << ":" << socket.localport() << "." << endl;
64 #endif // DEBUG
65         socket.listen(MAX_PENDING_CONNECTIONS);
66 #ifdef DEBUG
67         cerr << __FILE__ <<  ": [despues de listen()] escuchando en "
68                 << socket.localhost() << ":" << socket.localport() << "." << endl;
69 #endif // DEBUG
70 }
71
72 void TCPServer::finish(bool attach) {
73         //socket_mutex.lock();
74         socket.shutdown(sockbuf::shut_readwrite);
75         //socket_mutex.unlock();
76         Runnable::finish(attach);
77 }
78
79 void TCPServer::on_connection_finished(Connection* connection) {
80 #ifdef DEBUG
81         cerr << __FILE__ <<  ": on_connection_finished(connection = "
82                 << connection << ")" << endl;
83 #endif // DEBUG
84         Glib::Mutex::Lock lock(connections_mutex);
85         connections.remove(connection);
86 #ifdef DEBUG
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;
91         }
92 #endif // DEBUG
93 }
94
95 /// \todo TODO: ver tema de timeout o como salir de un accept().
96 void TCPServer::real_run(void) {
97 #ifdef DEBUG
98         cerr << __FILE__ <<  ": real_run()" << endl;
99 #endif // DEBUG
100         Connection* connection;
101         while (!stop) {
102                 // Forma grasa de salir del accept: crear conexion que salga al toque.
103                 connection = new_connection(socket.accept());
104 #ifdef DEBUG
105                 cerr << __FILE__ <<  ": real_run(): connection = " << connection
106                         << endl;
107 #endif // DEBUG
108                 Glib::Mutex::Lock lock(connections_mutex);
109                 // XXX connections_mutex.lock();
110                 connections.push_back(connection);
111 #ifdef DEBUG
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;
116                 }
117 #endif // DEBUG
118                 // XXX connections_mutex.unlock(); // Si pongo el mutex antes del run(),
119                 //                                    muere.
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),
125                                         connection));
126                 connection->run();
127         }
128 }
129
130 bool TCPServer::disconnect(const std::string& host, const Connection::Port& port) {
131 #ifdef DEBUG
132         cerr << __FILE__ <<  ": disconnect(host = " << host
133                 << ", port = " << port << ")" << endl;
134 #endif // DEBUG
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)) {
139                         (*con)->finish();
140                         return true;
141                 }
142         }
143         return false;
144 }
145
146 TCPServer::ConnectionInfoList TCPServer::get_connected(void) {
147 #ifdef DEBUG
148         cerr << __FILE__ <<  ": get_connected()" << endl;
149 #endif // DEBUG
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() };
156                 cl.push_back(ci);
157         }
158         return cl;
159 }
160
161 } // namespace Server
162
163 } // namespace PlaQui
164