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++/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 simulator_mutex.unlock();
83 transmissions_mutex.lock();
84 for (TransmitterList::iterator i = transmissions.begin();
85 i != transmissions.end(); i++) {
86 (*i)->send(simulator.get_state_as_xml());
88 transmissions_mutex.unlock();
89 Glib::usleep(1000000);
93 bool Plant::transmission_start(string& host, Connection::Port& port) {
95 cerr << __FILE__ << "(" << __LINE__ << ")"
96 << ": transmission_start(host = " << host <<
97 ", port = " << port << ")." << endl;
99 Glib::Mutex::Lock lock(transmissions_mutex);
100 for (TransmitterList::iterator i = transmissions.begin();
101 i != transmissions.end(); i++) {
102 if (((*i)->get_host() == host) && ((*i)->get_port() == port)) {
104 cerr << __FILE__ << "(" << __LINE__ << ")"
105 << ": transmission_start ERROR: ya existe."
113 trans = new Transmitter(host, port);
114 } catch (const sockerr& e) { // TODO - Hace mas selectivo el catch?
116 cerr << __FILE__ << "(" << __LINE__ << ")"
117 << ": transmission_start ERROR (" << e.serrno()
118 << "): " << e.errstr() << endl;
122 // } catch (...) { // TODO - Hace mas selectivo el catch?
124 // cerr << __FILE__ << "(" << __LINE__ << ")"
125 // << ": transmission_start ERROR: desconocido."
131 transmissions.push_back(trans);
133 host = trans->get_host();
134 port = trans->get_port();
138 bool Plant::transmission_stop(const string& host,
139 const Connection::Port& port) {
141 cerr << __FILE__ << "(" << __LINE__ << ")"
142 << ": transmission_stop(host = " << host <<
143 ", port = " << port << ")." << endl;
145 Glib::Mutex::Lock lock(transmissions_mutex);
146 for (TransmitterList::iterator i = transmissions.begin();
147 i != transmissions.end(); i++) {
148 if (((*i)->get_host() == host) && ((*i)->get_port() == port)) {
153 return false; // No la encontró.
156 bool Plant::set_open(const std::string& element, bool open) {
158 cerr << __FILE__ << "(" << __LINE__ << ")"
159 << ": set_open(element = " << element <<
160 ", open = " << open << ")." << endl;
162 Glib::Mutex::Lock lock(simulator_mutex);
163 return simulator.set_open(element, open);
166 const string Plant::get_xml(void) const {
168 cerr << __FILE__ << "(" << __LINE__ << ")"
169 << ": get_xml()." << endl;
172 ifstream ifs(filename.c_str());
178 bool Plant::transmission_exists(const string& host,
179 const Connection::Port& port) {
180 Glib::Mutex::Lock lock(transmissions_mutex);
181 for (TransmitterList::const_iterator i = transmissions.begin();
182 i != transmissions.end(); i++) {
183 if (((*i)->get_host() == host) && ((*i)->get_oprt() == port)) {
187 return false; // No la encontró.
191 //const std::string& Plant::get_filename(void) const {
195 /// \todo FIXME esto deberia estar protegido por un mutex.
196 //Plant::SignalUpdated& Plant::signal_updated(void) {
200 } // namespace Server
202 } // namespace PlaQui