From 71586a11d38b52295c9fb1884d4010adf6dd23b0 Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Thu, 13 Nov 2003 20:10:26 +0000 Subject: [PATCH] =?utf8?q?-=20Se=20agrega=20una=20signal=5Fconnected=20al?= =?utf8?q?=20ControlClient=20(por=20ahora=20dummy).=20-=20Se=20agrega=20un?= =?utf8?q?=20par=C3=A1metro=20a=20la=20signal=5Ferror=5Freceived=20con=20u?= =?utf8?q?n=20codigo=20de=20error.=20-=20Se=20actualiza=20el=20ejemplo=20y?= =?utf8?q?=20agrega=20el=20ejemplo=20(ouch!=20perdon=20ricky,=20me=20habia?= =?utf8?q?=20=20=20olvidado=20de=20subirlo!).?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- Server/include/plaqui/server/controlclient.h | 35 ++++-- Server/src/controlclient.cpp | 37 +++--- Server/tests/client_test.cpp | 122 +++++++++++++++++++ 3 files changed, 172 insertions(+), 22 deletions(-) create mode 100644 Server/tests/client_test.cpp diff --git a/Server/include/plaqui/server/controlclient.h b/Server/include/plaqui/server/controlclient.h index 95682c5..66af2ea 100644 --- a/Server/include/plaqui/server/controlclient.h +++ b/Server/include/plaqui/server/controlclient.h @@ -43,16 +43,30 @@ namespace Server { public: + /// Tipo de señal para indicar que se conectó. + typedef SigC::Signal0 SignalConnected; + /// Tipo de señal para indicar que se recibió una respuesta OK. typedef SigC::Signal0 SignalOKReceived; /// Tipo de señal para indicar que se recibió un error. - typedef SigC::Signal0 SignalErrorReceived; + typedef SigC::Signal1 SignalErrorReceived; // Atributos. + private: + + /// Host al cual conectarse. + std::string host; + + /// Puerto al cual conectarse. + int port; + protected: + /// Señal para indicar que se conectó. + SignalConnected connected; + /// Señal para indicar que se recibió una respuesta OK. SignalOKReceived ok_received; @@ -84,6 +98,18 @@ namespace Server { */ ControlClient(std::string host = "localhost", int port = 7522); + /** + * Envía un comando al servidor. + * + * \param command Comando a enviar. + */ + void send(const Command& command); + + /** + * Obtiene la señal para indicar que se conectó. + */ + SignalConnected& signal_connected(void); + /** * Obtiene la señal para indicar que se recibió una respuesta OK. */ @@ -94,13 +120,6 @@ namespace Server { */ SignalErrorReceived& signal_error_received(void); - /** - * Envía un comando al servidor. - * - * \param command Comando a enviar. - */ - void send(const Command& command); - }; } diff --git a/Server/src/controlclient.cpp b/Server/src/controlclient.cpp index 6a33e32..b26385c 100644 --- a/Server/src/controlclient.cpp +++ b/Server/src/controlclient.cpp @@ -44,21 +44,26 @@ ControlClient::~ControlClient(void) { } ControlClient::ControlClient(string host, int port): - Connection(sockbuf::sock_stream) { + Connection(sockbuf::sock_stream), host(host), port(port) { #ifdef DEBUG cerr << __FILE__ << ": host = " << host << " | port = " << port << endl; #endif // DEBUG - socket->connect(host.c_str(), port); - //if (!socket->is_open()) { - // throw ios::failure("Can't connect!"); - //} } void ControlClient::real_run(void) { #ifdef DEBUG cerr << __FILE__ << ": real_run." << endl; #endif // DEBUG + socket->connect(host.c_str(), port); + // TODO - mejorar manejo de errores de conexion. + // volver a poner signal_disconnected()? reciclar signal_error_received() + // y/o llamarla signal_error()? + if (false) { + finish(); + } else { + connected(); + } while (!stop) { HTTPResponse response; try { @@ -74,7 +79,7 @@ void ControlClient::real_run(void) { cerr << __FILE__ << " : real_run() ERROR nro: " << e << endl; #endif // DEBUG // TODO - pasar como parametro codigo de error o algo. - error_received(); + error_received(e); continue; } switch (response.status_code) { @@ -82,12 +87,23 @@ void ControlClient::real_run(void) { ok_received(); break; default: - error_received(); + 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; } @@ -96,13 +112,6 @@ ControlClient::SignalErrorReceived& ControlClient::signal_error_received(void) { return error_received; } -void ControlClient::send(const Command& command) { - socket << command << flush; -#ifdef DEBUG - cerr << __FILE__ << ": send() Enviado!" << endl; -#endif // DEBUG -} - } // namespace Server } // namespace PlaQui diff --git a/Server/tests/client_test.cpp b/Server/tests/client_test.cpp new file mode 100644 index 0000000..3f9f52c --- /dev/null +++ b/Server/tests/client_test.cpp @@ -0,0 +1,122 @@ +// vim: set noexpandtab tabstop=4 shiftwidth=4: +//---------------------------------------------------------------------------- +// PlaQui +//---------------------------------------------------------------------------- +// This file is part of PlaQui. +// +// PlaQui is free software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the Free Software +// Foundation; either version 2 of the License, or (at your option) any later +// version. +// +// PlaQui is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +// details. +// +// You should have received a copy of the GNU General Public License along +// with PlaQui; if not, write to the Free Software Foundation, Inc., 59 Temple +// Place, Suite 330, Boston, MA 02111-1307 USA +//---------------------------------------------------------------------------- +// Creado: jue nov 13 00:53:38 ART 2003 +// Autores: Leandro Lucarella +//---------------------------------------------------------------------------- +// +// $Id$ +// + +#include "plaqui/server/controlclient.h" +#include "plaqui/server/string.h" +//#include +#include +#include +#include +#include + +using namespace std; +using namespace PlaQui::Server; + +ControlClient* client = NULL; + +void on_finished(void) { + client = NULL; +} + +void on_connected(void) { + cout << " Conectado! :-)" << endl; +} + +void on_ok_received(void) { + cout << " Respuesta recibida: OK! :-D" << endl; +} + +void on_error_received(unsigned code) { + cout << " Respuesta recibida: Error nro " << code << "! :-(" << endl; +} + +int main(int argc, char* argv[]) { + + // Termina con mas informacion si hay una excepcion no manejada. + set_terminate (__gnu_cxx::__verbose_terminate_handler); + + // Mensaje de bienvenida. + cout << "Client test. Modo de uso: " << endl; + cout << "\t" << argv[0] << " [host] [port]" << endl; + cout << "Luego se envian comandos con el siguiente formato:" << endl; + cout << "\t [] [] [...] []" << endl; + + // Parámetros. + string host = "localhost"; + if (argc > 1) { + // Obtengo host. + host = argv[1]; + } + unsigned port = 7522; + if (argc > 2) { + // Obtengo puerto. + stringstream str(argv[2]); + str >> port; + } + + // Inicializa threads. + Glib::thread_init(); + + try { + // Corre el cliente. + client = new ControlClient(host, port); + client->signal_finished().connect(SigC::slot(on_finished)); + client->signal_connected().connect(SigC::slot(on_connected)); + client->signal_ok_received().connect(SigC::slot(on_ok_received)); + client->signal_error_received().connect(SigC::slot(on_error_received)); + client->run(); + char buf[BUFSIZ]; + while (client && cin.getline(buf, BUFSIZ)) { + vector v = String(buf).split(' '); + switch (v.size()) { + case 0: + client->send(Command()); + break; + case 1: + client->send(Command(v[0])); + break; + case 2: + client->send(Command(v[0], v[1])); + break; + default: + Command cmd(v[0], v[1]); + v.erase(v.begin(), v.begin() + 1); + cmd.set_args(v); + client->send(cmd); + break; + } + } + } catch (const char* e) { + cerr << "Error: " << e << endl; + } catch (exception& e) { + cerr << "Error: " << e.what() << endl; + } catch (...) { + cerr << "Error desconocido!" << endl; + } + + return 0; +} -- 2.43.0