]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blobdiff - Server/src/plant.cpp
Se pone mas prolijo el tema de la respuesta.
[z.facultad/75.42/plaqui.git] / Server / src / plant.cpp
index 5704a88ddb16fefb9db741fdd6b3c6a9a201779c..1187c517533840a389d7db58ef29802ef7ceed4c 100644 (file)
@@ -61,7 +61,8 @@ 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;
@@ -85,18 +86,26 @@ void Plant::real_run(void) throw() {
        cerr << __FILE__ << "(" << __LINE__ << ")"
                << ": real_run." << endl;
 #endif // DEBUG
+       unsigned wait;
        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);
        }
 }
 
@@ -201,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 (<?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) {