]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blobdiff - Server/src/plant.cpp
Se agrega un try para tratar de solucionar situaciones en las que el server
[z.facultad/75.42/plaqui.git] / Server / src / plant.cpp
index 970fbabbb2fd2be308659ff40e4bf177584c0882..f3a0a98c2962c846955440429b982463a76b90c7 100644 (file)
@@ -61,23 +61,12 @@ Plant::~Plant(void) {
        }
 }
 
        }
 }
 
-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;
 #endif // DEBUG
 #ifdef DEBUG
        cerr << __FILE__ << "(" << __LINE__ << ")"
                << ": 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) throw() {
 }
 
 void Plant::real_run(void) throw() {
@@ -85,18 +74,26 @@ void Plant::real_run(void) throw() {
        cerr << __FILE__ << "(" << __LINE__ << ")"
                << ": real_run." << endl;
 #endif // DEBUG
        cerr << __FILE__ << "(" << __LINE__ << ")"
                << ": real_run." << endl;
 #endif // DEBUG
+       unsigned wait;
        while (!stop()) {
        while (!stop()) {
-               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);
+               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(100000);
        }
 }
 
        }
 }
 
@@ -121,25 +118,16 @@ bool Plant::transmission_start(string& host, Connection::Port& port) {
        Transmitter* trans;
        try {
                trans = new Transmitter(host, port);
        Transmitter* trans;
        try {
                trans = new Transmitter(host, port);
-       } catch (const sockerr& e) { // TODO - Hace mas selectivo el catch?
+       } catch (const sockerr& e) {
 #ifdef DEBUG
                cerr << __FILE__ << "(" << __LINE__ << ")"
                        << ": transmission_start ERROR (" << e.serrno()
                        << "): " << e.errstr() << endl;
 #endif // DEBUG
 #ifdef DEBUG
                cerr << __FILE__ << "(" << __LINE__ << ")"
                        << ": transmission_start ERROR (" << e.serrno()
                        << "): " << e.errstr() << endl;
 #endif // DEBUG
-               //delete trans;
                return false;
                return false;
-//     } catch (...) { // TODO - Hace mas selectivo el catch?
-//#ifdef DEBUG
-//             cerr << __FILE__ << "(" << __LINE__ << ")"
-//                     << ": transmission_start ERROR: desconocido."
-//                     << endl;
-//#endif // DEBUG
-//             //delete trans;
-//             return false;
        }
        transmissions.push_back(trans);
        }
        transmissions.push_back(trans);
-       trans->signal_finished().connect(SigC::bind<Transmitter*>(
+       trans->signal_finished().connect(SigC::bind(
                                SigC::slot_class(*this, &Plant::on_transmission_finished),
                                trans));
        trans->run();
                                SigC::slot_class(*this, &Plant::on_transmission_finished),
                                trans));
        trans->run();
@@ -201,32 +189,22 @@ const string Plant::get_xml(void) const {
 #endif // DEBUG
        ostringstream oss;
        ifstream ifs(filename.c_str());
 #endif // DEBUG
        ostringstream oss;
        ifstream ifs(filename.c_str());
+       // FIXME Saco la línea de definición de XML (<?xml ?>), ver si esta hecho muy
+       // feo.
+       ifs.ignore(50, '\n'); // Ignora 50 caracteres o hasta un enter.
        ifs >> oss.rdbuf();
        return oss.str();
 }
 
        ifs >> oss.rdbuf();
        return oss.str();
 }
 
-/*
-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ó.
+void Plant::set_frequency(unsigned hz) {
+       Glib::Mutex::Lock lock(wait_time_mutex);
+       wait_time = hz ? (1000000u/hz) : DEFAULT_WAIT_TIME;
 }
 }
-*/
-
-//const std::string& Plant::get_filename(void) const {
-//     return filename;
-//}
 
 
-/// \todo FIXME esto deberia estar protegido por un mutex.
-//Plant::SignalUpdated& Plant::signal_updated(void) {
-//     return updated;
-//}
+void Plant::set_paused(bool paused_) {
+       Glib::Mutex::Lock lock(paused_mutex);
+       paused = paused_;
+}
 
 } // namespace Server
 
 
 } // namespace Server