]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Server/src/plant.cpp
ce9f737d19822eba5ba4dac9d74875f36a9c8a64
[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++/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                 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());
87                 }
88                 transmissions_mutex.unlock();
89                 Glib::usleep(1000000);
90         }
91 }
92
93 bool Plant::transmission_start(string& host, Connection::Port& port) {
94 #ifdef DEBUG
95                 cerr << __FILE__ << "(" << __LINE__ << ")"
96                         << ": transmission_start(host = " << host <<
97                         ", port = " << port << ")." << endl;
98 #endif // DEBUG
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)) {
103 #ifdef DEBUG
104                         cerr << __FILE__ << "(" << __LINE__ << ")"
105                                 << ": transmission_start ERROR: ya existe."
106                                 << endl;
107 #endif // DEBUG
108                         return false;
109                 }
110         }
111         Transmitter* trans;
112         try {
113                 trans = new Transmitter(host, port);
114         } catch (const sockerr& e) { // TODO - Hace mas selectivo el catch?
115 #ifdef DEBUG
116                 cerr << __FILE__ << "(" << __LINE__ << ")"
117                         << ": transmission_start ERROR (" << e.serrno()
118                         << "): " << e.errstr() << endl;
119 #endif // DEBUG
120                 //delete trans;
121                 return false;
122 //      } catch (...) { // TODO - Hace mas selectivo el catch?
123 //#ifdef DEBUG
124 //              cerr << __FILE__ << "(" << __LINE__ << ")"
125 //                      << ": transmission_start ERROR: desconocido."
126 //                      << endl;
127 //#endif // DEBUG
128 //              //delete trans;
129 //              return false;
130         }
131         transmissions.push_back(trans);
132         trans->run();
133         host = trans->get_host();
134         port = trans->get_port();
135         return true;
136 }
137
138 bool Plant::transmission_stop(const string& host,
139                 const Connection::Port& port) {
140 #ifdef DEBUG
141                 cerr << __FILE__ << "(" << __LINE__ << ")"
142                         << ": transmission_stop(host = " << host <<
143                         ", port = " << port << ")." << endl;
144 #endif // DEBUG
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)) {
149                         (*i)->finish();
150                         return true;
151                 }
152         }
153         return false; // No la encontró.
154 }
155
156 bool Plant::on_transmission_finished(Transmitter* transmission) {
157 #ifdef DEBUG
158         cerr << __FILE__ << "(" << __LINE__ << ")"
159                 <<  ": on_transmission_finished(transmission = "
160                 << transmission << ")" << endl;
161 #endif // DEBUG
162         Glib::Mutex::Lock lock(transmissions_mutex);
163         transmissions.remove(transmission);
164 #ifdef DEBUG
165         cerr << __FILE__ << "(" << __LINE__ << ")"
166                 <<  ": lista de conexiones" << endl;
167         for (TransmitterList::const_iterator i = transmissions.begin();
168                         i != transmissions.end(); i++) {
169                 cerr << "\t " << *i << endl;
170         }
171 #endif // DEBUG
172 }
173
174 bool Plant::set_open(const std::string& element, bool open) {
175 #ifdef DEBUG
176                 cerr << __FILE__ << "(" << __LINE__ << ")"
177                         << ": set_open(element = " << element <<
178                         ", open = " << open << ")." << endl;
179 #endif // DEBUG
180         Glib::Mutex::Lock lock(simulator_mutex);
181         return simulator.set_open(element, open);
182 }
183
184 const string Plant::get_xml(void) const {
185 #ifdef DEBUG
186                 cerr << __FILE__ << "(" << __LINE__ << ")"
187                         << ": get_xml()." << endl;
188 #endif // DEBUG
189         ostringstream oss;
190         ifstream ifs(filename.c_str());
191         ifs >> oss.rdbuf();
192         return oss.str();
193 }
194
195 /*
196 bool Plant::transmission_exists(const string& host,
197                 const Connection::Port& port) {
198         Glib::Mutex::Lock lock(transmissions_mutex);
199         for (TransmitterList::const_iterator i = transmissions.begin();
200                         i != transmissions.end(); i++) {
201                 if (((*i)->get_host() == host) && ((*i)->get_oprt() == port)) {
202                         return true;
203                 }
204         }
205         return false; // No la encontró.
206 }
207 */
208
209 //const std::string& Plant::get_filename(void) const {
210 //      return filename;
211 //}
212
213 /// \todo FIXME esto deberia estar protegido por un mutex.
214 //Plant::SignalUpdated& Plant::signal_updated(void) {
215 //      return updated;
216 //}
217
218 } // namespace Server
219
220 } // namespace PlaQui
221