]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Server/src/tcpserver.cpp
0f97fe5a762b5fbaebcfc0410ff62685cbad39cf
[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(int port): socket(sockbuf::sock_stream) {
47 #ifdef DEBUG
48         cerr << __FILE__ <<  ": port = " << port << endl;
49 #endif // DEBUG
50         socket.bind(port);
51 #ifdef DEBUG
52         cerr << __FILE__ <<  ": escuchando en " << socket.localhost()
53                 << ":" << socket.localport() << "." << endl;
54 #endif // DEBUG
55         socket.listen(MAX_PENDING_CONNECTIONS);
56 #ifdef DEBUG
57         cerr << __FILE__ <<  ": [despues de listen()] escuchando en "
58                 << socket.localhost() << ":" << socket.localport() << "." << endl;
59 #endif // DEBUG
60 }
61
62 void TCPServer::on_connection_finished(Connection* connection) {
63 #ifdef DEBUG
64         cerr << __FILE__ <<  ": on_connection_finished(connection = "
65                 << connection << ")" << endl;
66 #endif // DEBUG
67         Glib::Mutex::Lock lock(connections_mutex);
68         connections.remove(connection);
69 #ifdef DEBUG
70         cerr << __FILE__ <<  ": lista de conexiones" << endl;
71         for (ConnectionList::const_iterator i = connections.begin();
72                         i != connections.end(); i++) {
73                 cerr << "\t " << *i << endl;
74         }
75 #endif // DEBUG
76 }
77
78 /// \todo TODO: ver tema de timeout o como salir de un accept().
79 void TCPServer::real_run(void) {
80 #ifdef DEBUG
81         cerr << __FILE__ <<  ": real_run()" << endl;
82 #endif // DEBUG
83         Connection* connection;
84         while (!stop) {
85                 // Forma grasa de salir del accept: crear conexion que salga al toque.
86                 connection = new_connection(socket.accept());
87 #ifdef DEBUG
88                 cerr << __FILE__ <<  ": real_run(): connection = " << connection
89                         << endl;
90 #endif // DEBUG
91                 Glib::Mutex::Lock lock(connections_mutex);
92                 // XXX connections_mutex.lock();
93                 connections.push_back(connection);
94 #ifdef DEBUG
95                 cerr << __FILE__ <<  ": real_run(): lista de conexiones" << endl;
96                 for (ConnectionList::const_iterator i = connections.begin();
97                                 i != connections.end(); i++) {
98                         cerr << "\t " << *i << endl;
99                 }
100 #endif // DEBUG
101                 // XXX connections_mutex.unlock(); // Si pongo el mutex antes del run(),
102                 //                                    muere.
103                 // Conecto la señal para cuando termina una conexión, borrarla.
104                 connection->signal_finished().connect(
105                                 SigC::bind<Connection*>(
106                                         SigC::slot_class(*this,
107                                                 &TCPServer::on_connection_finished),
108                                         connection));
109                 connection->run();
110         }
111 }
112
113 TCPServer::ConnectionInfoList TCPServer::get_connected(void) {
114 #ifdef DEBUG
115         cerr << __FILE__ <<  ": get_connected()" << endl;
116 #endif // DEBUG
117         TCPServer::ConnectionInfoList con;
118         Glib::Mutex::Lock lock(connections_mutex);
119         for (ConnectionList::const_iterator i = connections.begin();
120                         i != connections.end(); i++) {
121                 TCPServer::ConnectionInfo ci =
122                         { (*i)->get_peerhost(), (*i)->get_peerport() };
123                 con.push_back(ci);
124         }
125         return con;
126 }
127
128 } // namespace Server
129
130 } // namespace PlaQui
131