]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Server/src/plant.cpp
El Transmitter ya anda bien (se puede escuchar con un nc -p [puerto] -l -u :).
[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 #ifdef DEBUG
32 #       include <iostream>
33 #endif // DEBUG
34
35 using namespace std;
36
37 namespace PlaQui {
38
39 namespace Server {
40
41 Plant::~Plant(void) {
42 #ifdef DEBUG
43         cerr << __FILE__ << ": destructor." << endl;
44 #endif // DEBUG
45         // Termino transmisiones.
46         Glib::Mutex::Lock lock(transmissions_mutex);
47         for (TransmitterList::iterator trans = transmissions.end();
48                         trans != transmissions.end(); trans++) {
49                 (*trans)->finish(true);
50         }
51 }
52
53 Plant::Plant(const string& filename): simulator(filename) {
54 #ifdef DEBUG
55         cerr << __FILE__ << ": constructor. filename = " << filename << endl;
56 #endif // DEBUG
57         // TODO plant
58 /*      simulator.add_pump("bomba1");
59         simulator.add_conduct("c");
60         simulator.add_conduct("c1");
61         simulator.add_drainage("d");
62         simulator.add_tank("tanque");
63         
64         simulator.connect("bomba1", "c", Model::IConector::OUT);
65         simulator.connect("c", "tanque", Model::IConector::OUT);
66         simulator.connect("tanque", "c1", Model::IConector::OUT);
67         simulator.connect("c1", "d", Model::IConector::OUT);
68 */      
69 }
70
71 void Plant::real_run(void) {
72 #ifdef DEBUG
73         cerr << __FILE__ << ": real_run." << endl;
74 #endif // DEBUG
75         while (!stop) {
76                 simulator.simulate();
77                 Glib::Mutex::Lock lock(transmissions_mutex);
78                 for (TransmitterList::iterator i = transmissions.begin();
79                                 i != transmissions.end(); i++) {
80                         (*i)->send(simulator.get_state_as_xml());
81                 }
82                 Glib::usleep(1000000);
83         }
84 }
85
86 bool Plant::transmission_start(const string& host,
87                 const Connection::Port& port) {
88         Glib::Mutex::Lock lock(transmissions_mutex);
89         for (TransmitterList::iterator i = transmissions.begin();
90                         i != transmissions.end(); i++) {
91                 if (((*i)->get_host() == host) && ((*i)->get_port() == port)) {
92                         return false;
93                 }
94         }
95         Transmitter* trans;
96         try {
97                 trans = new Transmitter(host, port);
98         } catch (...) { // TODO - Hace mas selectivo el catch?
99                 delete trans;
100                 return false;
101         }
102         transmissions.push_back(trans);
103         trans->run();
104         return true;
105 }
106
107 bool Plant::transmission_stop(const string& host,
108                 const Connection::Port& port) {
109         Glib::Mutex::Lock lock(transmissions_mutex);
110         for (TransmitterList::iterator i = transmissions.begin();
111                         i != transmissions.end(); i++) {
112                 if (((*i)->get_host() == host) && ((*i)->get_port() == port)) {
113                         (*i)->finish();
114                         return true;
115                 }
116         }
117         return false; // No la encontró.
118 }
119
120 /*
121 bool Plant::transmission_exists(const string& host,
122                 const Connection::Port& port) {
123         Glib::Mutex::Lock lock(transmissions_mutex);
124         for (TransmitterList::const_iterator i = transmissions.begin();
125                         i != transmissions.end(); i++) {
126                 if (((*i)->get_host() == host) && ((*i)->get_oprt() == port)) {
127                         return true;
128                 }
129         }
130         return false; // No la encontró.
131 }
132 */
133
134 //const std::string& Plant::get_name(void) const {
135 //      return name;
136 //}
137
138 /// \todo FIXME esto deberia estar protegido por un mutex.
139 //Plant::SignalUpdated& Plant::signal_updated(void) {
140 //      return updated;
141 //}
142
143 } // namespace Server
144
145 } // namespace PlaQui
146