From: Leandro Lucarella Date: Mon, 17 Nov 2003 15:41:54 +0000 (+0000) Subject: - Se agrega una planta de prueba (usando Simulator). X-Git-Tag: svn_import~278 X-Git-Url: https://git.llucax.com/z.facultad/75.42/plaqui.git/commitdiff_plain/989d0da7e93d155dde399bf3047ab6b4af550bb6?ds=sidebyside - Se agrega una planta de prueba (usando Simulator). - En teoría ya andan los siguientes comandos: * (A) /server/status * /server/stop (mandandole una segunda conexion para que muera) * (A) /connection/list * (A) /connection/stop// * (A) /transmission/list * /transmission/start/// * /transmission/stop// * (A) /plant/list * (A) /plant/stop/ Los que dicen (A) estan minimamente probados y andando bien. Los de transmision no los pude probar todavia porque me falta hacer la parte del cliente. En realidad no anda ninguno porque todavia no mandan XML :-D - Ahora las transmisiones son de la planta no del server (cosa que no me termina de agradar pero me simplificaba un poco las cosas). --- diff --git a/Server/include/plaqui/server/plant.h b/Server/include/plaqui/server/plant.h index 92f6afd..7709063 100644 --- a/Server/include/plaqui/server/plant.h +++ b/Server/include/plaqui/server/plant.h @@ -29,6 +29,8 @@ #define PLAQUI_PLANT_H #include "plaqui/server/runnable.h" +#include "plaqui/server/transmitter.h" +#include "simulator.h" #include namespace PlaQui { @@ -41,18 +43,35 @@ namespace Server { // Tipos. + private: + + /// Lista de conexiones de control. + typedef std::list TransmitterList; + public: /// Tipo de señal para indicar que se actualizó la planta. - typedef SigC::Signal0 SignalUpdated; + //typedef SigC::Signal0 SignalUpdated; // Atributos. private: + /// Transmisiones del estado de las plantas. + TransmitterList transmissions; + + /// Mutex para las transmisiones. + Glib::Mutex transmissions_mutex; + + /// Simulador usado para calcular el estado de la planta. + Model::Simulator simulator; + /// Señal para indicar que se actualizó la planta. - SignalUpdated updated; + //SignalUpdated updated; + + /// Nombre de la planta. + //std::string name; // Métodos. @@ -74,14 +93,31 @@ namespace Server { /** * Constructor desde un archivo. * + * \param name Nombre de la planta. * \param filename Nombre del archivo de donde obtener la planta. */ Plant(const std::string& filename); + /** + * Comienza una transmisión del estado de la planta. + * + * \return true si comenzó la transmisión, false si hubo problemas. + */ + bool transmission_start(const string& host, + const Connection::Port& port); + + /** + * Comienza una transmisión del estado de la planta. + * + * \return true si detuvo la transmisión, false si no existía. + */ + bool transmission_stop(const string& host, + const Connection::Port& port); + /** * Obtiene la señal para indicar que se actualizó la planta. */ - SignalUpdated& signal_updated(void); + //SignalUpdated& signal_updated(void); }; diff --git a/Server/include/plaqui/server/server.h b/Server/include/plaqui/server/server.h index 1179fdc..5f2e959 100644 --- a/Server/include/plaqui/server/server.h +++ b/Server/include/plaqui/server/server.h @@ -35,7 +35,7 @@ #include "plaqui/server/command.h" #include #include -#include +#include namespace PlaQui { @@ -51,22 +51,13 @@ namespace Server { private: - /// Lista de conexiones de control. - typedef std::list TransmitterList; - /// Lista de plantas químicas. - typedef std::list PlantList; + typedef std::map PlantList; // Atributos. private: - /// Transmisiones del estado de las plantas. - TransmitterList transmissions; - - /// Mutex para las transmisiones. - Glib::Mutex transmissions_mutex; - // Plantas disponibles en el servidor. PlantList plants; @@ -101,6 +92,31 @@ namespace Server { */ HTTPResponse* cmd_connection_stop(const Command& command); + /** + * Maneja el comando transmission/list. + */ + HTTPResponse* cmd_transmission_list(void); + + /** + * Maneja el comando transmission/start. + */ + HTTPResponse* cmd_transmission_start(const Command& command); + + /** + * Maneja el comando transmission/stop. + */ + HTTPResponse* cmd_transmission_stop(const Command& command); + + /** + * Maneja el comando plant/list. + */ + HTTPResponse* cmd_plant_list(void); + + /** + * Maneja el comando plant/stop. + */ + HTTPResponse* cmd_plant_stop(const Command& command); + public: /** @@ -116,37 +132,24 @@ namespace Server { Server(int port = 7522); /** - * Comienza la transimisión del estado de una planta. - * - * \param host Host al cual se quiere transmitir. - * \param port Puerto al cual transmitir. - * - * \return true si se pudo empezar a transmitir, false si no. - * - * \todo Ver si es necesario que devuelva algo y si devuelve ver si - * no sería mejor que dé más información (si no se pudo abrir - * o si ya estaba abierto por ejemplo. + * Maneja los comandos recibidos por las conexiones. */ - bool start_transmission(std::string host = "localhost", - int port = 7528); + void on_control_command_received(const Command& command, + ControlServer* controlserver); /** - * Finaliza la transimisión del estado de una planta. - * - * \param host Host al cual se quiere dejar de transmitir. - * \param port Puerto al cual dejar de transmitir. + * Se encarga de borrar una planta de la lista cuando finaliza. * - * \return true si se pudo empezar a transmitir, false si no. - * - * \todo Ver de hacer un TransmissionManager. + * \param connection Conexión a eliminar. */ - bool stop_transmission(std::string host, int port); + void on_plant_finished(const char* plant); /** - * Maneja los comandos recibidos por las conexiones. + * Envia el estado de una planta cuando esta se actualiza. + * + * \param plant Planta actualizada. */ - void on_control_command_received(const Command& command, - ControlServer* controlserver); + void on_plant_updated(const Plant* const); }; diff --git a/Server/include/plaqui/server/transmitter.h b/Server/include/plaqui/server/transmitter.h index 93b47e9..87b0bd2 100644 --- a/Server/include/plaqui/server/transmitter.h +++ b/Server/include/plaqui/server/transmitter.h @@ -29,6 +29,7 @@ #define PLAQUI_TRANSMITTER_H #include "plaqui/server/connection.h" +//#include "plaqui/server/plant.h" #include namespace PlaQui { @@ -38,6 +39,8 @@ namespace Server { /// Conexión para transmitir el estado de una planta. class Transmitter: public Connection { + // Métodos. + private: /** @@ -58,7 +61,13 @@ namespace Server { * \param host Host al cual transmitir. * \param port Puerto al cual transmitir. */ - Transmitter(std::string host = "localhost", int port = 7528); + Transmitter(const std::string& _host = "localhost", + const Connection::Port& _port = 7528); + + /** + * Envia datos. + */ + void send(const std::string& data); }; diff --git a/Server/src/Makefile b/Server/src/Makefile index d8e95f3..110f072 100644 --- a/Server/src/Makefile +++ b/Server/src/Makefile @@ -28,10 +28,11 @@ # Directorio con los .h INCLUDE_BASE_DIR=../include INCLUDE_DIR=$(INCLUDE_BASE_DIR)/plaqui/server +MODEL_DIR=../../Model # Opciones para el compilador. -CXXFLAGS=-ansi -pedantic -Wall -I$(INCLUDE_BASE_DIR) \ - `pkg-config --cflags glibmm-2.0 gthread-2.0` +CXXFLAGS=-ansi -pedantic -Wall -I$(INCLUDE_BASE_DIR) -I$(MODEL_DIR)/include \ + `pkg-config --cflags glibmm-2.0 gthread-2.0` `xml2-config --cflags` CXXFLAGS+=-g -DDEBUG #CXXFLAGS+=-g #CXXFLAGS+=-O2 @@ -58,6 +59,10 @@ httpmessage_h=$(string_h) $(httpheaders_h) $(INCLUDE_DIR)/httpmessage.h objects+=httpmessage.o httpmessage.o: $(httpmessage_h) httpmessage.cpp +httpresponse_h=$(string_h) $(httperror_h) $(httpmessage_h) $(INCLUDE_DIR)/httprequest.h +objects+=httpresponse.o +httpresponse.o: $(httpresponse_h) httpresponse.cpp + httprequest_h=$(string_h) $(httpmessage_h) $(INCLUDE_DIR)/httprequest.h objects+=httprequest.o httprequest.o: $(httprequest_h) httprequest.cpp @@ -66,9 +71,9 @@ command_h=$(string_h) $(httprequest_h) $(INCLUDE_DIR)/command.h objects+=command.o command.o: $(command_h) command.cpp -httpresponse_h=$(string_h) $(httperror_h) $(httpmessage_h) $(INCLUDE_DIR)/httprequest.h -objects+=httpresponse.o -httpresponse.o: $(httpresponse_h) httpresponse.cpp +plant_h=$(INCLUDE_DIR)/plant.h +objects+=plant.o +plant.o: $(plant_h) plant.cpp runnable_h=$(INCLUDE_DIR)/runnable.h objects+=runnable.o diff --git a/Server/src/command.cpp b/Server/src/command.cpp index f0e3ef1..47a4a60 100644 --- a/Server/src/command.cpp +++ b/Server/src/command.cpp @@ -54,7 +54,13 @@ Command::Command(const string& _target, const string& _command) { } void Command::build(void) { - uri = string("/") + target + '/' + command + String::join(args, "/"); + uri = string("/") + target; + if (command.length()) { + uri += '/' + command; + if (args.size()) { + uri += '/' + String::join(args, "/"); + } + } #ifdef DEBUG cerr << __FILE__ << ": build() = " << uri << endl; #endif // DEBUG diff --git a/Server/src/plant.cpp b/Server/src/plant.cpp new file mode 100644 index 0000000..49ad53a --- /dev/null +++ b/Server/src/plant.cpp @@ -0,0 +1,146 @@ +// 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: dom nov 16 13:03:33 ART 2003 +// Autores: Leandro Lucarella +//---------------------------------------------------------------------------- +// +// $Id$ +// + +#include "plaqui/server/plant.h" +#include +#include +#ifdef DEBUG +# include +#endif // DEBUG + +using namespace std; + +namespace PlaQui { + +namespace Server { + +Plant::~Plant(void) { +#ifdef DEBUG + cerr << __FILE__ << ": destructor." << endl; +#endif // DEBUG + // Termino transmisiones. + Glib::Mutex::Lock lock(transmissions_mutex); + for (TransmitterList::iterator trans = transmissions.end(); + trans != transmissions.end(); trans++) { + (*trans)->finish(true); + } +} + +Plant::Plant(const string& filename): simulator(filename) { +#ifdef DEBUG + cerr << __FILE__ << ": constructor. filename = " << filename << endl; +#endif // DEBUG + // TODO plant + simulator.add_pump("bomba1"); + simulator.add_conduct("c"); + simulator.add_conduct("c1"); + simulator.add_drainage("d"); + simulator.add_tank("tanque"); + + simulator.connect("bomba1", "c", Model::IConector::OUT); + simulator.connect("c", "tanque", Model::IConector::OUT); + simulator.connect("tanque", "c1", Model::IConector::OUT); + simulator.connect("c1", "d", Model::IConector::OUT); + +} + +void Plant::real_run(void) { +#ifdef DEBUG + cerr << __FILE__ << ": real_run." << endl; +#endif // DEBUG + while (!stop) { + simulator.simulate(); + Glib::Mutex::Lock lock(transmissions_mutex); + for (TransmitterList::iterator i = transmissions.begin(); + i != transmissions.end(); i++) { + (*i)->send(simulator.get_state_as_xml()); + } + Glib::usleep(1000000); + } +} + +bool Plant::transmission_start(const string& host, + const Connection::Port& port) { + Glib::Mutex::Lock lock(transmissions_mutex); + for (TransmitterList::iterator i = transmissions.begin(); + i != transmissions.end(); i++) { + if (((*i)->get_host() == host) && ((*i)->get_port() == port)) { + return false; + } + } + Transmitter* trans; + try { + trans = new Transmitter(host, port); + } catch (...) { // TODO - Hace mas selectivo el catch? + delete trans; + return false; + } + transmissions.push_back(trans); + trans->run(); + return true; +} + +bool Plant::transmission_stop(const string& host, + const Connection::Port& port) { + Glib::Mutex::Lock lock(transmissions_mutex); + for (TransmitterList::iterator i = transmissions.begin(); + i != transmissions.end(); i++) { + if (((*i)->get_host() == host) && ((*i)->get_port() == port)) { + (*i)->finish(); + return true; + } + } + return false; // No la encontró. +} + +/* +bool Plant::transmission_exists(const string& host, + const Connection::Port& port) { + Glib::Mutex::Lock lock(transmissions_mutex); + for (TransmitterList::const_iterator i = transmissions.begin(); + i != transmissions.end(); i++) { + if (((*i)->get_host() == host) && ((*i)->get_oprt() == port)) { + return true; + } + } + return false; // No la encontró. +} +*/ + +//const std::string& Plant::get_name(void) const { +// return name; +//} + +/// \todo FIXME esto deberia estar protegido por un mutex. +//Plant::SignalUpdated& Plant::signal_updated(void) { +// return updated; +//} + +} // namespace Server + +} // namespace PlaQui + diff --git a/Server/src/runnable.cpp b/Server/src/runnable.cpp index 49d0abb..68296e4 100644 --- a/Server/src/runnable.cpp +++ b/Server/src/runnable.cpp @@ -26,7 +26,7 @@ // #include "plaqui/server/runnable.h" -#include +#include #include #ifdef DEBUG # include diff --git a/Server/src/server.cpp b/Server/src/server.cpp index d4dd52a..8704d69 100644 --- a/Server/src/server.cpp +++ b/Server/src/server.cpp @@ -46,25 +46,27 @@ Server::~Server(void) { #ifdef DEBUG cerr << __FILE__ << ": destructor." << endl; #endif // DEBUG + // Termino plantas. + Glib::Mutex::Lock lock(plants_mutex); + for (PlantList::iterator i = plants.end(); i != plants.end(); i++) { + i->second->finish(true); + } } -Server::Server(int port): - TCPServer(port) { +Server::Server(int port): TCPServer(port) { #ifdef DEBUG cerr << __FILE__ << ": port = " << port << endl; #endif // DEBUG + // FIXME + Glib::Mutex::Lock lock(plants_mutex); + plants["prueba"] = new Plant("prueba.xml"); + plants["prueba"]->signal_finished().connect( + SigC::bind( + SigC::slot_class(*this, &Server::on_plant_finished), + "prueba")); + plants["prueba"]->run(); } -/// \todo Implementar. -bool Server::start_transmission(string host, int port) { -#ifdef DEBUG - cerr << __FILE__ << ": start_transmission(host = " << host - << " | port = " << port << ")" << endl; -#endif // DEBUG - // TODO - return false; -} - Connection* Server::new_connection( const sockbuf::sockdesc& sd) { #ifdef DEBUG @@ -81,17 +83,21 @@ Connection* Server::new_connection( return connection; } -/// \todo Implementar. -bool Server::stop_transmission(string host, int port) { +void Server::on_plant_updated(const Plant* plant) { #ifdef DEBUG - cerr << __FILE__ << ": stop_transmission(host = " << host - << " | port = " << port << ")" << endl; + cerr << __FILE__ << ": on_plant_updated(plant = " << plant << ")." << endl; #endif // DEBUG - // TODO - return false; } -/// \todo Implementar. +void Server::on_plant_finished(const char* plant) { +#ifdef DEBUG + cerr << __FILE__ << ": on_plant_finished(plant_name = " << plant << endl; +#endif // DEBUG + Glib::Mutex::Lock lock(plants_mutex); + plants.erase(plant); +} + +/// \todo Terminar de implementar. void Server::on_control_command_received(const Command& command, ControlServer* controlserver) { #ifdef DEBUG @@ -123,11 +129,25 @@ void Server::on_control_command_received(const Command& command, "Invalid command for 'connection' taget!"); } } else if (command.get_target() == "transmission") { - response = new HTTPResponse(HTTPMessage::NOT_FOUND, - "Invalid command for 'transmission' taget!"); + if (command.get_command() == "list") { + response = cmd_transmission_list(); + } else if (command.get_command() == "start") { + response = cmd_transmission_start(command); + } else if (command.get_command() == "stop") { + response = cmd_transmission_stop(command); + } else { + response = new HTTPResponse(HTTPMessage::NOT_FOUND, + "Invalid command for 'transmission' taget!"); + } } else if (command.get_target() == "plant") { - response = new HTTPResponse(HTTPMessage::NOT_FOUND, - "Invalid command for 'plant' taget!"); + if (command.get_command() == "list") { + response = cmd_plant_list(); + } else if (command.get_command() == "stop") { + response = cmd_plant_stop(command); + } else { + response = new HTTPResponse(HTTPMessage::NOT_FOUND, + "Invalid command for 'plant' taget!"); + } } else { response = new HTTPResponse(HTTPMessage::NOT_FOUND, "Invalid taget!"); } @@ -148,11 +168,11 @@ HTTPResponse* Server::cmd_server_status(void) const { stringstream response_xml; response_xml << "" << endl; response_xml << " " << endl; - response_xml << " PlaQui v0.7" << endl; + response_xml << " PlaQui v0.8" << endl; response_xml << " " << endl; response_xml << " " << endl; response_xml << "

