1 // vim: set noexpandtab tabstop=4 shiftwidth=4:
2 //----------------------------------------------------------------------------
4 //----------------------------------------------------------------------------
5 // This file is part of PlaQui.
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
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
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 //----------------------------------------------------------------------------
28 #include "plaqui/server/plant.h"
29 #include <glibmm/timer.h>
30 #include <sigc++/class_slot.h>
44 cerr << __FILE__ << "(" << __LINE__ << ")"
45 << ": destructor." << endl;
47 // Mando a terminar todas las transmisiones.
48 transmissions_mutex.lock();
49 for (TransmitterList::iterator trans = transmissions.begin();
50 trans != transmissions.end(); trans++) {
53 TransmitterList::size_type count = transmissions.size();
54 transmissions_mutex.unlock();
55 // Espero que terminen realmente.
57 Glib::usleep(10000); // 10 milisegundos
58 transmissions_mutex.lock();
59 count = transmissions.size();
60 transmissions_mutex.unlock();
64 Plant::Plant(const string& filename): simulator(filename), filename(filename),
65 wait_time(DEFAULT_WAIT_TIME), paused(true) {
67 cerr << __FILE__ << "(" << __LINE__ << ")"
68 << ": constructor. filename = " << filename << endl;
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");
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);
84 void Plant::real_run(void) throw() {
86 cerr << __FILE__ << "(" << __LINE__ << ")"
87 << ": real_run." << endl;
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();
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);
103 transmissions_mutex.unlock();
104 wait_time_mutex.lock();
106 wait_time_mutex.unlock();
112 bool Plant::transmission_start(string& host, Connection::Port& port) {
114 cerr << __FILE__ << "(" << __LINE__ << ")"
115 << ": transmission_start(host = " << host <<
116 ", port = " << port << ")." << endl;
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)) {
123 cerr << __FILE__ << "(" << __LINE__ << ")"
124 << ": transmission_start ERROR: ya existe."
132 trans = new Transmitter(host, port);
133 } catch (const sockerr& e) { // TODO - Hace mas selectivo el catch?
135 cerr << __FILE__ << "(" << __LINE__ << ")"
136 << ": transmission_start ERROR (" << e.serrno()
137 << "): " << e.errstr() << endl;
141 // } catch (...) { // TODO - Hace mas selectivo el catch?
143 // cerr << __FILE__ << "(" << __LINE__ << ")"
144 // << ": transmission_start ERROR: desconocido."
150 transmissions.push_back(trans);
151 trans->signal_finished().connect(SigC::bind(
152 SigC::slot_class(*this, &Plant::on_transmission_finished),
155 host = trans->get_host();
156 port = trans->get_port();
160 bool Plant::transmission_stop(const string& host,
161 const Connection::Port& port) {
163 cerr << __FILE__ << "(" << __LINE__ << ")"
164 << ": transmission_stop(host = " << host <<
165 ", port = " << port << ")." << endl;
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)) {
175 return false; // No la encontró.
178 void Plant::on_transmission_finished(Transmitter* transmission) {
180 cerr << __FILE__ << "(" << __LINE__ << ")"
181 << ": on_transmission_finished(transmission = "
182 << transmission << ")" << endl;
184 Glib::Mutex::Lock lock(transmissions_mutex);
185 transmissions.remove(transmission);
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;
196 bool Plant::set_open(const std::string& element, bool open) {
198 cerr << __FILE__ << "(" << __LINE__ << ")"
199 << ": set_open(element = " << element <<
200 ", open = " << open << ")." << endl;
202 Glib::Mutex::Lock lock(simulator_mutex);
203 return simulator.set_open(element, open);
206 const string Plant::get_xml(void) const {
208 cerr << __FILE__ << "(" << __LINE__ << ")"
209 << ": get_xml()." << endl;
212 ifstream ifs(filename.c_str());
217 void Plant::set_frequency(unsigned hz) {
218 Glib::Mutex::Lock lock(wait_time_mutex);
219 wait_time = hz ? (1000000u/hz) : DEFAULT_WAIT_TIME;
222 void Plant::set_paused(bool paused_) {
223 Glib::Mutex::Lock lock(paused_mutex);
228 bool Plant::transmission_exists(const string& host,
229 const Connection::Port& port) {
230 Glib::Mutex::Lock lock(transmissions_mutex);
231 for (TransmitterList::const_iterator i = transmissions.begin();
232 i != transmissions.end(); i++) {
233 if (((*i)->get_host() == host) && ((*i)->get_oprt() == port)) {
237 return false; // No la encontró.
241 //const std::string& Plant::get_filename(void) const {
245 /// \todo FIXME esto deberia estar protegido por un mutex.
246 //Plant::SignalUpdated& Plant::signal_updated(void) {
250 } // namespace Server
252 } // namespace PlaQui