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: Sat Oct 18 18:18:36 2003
22 // Autores: Leandro Lucarella <llucare@fi.uba.ar>
23 //----------------------------------------------------------------------------
28 #include "plaqui/server/response.h"
29 #include "plaqui/server/controlclient.h"
30 #include <glibmm/timer.h>
31 #include <sigc++/class_slot.h>
42 ControlClient::~ControlClient(void) {
44 cerr << __FILE__ << "(" << __LINE__ << ")"
45 << ": destructor." << endl;
47 // FIXME Temporal: espero que el receiver muera.
52 Glib::usleep(100000); // 0,1 segundo
56 ControlClient::ControlClient(const string& _host,
57 const Connection::Port& _port) throw(sockerr):
58 Connection(sockbuf::sock_stream, _host, _port) {
59 socket->connect(host.c_str(), port);
60 host = socket->localhost();
61 port = socket->localport();
63 cerr << __FILE__ << "(" << __LINE__ << ")"
64 << ": host = " << host
65 << " | port = " << port << endl;
68 receiver = new Receiver(7528, host);
69 receiver->signal_finished().connect(SigC::slot_class(*this,
70 &ControlClient::on_receiver_finished));
71 receiver->signal_error().connect(SigC::slot_class(*this,
72 &ControlClient::on_receiver_error));
75 void ControlClient::real_run(void) throw() {
77 cerr << __FILE__ << "(" << __LINE__ << ")"
78 << ": real_run." << endl;
79 cerr << ": REAL | host = " << host
80 << " | port = " << port << endl;
82 // TODO Temporal: el receiver empieza a escuchar.
88 // Si se cerró el socket.
89 } catch (const ios::failure& e) {
90 // FIXME poner buenos codigos de error.
91 signal_error().emit(1000000, "Se desconectó.");
93 } catch (const sockerr& e) {
94 signal_error().emit(e.serrno(), e.errstr());
96 } catch (const xmlpp::parse_error& e) {
97 signal_error().emit(100001, e.what());
99 // Si hay un error al parsear la respuesta.
100 } catch (const HTTPResponse::Error& e) {
102 cerr << __FILE__ << "(" << __LINE__ << ")"
103 << " : real_run() ERROR nro: " << e << endl;
105 signal_error().emit(e, "La respuesta recibida es inválida");
108 switch (response.get_code()) {
110 ok_received(response.get_contents());
113 error_received(response.get_code(), response.get_description());
119 void ControlClient::send(const Command& command) {
121 socket << command << flush;
122 } catch (const sockerr& e) {
123 signal_error().emit(e.serrno(), e.errstr());
127 cerr << __FILE__ << "(" << __LINE__ << ")"
128 << ": send() Enviado!" << endl;
132 void ControlClient::on_receiver_finished(void) {
136 void ControlClient::on_receiver_error(const Runnable::Error& code,
137 const string& desc) {
138 signal_error().emit(code, string("Receiver Error: ") + desc);
141 ControlClient::SignalOKReceived& ControlClient::signal_ok_received(void) {
145 ControlClient::SignalErrorReceived& ControlClient::signal_error_received(void) {
146 return error_received;
150 ControlClient::SignalFrameReceived& ControlClient::signal_frame_received(void) {
151 // XXX - cuidado, esto puede dar quilombo si no esta protegido por un mutex,
152 // aunque no deberia porque la señal no es llamada hasta que no se empice
153 // la transmision y la señal se conecta antes de pedir la transmision.
154 return receiver->signal_frame_received();
157 } // namespace Server
159 } // namespace PlaQui