]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Server/src/plant.cpp
Scripts de configuracion a nivel global.
[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         // 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);
52         }
53 }
54
55 Plant::Plant(const string& filename): simulator(filename), filename(filename) {
56 #ifdef DEBUG
57         cerr << __FILE__ << "(" << __LINE__ << ")"
58                 << ": constructor. filename = " << filename << endl;
59 #endif // DEBUG
60         // TODO plant
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");
66         
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);
71 */      
72 }
73
74 void Plant::real_run(void) {
75 #ifdef DEBUG
76         cerr << __FILE__ << "(" << __LINE__ << ")"
77                 << ": real_run." << endl;
78 #endif // DEBUG
79         while (!stop) {
80                 simulator_mutex.lock();
81                 simulator.simulate();
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);
88                 }
89                 transmissions_mutex.unlock();
90                 Glib::usleep(1000000);
91         }
92 }
93
94 bool Plant::transmission_start(string& host, Connection::Port& port) {
95 #ifdef DEBUG
96                 cerr << __FILE__ << "(" << __LINE__ << ")"
97                         << ": transmission_start(host = " << host <<
98                         ", port = " << port << ")." << endl;
99 #endif // DEBUG
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)) {
104 #ifdef DEBUG
105                         cerr << __FILE__ << "(" << __LINE__ << ")"
106                                 << ": transmission_start ERROR: ya existe."
107                                 << endl;
108 #endif // DEBUG
109                         return false;
110                 }
111         }
112         Transmitter* trans;
113         try {
114                 trans = new Transmitter(host, port);
115         } catch (const sockerr& e) { // TODO - Hace mas selectivo el catch?
116 #ifdef DEBUG
117                 cerr << __FILE__ << "(" << __LINE__ << ")"
118                         << ": transmission_start ERROR (" << e.serrno()
119                         << "): " << e.errstr() << endl;
120 #endif // DEBUG
121                 //delete trans;
122                 return false;
123 //      } catch (...) { // TODO - Hace mas selectivo el catch?
124 //#ifdef DEBUG
125 //              cerr << __FILE__ << "(" << __LINE__ << ")"
126 //                      << ": transmission_start ERROR: desconocido."
127 //                      << endl;
128 //#endif // DEBUG
129 //              //delete trans;
130 //              return false;
131         }
132         transmissions.push_back(trans);
133         trans->signal_finished().connect(SigC::bind<Transmitter*>(
134                                 SigC::slot_class(*this, &Plant::on_transmission_finished),
135                                 trans));
136         trans->run();
137         host = trans->get_host();
138         port = trans->get_port();
139         return true;
140 }
141
142 bool Plant::transmission_stop(const string& host,
143                 const Connection::Port& port) {
144 #ifdef DEBUG
145         cerr << __FILE__ << "(" << __LINE__ << ")"
146                 << ": transmission_stop(host = " << host <<
147                 ", port = " << port << ")." << endl;
148 #endif // DEBUG
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)) {
153                         (*i)->finish();
154                         return true;
155                 }
156         }
157         return false; // No la encontró.
158 }
159
160 void Plant::on_transmission_finished(Transmitter* transmission) {
161 #ifdef DEBUG
162         cerr << __FILE__ << "(" << __LINE__ << ")"
163                 <<  ": on_transmission_finished(transmission = "
164                 << transmission << ")" << endl;
165 #endif // DEBUG
166         Glib::Mutex::Lock lock(transmissions_mutex);
167         transmissions.remove(transmission);
168 #ifdef DEBUG
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;
174         }
175 #endif // DEBUG
176 }
177
178 bool Plant::set_open(const std::string& element, bool open) {
179 #ifdef DEBUG
180                 cerr << __FILE__ << "(" << __LINE__ << ")"
181                         << ": set_open(element = " << element <<
182                         ", open = " << open << ")." << endl;
183 #endif // DEBUG
184         Glib::Mutex::Lock lock(simulator_mutex);
185         return simulator.set_open(element, open);
186 }
187
188 const string Plant::get_xml(void) const {
189 #ifdef DEBUG
190                 cerr << __FILE__ << "(" << __LINE__ << ")"
191                         << ": get_xml()." << endl;
192 #endif // DEBUG
193         ostringstream oss;
194         ifstream ifs(filename.c_str());
195         ifs >> oss.rdbuf();
196         return oss.str();
197 }
198
199 /*
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)) {
206                         return true;
207                 }
208         }
209         return false; // No la encontró.
210 }
211 */
212
213 //const std::string& Plant::get_filename(void) const {
214 //      return filename;
215 //}
216
217 /// \todo FIXME esto deberia estar protegido por un mutex.
218 //Plant::SignalUpdated& Plant::signal_updated(void) {
219 //      return updated;
220 //}
221
222 } // namespace Server
223
224 } // namespace PlaQui
225