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/controlserver.h"
29 #include "plaqui/server/command.h"
30 #include "plaqui/server/httperror.h"
31 #include "plaqui/server/httpresponse.h"
35 # include "plaqui/server/string.h"
45 ControlServer::~ControlServer(void) {
47 cerr << __FILE__ << "(" << __LINE__ << ")"
48 << ": destructor." << endl;
52 ControlServer::ControlServer(const sockbuf::sockdesc& sd):
55 cerr << __FILE__ << "(" << __LINE__ << ")"
56 << ": sd = " << sd.sock << endl;
60 void ControlServer::real_run(void) throw() {
62 cerr << __FILE__ << "(" << __LINE__ << ")"
63 << ": real_run()" << endl;
69 //Glib::Mutex::Lock lock(socket_mutex);
71 } catch (const ios::failure& e) {
72 // TODO poner buenos codigos de error.
73 signal_error().emit(1000000, "Se desconectó.");
75 } catch (const sockerr& e) {
76 signal_error().emit(e.serrno(), e.errstr());
78 // Si se cerró el socket.
79 //} catch (const ios::failure& e) {
82 // Si hay un error al parsear el comando, se envia una respuesta con el
84 } catch (const HTTPError& e) {
86 cerr << __FILE__ << "(" << __LINE__ << ")"
87 << " : real_run() ERROR: status_code = "
88 << e.code << " | reason = " << HTTPMessage::reason(e.code)
89 << " | desc = " << e.what() << endl;
91 //Glib::Mutex::Lock lock(socket_mutex);
92 socket << HTTPResponse(e) << flush;
95 // TODO agregar las verificaciones de abajo a HTTPRequest y padres.
96 // Actualizacion: Estoy usando trim() en casi todos lados, no debería
99 // Primera línea no vacía (que debe ser el request).
100 bool is_first = true;
101 while (!stop && socket.getline(buf, BUFSIZ)) {
103 cerr << __FILE__ << "(" << __LINE__ << ")"
104 << " Recibiendo inea: " << buf << endl;
106 int len = strlen(buf);
107 // Si tiene un retorno de carro, lo elimina.
108 if (len && (buf[len-1] == '\r')) {
111 // Si tiene contenido, lo agrego a la información del request.
113 // Si es la primera línea, es el request.
115 request.set_request(buf, socket->peerhost(),
119 // TODO request.parse_header(buf);
121 // Si viene la línea vacía
123 // Si no es la primera, terminan las cabeceras HTTP.
125 // Podría ir un break.
129 // Si es la primera, no pasa nada, sigue esperando un request.
134 cerr << __FILE__ << "(" << __LINE__ << ")"
135 << " : real_run() Despachando comando: target = "
136 << command.get_target() << " | command = " << command.get_command()
137 << " | args = [" << String::join(command.get_args(), ", ") << "]"
141 command_received(command);
145 void ControlServer::send(const HTTPResponse& response) {
146 //Glib::Mutex::Lock lock(socket_mutex);
147 socket << response << flush;
149 cerr << __FILE__ << "(" << __LINE__ << ")"
150 << ": send() Enviado!" << endl;
154 ControlServer::SignalCommandReceived& ControlServer::signal_command_received(void) {
155 return command_received;
158 } // namespace Server
160 } // namespace PlaQui