]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Server/src/controlserver.cpp
966e362aa96ced609d070f65a36f2a463d993786
[z.facultad/75.42/plaqui.git] / Server / src / controlserver.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/controlserver.h"
29 #include "plaqui/server/command.h"
30 #include "plaqui/server/httperror.h"
31 #include "plaqui/server/httpresponse.h"
32 #ifdef DEBUG
33 #       include "plaqui/server/string.h"
34 #       include <iostream>
35 #endif // DEBUG
36
37 using namespace std;
38
39 namespace PlaQui {
40
41 namespace Server {
42
43 ControlServer::~ControlServer(void) {
44 #ifdef DEBUG
45         cerr << __FILE__ << "(" << __LINE__ << ")"
46                 << ": destructor." << endl;
47 #endif // DEBUG
48 }
49
50 ControlServer::ControlServer(const sockbuf::sockdesc& sd):
51                 Connection(sd) {
52 #ifdef DEBUG
53         cerr << __FILE__ << "(" << __LINE__ << ")"
54                 << ": sd = " << sd.sock << endl;
55 #endif // DEBUG
56 }
57
58 void ControlServer::real_run(void) throw() {
59 #ifdef DEBUG
60         cerr << __FILE__ << "(" << __LINE__ << ")"
61                 << ": real_run()" << endl;
62 #endif // DEBUG
63         while (!stop()) {
64                 Command command;
65                 try {
66                         socket >> command;
67                 } catch (const ios::failure& e) {
68                         // TODO poner buenos codigos de error.
69                         signal_error().emit(200000, "Se desconectó.");
70                         return;
71                 } catch (const sockerr& e) {
72                         signal_error().emit(e.serrno(), e.errstr());
73                         return;
74                 // Si hay un error al parsear el comando, se envia una respuesta con el
75                 // error.
76                 } catch (const HTTPError& e) {
77 #ifdef DEBUG
78                         cerr << __FILE__ << "(" << __LINE__ << ")"
79                                 << " : real_run() ERROR: status_code = "
80                                 << e.code << " | reason = " << HTTPMessage::reason(e.code)
81                                 << " | desc = " << e.what() << endl;
82 #endif // DEBUG
83                         socket << HTTPResponse(e) << flush;
84                         continue;
85                 }
86 #ifdef DEBUG
87                 cerr << __FILE__ << "(" << __LINE__ << ")"
88                         << " : real_run() Despachando comando: target = "
89                         << command.get_target() << " | command = " << command.get_command()
90                         << " | args = [" << String::join(command.get_args(), ", ") << "]"
91                         << endl;
92 #endif // DEBUG
93                 // Manda el comando.
94                 command_received(command);
95         }
96 }
97
98 void ControlServer::send(const Response& response) {
99         socket << response << flush;
100 #ifdef DEBUG
101         cerr << __FILE__ << "(" << __LINE__ << ")"
102                 << ": send() Enviado!" << endl;
103 #endif // DEBUG
104 }
105
106 ControlServer::SignalCommandReceived& ControlServer::signal_command_received(void) {
107         return command_received;
108 }
109
110 } // namespace Server
111
112 } // namespace PlaQui
113