PlaQui

" << endl; - response_xml << "

versión 0.7

" << endl; + response_xml << "

versión 0.8

" << endl; /* response_xml << "

Comando

" << endl; response_xml << "
    " << endl; response_xml << "
  • Target: " << command.get_target() << endl; @@ -164,8 +184,8 @@ HTTPResponse* Server::cmd_server_status(void) const { response_xml << "
  • " << *i << "
  • " << endl; } response_xml << " " << endl; - response_xml << "
" << endl; -*/ response_xml << "

Desarrollado por

" << endl; + response_xml << " " << endl;*/ + response_xml << "

Desarrollado por

" << endl; response_xml << "
    " << endl; response_xml << "
  • Nicolás Dimov.
  • " << endl; response_xml << "
  • Leandro Lucarella.
  • " << endl; @@ -186,11 +206,11 @@ HTTPResponse* Server::cmd_connection_list(void) { stringstream response_xml; response_xml << "" << endl; response_xml << " " << endl; - response_xml << " PlaQui v0.7" << endl; + response_xml << " PlaQui v0.8" << endl; response_xml << " " << endl; response_xml << " " << endl; response_xml << "

    PlaQui

    " << endl; - response_xml << "

    versión 0.7

    " << endl; + response_xml << "

    versión 0.8

    " << endl; response_xml << "

    Lista de conexiones:

    " << endl; response_xml << "
      " << endl; for (TCPServer::ConnectionInfoList::const_iterator i = cil.begin(); @@ -227,6 +247,137 @@ HTTPResponse* Server::cmd_connection_stop(const Command& command) { } } +HTTPResponse* Server::cmd_transmission_list(void) { + // FIXME + stringstream response_xml; + response_xml << "" << endl; + response_xml << " " << endl; + response_xml << " PlaQui v0.8" << endl; + response_xml << " " << endl; + response_xml << " " << endl; + response_xml << "

      PlaQui

      " << endl; + response_xml << "

      versión 0.8

      " << endl; + response_xml << "

      Lista de transmisiones:

      " << endl; + response_xml << "
        " << endl; + // TODO - recorrer todas las plantas y sus transmisiones. +/* transmissions_mutex.lock(); + for (TransmitterList::const_iterator i = transmissions.begin(); + i != transmissions.end(); i++) { + response_xml << "
      • " << (*i)->get_host() << ":" + << (*i)->get_port() << " [get_host() << "/" << (*i)->get_port() + << "\">desconectar]
      • " << endl; + } + transmissions_mutex.unlock();*/ + response_xml << "
      " << endl; + response_xml << "
      " << endl; + response_xml << " Copyleft 2003 - bajo los " << endl; + response_xml << " términos de la licencia GPL" << endl; + response_xml << "
      " << endl; + response_xml << " " << endl; + response_xml << "" << endl; + return new HTTPResponse(HTTPMessage::OK, response_xml.str()); +} + +HTTPResponse* Server::cmd_transmission_start(const Command& command) { + const Command::Arguments& args = command.get_args(); + if (args.size() < 3) { + return new HTTPResponse(HTTPMessage::CONFLICT, + "Faltan argumentos."); + } else { + const string& plant = args[0]; + const string& host = args[1]; + Connection::Port port = String(args[2]).to(port); + Glib::Mutex::Lock lock(plants_mutex); + PlantList::iterator p = plants.find(plant); + if (p == plants.end()) { + return new HTTPResponse(HTTPMessage::NOT_FOUND, + string("No existe la planta '") + plant + "'."); + // TODO - agregar chequeo de que la transmision a ese host:port no + // exista para otra planta? + } 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] + "."); + } else { + return new HTTPResponse(HTTPMessage::INTERNAL_SERVER_ERROR, + string("Error al crear la transmisión a de la planta '") + + plant + "' a " + host + ":" + args[2] + "."); + } + } +} + +HTTPResponse* Server::cmd_transmission_stop(const Command& command) { + const Command::Arguments& args = command.get_args(); + if (args.size() < 2) { + return new HTTPResponse(HTTPMessage::CONFLICT, + "Faltan argumentos."); + } else { + const string& host = args[0]; + Connection::Port port = String(args[1]).to(port); + for (PlantList::iterator i = plants.begin(); i != plants.end(); i++) { + // TODO - agregar chequeo para saber si existe una conexion (para + // tirar error de que no hay conexion o de que no se pudo + // desconectar. + if (i->second->transmission_stop(host, port)) { + return new HTTPResponse(HTTPMessage::OK, + string("Se finaliza la transmisión de la planta '") + + i->first + "' a " + host + ":" + args[1] + "."); + } + } + return new HTTPResponse(HTTPMessage::NOT_FOUND, + string("No se puede finalizar la transmisión a ") + + host + ":" + args[1] + "."); + } +} + +HTTPResponse* Server::cmd_plant_list(void) { + // FIXME + stringstream response_xml; + response_xml << "" << endl; + response_xml << " " << endl; + response_xml << " PlaQui v0.8" << endl; + response_xml << " " << endl; + response_xml << " " << endl; + response_xml << "

      PlaQui

      " << endl; + response_xml << "

      versión 0.8

      " << endl; + response_xml << "

      Lista de plantas:

      " << endl; + response_xml << "
        " << endl; + plants_mutex.lock(); + for (PlantList::const_iterator i = plants.begin(); + i != plants.end(); i++) { + response_xml << "
      • " << i->first + << " [first << "\">parar]
      • " + << endl; + } + plants_mutex.unlock(); + response_xml << "
      " << endl; + response_xml << "
      " << endl; + response_xml << " Copyleft 2003 - bajo los " << endl; + response_xml << " términos de la licencia GPL" << endl; + response_xml << "
      " << endl; + response_xml << " " << endl; + response_xml << "" << endl; + return new HTTPResponse(HTTPMessage::OK, response_xml.str()); +} + +HTTPResponse* Server::cmd_plant_stop(const Command& command) { + if (!command.get_args().size()) { + return new HTTPResponse(HTTPMessage::CONFLICT, + "Faltan argumentos."); + } + Glib::Mutex::Lock lock(plants_mutex); + const string name = command.get_args()[0]; + if (plants.find(name) == plants.end()) { + return new HTTPResponse(HTTPMessage::NOT_FOUND, + string("No existe la planta ") + name); + } + // TODO Ver si al frenar la planta se destruye (no deberia!!!) + plants[name]->finish(); + return new HTTPResponse(HTTPMessage::OK, + string("La planta '") + name + "' se cerrará en instantes..."); +} + } // namespace Server } // namespace PlaQui diff --git a/Server/src/tcpserver.cpp b/Server/src/tcpserver.cpp index d15f34d..e78e53c 100644 --- a/Server/src/tcpserver.cpp +++ b/Server/src/tcpserver.cpp @@ -41,6 +41,11 @@ TCPServer::~TCPServer(void) { #ifdef DEBUG cerr << __FILE__ << ": destructor." << endl; #endif // DEBUG + Glib::Mutex::Lock lock(connections_mutex); + for (ConnectionList::iterator con = connections.begin(); + con != connections.end(); con++) { + (*con)->finish(true); + } } TCPServer::TCPServer(const Connection::Port& port): socket(sockbuf::sock_stream) { diff --git a/Server/src/transmitter.cpp b/Server/src/transmitter.cpp index 4211e51..a195f6f 100644 --- a/Server/src/transmitter.cpp +++ b/Server/src/transmitter.cpp @@ -26,6 +26,7 @@ // #include "plaqui/server/transmitter.h" +#include #include #include #ifdef DEBUG @@ -44,29 +45,33 @@ Transmitter::~Transmitter(void) { #endif // DEBUG } -/// \todo debría conectarse en real_run() (?) -Transmitter::Transmitter(string host, int port): +Transmitter::Transmitter(const string& _host, const Connection::Port& _port): Connection(sockbuf::sock_dgram) { #ifdef DEBUG - cerr << __FILE__ << ": host = " << host - << " | port = " << port << endl; + cerr << __FILE__ << ": host = " << _host + << " | port = " << _port << endl; #endif // DEBUG - // FIXME - deberia ir en run(). - socket->connect(host.c_str(), port); + socket->connect(_host.c_str(), _port); + host = socket->peerhost(); + port = socket->peerport(); } /// \todo debría dar una excepción (?) void Transmitter::real_run(void) { #ifdef DEBUG - // FIXME - debería tirar una excepción? - if (!socket->is_open()) { - cerr << "No se pudo conectar a " << socket->peerhost() << - ":" << socket->peerport() << "." << endl; - } else { - cerr << "Conectado a " << socket->peerhost() << - ":" << socket->peerport() << "." << endl; + cerr << __FILE__ << ": real_run()." << endl; +#endif // DEBUG + // No hace nada, porque solo actua cuando se manda algo con send(). + while (!stop) { + Glib::usleep(1000); } +} + +void Transmitter::send(const string& data) { +#ifdef DEBUG + cerr << __FILE__ << ": send(data = " << data << ")." << endl; #endif // DEBUG + socket << data << flush; } } // namespace Server diff --git a/Server/tests/Makefile b/Server/tests/Makefile index 2479290..351525f 100644 --- a/Server/tests/Makefile +++ b/Server/tests/Makefile @@ -34,14 +34,16 @@ INCLUDE_DIR=$(INCLUDE_BASE_DIR) # Ubicación de archivos .a LIB_FILES=../src +MODEL_DIR=../../Model # Opciones para el compilador. -CXXFLAGS=-ansi -pedantic -Wall -I$(INCLUDE_DIR) \ - `pkg-config --cflags glibmm-2.0 gthread-2.0` +CXXFLAGS=-ansi -pedantic -Wall -I$(INCLUDE_DIR) -I$(MODEL_DIR)/include \ + `pkg-config --cflags glibmm-2.0 gthread-2.0` `xml2-config --cflags` CXXFLAGS+=-g -DDEBUG #CXXFLAGS+=-g #CXXFLAGS+=-O2 -LDFLAGS=-lsocket++ `pkg-config --libs glibmm-2.0 gthread-2.0` #-L$(LIB_FILES) +LDFLAGS=-lsocket++ `pkg-config --libs glibmm-2.0 gthread-2.0` \ + `xml2-config --libs` #-L$(LIB_FILES) TARGETS=server_test client_test @@ -53,7 +55,7 @@ $(LIB_FILES)/server.a: cd $(LIB_FILES) && $(MAKE) # Tests -server_test: $(LIB_FILES)/server.a server_test.o +server_test: $(LIB_FILES)/server.a $(MODEL_DIR)/src/model.a server_test.o # Tests client_test: $(LIB_FILES)/server.a client_test.o