]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Server/src/plant.cpp
se borran las lineas al eliminar un item, esto no esta totalmente testeado
[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 #include <iterator>
33 #include <algorithm>
34 #ifdef DEBUG
35 #       include <iostream>
36 #endif // DEBUG
37
38 using namespace std;
39
40 namespace PlaQui {
41
42 namespace Server {
43
44 Plant::~Plant(void) {
45 #ifdef DEBUG
46         cerr << __FILE__ << ": destructor." << endl;
47 #endif // DEBUG
48         // Termino transmisiones.
49         Glib::Mutex::Lock lock(transmissions_mutex);
50         for (TransmitterList::iterator trans = transmissions.end();
51                         trans != transmissions.end(); trans++) {
52                 (*trans)->finish(true);
53         }
54 }
55
56 Plant::Plant(const string& filename): simulator(filename), filename(filename) {
57 #ifdef DEBUG
58         cerr << __FILE__ << ": 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__ << ": real_run." << endl;
77 #endif // DEBUG
78         while (!stop) {
79                 simulator_mutex.lock();
80                 simulator.simulate();
81                 simulator_mutex.unlock();
82                 transmissions_mutex.lock();
83                 for (TransmitterList::iterator i = transmissions.begin();
84                                 i != transmissions.end(); i++) {
85                         (*i)->send(simulator.get_state_as_xml());
86                 }
87                 transmissions_mutex.unlock();
88                 Glib::usleep(1000000);
89         }
90 }
91
92 bool Plant::transmission_start(string& host, Connection::Port& port) {
93         Glib::Mutex::Lock lock(transmissions_mutex);
94         for (TransmitterList::iterator i = transmissions.begin();
95                         i != transmissions.end(); i++) {
96                 if (((*i)->get_host() == host) && ((*i)->get_port() == port)) {
97                         return false;
98                 }
99         }
100         Transmitter* trans;
101         try {
102                 trans = new Transmitter(host, port);
103         } catch (...) { // TODO - Hace mas selectivo el catch?
104                 delete trans;
105                 return false;
106         }
107         transmissions.push_back(trans);
108         trans->run();
109         return true;
110 }
111
112 bool Plant::transmission_stop(const string& host,
113                 const Connection::Port& port) {
114         Glib::Mutex::Lock lock(transmissions_mutex);
115         for (TransmitterList::iterator i = transmissions.begin();
116                         i != transmissions.end(); i++) {
117                 if (((*i)->get_host() == host) && ((*i)->get_port() == port)) {
118                         (*i)->finish();
119                         return true;
120                 }
121         }
122         return false; // No la encontró.
123 }
124
125 bool Plant::set_open(const std::string& element, bool open) {
126         Glib::Mutex::Lock lock(simulator_mutex);
127         return simulator.set_open(element, open);
128 }
129
130 const string Plant::get_xml(void) const {
131         ostringstream oss;
132         try {
133                 ifstream ifs(filename.c_str());
134                 ifs >> noskipws;
135                 copy(istream_iterator<char>(ifs), istream_iterator<char>(),
136                                 ostream_iterator<char>(oss));
137         } catch (...) { // TODO hacerlo mas selectivo?
138                 return "";
139         }
140         return oss.str();
141 }
142
143 /*
144 bool Plant::transmission_exists(const string& host,
145                 const Connection::Port& port) {
146         Glib::Mutex::Lock lock(transmissions_mutex);
147         for (TransmitterList::const_iterator i = transmissions.begin();
148                         i != transmissions.end(); i++) {
149                 if (((*i)->get_host() == host) && ((*i)->get_oprt() == port)) {
150                         return true;
151                 }
152         }
153         return false; // No la encontró.
154 }
155 */
156
157 //const std::string& Plant::get_name(void) const {
158 //      return name;
159 //}
160
161 /// \todo FIXME esto deberia estar protegido por un mutex.
162 //Plant::SignalUpdated& Plant::signal_updated(void) {
163 //      return updated;
164 //}
165
166 } // namespace Server
167
168 } // namespace PlaQui
169