// $Id$
//
-#include "controlclient.h"
+#include "plaqui/server/httpresponse.h"
+#include "plaqui/server/controlclient.h"
+#ifdef DEBUG
+# include <iostream>
+#endif // DEBUG
-using namespace Plaqui;
+using namespace std;
-ControlClient::ControlClient(std::string host, int port):
- Connection(sockbuf::sock_stream) {
- socket->connect(host.c_str(), port);
+namespace PlaQui {
+
+namespace Server {
+
+ControlClient::~ControlClient(void) {
+#ifdef DEBUG
+ cerr << __FILE__ << ": destructor." << endl;
+#endif // DEBUG
+}
+
+ControlClient::ControlClient(string host, int port):
+ Connection(sockbuf::sock_stream), host(host), port(port) {
+#ifdef DEBUG
+ cerr << __FILE__ << ": host = " << host
+ << " | port = " << port << endl;
+#endif // DEBUG
+}
+
+void ControlClient::real_run(void) {
+#ifdef DEBUG
+ cerr << __FILE__ << ": real_run." << endl;
+#endif // DEBUG
+ try {
+ socket->connect(host.c_str(), port);
+ } catch (const sockerr& e) {
+ // Poner una señal de error específica?
+ error_received(1);
+ finish();
+ return;
+ }
+ host = socket->peerhost();
+ port = socket->peerport();
+ // TODO sacar a la mierda?
+ connected();
+ while (!stop) {
+ HTTPResponse response;
+ try {
+ //Glib::Mutex::Lock lock(socket_mutex);
+ socket >> response;
+ // Si se cerró el socket.
+ } catch (const ios::failure& e) {
+ stop = true;
+ continue;
+ // Si hay un error al parsear la respuesta.
+ } catch (const HTTPResponse::Error& e) {
+#ifdef DEBUG
+ cerr << __FILE__ << " : real_run() ERROR nro: " << e << endl;
+#endif // DEBUG
+ // TODO - pasar como parametro codigo de error o algo.
+ error_received(e);
+ continue;
+ }
+ switch (response.status_code) {
+ case HTTPMessage::OK:
+ ok_received(response.get_body());
+ break;
+ default:
+ error_received(response.status_code);
+ break;
+ }
+ }
+}
+
+void ControlClient::send(const Command& command) {
+ socket << command << flush;
+#ifdef DEBUG
+ cerr << __FILE__ << ": send() Enviado!" << endl;
+#endif // DEBUG
}
+ControlClient::SignalConnected& ControlClient::signal_connected(void) {
+ return connected;
+}
+
+ControlClient::SignalOKReceived& ControlClient::signal_ok_received(void) {
+ return ok_received;
+}
+
+ControlClient::SignalErrorReceived& ControlClient::signal_error_received(void) {
+ return error_received;
+}
+
+} // namespace Server
+
+} // namespace PlaQui
+