From 802f79cdb0d688127b8a639cd3173d801a1466cd Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Tue, 21 Oct 2003 05:14:25 +0000 Subject: [PATCH] Se muda el namespace Plaqui a PlaQui::Server. Se completa un poco la clase Runnable. --- Server/include/plaqui/server/connection.h | 13 ++--- Server/include/plaqui/server/controlclient.h | 6 ++- Server/include/plaqui/server/controlserver.h | 5 +- Server/include/plaqui/server/receiver.h | 6 ++- Server/include/plaqui/server/request.h | 6 ++- Server/include/plaqui/server/runnable.h | 57 ++++++++++++++++++-- Server/include/plaqui/server/server.h | 6 ++- Server/include/plaqui/server/string.h | 6 ++- Server/include/plaqui/server/transmitter.h | 6 ++- Server/src/connection.cpp | 2 +- Server/src/controlclient.cpp | 2 +- Server/src/controlserver.cpp | 11 +--- Server/src/receiver.cpp | 2 +- Server/src/request.cpp | 2 +- Server/src/runnable.cpp | 37 +++++++++++-- Server/src/server.cpp | 2 +- Server/src/string.cpp | 2 +- Server/src/transmitter.cpp | 2 +- Server/tests/server_test.cpp | 2 +- 19 files changed, 135 insertions(+), 40 deletions(-) diff --git a/Server/include/plaqui/server/connection.h b/Server/include/plaqui/server/connection.h index ef1fa24..5b777db 100644 --- a/Server/include/plaqui/server/connection.h +++ b/Server/include/plaqui/server/connection.h @@ -31,7 +31,9 @@ #include "plaqui/server/runnable.h" #include -namespace Plaqui { +namespace PlaQui { + +namespace Server { /// Conexión. class Connection: public Runnable { @@ -58,13 +60,6 @@ namespace Plaqui { */ Connection(const sockbuf::sockdesc& sd); - /** - * Constructor. - * - * \param socket Socket a usar en la conexión. - */ - //Connection(const sockinetbuf& sb); - /** * Constructor. * @@ -76,4 +71,6 @@ namespace Plaqui { } +} + #endif // PLAQUI_CONNECTION_H diff --git a/Server/include/plaqui/server/controlclient.h b/Server/include/plaqui/server/controlclient.h index 3b61edc..8341a5f 100644 --- a/Server/include/plaqui/server/controlclient.h +++ b/Server/include/plaqui/server/controlclient.h @@ -31,7 +31,9 @@ #include "plaqui/server/connection.h" #include -namespace Plaqui { +namespace PlaQui { + +namespace Server { /// Conexión para enviar comandos de control a una planta. class ControlClient: public Connection { @@ -63,4 +65,6 @@ namespace Plaqui { } +} + #endif // PLAQUI_CONTROLCLIENT_H diff --git a/Server/include/plaqui/server/controlserver.h b/Server/include/plaqui/server/controlserver.h index 99f8dea..7ad14cb 100644 --- a/Server/include/plaqui/server/controlserver.h +++ b/Server/include/plaqui/server/controlserver.h @@ -31,7 +31,9 @@ #include "plaqui/server/connection.h" #include -namespace Plaqui { +namespace PlaQui { + +namespace Server { /// Conexión para recibir comandos de control para una planta. class ControlServer: public Connection { @@ -61,5 +63,6 @@ namespace Plaqui { } +} #endif // PLAQUI_CONTROLSERVER_H diff --git a/Server/include/plaqui/server/receiver.h b/Server/include/plaqui/server/receiver.h index 91c3014..4bfda77 100644 --- a/Server/include/plaqui/server/receiver.h +++ b/Server/include/plaqui/server/receiver.h @@ -32,7 +32,9 @@ #include #include -namespace Plaqui { +namespace PlaQui { + +namespace Server { /// Conexión para recibir el estado de una planta. class Receiver: public Connection { @@ -61,4 +63,6 @@ namespace Plaqui { } +} + #endif // PLAQUI_RECEIVER_H diff --git a/Server/include/plaqui/server/request.h b/Server/include/plaqui/server/request.h index acc5091..86541bc 100644 --- a/Server/include/plaqui/server/request.h +++ b/Server/include/plaqui/server/request.h @@ -31,7 +31,9 @@ #include #include -namespace Plaqui { +namespace PlaQui { + +namespace Server { /// Conexión. class Request: public std::map { @@ -93,4 +95,6 @@ namespace Plaqui { } +} + #endif // PLAQUI_REQUEST_H diff --git a/Server/include/plaqui/server/runnable.h b/Server/include/plaqui/server/runnable.h index 39a221f..145a231 100644 --- a/Server/include/plaqui/server/runnable.h +++ b/Server/include/plaqui/server/runnable.h @@ -29,16 +29,46 @@ #define PLAQUI_RUNNABLE_H #include +#include -namespace Plaqui { +namespace PlaQui { + +namespace Server { /// ealizauna tarea (generalmente en un thread). class Runnable { + // Tipos. + + public: + + /// Tipo de señal para indicar que se finalizó la tarea. + typedef SigC::Signal0 SignalFinished; + + // Atributos. + private: - // Thread en el cual correr la tarea. + + /// Thread en el cual correr la tarea. Glib::Thread* thread; + /// Indica si se debe frinalizar la tarea. + bool stop; + + /// Señal que indica que se finalizó la tarea. + SignalFinished finished; + + // Métodos. + + private: + + /** + * Corre la tarea controlando cuando termina. + * + * \param runner Objeto con la tarea a realizar. + */ + static void static_run(Runnable* runner); + protected: /// Realiza la terea. @@ -57,15 +87,36 @@ namespace Plaqui { Runnable(void); /** - * Comienza la tares. + * Comienza la tarea. * * \param detach Si es true se corre en un thread propio. Si no no * retorna hasta que finaliza. */ virtual void run(bool detach = true); + /** + * Finaliza la tarea. + * + * \param attach Si es true, la función no retorna hasta que no + * finalice la tearea (no recomendable). + * + * \see Para saber cuando la tarea fue finalizada puede utilizar + * la señal signal_finished(). + */ + virtual void finish(bool attach = false); + + /** + * Obtiene la señal que avisa cuando la tarea es finalizada. + * + * \see Para saber cuando la tarea fue finalizada puede utilizar + * la señal signal_finished(). + */ + SignalFinished& signal_finished(void); + }; } +} + #endif // PLAQUI_RUNNABLE_H diff --git a/Server/include/plaqui/server/server.h b/Server/include/plaqui/server/server.h index 2364273..6265b6f 100644 --- a/Server/include/plaqui/server/server.h +++ b/Server/include/plaqui/server/server.h @@ -34,7 +34,9 @@ #include #include -namespace Plaqui { +namespace PlaQui { + +namespace Server { /** * Servidor de plantas químicas. @@ -112,4 +114,6 @@ namespace Plaqui { } +} + #endif // PLAQUI_SERVER_H diff --git a/Server/include/plaqui/server/string.h b/Server/include/plaqui/server/string.h index 292055f..8ab2b8f 100644 --- a/Server/include/plaqui/server/string.h +++ b/Server/include/plaqui/server/string.h @@ -30,7 +30,9 @@ #include -namespace Plaqui { +namespace PlaQui { + +namespace Server { /// Conexión. class String: public std::string { @@ -73,4 +75,6 @@ namespace Plaqui { } +} + #endif // PLAQUI_STRING_H diff --git a/Server/include/plaqui/server/transmitter.h b/Server/include/plaqui/server/transmitter.h index faff2ef..c3467a4 100644 --- a/Server/include/plaqui/server/transmitter.h +++ b/Server/include/plaqui/server/transmitter.h @@ -31,7 +31,9 @@ #include "plaqui/server/connection.h" #include -namespace Plaqui { +namespace PlaQui { + +namespace Server { /// Conexión para transmitir el estado de una planta. class Transmitter: public Connection { @@ -62,4 +64,6 @@ namespace Plaqui { } +} + #endif // PLAQUI_TRANSMITTER_H diff --git a/Server/src/connection.cpp b/Server/src/connection.cpp index 44d74df..c97685f 100644 --- a/Server/src/connection.cpp +++ b/Server/src/connection.cpp @@ -28,7 +28,7 @@ #include "plaqui/server/connection.h" #include -using namespace Plaqui; +using namespace PlaQui::Server; Connection::Connection(const sockbuf::sockdesc& sd): socket(sd) { diff --git a/Server/src/controlclient.cpp b/Server/src/controlclient.cpp index 62e693e..7138b44 100644 --- a/Server/src/controlclient.cpp +++ b/Server/src/controlclient.cpp @@ -29,7 +29,7 @@ // XXX #include -using namespace Plaqui; +using namespace PlaQui::Server; ControlClient::ControlClient(std::string host, int port): Connection(sockbuf::sock_stream) { diff --git a/Server/src/controlserver.cpp b/Server/src/controlserver.cpp index 69de3a7..8789af1 100644 --- a/Server/src/controlserver.cpp +++ b/Server/src/controlserver.cpp @@ -30,7 +30,7 @@ #include #include -using namespace Plaqui; +using namespace PlaQui::Server; ControlServer::ControlServer(const sockbuf::sockdesc& sd): Connection(sd) { @@ -50,20 +50,13 @@ void ControlServer::real_run(void) { bool is_first = true; while (!stop && socket.getline(buf, BUFFER_SIZE)) { #ifdef DEBUG - std::cerr << "Reciviendo (crudo): " << buf << std::endl; + std::cerr << "Reciviendo linea: " << buf << std::endl; #endif // DEBUG int len = strlen(buf); // Si tiene un retorno de carro, lo elimina. if (len && (buf[len-1] == '\r')) { buf[--len] = '\0'; } -#ifdef DEBUG - std::cerr << "Reciviendo (sin \\r): " << buf << std::endl; - std::cerr << "len: " << len << std::endl; - if (len == 1) { - std::cerr << std::hex << "Caracter: " << *buf << std::endl; - } -#endif // DEBUG // Si tiene contenido, lo agrego a la información del request. if (len) { // Si es la primera línea, es el request. diff --git a/Server/src/receiver.cpp b/Server/src/receiver.cpp index 4f720f4..c492fe1 100644 --- a/Server/src/receiver.cpp +++ b/Server/src/receiver.cpp @@ -27,7 +27,7 @@ #include "plaqui/server/receiver.h" -using namespace Plaqui; +using namespace PlaQui::Server; Receiver::Receiver(int port, std::string host): Connection(sockbuf::sock_dgram) { diff --git a/Server/src/request.cpp b/Server/src/request.cpp index 1eb8b0b..92bb7e1 100644 --- a/Server/src/request.cpp +++ b/Server/src/request.cpp @@ -34,7 +34,7 @@ # include #endif // DEBUG -using namespace Plaqui; +using namespace PlaQui::Server; const std::string Request::CHARS_DIGIT = "0123456789"; diff --git a/Server/src/runnable.cpp b/Server/src/runnable.cpp index 0e8b4bd..66579ac 100644 --- a/Server/src/runnable.cpp +++ b/Server/src/runnable.cpp @@ -32,12 +32,19 @@ # include #endif // DEBUG -using namespace Plaqui; +PlaQui::Server::Runnable::Runnable(void): thread(0), stop(false) { +} -Runnable::Runnable(void): thread(0) { +void PlaQui::Server::Runnable::static_run(PlaQui::Server::Runnable* runner) { +#ifdef DEBUG + std::cerr << __FILE__ << ": runner = " << runner << std::endl; +#endif // DEBUG + runner->real_run(); + runner->signal_finished().emit(); + delete runner; } -void Runnable::run(bool detach) { +void PlaQui::Server::Runnable::run(bool detach) { #ifdef DEBUG std::cerr << __FILE__ << ": detach = " << detach << std::endl; #endif // DEBUG @@ -47,9 +54,13 @@ void Runnable::run(bool detach) { if (!Glib::thread_supported()) { Glib::thread_init(); } - // Corremos el thread. + // Corremos el thread en una funcion estática para poder destruirlo al + // finalizar, pasandole el puntero al objeto. thread = Glib::Thread::create( - SigC::slot_class(*this, &Runnable::real_run), true); + SigC::bind( + SigC::slot(&PlaQui::Server::Runnable::static_run), + this), + true); // Si no corremos la tarea normalmente. } else { real_run(); @@ -57,3 +68,19 @@ void Runnable::run(bool detach) { } +void PlaQui::Server::Runnable::finish(bool attach) { +#ifdef DEBUG + std::cerr << __FILE__ << ": attach = " << attach << std::endl; +#endif // DEBUG + // TODO - necesita un mutex? + stop = true; + if (attach) { + thread->join(); + } +} + +PlaQui::Server::Runnable::SignalFinished& +PlaQui::Server::Runnable::signal_finished(void) { + return finished; +} + diff --git a/Server/src/server.cpp b/Server/src/server.cpp index 6507d6d..1206b1c 100644 --- a/Server/src/server.cpp +++ b/Server/src/server.cpp @@ -30,7 +30,7 @@ # include #endif // DEBUG -using namespace Plaqui; +using namespace PlaQui::Server; Server::Server(int port): socket(sockbuf::sock_stream) { diff --git a/Server/src/string.cpp b/Server/src/string.cpp index caa657b..24904a7 100644 --- a/Server/src/string.cpp +++ b/Server/src/string.cpp @@ -32,7 +32,7 @@ # include #endif // DEBUG -using namespace Plaqui; +using namespace PlaQui::Server; const std::string String::SPACE_CHARS = " \t\n\r"; diff --git a/Server/src/transmitter.cpp b/Server/src/transmitter.cpp index d4306e3..37113cc 100644 --- a/Server/src/transmitter.cpp +++ b/Server/src/transmitter.cpp @@ -29,7 +29,7 @@ #include #include -using namespace Plaqui; +using namespace PlaQui::Server; Transmitter::Transmitter(std::string host, int port): Connection(sockbuf::sock_dgram) { diff --git a/Server/tests/server_test.cpp b/Server/tests/server_test.cpp index 2e16935..5b0df00 100644 --- a/Server/tests/server_test.cpp +++ b/Server/tests/server_test.cpp @@ -32,7 +32,7 @@ #include using namespace std; -using namespace Plaqui; +using namespace PlaQui::Server; int main(int argc, char* argv[]) { -- 2.43.0