]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Server/src/plant.cpp
b01f1a51ee6c7c153eeda30b7964f9d90f5efdba
[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::set_open(const std::string& element, bool open) {
157 #ifdef DEBUG
158                 cerr << __FILE__ << "(" << __LINE__ << ")"
159                         << ": set_open(element = " << element <<
160                         ", open = " << open << ")." << endl;
161 #endif // DEBUG
162         Glib::Mutex::Lock lock(simulator_mutex);
163         return simulator.set_open(element, open);
164 }
165
166 const string Plant::get_xml(void) const {
167 #ifdef DEBUG
168                 cerr << __FILE__ << "(" << __LINE__ << ")"
169                         << ": get_xml()." << endl;
170 #endif // DEBUG
171         ostringstream oss;
172         ifstream ifs(filename.c_str());
173         ifs >> oss.rdbuf();
174         return oss.str();
175 }
176
177 /*
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)) {
184                         return true;
185                 }
186         }
187         return false; // No la encontró.
188 }
189 */
190
191 //const std::string& Plant::get_filename(void) const {
192 //      return filename;
193 //}
194
195 /// \todo FIXME esto deberia estar protegido por un mutex.
196 //Plant::SignalUpdated& Plant::signal_updated(void) {
197 //      return updated;
198 //}
199
200 } // namespace Server
201
202 } // namespace PlaQui
203