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 // Termino transmisiones.
48 Glib::Mutex::Lock lock(transmissions_mutex);
49 for (TransmitterList::iterator trans = transmissions.end();
50 trans != transmissions.end(); trans++) {
51 (*trans)->finish(true);
55 Plant::Plant(const string& filename): simulator(filename), filename(filename) {
57 cerr << __FILE__ << "(" << __LINE__ << ")"
58 << ": constructor. filename = " << filename << endl;
61 /* simulator.add_pump("bomba1");
62 simulator.add_conduct("c");
63 simulator.add_conduct("c1");
64 simulator.add_drainage("d");
65 simulator.add_tank("tanque");
67 simulator.connect("bomba1", "c", Model::IConector::OUT);
68 simulator.connect("c", "tanque", Model::IConector::OUT);
69 simulator.connect("tanque", "c1", Model::IConector::OUT);
70 simulator.connect("c1", "d", Model::IConector::OUT);
74 void Plant::real_run(void) {
76 cerr << __FILE__ << "(" << __LINE__ << ")"
77 << ": real_run." << endl;
80 simulator_mutex.lock();
82 string plantstatus = simulator.get_state_as_xml();
83 simulator_mutex.unlock();
84 transmissions_mutex.lock();
85 for (TransmitterList::iterator i = transmissions.begin();
86 i != transmissions.end(); i++) {
87 (*i)->send(plantstatus);
89 transmissions_mutex.unlock();
90 Glib::usleep(1000000);
94 bool Plant::transmission_start(string& host, Connection::Port& port) {
96 cerr << __FILE__ << "(" << __LINE__ << ")"
97 << ": transmission_start(host = " << host <<
98 ", port = " << port << ")." << endl;
100 Glib::Mutex::Lock lock(transmissions_mutex);
101 for (TransmitterList::iterator i = transmissions.begin();
102 i != transmissions.end(); i++) {
103 if (((*i)->get_host() == host) && ((*i)->get_port() == port)) {
105 cerr << __FILE__ << "(" << __LINE__ << ")"
106 << ": transmission_start ERROR: ya existe."
114 trans = new Transmitter(host, port);
115 } catch (const sockerr& e) { // TODO - Hace mas selectivo el catch?
117 cerr << __FILE__ << "(" << __LINE__ << ")"
118 << ": transmission_start ERROR (" << e.serrno()
119 << "): " << e.errstr() << endl;
123 // } catch (...) { // TODO - Hace mas selectivo el catch?
125 // cerr << __FILE__ << "(" << __LINE__ << ")"
126 // << ": transmission_start ERROR: desconocido."
132 transmissions.push_back(trans);
133 trans->signal_finished().connect(SigC::bind<Transmitter*>(
134 SigC::slot_class(*this, &Plant::on_transmission_finished),
137 host = trans->get_host();
138 port = trans->get_port();
142 bool Plant::transmission_stop(const string& host,
143 const Connection::Port& port) {
145 cerr << __FILE__ << "(" << __LINE__ << ")"
146 << ": transmission_stop(host = " << host <<
147 ", port = " << port << ")." << endl;
149 Glib::Mutex::Lock lock(transmissions_mutex);
150 for (TransmitterList::iterator i = transmissions.begin();
151 i != transmissions.end(); i++) {
152 if (((*i)->get_host() == host) && ((*i)->get_port() == port)) {
157 return false; // No la encontró.
160 void Plant::on_transmission_finished(Transmitter* transmission) {
162 cerr << __FILE__ << "(" << __LINE__ << ")"
163 << ": on_transmission_finished(transmission = "
164 << transmission << ")" << endl;
166 Glib::Mutex::Lock lock(transmissions_mutex);
167 transmissions.remove(transmission);
169 cerr << __FILE__ << "(" << __LINE__ << ")"
170 << ": lista de conexiones" << endl;
171 for (TransmitterList::const_iterator i = transmissions.begin();
172 i != transmissions.end(); i++) {
173 cerr << "\t " << *i << endl;
178 bool Plant::set_open(const std::string& element, bool open) {
180 cerr << __FILE__ << "(" << __LINE__ << ")"
181 << ": set_open(element = " << element <<
182 ", open = " << open << ")." << endl;
184 Glib::Mutex::Lock lock(simulator_mutex);
185 return simulator.set_open(element, open);
188 const string Plant::get_xml(void) const {
190 cerr << __FILE__ << "(" << __LINE__ << ")"
191 << ": get_xml()." << endl;
194 ifstream ifs(filename.c_str());
200 bool Plant::transmission_exists(const string& host,
201 const Connection::Port& port) {
202 Glib::Mutex::Lock lock(transmissions_mutex);
203 for (TransmitterList::const_iterator i = transmissions.begin();
204 i != transmissions.end(); i++) {
205 if (((*i)->get_host() == host) && ((*i)->get_oprt() == port)) {
209 return false; // No la encontró.
213 //const std::string& Plant::get_filename(void) const {
217 /// \todo FIXME esto deberia estar protegido por un mutex.
218 //Plant::SignalUpdated& Plant::signal_updated(void) {
222 } // namespace Server
224 } // namespace PlaQui