// $Id$
//
-#include "controlserver.h"
+#include "plaqui/server/controlserver.h"
+#include "plaqui/server/command.h"
+#include "plaqui/server/httperror.h"
+#include "plaqui/server/httpresponse.h"
+#ifdef DEBUG
+# include "plaqui/server/string.h"
+# include <iostream>
+#endif // DEBUG
-using namespace Plaqui;
+using namespace std;
-//ControlServer::ControlServer(const iosockinet& socket) {
-//}
+namespace PlaQui {
+
+namespace Server {
+
+ControlServer::~ControlServer(void) {
+#ifdef DEBUG
+ cerr << __FILE__ << "(" << __LINE__ << ")"
+ << ": destructor." << endl;
+#endif // DEBUG
+}
+
+ControlServer::ControlServer(const sockbuf::sockdesc& sd):
+ Connection(sd) {
+#ifdef DEBUG
+ cerr << __FILE__ << "(" << __LINE__ << ")"
+ << ": sd = " << sd.sock << endl;
+#endif // DEBUG
+}
+
+void ControlServer::real_run(void) throw() {
+#ifdef DEBUG
+ cerr << __FILE__ << "(" << __LINE__ << ")"
+ << ": real_run()" << endl;
+#endif // DEBUG
+ while (!stop()) {
+ Command command;
+ try {
+ socket >> command;
+ } catch (const ios::failure& e) {
+ // TODO poner buenos codigos de error.
+ signal_error().emit(200000, "Se desconectó.");
+ return;
+ } catch (const sockerr& e) {
+ signal_error().emit(e.serrno(), e.errstr());
+ return;
+ // Si hay un error al parsear el comando, se envia una respuesta con el
+ // error.
+ } catch (const HTTPError& e) {
+#ifdef DEBUG
+ cerr << __FILE__ << "(" << __LINE__ << ")"
+ << " : real_run() ERROR: status_code = "
+ << e.code << " | reason = " << HTTPMessage::reason(e.code)
+ << " | desc = " << e.what() << endl;
+#endif // DEBUG
+ socket << HTTPResponse(e) << flush;
+ continue;
+ }
+#ifdef DEBUG
+ cerr << __FILE__ << "(" << __LINE__ << ")"
+ << " : real_run() Despachando comando: target = "
+ << command.get_target() << " | command = " << command.get_command()
+ << " | args = [" << String::join(command.get_args(), ", ") << "]"
+ << endl;
+#endif // DEBUG
+ // Manda el comando.
+ command_received(command);
+ }
+}
+
+void ControlServer::send(const Response& response) {
+ try {
+ socket << response << flush;
+ } catch (const sockerr& e) {
+ cerr << __FILE__ << "(" << __LINE__ << ") send(): "
+ << "Socket Error: " << e.operation() << " | serrno = "
+ << e.serrno() << " | errstr = " << e.errstr() << endl;
+ } catch (const exception& e) {
+ cerr << __FILE__ << "(" << __LINE__ << ") send(): "
+ << "Error: " << e.what() << endl;
+ } catch (...) {
+ cerr << __FILE__ << "(" << __LINE__ << ") send(): "
+ << "Error DESCONOCIDO!!!!!!!!" << endl;
+ }
+#ifdef DEBUG
+ cerr << __FILE__ << "(" << __LINE__ << ")"
+ << ": send() Enviado!" << endl;
+#endif // DEBUG
+}
+
+ControlServer::SignalCommandReceived& ControlServer::signal_command_received(void) {
+ return command_received;
+}
+
+} // namespace Server
+
+} // namespace PlaQui