]> git.llucax.com Git - z.facultad/75.42/plaqui.git/commitdiff
- Se arregla un semi-bug en el transmitter.
authorLeandro Lucarella <llucax@gmail.com>
Tue, 18 Nov 2003 21:06:55 +0000 (21:06 +0000)
committerLeandro Lucarella <llucax@gmail.com>
Tue, 18 Nov 2003 21:06:55 +0000 (21:06 +0000)
- Se agrega comando /plant/get/<planta> para obtener el XML se la planta.

Server/include/plaqui/server/plant.h
Server/include/plaqui/server/server.h
Server/include/plaqui/server/transmitter.h
Server/src/plant.cpp
Server/src/server.cpp
Server/src/transmitter.cpp

index 7709063080e96e85fcad29235d73dd0ef24bda54..c6e64a6c5fcc7442ddef79531773257d57b6414b 100644 (file)
@@ -48,11 +48,6 @@ namespace Server {
                        /// Lista de conexiones de control.
                        typedef std::list<Transmitter*> TransmitterList;
 
-               public:
-
-                       /// Tipo de señal para indicar que se actualizó la planta.
-                       //typedef SigC::Signal0<void> SignalUpdated;
-
 
                // Atributos.
 
@@ -67,11 +62,8 @@ namespace Server {
                        /// Simulador usado para calcular el estado de la planta.
                        Model::Simulator simulator;
 
-                       /// Señal para indicar que se actualizó la planta.
-                       //SignalUpdated updated;
-
-                       /// Nombre de la planta.
-                       //std::string name;
+                       /// Nombre del archivo donde esta el XML de la planta.
+                       std::string filename;
 
 
                // Métodos.
@@ -103,8 +95,7 @@ namespace Server {
                         *
                         * \return true si comenzó la transmisión, false si hubo problemas.
                         */
-                       bool transmission_start(const string& host,
-                                       const Connection::Port& port);
+                       bool transmission_start(string& host, Connection::Port& port);
 
                        /**
                         * Comienza una transmisión del estado de la planta.
@@ -115,9 +106,9 @@ namespace Server {
                                        const Connection::Port& port);
 
                        /**
-                        * Obtiene la señal para indicar que se actualizó la planta.
+                        * Obtiene el XML de la planta.
                         */
-                       //SignalUpdated& signal_updated(void);
+                       const std::string get_xml(void) const;
 
        };
 
index 75fbfdaca53014914cf5b728b5abb1144f01ef7d..21a1958e798dd6dc6973d8c65bd28e801059a6b5 100644 (file)
@@ -112,6 +112,11 @@ namespace Server {
                         */
                        HTTPResponse* cmd_plant_list(void);
 
+                       /**
+                        * Maneja el comando plant/get.
+                        */
+                       HTTPResponse* cmd_plant_get(const Command& command);
+
                        /**
                         * Maneja el comando plant/stop.
                         */
index 87b0bd21346b92aa6373b483e51646e97a5ecd72..211a31ea9b73fc001ca95d6f99ddfa7a57ff168d 100644 (file)
@@ -58,11 +58,13 @@ namespace Server {
                        /**
                         * Constructor.
                         *
+                        * Los parámetros son modificados por los valores reales que toma
+                        * una vez conectado.
+                        *
                         * \param host Host al cual transmitir.
                         * \param port Puerto al cual transmitir.
                         */
-                       Transmitter(const std::string& _host = "localhost",
-                                       const Connection::Port& _port = 7528);
+                       Transmitter(std::string& _host, Connection::Port& _port);
 
                        /**
                         * Envia datos.
index c56035cb6cd18aedbbfebc3168bd9a60399463f2..69ce671c511c442be112a5580b78d2c8ac7f160c 100644 (file)
@@ -28,6 +28,9 @@
 #include "plaqui/server/plant.h"
 #include <glibmm/timer.h>
 #include <sigc++/slot.h>
+#include <fstream>
+#include <iterator>
+#include <algorithm>
 #ifdef DEBUG
 #      include <iostream>
 #endif // DEBUG
@@ -50,7 +53,7 @@ Plant::~Plant(void) {
        }
 }
 
-Plant::Plant(const string& filename): simulator(filename) {
+Plant::Plant(const string& filename): simulator(filename), filename(filename) {
 #ifdef DEBUG
        cerr << __FILE__ << ": constructor. filename = " << filename << endl;
 #endif // DEBUG
@@ -83,8 +86,7 @@ void Plant::real_run(void) {
        }
 }
 
-bool Plant::transmission_start(const string& host,
-               const Connection::Port& port) {
+bool Plant::transmission_start(string& host, Connection::Port& port) {
        Glib::Mutex::Lock lock(transmissions_mutex);
        for (TransmitterList::iterator i = transmissions.begin();
                        i != transmissions.end(); i++) {
@@ -117,6 +119,19 @@ bool Plant::transmission_stop(const string& host,
        return false; // No la encontró.
 }
 
+const string Plant::get_xml(void) const {
+       ostringstream oss;
+       try {
+               ifstream ifs(filename.c_str());
+               ifs >> noskipws;
+               copy(istream_iterator<char>(ifs), istream_iterator<char>(),
+                               ostream_iterator<char>(oss));
+       } catch (...) { // TODO hacerlo mas selectivo?
+               return "";
+       }
+       return oss.str();
+}
+
 /*
 bool Plant::transmission_exists(const string& host,
                const Connection::Port& port) {
index 837295fbb071f54ba1753e0dd57283f32d5b8871..bb66fe522f5a3f9503b9b60b11cdc591c4f07b28 100644 (file)
@@ -143,6 +143,8 @@ void Server::on_control_command_received(const Command& command,
        } else if (command.get_target() == "plant") {
                if (command.get_command() == "list") {
                        response = cmd_plant_list();
+               } else if (command.get_command() == "get") {
+                       response = cmd_plant_get(command);
                } else if (command.get_command() == "stop") {
                        response = cmd_plant_stop(command);
                } else {
@@ -260,10 +262,10 @@ HTTPResponse* Server::cmd_transmission_list(void) {
        response_xml << "        <p>versión 0.8</p>" << endl;
        response_xml << "        <h2>Lista de transmisiones:</h2>" << endl;
        response_xml << "        <ul>" << endl;
-       // TODO - recorrer todas las plantas y sus transmisiones.
-/*     transmissions_mutex.lock();
-       for (TransmitterList::const_iterator i = transmissions.begin();
-                       i != transmissions.end(); i++) {
+/*TODO plants_mutex.lock();
+       for (PlantList::const_iterator i = plants.begin();
+                       i != plants.end(); i++) {
+               trans
                response_xml << "       <li>" << (*i)->get_host() << ":"
                        << (*i)->get_port() << " [<a href=\"/transmission/stop/"
                        << (*i)->get_host() << "/" << (*i)->get_port()
@@ -286,8 +288,8 @@ HTTPResponse* Server::cmd_transmission_start(const Command& command) {
                return new HTTPResponse(HTTPMessage::CONFLICT,
                                "Faltan argumentos.");
        } else {
-               const string& plant = args[0];
-               const string& host = args[1];
+               string plant = args[0];
+               string host = args[1];
                Connection::Port port = String(args[2]).to(port);
                Glib::Mutex::Lock lock(plants_mutex);
                PlantList::iterator p = plants.find(plant);
@@ -299,7 +301,7 @@ HTTPResponse* Server::cmd_transmission_start(const Command& command) {
                } else if (plants[plant]->transmission_start(host, port)) {
                        return new HTTPResponse(HTTPMessage::OK,
                                        string("Se empieza a transmitir la planta '") + plant
-                                       + "' a " + host + ":" + args[1] + ".");
+                                       + "' a " + host + ":" + String().from(port) + ".");
                } else {
                        return new HTTPResponse(HTTPMessage::INTERNAL_SERVER_ERROR,
                                        string("Error al crear la transmisión a de la planta '")
@@ -362,6 +364,26 @@ HTTPResponse* Server::cmd_plant_list(void) {
        return new HTTPResponse(HTTPMessage::OK, response_xml.str());
 }
 
+HTTPResponse* Server::cmd_plant_get(const Command& command) {
+       if (!command.get_args().size()) {
+               return new HTTPResponse(HTTPMessage::CONFLICT,
+                               "Faltan argumentos.");
+       }
+       Glib::Mutex::Lock lock(plants_mutex);
+       string name = command.get_args()[0];
+       if (plants.find(name) == plants.end()) {
+               return new HTTPResponse(HTTPMessage::NOT_FOUND,
+                               string("No existe la planta ") + name);
+       }
+       string xml = plants[name]->get_xml();
+       if (xml.length()) {
+               return new HTTPResponse(HTTPMessage::OK, xml);
+       } else {
+               return new HTTPResponse(HTTPMessage::INTERNAL_SERVER_ERROR,
+                               ("No se pudo obtener el XML de la planta ") + name);
+       }
+}
+
 HTTPResponse* Server::cmd_plant_stop(const Command& command) {
        if (!command.get_args().size()) {
                return new HTTPResponse(HTTPMessage::CONFLICT,
index a1ac7b0deb24df8c0839209b83a89cfa628cd203..de940f98c187097db7a050c543e96876781c0b8c 100644 (file)
@@ -45,15 +45,19 @@ Transmitter::~Transmitter(void) {
 #endif // DEBUG
 }
 
-Transmitter::Transmitter(const string& _host, const Connection::Port& _port):
+Transmitter::Transmitter(string& _host, Connection::Port& _port):
                Connection(sockbuf::sock_dgram, _host, _port) {
 #ifdef DEBUG
        cerr << __FILE__ << ": _host = " << _host
                << " | _port = " << _port << endl;
 #endif // DEBUG
        socket->connect(host.c_str(), port);
-       host = socket->peerhost();
-       port = socket->peerport();
+       // Reasigno el host y puerto bien, tanto de este objeto como los que se
+       // environ para indicar su valor correcto.
+       host  = socket->peerhost();
+       port  = socket->peerport();
+       _host = socket->peerhost();
+       _port = socket->peerport();
 }
 
 /// \todo debría dar una excepción (?)