//
#include "plaqui/server/transmitter.h"
+#include <glibmm/timer.h>
#include <socket++/sockinet.h>
#include <string>
#ifdef DEBUG
# include <iostream>
#endif // DEBUG
-PlaQui::Server::Transmitter::~Transmitter(void) {
+using namespace std;
+
+namespace PlaQui {
+
+namespace Server {
+
+Transmitter::~Transmitter(void) {
#ifdef DEBUG
- std::cerr << __FILE__ << ": destructor." << std::endl;
+ cerr << __FILE__ << "(" << __LINE__ << ")"
+ << ": destructor." << endl;
#endif // DEBUG
}
-/// \todo debría conectarse en real_run() (?)
-PlaQui::Server::Transmitter::Transmitter(std::string host, int port):
- PlaQui::Server::Connection(sockbuf::sock_dgram) {
+Transmitter::Transmitter(const string& _host, const Connection::Port& _port)
+ throw(sockerr): Connection(sockbuf::sock_dgram, _host, _port) {
#ifdef DEBUG
- std::cerr << __FILE__ << ": host = " << host
- << " | port = " << port << std::endl;
+ cerr << __FILE__ << "(" << __LINE__ << ")"
+ << ": _host = " << _host
+ << " | _port = " << _port << endl;
#endif // DEBUG
- // FIXME - deberia ir en run().
socket->connect(host.c_str(), port);
+ // Reasigno el host y puerto bien.
+ host = socket->peerhost();
+ port = socket->peerport();
+#ifdef DEBUG
+ cerr << __FILE__ << "(" << __LINE__ << ")"
+ << ": (real) host = " << _host
+ << " | port = " << _port << endl;
+#endif // DEBUG
}
/// \todo debría dar una excepción (?)
-void PlaQui::Server::Transmitter::real_run(void) {
+void Transmitter::real_run(void) {
#ifdef DEBUG
- // FIXME - debería tirar una excepción?
- if (!socket->is_open()) {
- std::cerr << "No se pudo conectar a " << socket->peerhost() <<
- ":" << socket->peerport() << "." << std::endl;
- } else {
- std::cerr << "Conectado a " << socket->peerhost() <<
- ":" << socket->peerport() << "." << std::endl;
+ cerr << __FILE__ << "(" << __LINE__ << ")"
+ << ": real_run()." << endl;
+#endif // DEBUG
+ // No hace nada, porque solo actua cuando se manda algo con send().
+ while (!stop) {
+ Glib::usleep(500000); // 1/2 segundo
}
+}
+
+void Transmitter::send(const string& data) {
+#ifdef DEBUG
+ cerr << __FILE__ << "(" << __LINE__ << ")"
+ << ": send(data = " << data << ")." << endl;
#endif // DEBUG
+ if (stop) {
+ return;
+ }
+ try {
+ socket << data << flush;
+ } catch (const sockerr& e) {
+ error(e.serrno(), e.errstr());
+ stop = true;
+ }
}
+} // namespace Server
+
+} // namespace PlaQui
+