X-Git-Url: https://git.llucax.com/z.facultad/75.42/plaqui.git/blobdiff_plain/c0e0cf58ce033421d125afb813636f102456592c..c90e905d76aed75379d220478e97fda0d34ec59b:/Server/src/plant.cpp?ds=inline diff --git a/Server/src/plant.cpp b/Server/src/plant.cpp index e46e692..ce9f737 100644 --- a/Server/src/plant.cpp +++ b/Server/src/plant.cpp @@ -29,8 +29,6 @@ #include #include #include -#include -#include #ifdef DEBUG # include #endif // DEBUG @@ -43,7 +41,8 @@ namespace Server { Plant::~Plant(void) { #ifdef DEBUG - cerr << __FILE__ << ": destructor." << endl; + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": destructor." << endl; #endif // DEBUG // Termino transmisiones. Glib::Mutex::Lock lock(transmissions_mutex); @@ -55,7 +54,8 @@ Plant::~Plant(void) { Plant::Plant(const string& filename): simulator(filename), filename(filename) { #ifdef DEBUG - cerr << __FILE__ << ": constructor. filename = " << filename << endl; + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": constructor. filename = " << filename << endl; #endif // DEBUG // TODO plant /* simulator.add_pump("bomba1"); @@ -73,7 +73,8 @@ Plant::Plant(const string& filename): simulator(filename), filename(filename) { void Plant::real_run(void) { #ifdef DEBUG - cerr << __FILE__ << ": real_run." << endl; + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": real_run." << endl; #endif // DEBUG while (!stop) { simulator_mutex.lock(); @@ -90,27 +91,57 @@ void Plant::real_run(void) { } bool Plant::transmission_start(string& host, Connection::Port& port) { +#ifdef DEBUG + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": transmission_start(host = " << host << + ", port = " << port << ")." << endl; +#endif // DEBUG Glib::Mutex::Lock lock(transmissions_mutex); for (TransmitterList::iterator i = transmissions.begin(); i != transmissions.end(); i++) { if (((*i)->get_host() == host) && ((*i)->get_port() == port)) { +#ifdef DEBUG + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": transmission_start ERROR: ya existe." + << endl; +#endif // DEBUG return false; } } Transmitter* trans; try { trans = new Transmitter(host, port); - } catch (...) { // TODO - Hace mas selectivo el catch? - delete trans; + } catch (const sockerr& e) { // TODO - Hace mas selectivo el catch? +#ifdef DEBUG + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": transmission_start ERROR (" << e.serrno() + << "): " << e.errstr() << endl; +#endif // DEBUG + //delete trans; return false; +// } catch (...) { // TODO - Hace mas selectivo el catch? +//#ifdef DEBUG +// cerr << __FILE__ << "(" << __LINE__ << ")" +// << ": transmission_start ERROR: desconocido." +// << endl; +//#endif // DEBUG +// //delete trans; +// return false; } transmissions.push_back(trans); trans->run(); + host = trans->get_host(); + port = trans->get_port(); return true; } bool Plant::transmission_stop(const string& host, const Connection::Port& port) { +#ifdef DEBUG + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": transmission_stop(host = " << host << + ", port = " << port << ")." << endl; +#endif // DEBUG Glib::Mutex::Lock lock(transmissions_mutex); for (TransmitterList::iterator i = transmissions.begin(); i != transmissions.end(); i++) { @@ -122,21 +153,42 @@ bool Plant::transmission_stop(const string& host, return false; // No la encontrĂ³. } +bool Plant::on_transmission_finished(Transmitter* transmission) { +#ifdef DEBUG + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": on_transmission_finished(transmission = " + << transmission << ")" << endl; +#endif // DEBUG + Glib::Mutex::Lock lock(transmissions_mutex); + transmissions.remove(transmission); +#ifdef DEBUG + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": lista de conexiones" << endl; + for (TransmitterList::const_iterator i = transmissions.begin(); + i != transmissions.end(); i++) { + cerr << "\t " << *i << endl; + } +#endif // DEBUG +} + bool Plant::set_open(const std::string& element, bool open) { +#ifdef DEBUG + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": set_open(element = " << element << + ", open = " << open << ")." << endl; +#endif // DEBUG Glib::Mutex::Lock lock(simulator_mutex); return simulator.set_open(element, open); } const string Plant::get_xml(void) const { +#ifdef DEBUG + cerr << __FILE__ << "(" << __LINE__ << ")" + << ": get_xml()." << endl; +#endif // DEBUG ostringstream oss; - try { - ifstream ifs(filename.c_str()); - ifs >> noskipws; - copy(istream_iterator(ifs), istream_iterator(), - ostream_iterator(oss)); - } catch (...) { // TODO hacerlo mas selectivo? - return ""; - } + ifstream ifs(filename.c_str()); + ifs >> oss.rdbuf(); return oss.str(); } @@ -154,8 +206,8 @@ bool Plant::transmission_exists(const string& host, } */ -//const std::string& Plant::get_name(void) const { -// return name; +//const std::string& Plant::get_filename(void) const { +// return filename; //} /// \todo FIXME esto deberia estar protegido por un mutex.