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++/class_slot.h>
44 cerr << __FILE__ << "(" << __LINE__ << ")"
45 << ": destructor." << endl;
47 // Mando a terminar todas las transmisiones.
48 transmissions_mutex.lock();
49 for (TransmitterList::iterator trans = transmissions.begin();
50 trans != transmissions.end(); trans++) {
53 TransmitterList::size_type count = transmissions.size();
54 transmissions_mutex.unlock();
55 // Espero que terminen realmente.
57 Glib::usleep(10000); // 10 milisegundos
58 transmissions_mutex.lock();
59 count = transmissions.size();
60 transmissions_mutex.unlock();
64 Plant::Plant(const string& filename): simulator(filename), filename(filename),
65 wait_time(DEFAULT_WAIT_TIME), paused(true) {
67 cerr << __FILE__ << "(" << __LINE__ << ")"
68 << ": constructor. filename = " << filename << endl;
72 void Plant::real_run(void) throw() {
74 cerr << __FILE__ << "(" << __LINE__ << ")"
75 << ": real_run." << endl;
79 if (paused) { // Si está pausada, espera un tiempo sin simular.
80 Glib::usleep(DEFAULT_WAIT_TIME);
81 } else { // Si está andando, simula y manda estado.
82 simulator_mutex.lock();
84 string plantstatus = simulator.get_state_as_xml();
85 simulator_mutex.unlock();
86 transmissions_mutex.lock();
87 for (TransmitterList::iterator i = transmissions.begin();
88 i != transmissions.end(); i++) {
89 (*i)->send(plantstatus);
91 transmissions_mutex.unlock();
92 wait_time_mutex.lock();
94 wait_time_mutex.unlock();
100 bool Plant::transmission_start(string& host, Connection::Port& port) {
102 cerr << __FILE__ << "(" << __LINE__ << ")"
103 << ": transmission_start(host = " << host <<
104 ", port = " << port << ")." << endl;
106 Glib::Mutex::Lock lock(transmissions_mutex);
107 for (TransmitterList::iterator i = transmissions.begin();
108 i != transmissions.end(); i++) {
109 if (((*i)->get_host() == host) && ((*i)->get_port() == port)) {
111 cerr << __FILE__ << "(" << __LINE__ << ")"
112 << ": transmission_start ERROR: ya existe."
120 trans = new Transmitter(host, port);
121 } catch (const sockerr& e) {
123 cerr << __FILE__ << "(" << __LINE__ << ")"
124 << ": transmission_start ERROR (" << e.serrno()
125 << "): " << e.errstr() << endl;
129 transmissions.push_back(trans);
130 trans->signal_finished().connect(SigC::bind(
131 SigC::slot_class(*this, &Plant::on_transmission_finished),
134 host = trans->get_host();
135 port = trans->get_port();
139 bool Plant::transmission_stop(const string& host,
140 const Connection::Port& port) {
142 cerr << __FILE__ << "(" << __LINE__ << ")"
143 << ": transmission_stop(host = " << host <<
144 ", port = " << port << ")." << endl;
146 Glib::Mutex::Lock lock(transmissions_mutex);
147 for (TransmitterList::iterator i = transmissions.begin();
148 i != transmissions.end(); i++) {
149 if (((*i)->get_host() == host) && ((*i)->get_port() == port)) {
154 return false; // No la encontró.
157 void Plant::on_transmission_finished(Transmitter* transmission) {
159 cerr << __FILE__ << "(" << __LINE__ << ")"
160 << ": on_transmission_finished(transmission = "
161 << transmission << ")" << endl;
163 Glib::Mutex::Lock lock(transmissions_mutex);
164 transmissions.remove(transmission);
166 cerr << __FILE__ << "(" << __LINE__ << ")"
167 << ": lista de conexiones" << endl;
168 for (TransmitterList::const_iterator i = transmissions.begin();
169 i != transmissions.end(); i++) {
170 cerr << "\t " << *i << endl;
175 bool Plant::set_open(const std::string& element, bool open) {
177 cerr << __FILE__ << "(" << __LINE__ << ")"
178 << ": set_open(element = " << element <<
179 ", open = " << open << ")." << endl;
181 Glib::Mutex::Lock lock(simulator_mutex);
182 return simulator.set_open(element, open);
185 const string Plant::get_xml(void) const {
187 cerr << __FILE__ << "(" << __LINE__ << ")"
188 << ": get_xml()." << endl;
191 ifstream ifs(filename.c_str());
192 // FIXME Saco la línea de definición de XML (<?xml ?>), ver si esta hecho muy
194 ifs.ignore(50, '\n'); // Ignora 50 caracteres o hasta un enter.
199 void Plant::set_frequency(unsigned hz) {
200 Glib::Mutex::Lock lock(wait_time_mutex);
201 wait_time = hz ? (1000000u/hz) : DEFAULT_WAIT_TIME;
204 void Plant::set_paused(bool paused_) {
205 Glib::Mutex::Lock lock(paused_mutex);
209 } // namespace Server
211 } // namespace PlaQui