X-Git-Url: https://git.llucax.com/z.facultad/75.42/plaqui.git/blobdiff_plain/7e74b790d290cd7d776349503bb361c47933c01f..1855f998e5c0e51e2c1958724ac84cc13a87d84c:/Server/src/plant.cpp diff --git a/Server/src/plant.cpp b/Server/src/plant.cpp index b01f1a5..1187c51 100644 --- a/Server/src/plant.cpp +++ b/Server/src/plant.cpp @@ -27,7 +27,7 @@ #include "plaqui/server/plant.h" #include -#include +#include #include #ifdef DEBUG # include @@ -44,15 +44,25 @@ Plant::~Plant(void) { cerr << __FILE__ << "(" << __LINE__ << ")" << ": destructor." << endl; #endif // DEBUG - // Termino transmisiones. - Glib::Mutex::Lock lock(transmissions_mutex); - for (TransmitterList::iterator trans = transmissions.end(); + // Mando a terminar todas las transmisiones. + transmissions_mutex.lock(); + for (TransmitterList::iterator trans = transmissions.begin(); trans != transmissions.end(); trans++) { - (*trans)->finish(true); + (*trans)->finish(); + } + TransmitterList::size_type count = transmissions.size(); + transmissions_mutex.unlock(); + // Espero que terminen realmente. + while (count) { + Glib::usleep(10000); // 10 milisegundos + transmissions_mutex.lock(); + count = transmissions.size(); + transmissions_mutex.unlock(); } } -Plant::Plant(const string& filename): simulator(filename), filename(filename) { +Plant::Plant(const string& filename): simulator(filename), filename(filename), + wait_time(DEFAULT_WAIT_TIME), paused(true) { #ifdef DEBUG cerr << __FILE__ << "(" << __LINE__ << ")" << ": constructor. filename = " << filename << endl; @@ -71,22 +81,31 @@ Plant::Plant(const string& filename): simulator(filename), filename(filename) { */ } -void Plant::real_run(void) { +void Plant::real_run(void) throw() { #ifdef DEBUG cerr << __FILE__ << "(" << __LINE__ << ")" << ": real_run." << endl; #endif // DEBUG - while (!stop) { - simulator_mutex.lock(); - simulator.simulate(); - simulator_mutex.unlock(); - transmissions_mutex.lock(); - for (TransmitterList::iterator i = transmissions.begin(); - i != transmissions.end(); i++) { - (*i)->send(simulator.get_state_as_xml()); + unsigned wait; + while (!stop()) { + if (paused) { // Si está pausada, espera un tiempo sin simular. + Glib::usleep(DEFAULT_WAIT_TIME); + } else { // Si está andando, simula y manda estado. + simulator_mutex.lock(); + simulator.simulate(); + string plantstatus = simulator.get_state_as_xml(); + simulator_mutex.unlock(); + transmissions_mutex.lock(); + for (TransmitterList::iterator i = transmissions.begin(); + i != transmissions.end(); i++) { + (*i)->send(plantstatus); + } + transmissions_mutex.unlock(); + wait_time_mutex.lock(); + wait = wait_time; + wait_time_mutex.unlock(); + Glib::usleep(wait); } - transmissions_mutex.unlock(); - Glib::usleep(1000000); } } @@ -129,6 +148,9 @@ bool Plant::transmission_start(string& host, Connection::Port& port) { // return false; } transmissions.push_back(trans); + trans->signal_finished().connect(SigC::bind( + SigC::slot_class(*this, &Plant::on_transmission_finished), + trans)); trans->run(); host = trans->get_host(); port = trans->get_port(); @@ -138,9 +160,9 @@ bool Plant::transmission_start(string& host, Connection::Port& port) { bool Plant::transmission_stop(const string& host, const Connection::Port& port) { #ifdef DEBUG - cerr << __FILE__ << "(" << __LINE__ << ")" - << ": transmission_stop(host = " << host << - ", port = " << port << ")." << endl; + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": transmission_stop(host = " << host << + ", port = " << port << ")." << endl; #endif // DEBUG Glib::Mutex::Lock lock(transmissions_mutex); for (TransmitterList::iterator i = transmissions.begin(); @@ -153,6 +175,24 @@ bool Plant::transmission_stop(const string& host, return false; // No la encontró. } +void Plant::on_transmission_finished(Transmitter* transmission) { +#ifdef DEBUG + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": on_transmission_finished(transmission = " + << transmission << ")" << endl; +#endif // DEBUG + Glib::Mutex::Lock lock(transmissions_mutex); + transmissions.remove(transmission); +#ifdef DEBUG + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": lista de conexiones" << endl; + for (TransmitterList::const_iterator i = transmissions.begin(); + i != transmissions.end(); i++) { + cerr << "\t " << *i << endl; + } +#endif // DEBUG +} + bool Plant::set_open(const std::string& element, bool open) { #ifdef DEBUG cerr << __FILE__ << "(" << __LINE__ << ")" @@ -170,10 +210,23 @@ const string Plant::get_xml(void) const { #endif // DEBUG ostringstream oss; ifstream ifs(filename.c_str()); + // XXX Saco la línea de definición de XML (), ver si esta hecho muy + // feo. + ifs.ignore(50, '\n'); // Ignora 50 caracteres o hasta un enter. ifs >> oss.rdbuf(); return oss.str(); } +void Plant::set_frequency(unsigned hz) { + Glib::Mutex::Lock lock(wait_time_mutex); + wait_time = hz ? (1000000u/hz) : DEFAULT_WAIT_TIME; +} + +void Plant::set_paused(bool paused_) { + Glib::Mutex::Lock lock(paused_mutex); + paused = paused_; +} + /* bool Plant::transmission_exists(const string& host, const Connection::Port& port) {