]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Server/src/plant.cpp
Se agrega un archivo olvidado
[z.facultad/75.42/plaqui.git] / Server / src / plant.cpp
1 // vim: set noexpandtab tabstop=4 shiftwidth=4:
2 //----------------------------------------------------------------------------
3 //                                  PlaQui
4 //----------------------------------------------------------------------------
5 // This file is part of PlaQui.
6 //
7 // PlaQui is free software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the Free Software
9 // Foundation; either version 2 of the License, or (at your option) any later
10 // version.
11 //
12 // PlaQui is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 // details.
16 //
17 // You should have received a copy of the GNU General Public License along
18 // with PlaQui; if not, write to the Free Software Foundation, Inc., 59 Temple
19 // Place, Suite 330, Boston, MA  02111-1307  USA
20 //----------------------------------------------------------------------------
21 // Creado:  dom nov 16 13:03:33 ART 2003
22 // Autores: Leandro Lucarella <llucare@fi.uba.ar>
23 //----------------------------------------------------------------------------
24 //
25 // $Id$
26 //
27
28 #include "plaqui/server/plant.h"
29 #include <glibmm/timer.h>
30 #include <sigc++/class_slot.h>
31 #include <fstream>
32 #ifdef DEBUG
33 #       include <iostream>
34 #endif // DEBUG
35
36 using namespace std;
37
38 namespace PlaQui {
39
40 namespace Server {
41
42 Plant::~Plant(void) {
43 #ifdef DEBUG
44         cerr << __FILE__ << "(" << __LINE__ << ")"
45                 << ": destructor." << endl;
46 #endif // DEBUG
47         // Mando a terminar todas las transmisiones.
48         transmissions_mutex.lock();
49         for (TransmitterList::iterator trans = transmissions.begin();
50                         trans != transmissions.end(); trans++) {
51                 (*trans)->finish();
52         }
53         TransmitterList::size_type count = transmissions.size();
54         transmissions_mutex.unlock();
55         // Espero que terminen realmente.
56         while (count) {
57                 Glib::usleep(10000); // 10 milisegundos
58                 transmissions_mutex.lock();
59                 count = transmissions.size();
60                 transmissions_mutex.unlock();
61         }
62 }
63
64 Plant::Plant(const string& filename): simulator(filename), filename(filename),
65                 wait_time(DEFAULT_WAIT_TIME), paused(true) {
66 #ifdef DEBUG
67         cerr << __FILE__ << "(" << __LINE__ << ")"
68                 << ": constructor. filename = " << filename << endl;
69 #endif // DEBUG
70         // TODO plant
71 /*      simulator.add_pump("bomba1");
72         simulator.add_conduct("c");
73         simulator.add_conduct("c1");
74         simulator.add_drainage("d");
75         simulator.add_tank("tanque");
76         
77         simulator.connect("bomba1", "c", Model::IConector::OUT);
78         simulator.connect("c", "tanque", Model::IConector::OUT);
79         simulator.connect("tanque", "c1", Model::IConector::OUT);
80         simulator.connect("c1", "d", Model::IConector::OUT);
81 */      
82 }
83
84 void Plant::real_run(void) throw() {
85 #ifdef DEBUG
86         cerr << __FILE__ << "(" << __LINE__ << ")"
87                 << ": real_run." << endl;
88 #endif // DEBUG
89         unsigned wait;
90         while (!stop()) {
91                 if (paused) { // Si está pausada, espera un tiempo sin simular.
92                         Glib::usleep(DEFAULT_WAIT_TIME);
93                 } else { // Si está andando, simula y manda estado.
94                         simulator_mutex.lock();
95                         simulator.simulate();
96                         string plantstatus = simulator.get_state_as_xml();
97                         simulator_mutex.unlock();
98                         transmissions_mutex.lock();
99                         for (TransmitterList::iterator i = transmissions.begin();
100                                         i != transmissions.end(); i++) {
101                                 (*i)->send(plantstatus);
102                         }
103                         transmissions_mutex.unlock();
104                         wait_time_mutex.lock();
105                         wait = wait_time;
106                         wait_time_mutex.unlock();
107                         Glib::usleep(wait);
108                 }
109         }
110 }
111
112 bool Plant::transmission_start(string& host, Connection::Port& port) {
113 #ifdef DEBUG
114                 cerr << __FILE__ << "(" << __LINE__ << ")"
115                         << ": transmission_start(host = " << host <<
116                         ", port = " << port << ")." << endl;
117 #endif // DEBUG
118         Glib::Mutex::Lock lock(transmissions_mutex);
119         for (TransmitterList::iterator i = transmissions.begin();
120                         i != transmissions.end(); i++) {
121                 if (((*i)->get_host() == host) && ((*i)->get_port() == port)) {
122 #ifdef DEBUG
123                         cerr << __FILE__ << "(" << __LINE__ << ")"
124                                 << ": transmission_start ERROR: ya existe."
125                                 << endl;
126 #endif // DEBUG
127                         return false;
128                 }
129         }
130         Transmitter* trans;
131         try {
132                 trans = new Transmitter(host, port);
133         } catch (const sockerr& e) { // TODO - Hace mas selectivo el catch?
134 #ifdef DEBUG
135                 cerr << __FILE__ << "(" << __LINE__ << ")"
136                         << ": transmission_start ERROR (" << e.serrno()
137                         << "): " << e.errstr() << endl;
138 #endif // DEBUG
139                 //delete trans;
140                 return false;
141 //      } catch (...) { // TODO - Hace mas selectivo el catch?
142 //#ifdef DEBUG
143 //              cerr << __FILE__ << "(" << __LINE__ << ")"
144 //                      << ": transmission_start ERROR: desconocido."
145 //                      << endl;
146 //#endif // DEBUG
147 //              //delete trans;
148 //              return false;
149         }
150         transmissions.push_back(trans);
151         trans->signal_finished().connect(SigC::bind(
152                                 SigC::slot_class(*this, &Plant::on_transmission_finished),
153                                 trans));
154         trans->run();
155         host = trans->get_host();
156         port = trans->get_port();
157         return true;
158 }
159
160 bool Plant::transmission_stop(const string& host,
161                 const Connection::Port& port) {
162 #ifdef DEBUG
163         cerr << __FILE__ << "(" << __LINE__ << ")"
164                 << ": transmission_stop(host = " << host <<
165                 ", port = " << port << ")." << endl;
166 #endif // DEBUG
167         Glib::Mutex::Lock lock(transmissions_mutex);
168         for (TransmitterList::iterator i = transmissions.begin();
169                         i != transmissions.end(); i++) {
170                 if (((*i)->get_host() == host) && ((*i)->get_port() == port)) {
171                         (*i)->finish();
172                         return true;
173                 }
174         }
175         return false; // No la encontró.
176 }
177
178 void Plant::on_transmission_finished(Transmitter* transmission) {
179 #ifdef DEBUG
180         cerr << __FILE__ << "(" << __LINE__ << ")"
181                 <<  ": on_transmission_finished(transmission = "
182                 << transmission << ")" << endl;
183 #endif // DEBUG
184         Glib::Mutex::Lock lock(transmissions_mutex);
185         transmissions.remove(transmission);
186 #ifdef DEBUG
187         cerr << __FILE__ << "(" << __LINE__ << ")"
188                 <<  ": lista de conexiones" << endl;
189         for (TransmitterList::const_iterator i = transmissions.begin();
190                         i != transmissions.end(); i++) {
191                 cerr << "\t " << *i << endl;
192         }
193 #endif // DEBUG
194 }
195
196 bool Plant::set_open(const std::string& element, bool open) {
197 #ifdef DEBUG
198                 cerr << __FILE__ << "(" << __LINE__ << ")"
199                         << ": set_open(element = " << element <<
200                         ", open = " << open << ")." << endl;
201 #endif // DEBUG
202         Glib::Mutex::Lock lock(simulator_mutex);
203         return simulator.set_open(element, open);
204 }
205
206 const string Plant::get_xml(void) const {
207 #ifdef DEBUG
208                 cerr << __FILE__ << "(" << __LINE__ << ")"
209                         << ": get_xml()." << endl;
210 #endif // DEBUG
211         ostringstream oss;
212         ifstream ifs(filename.c_str());
213         // XXX Saco la línea de definición de XML (<?xml ?>), ver si esta hecho muy
214         // feo.
215         ifs.ignore(50, '\n'); // Ignora 50 caracteres o hasta un enter.
216         ifs >> oss.rdbuf();
217         return oss.str();
218 }
219
220 void Plant::set_frequency(unsigned hz) {
221         Glib::Mutex::Lock lock(wait_time_mutex);
222         wait_time = hz ? (1000000u/hz) : DEFAULT_WAIT_TIME;
223 }
224
225 void Plant::set_paused(bool paused_) {
226         Glib::Mutex::Lock lock(paused_mutex);
227         paused = paused_;
228 }
229
230 /*
231 bool Plant::transmission_exists(const string& host,
232                 const Connection::Port& port) {
233         Glib::Mutex::Lock lock(transmissions_mutex);
234         for (TransmitterList::const_iterator i = transmissions.begin();
235                         i != transmissions.end(); i++) {
236                 if (((*i)->get_host() == host) && ((*i)->get_oprt() == port)) {
237                         return true;
238                 }
239         }
240         return false; // No la encontró.
241 }
242 */
243
244 //const std::string& Plant::get_filename(void) const {
245 //      return filename;
246 //}
247
248 /// \todo FIXME esto deberia estar protegido por un mutex.
249 //Plant::SignalUpdated& Plant::signal_updated(void) {
250 //      return updated;
251 //}
252
253 } // namespace Server
254
255 } // namespace PlaQui
256