From b92547a5de3a3fb4f14a2bea4a4e776b8da3cb5c Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Fri, 7 Nov 2003 02:11:23 +0000 Subject: [PATCH] Algunos cambios mas, ya es probable que compile de nuevo (aunque no ande como deberia). --- Server/include/plaqui/server/httpheaders.h | 5 + Server/include/plaqui/server/httpmessage.h | 1 + Server/include/plaqui/server/httprequest.h | 2 +- Server/src/connection.cpp | 22 +++-- Server/src/controlclient.cpp | 32 +++--- Server/src/controlserver.cpp | 109 ++++++++++++--------- Server/src/httpheaders.cpp | 12 ++- Server/src/httpmessage.cpp | 23 +++-- Server/src/httprequest.cpp | 95 ++++++++++-------- Server/src/httpresponse.cpp | 82 +++++++++------- Server/src/receiver.cpp | 32 +++--- Server/src/runnable.cpp | 37 ++++--- Server/src/server.cpp | 42 +++++--- Server/src/string.cpp | 29 ++++-- Server/src/tcpserver.cpp | 56 ++++++----- Server/src/transmitter.cpp | 30 ++++-- docs/server.txt | 41 +++++++- 17 files changed, 417 insertions(+), 233 deletions(-) diff --git a/Server/include/plaqui/server/httpheaders.h b/Server/include/plaqui/server/httpheaders.h index 8eb42ed..1a9c5aa 100644 --- a/Server/include/plaqui/server/httpheaders.h +++ b/Server/include/plaqui/server/httpheaders.h @@ -30,6 +30,8 @@ #include #include +#include +#include namespace PlaQui { @@ -56,6 +58,9 @@ namespace Server { friend std::ostream& operator<<(std::ostream& os, const HTTPHeaders& h); + }; + +} } diff --git a/Server/include/plaqui/server/httpmessage.h b/Server/include/plaqui/server/httpmessage.h index 888eacf..33f546f 100644 --- a/Server/include/plaqui/server/httpmessage.h +++ b/Server/include/plaqui/server/httpmessage.h @@ -28,6 +28,7 @@ #ifndef PLAQUI_HTTPMESSAGE_H #define PLAQUI_HTTPMESSAGE_H +#include "plaqui/server/httpheaders.h" #include namespace PlaQui { diff --git a/Server/include/plaqui/server/httprequest.h b/Server/include/plaqui/server/httprequest.h index 55748e7..92c6de7 100644 --- a/Server/include/plaqui/server/httprequest.h +++ b/Server/include/plaqui/server/httprequest.h @@ -104,7 +104,7 @@ namespace Server { HTTPRequest(const std::string& uri = "/", const HTTPMethod& method = GET, const std::string& query = "", - const std::string& http = "1.1"); + const std::string& version = "1.1"); /** * Obtiene los datos del pedido HTTP desde un texto. diff --git a/Server/src/connection.cpp b/Server/src/connection.cpp index 18b5909..408239e 100644 --- a/Server/src/connection.cpp +++ b/Server/src/connection.cpp @@ -31,23 +31,33 @@ # include #endif // DEBUG -PlaQui::Server::Connection::~Connection(void) { +using namespace std; + +namespace PlaQui { + +namespace Server { + +Connection::~Connection(void) { #ifdef DEBUG - std::cerr << __FILE__ << ": destructor." << std::endl; + cerr << __FILE__ << ": destructor." << endl; #endif // DEBUG } -PlaQui::Server::Connection::Connection(const sockbuf::sockdesc& sd): +Connection::Connection(const sockbuf::sockdesc& sd): socket(sd) { #ifdef DEBUG - std::cerr << __FILE__ << ": sd = " << sd.sock << std::endl; + cerr << __FILE__ << ": sd = " << sd.sock << endl; #endif // DEBUG } -PlaQui::Server::Connection::Connection(sockbuf::type type): +Connection::Connection(sockbuf::type type): socket(type) { #ifdef DEBUG - std::cerr << __FILE__ << ": type = " << type << std::endl; + cerr << __FILE__ << ": type = " << type << endl; #endif // DEBUG } +} // namespace Server + +} // namespace PlaQui + diff --git a/Server/src/controlclient.cpp b/Server/src/controlclient.cpp index 3ceaa9f..2c76de3 100644 --- a/Server/src/controlclient.cpp +++ b/Server/src/controlclient.cpp @@ -30,35 +30,45 @@ # include #endif // DEBUG -PlaQui::Server::ControlClient::~ControlClient(void) { +using namespace std; + +namespace PlaQui { + +namespace Server { + +ControlClient::~ControlClient(void) { #ifdef DEBUG - std::cerr << __FILE__ << ": destructor." << std::endl; + cerr << __FILE__ << ": destructor." << endl; #endif // DEBUG } -PlaQui::Server::ControlClient::ControlClient(std::string host, int port): +ControlClient::ControlClient(string host, int port): Connection(sockbuf::sock_stream) { #ifdef DEBUG - std::cerr << __FILE__ << ": host" << host - << " | port = " << port << std::endl; + cerr << __FILE__ << ": host" << host + << " | port = " << port << endl; #endif // DEBUG // FIXME - poner en run(). socket->connect(host.c_str(), port); } -void PlaQui::Server::ControlClient::real_run(void) { +void ControlClient::real_run(void) { #ifdef DEBUG - std::cerr << __FILE__ << ": real_run." << std::endl; + cerr << __FILE__ << ": real_run." << endl; #endif // DEBUG #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; + cerr << "No se pudo conectar a " << socket->peerhost() << + ":" << socket->peerport() << "." << endl; } else { - std::cerr << "Conectado a " << socket->peerhost() << - ":" << socket->peerport() << "." << std::endl; + cerr << "Conectado a " << socket->peerhost() << + ":" << socket->peerport() << "." << endl; } #endif // DEBUG } +} // namespace Server + +} // namespace PlaQui + diff --git a/Server/src/controlserver.cpp b/Server/src/controlserver.cpp index 0587bc1..348d772 100644 --- a/Server/src/controlserver.cpp +++ b/Server/src/controlserver.cpp @@ -26,37 +26,47 @@ // #include "plaqui/server/controlserver.h" -#include "plaqui/server/request.h" +#include "plaqui/server/httprequest.h" #include #include #ifdef DEBUG # include #endif // DEBUG -PlaQui::Server::ControlServer::~ControlServer(void) { +using namespace std; + +namespace PlaQui { + +namespace Server { + +ControlServer::~ControlServer(void) { #ifdef DEBUG - std::cerr << __FILE__ << ": destructor." << std::endl; + cerr << __FILE__ << ": destructor." << endl; #endif // DEBUG } -PlaQui::Server::ControlServer::ControlServer(const sockbuf::sockdesc& sd): +ControlServer::ControlServer(const sockbuf::sockdesc& sd): ServerConnection(sd) { #ifdef DEBUG - std::cerr << __FILE__ << ": sd = " << sd.sock << std::endl; + cerr << __FILE__ << ": sd = " << sd.sock << endl; #endif // DEBUG } -void PlaQui::Server::ControlServer::real_run(void) { - // FIXME se tiene que ir a la clase para poder frenarlo desde afuera. - bool stop = false; - char buf[BUFFER_SIZE]; +void ControlServer::real_run(void) { +#ifdef DEBUG + cerr << __FILE__ << ": real_run()" << endl; +#endif // DEBUG + char buf[BUFSIZ]; while (!stop) { - Request request; + HTTPRequest request; + socket >> request; + // TODO agregar las verificaciones de abajo a HTTPRequest y padres. +/* // Primera línea no vacía (que debe ser el request). bool is_first = true; - while (!stop && socket.getline(buf, BUFFER_SIZE)) { + while (!stop && socket.getline(buf, BUFSIZ)) { #ifdef DEBUG - std::cerr << "Recibiendo linea: " << buf << std::endl; + cerr << __FILE__ << " Recibiendo inea: " << buf << endl; #endif // DEBUG int len = strlen(buf); // Si tiene un retorno de carro, lo elimina. @@ -84,57 +94,64 @@ void PlaQui::Server::ControlServer::real_run(void) { // Si es la primera, no pasa nada, sigue esperando un request. } } - // TODO: Manda mensaje a la planta. - //signal_command_received().emit(request); - //dispatch_command(parse_command(sstr.str())); +*/ + // TODO: Manda el comando. + // Command command = parse_command(request.uri); + //signal_command_received().emit(command); #ifdef DEBUG - std::cerr << "Request: " << std::endl; + cerr << "Request: " << endl; for (Request::const_iterator i = request.begin(); i != request.end(); i++) { - std::cerr << " " << i->first << ": " << i->second << std::endl; + cerr << " " << i->first << ": " << i->second << endl; } #endif // DEBUG // FIXME - hacer respuesta XML. + // La respuesta hay que mandarla asincrónicamente porque no puedo + // responder hasta que la planta no se termine de actualizar, por + // ejemplo. stringstream response_xml; - socket << "HTTP/1.0 200 OK" << std::endl; + socket << "HTTP/1.0 200 OK" << endl; /* Date: Sun, 19 Oct 2003 15:11:14 GMT Server: Apache/1.3.28 (Debian GNU/Linux) Last-Modified: Mon, 28 Apr 2003 07:50:08 GMT -ETag: "110f4043-11a1-3eacdd30" Accept-Ranges: bytes */ - socket << "Content-Type: text/html; charset=iso-8859-1" << std::endl; - response_xml << "" << std::endl; - response_xml << " " << std::endl; - response_xml << " PlaQui v0.1" << std::endl; - response_xml << " " << std::endl; - response_xml << " " << std::endl; - response_xml << "

PlaQui

" << std::endl; - response_xml << "

versión 0.2

" << std::endl; - response_xml << "

Pedido HTTP

" << std::endl; - response_xml << "
    " << std::endl; + socket << "Content-Type: text/html; charset=iso-8859-1" << endl; + response_xml << "" << endl; + response_xml << " " << endl; + response_xml << " PlaQui v0.1" << endl; + response_xml << " " << endl; + response_xml << " " << endl; + response_xml << "

    PlaQui

    " << endl; + response_xml << "

    versión 0.2

    " << endl; + response_xml << "

    Pedido HTTP

    " << endl; + response_xml << "
      " << endl; for (Request::const_iterator i = request.begin(); i != request.end(); i++) { response_xml << "
    • " << i->first << ": " - << i->second << std::endl; + << i->second << endl; } - response_xml << "
    " << std::endl; - response_xml << "

    Desarrollado por

    " << std::endl; - response_xml << "
      " << std::endl; - response_xml << "
    • Nicolás Dimov.
    • " << std::endl; - response_xml << "
    • Leandro Lucarella.
    • " << std::endl; - response_xml << "
    • Ricardo Markiewicz.
    • " << std::endl; - response_xml << "
    " << std::endl; - response_xml << "
    " << std::endl; - response_xml << " Copyleft 2003 - bajo los " << std::endl; - response_xml << " términos de la licencia GPL" << std::endl; - response_xml << "
    " << std::endl; - response_xml << " " << std::endl; - response_xml << "" << std::endl; - socket << "Content-Length: " << response_xml.str().length() << std::endl; - socket << std::endl; - socket << response_xml.str() << std::flush; + response_xml << "
" << endl; + response_xml << "

Desarrollado por

" << endl; + response_xml << "
    " << endl; + response_xml << "
  • Nicolás Dimov.
  • " << endl; + response_xml << "
  • Leandro Lucarella.
  • " << endl; + response_xml << "
  • Ricardo Markiewicz.
  • " << endl; + response_xml << "
" << endl; + response_xml << "
" << endl; + response_xml << " Copyleft 2003 - bajo los " << endl; + response_xml << " términos de la licencia GPL" << endl; + response_xml << "
" << endl; + response_xml << " " << endl; + response_xml << "" << endl; + socket << "Content-Length: " << response_xml.str().length() << endl; + socket << endl; + socket << response_xml.str() << flush; } } +} // namespace Server + +} // namespace PlaQui + diff --git a/Server/src/httpheaders.cpp b/Server/src/httpheaders.cpp index cef6d05..89d8de4 100644 --- a/Server/src/httpheaders.cpp +++ b/Server/src/httpheaders.cpp @@ -26,13 +26,17 @@ // #include "plaqui/server/httpheaders.h" +#include "plaqui/server/string.h" //#include #ifdef DEBUG # include #endif // DEBUG using namespace std; -using namespace PlaQui::Server; + +namespace PlaQui { + +namespace Server { HTTPHeaders::~HTTPHeaders(void) { #ifdef DEBUG @@ -52,7 +56,7 @@ istream& operator>>(istream& is, HTTPHeaders& h) { // FIXME poner mejores excepciones. throw "Wrong header"; } - h[sbuf.substr(0, pos)] = sbuf.substr(pos + 1); + h[sbuf.substr(0, pos)] = String(sbuf.substr(pos + 1)).trim(); return is; } @@ -67,3 +71,7 @@ ostream& operator<<(ostream& os, const HTTPHeaders& h) { return os; } +} // namespace Server + +} // namespace PlaQui + diff --git a/Server/src/httpmessage.cpp b/Server/src/httpmessage.cpp index 737f636..3a563cd 100644 --- a/Server/src/httpmessage.cpp +++ b/Server/src/httpmessage.cpp @@ -33,7 +33,10 @@ #endif // DEBUG using namespace std; -using namespace PlaQui::Server; + +namespace PlaQui { + +namespace Server { HTTPMessage::~HTTPMessage(void) { #ifdef DEBUG @@ -82,9 +85,11 @@ istream& operator>>(istream& is, HTTPMessage& m) { stringstream body_ss; while (is.getline(buf, BUFSIZ)) { string sbuf = buf; - if (sbuf.length()) + if (sbuf.length()) { if (is_header) { - istringstream(buf) >> m.headers; + stringstream ss; + ss << buf; + ss >> m.headers; } else { body_ss << buf << endl; } @@ -97,15 +102,19 @@ istream& operator>>(istream& is, HTTPMessage& m) { } } // TODO si el body es un serializable, deberia auto deserializarse. - body = body_ss.str(); + m.body = body_ss.str(); return is; } -ostream& operator<<(ostream& os, HTTPMessage) { +ostream& operator<<(ostream& os, const HTTPMessage& m) { #ifdef DEBUG cerr << __FILE__ << ": operator<<()" << endl; #endif // DEBUG - return os << headers << "\r\n" // Fin de cabeceras - << body; + return os << m.headers << "\r\n" // Fin de cabeceras + << m.body; } +} // namespace Server + +} // namespace PlaQui + diff --git a/Server/src/httprequest.cpp b/Server/src/httprequest.cpp index 3614610..2fb8661 100644 --- a/Server/src/httprequest.cpp +++ b/Server/src/httprequest.cpp @@ -31,84 +31,94 @@ # include #endif // DEBUG -PlaQui::Server::HTTPRequest::~HTTPRequest(void) { +using namespace std; + +namespace PlaQui { + +namespace Server { + +HTTPRequest::~HTTPRequest(void) { #ifdef DEBUG - std::cerr << __FILE__ << ": destructor." << std::endl; + cerr << __FILE__ << ": destructor." << endl; #endif // DEBUG } -PlaQui::Server::HTTPRequest::HTTPRequest(const std::string& version): - PlaQui::Server::HTTPMessage(version) { +HTTPRequest::HTTPRequest(const string& uri, const HTTPMethod& method, + const string& query, const string& version): + HTTPMessage(version), method(method), uri(uri), query(query) { #ifdef DEBUG - std::cerr << __FILE__ << ": http_version = " << http_version << std::endl; + cerr << __FILE__ << ": uri = " << uri << " | " + << "method = " << ((method == GET) ? "GET" : "POST") << " | " + << "query = " << query << " | " + << "version = " << version << endl; #endif // DEBUG } /* -PlaQui::Server::HTTPRequest::HTTPRequest(const Serializable& body, - const std::string& version): - PlaQui::Server::HTTPMessage(body, version) { +HTTPRequest::HTTPRequest(const Serializable& body, + const string& version): + HTTPMessage(body, version) { #ifdef DEBUG - std::cerr << __FILE__ << ": http_version = " << http_version - << " | body = " << body.serialize() << std::endl; + cerr << __FILE__ << ": http_version = " << http_version + << " | body = " << body.serialize() << endl; #endif // DEBUG } -PlaQui::Server::HTTPRequest::HTTPRequest(const std::string& uri, - const PlaQui::Server::HTTPRequest::HTTPMethod& method, - std::string& query, std::string& version): - PlaQui::Server::HTTPMessage(body, version) { +HTTPRequest::HTTPRequest(const string& uri, + const HTTPRequest::HTTPMethod& method, + string& query, string& version): + HTTPMessage(body, version) { #ifdef DEBUG - std::cerr << __FILE__ << ": http_version = " << http_version - << " | body = " << body.serialize() << std::endl; + cerr << __FILE__ << ": http_version = " << http_version + << " | body = " << body.serialize() << endl; #endif // DEBUG } */ -std::istream& operator>>(std::istream& is, PlaQui::Server::HTTPRequest& req) { +istream& operator>>(istream& is, HTTPRequest& req) { #ifdef DEBUG - std::cerr << __FILE__ << ": operator>>()" << std::endl; + cerr << __FILE__ << ": operator>>()" << endl; #endif // DEBUG char buf[BUFSIZ]; // Obtengo primera línea (request) is.getline(buf, BUFSIZ); #ifdef DEBUG - std::cerr << "Recibiendo linea: " << buf << std::endl; + cerr << "Recibiendo linea: " << buf << endl; #endif // DEBUG - String line = buf; + String line(buf); // Si es la primera línea, es el request. if (line.length() < 3) { // FIXME - poner excepciones lindas. throw "HTTP/1.1 501 Method Not Implemented"; } // Averiguo método. - std::string::size_type pos = line.find_first_of(String::SPACE_CHARS); + string::size_type pos = line.find_first_of(String::SPACE_CHARS); String met = line.substr(0, pos); if (met.to_upper() == "GET") { - method = GET; + req.method = HTTPRequest::GET; } else if (met.to_upper() == "POST") { - method = POST; + req.method = HTTPRequest::POST; } else { // FIXME - poner excepciones lindas. throw "HTTP/1.1 501 Method Not Implemented"; } // Si tiene sólo el método, no es válido. line = line.substr(pos + 1); - iine.trim(); + line.trim(); if (!line.length()) { // FIXME - poner excepciones lindas. throw "HTTP/1.1 400 Bad Request"; } // Si tiene más espacios, tengo la URI y el protocolo (o un error). pos = line.find_first_of(String::SPACE_CHARS); - if (pos != std::string::npos) { + if (pos != string::npos) { // Si el resto es un protocolo válido, agrego más variables. String protocol = line.substr(pos + 1); protocol = protocol.trim(); if (protocol.to_upper() == "HTTP/1.0") { - version = "1.0"; + req.version = "1.0"; } else if (protocol.to_upper() == "HTTP/1.1") { - version = "1.1"; + req.version = "1.1"; // Si no es un error. } else { // FIXME - poner excepciones lindas. @@ -123,26 +133,33 @@ std::istream& operator>>(std::istream& is, PlaQui::Server::HTTPRequest& req) { throw "HTTP/1.1 400 Bad Request"; } // Si tiene un query string. - pos = request.find("?"); - if (pos != std::string::npos) { - query = line.substr(pos + 1); + pos = line.find("?"); + if (pos != string::npos) { + req.query = line.substr(pos + 1); } else { - query = ""; + req.query = ""; } - uri = line.substr(0, pos); + req.uri = line.substr(0, pos); // Fin de request, obtengo el mensaje. - is >> *(static_cast(this)); + is >> static_cast(req); + //PlaQui::Server::operator>>(is, static_cast(req)); + return is; } -std::ostream& operator<<(std::ostream& os, PlaQui::Server::HTTPRequest& req) { +ostream& operator<<(ostream& os, const HTTPRequest& req) { #ifdef DEBUG - std::cerr << __FILE__ << ": operator<<()" << std::endl; + cerr << __FILE__ << ": operator<<()" << endl; #endif // DEBUG - os << method << " " << uri; - if (query_string.length()) { - os << "?" << query; + os << req.method << " " << req.uri; + if (req.query.length()) { + os << "?" << req.query; } // TODO ver que este bien el \r\l - os << " HTTP/" << version << "\r\l" << *(static_cast(this)); + os << " HTTP/" << req.version << "\r\n" << static_cast(req); + return os; } +} // namespace Server + +} // namespace PlaQui + diff --git a/Server/src/httpresponse.cpp b/Server/src/httpresponse.cpp index fb6ba78..5a5b246 100644 --- a/Server/src/httpresponse.cpp +++ b/Server/src/httpresponse.cpp @@ -27,80 +27,88 @@ #include "plaqui/server/httpresponse.h" #include "plaqui/server/string.h" +#include #ifdef DEBUG # include #endif // DEBUG -PlaQui::Server::HTTPRequest::~HTTPResponse(void) { +using namespace std; + +namespace PlaQui { + +namespace Server { + +HTTPResponse::~HTTPResponse(void) { #ifdef DEBUG - std::cerr << __FILE__ << ": destructor." << std::endl; + cerr << __FILE__ << ": destructor." << endl; #endif // DEBUG } -PlaQui::Server::HTTPResponse::HTTPResponse(const std::string& version): - PlaQui::Server::HTTPMessage(version) { +HTTPResponse::HTTPResponse(const string& version): + HTTPMessage(version) { #ifdef DEBUG - std::cerr << __FILE__ << ": http_version = " << http_version << std::endl; + cerr << __FILE__ << ": version = " << version << endl; #endif // DEBUG } /* -PlaQui::Server::HTTPResponse::HTTPResponse(const Serializable& body, - const std::string& version): - PlaQui::Server::HTTPMessage(body, version) { +HTTPResponse::HTTPResponse(const Serializable& body, + const string& version): + HTTPMessage(body, version) { #ifdef DEBUG - std::cerr << __FILE__ << ": http_version = " << http_version - << " | body = " << body.serialize() << std::endl; + cerr << __FILE__ << ": http_version = " << http_version + << " | body = " << body.serialize() << endl; #endif // DEBUG } -PlaQui::Server::HTTPResponse::HTTPResponse(const std::string& uri, - const PlaQui::Server::HTTPResponse::HTTPMethod& method, - std::string& query, std::string& version): - PlaQui::Server::HTTPMessage(body, version) { +HTTPResponse::HTTPResponse(const string& uri, + const HTTPResponse::HTTPMethod& method, + string& query, string& version): + HTTPMessage(body, version) { #ifdef DEBUG - std::cerr << __FILE__ << ": http_version = " << http_version - << " | body = " << body.serialize() << std::endl; + cerr << __FILE__ << ": http_version = " << http_version + << " | body = " << body.serialize() << endl; #endif // DEBUG } */ -std::istream& operator>>(std::istream& is, PlaQui::Server::HTTPResponse& req) { +istream& operator>>(istream& is, HTTPResponse& resp) { #ifdef DEBUG - std::cerr << __FILE__ << ": operator>>()" << std::endl; + cerr << __FILE__ << ": operator>>()" << endl; #endif // DEBUG char buf[BUFSIZ]; // Obtengo primera línea (request) is.getline(buf, BUFSIZ); #ifdef DEBUG - std::cerr << "Recibiendo linea: " << buf << std::endl; + cerr << "Recibiendo linea: " << buf << endl; #endif // DEBUG - String line = buf; + String line(buf); // Si es la primera línea, es el request. if (line.to_upper().substr(0, 4) != "HTTP/") { // FIXME - poner excepciones lindas. throw "Not a HTTP response"; } // Averiguo la versión. - std::string::size_type pos = line.find_first_of(String::SPACE_CHARS, 5); - std::string ver = line.substr(5, pos); + string::size_type pos = line.find_first_of(String::SPACE_CHARS, 5); + string ver = line.substr(5, pos); if ((ver == "1.1") || (ver == "1.0")) { - version = ver; + resp.version = ver; } else { // FIXME - poner excepciones lindas. throw "Invalid HTTP version"; } // Si tiene sólo la versión HTTP, no es válido. - line = line.substr(pos + 1).trim(); + line = line.substr(pos + 1); + line.trim(); if (!line.length()) { // FIXME - poner excepciones lindas. throw "Invalid HTTP response"; } // Si tiene más espacios, tengo la razón (reason). pos = line.find_first_of(String::SPACE_CHARS); - if (pos != std::string::npos) { - // Si el resto es un protocolo válido, agrego más variables. - String reas = line.substr(pos + 1).trim(); + if (pos != string::npos) { + String r = line.substr(pos + 1); + resp.reason = r.trim(); line = line.substr(0, pos); } line = line.trim(); @@ -109,16 +117,24 @@ std::istream& operator>>(std::istream& is, PlaQui::Server::HTTPResponse& req) { // FIXME - poner excepciones lindas. throw "Invalid response code"; } - stringstream ss = line; // TODO ver forma mas linda de convertir - ss >> status_code; + stringstream ss; + ss << line; // TODO ver forma mas linda de convertir + ss >> resp.status_code; + is >> static_cast(resp); + return is; } -std::ostream& operator<<(std::ostream& os, PlaQui::Server::HTTPResponse& req) { +ostream& operator<<(ostream& os, const HTTPResponse& resp) { #ifdef DEBUG - std::cerr << __FILE__ << ": operator<<()" << std::endl; + cerr << __FILE__ << ": operator<<()" << endl; #endif // DEBUG - os << "HTTP/" << version << " " << status_code << " " << reason << "\r\l"; + os << "HTTP/" << resp.version << " " << resp.status_code << " " << resp.reason << "\r\n"; // TODO ver que este bien el \r\l - os << *(static_cast(this)); + os << static_cast(resp); + return os; } +} // namespace Server + +} // namespace PlaQui + diff --git a/Server/src/receiver.cpp b/Server/src/receiver.cpp index 9667e8c..2a8e9bf 100644 --- a/Server/src/receiver.cpp +++ b/Server/src/receiver.cpp @@ -30,33 +30,43 @@ # include #endif // DEBUG -PlaQui::Server::Receiver::~Receiver(void) { +using namespace std; + +namespace PlaQui { + +namespace Server { + +Receiver::~Receiver(void) { #ifdef DEBUG - std::cerr << __FILE__ << ": destructor." << std::endl; + cerr << __FILE__ << ": destructor." << endl; #endif // DEBUG } -PlaQui::Server::Receiver::Receiver(int port, std::string host): +Receiver::Receiver(int port, string host): ServerConnection(sockbuf::sock_dgram) { #ifdef DEBUG - std::cerr << __FILE__ << ": port = " << port - << " | host = " << host << std::endl; + cerr << __FILE__ << ": port = " << port + << " | host = " << host << endl; #endif // DEBUG // FIXME - deberia ir en run(). socket->bind(port); } -void PlaQui::Server::Receiver::real_run(void) { +void Receiver::real_run(void) { #ifdef DEBUG - std::cerr << __FILE__ << ": real_run." << std::endl; + cerr << __FILE__ << ": real_run." << endl; #endif // 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; + cerr << "No se pudo conectar a " << socket->peerhost() << + ":" << socket->peerport() << "." << endl; } else { - std::cerr << "Conectado a " << socket->peerhost() << - ":" << socket->peerport() << "." << std::endl; + cerr << "Conectado a " << socket->peerhost() << + ":" << socket->peerport() << "." << endl; } } +} // namespace Server + +} // namespace PlaQui + diff --git a/Server/src/runnable.cpp b/Server/src/runnable.cpp index cb3ba6f..0439ca0 100644 --- a/Server/src/runnable.cpp +++ b/Server/src/runnable.cpp @@ -32,32 +32,38 @@ # include #endif // DEBUG -PlaQui::Server::Runnable::~Runnable(void) { +using namespace std; + +namespace PlaQui { + +namespace Server { + +Runnable::~Runnable(void) { #ifdef DEBUG - std::cerr << __FILE__ << ": destructor(this = " << this << ")" - << std::endl; + cerr << __FILE__ << ": destructor(this = " << this << ")" + << endl; #endif // DEBUG } -PlaQui::Server::Runnable::Runnable(void): thread(0), stop(false) { +Runnable::Runnable(void): thread(0), stop(false) { #ifdef DEBUG - std::cerr << __FILE__ << ": constructor." << std::endl; + cerr << __FILE__ << ": constructor." << endl; #endif // DEBUG } -void PlaQui::Server::Runnable::static_run(Runnable* runner) { +void Runnable::static_run(Runnable* runner) { #ifdef DEBUG - std::cerr << __FILE__ << ": static_run(runner = " << runner << ")" - << std::endl; + cerr << __FILE__ << ": static_run(runner = " << runner << ")" + << endl; #endif // DEBUG runner->real_run(); runner->signal_finished().emit(); delete runner; } -void PlaQui::Server::Runnable::run(bool detach) { +void Runnable::run(bool detach) { #ifdef DEBUG - std::cerr << __FILE__ << ": run(detach = " << detach << ")" << std::endl; + cerr << __FILE__ << ": run(detach = " << detach << ")" << endl; #endif // DEBUG // Si vamos a correr la tarea en un thread. if (detach) { @@ -73,9 +79,9 @@ void PlaQui::Server::Runnable::run(bool detach) { } -void PlaQui::Server::Runnable::finish(bool attach) { +void Runnable::finish(bool attach) { #ifdef DEBUG - std::cerr << __FILE__ << ": finish(attach = " << attach << ")" << std::endl; + cerr << __FILE__ << ": finish(attach = " << attach << ")" << endl; #endif // DEBUG stop = true; if (attach) { @@ -83,8 +89,11 @@ void PlaQui::Server::Runnable::finish(bool attach) { } } -PlaQui::Server::Runnable::SignalFinished& -PlaQui::Server::Runnable::signal_finished(void) { +Runnable::SignalFinished& Runnable::signal_finished(void) { return finished; } +} // namespace Server + +} // namespace PlaQui + diff --git a/Server/src/server.cpp b/Server/src/server.cpp index 134bcf5..9d123f1 100644 --- a/Server/src/server.cpp +++ b/Server/src/server.cpp @@ -33,34 +33,40 @@ # include #endif // DEBUG -PlaQui::Server::Server::~Server(void) { +using namespace std; + +namespace PlaQui { + +namespace Server { + +Server::~Server(void) { #ifdef DEBUG - std::cerr << __FILE__ << ": destructor." << std::endl; + cerr << __FILE__ << ": destructor." << endl; #endif // DEBUG } -PlaQui::Server::Server::Server(int port): +Server::Server(int port): TCPServer(port) { #ifdef DEBUG - std::cerr << __FILE__ << ": port = " << port << std::endl; + cerr << __FILE__ << ": port = " << port << endl; #endif // DEBUG } /// \todo Implementar. -bool PlaQui::Server::Server::start_transmission(std::string host, int port) { +bool Server::start_transmission(string host, int port) { #ifdef DEBUG - std::cerr << __FILE__ << ": start_transmission(host = " << host - << " | port = " << port << ")" << std::endl; + cerr << __FILE__ << ": start_transmission(host = " << host + << " | port = " << port << ")" << endl; #endif // DEBUG // TODO return false; } -PlaQui::Server::Connection* PlaQui::Server::Server::new_connection( +Connection* Server::new_connection( const sockbuf::sockdesc& sd) { #ifdef DEBUG - std::cerr << __FILE__ << ": new_connection(sd = " << sd.sock << ")" - << std::endl; + cerr << __FILE__ << ": new_connection(sd = " << sd.sock << ")" + << endl; #endif // DEBUG ControlServer* connection = new ControlServer(sd); // TODO verificar si el new se hace bien? no creo. @@ -71,20 +77,24 @@ PlaQui::Server::Connection* PlaQui::Server::Server::new_connection( } /// \todo Implementar. -bool PlaQui::Server::Server::stop_transmission(std::string host, int port) { +bool Server::stop_transmission(string host, int port) { #ifdef DEBUG - std::cerr << __FILE__ << ": stop_transmission(host = " << host - << " | port = " << port << ")" << std::endl; + cerr << __FILE__ << ": stop_transmission(host = " << host + << " | port = " << port << ")" << endl; #endif // DEBUG // TODO return false; } /// \todo Implementar. -void PlaQui::Server::Server::on_control_command_received(void* command) { +void Server::on_control_command_received(void* command) { #ifdef DEBUG - std::cerr << __FILE__ << ": on_control_command_received(command = " - << command << ")" << std::endl; + cerr << __FILE__ << ": on_control_command_received(command = " + << command << ")" << endl; #endif // DEBUG } +} // namespace Server + +} // namespace PlaQui + diff --git a/Server/src/string.cpp b/Server/src/string.cpp index 7062bb0..15e783c 100644 --- a/Server/src/string.cpp +++ b/Server/src/string.cpp @@ -32,29 +32,38 @@ # include #endif // DEBUG -const std::string PlaQui::Server::String::SPACE_CHARS = " \t\n\r"; +using namespace std; -PlaQui::Server::String::~String(void) { +namespace PlaQui { + +namespace Server { + +const string String::SPACE_CHARS = " \t\n\r"; + +String::~String(void) { } -PlaQui::Server::String::String(const std::string& str): - std::string(str.c_str()) { +String::String(const string& str): + string(str.c_str()) { } -PlaQui::Server::String& PlaQui::Server::String::trim( - const PlaQui::Server::String& clist) { +String& String::trim(const String& clist) { erase(0, find_first_not_of(clist)); erase(find_last_not_of(clist) + 1); return *this; } -PlaQui::Server::String& PlaQui::Server::String::to_lower(void) { - std::transform(begin(), end(), begin(), tolower); +String& String::to_lower(void) { + transform(begin(), end(), begin(), tolower); return *this; } -PlaQui::Server::String& PlaQui::Server::String::to_upper(void) { - std::transform(begin(), end(), begin(), toupper); +String& String::to_upper(void) { + transform(begin(), end(), begin(), toupper); return *this; } +} // namespace Server + +} // namespace PlaQui + diff --git a/Server/src/tcpserver.cpp b/Server/src/tcpserver.cpp index 861b6ee..9d83135 100644 --- a/Server/src/tcpserver.cpp +++ b/Server/src/tcpserver.cpp @@ -27,78 +27,82 @@ #include "plaqui/server/tcpserver.h" #include -// FIXME - para probar mutex -#include #ifdef DEBUG # include #endif // DEBUG -const unsigned PlaQui::Server::TCPServer::MAX_PENDING_CONNECTIONS = 10; +using namespace std; -PlaQui::Server::TCPServer::~TCPServer(void) { +namespace PlaQui { + +namespace Server { + +const unsigned TCPServer::MAX_PENDING_CONNECTIONS = 10; + +TCPServer::~TCPServer(void) { #ifdef DEBUG - std::cerr << __FILE__ << ": destructor." << std::endl; + cerr << __FILE__ << ": destructor." << endl; #endif // DEBUG } -PlaQui::Server::TCPServer::TCPServer(int port): - socket(sockbuf::sock_stream) { +TCPServer::TCPServer(int port): socket(sockbuf::sock_stream) { #ifdef DEBUG - std::cerr << __FILE__ << ": port = " << port << std::endl; + cerr << __FILE__ << ": port = " << port << endl; #endif // DEBUG socket.bind(port); #ifdef DEBUG - std::cerr << __FILE__ << ": escuchando en " << socket.localhost() - << ":" << socket.localport() << "." << std::endl; + cerr << __FILE__ << ": escuchando en " << socket.localhost() + << ":" << socket.localport() << "." << endl; #endif // DEBUG socket.listen(MAX_PENDING_CONNECTIONS); #ifdef DEBUG - std::cerr << __FILE__ << ": [despues de listen()] escuchando en " - << socket.localhost() << ":" << socket.localport() << "." << std::endl; + cerr << __FILE__ << ": [despues de listen()] escuchando en " + << socket.localhost() << ":" << socket.localport() << "." << endl; #endif // DEBUG } -void PlaQui::Server::TCPServer::on_connection_finished( +void TCPServer::on_connection_finished( Connection* connection) { #ifdef DEBUG - std::cerr << __FILE__ << ": on_connection_finished(connection = " - << connection << ")" << std::endl; + cerr << __FILE__ << ": on_connection_finished(connection = " + << connection << ")" << endl; #endif // DEBUG Glib::Mutex::Lock lock(connections_mutex); connections.remove(connection); #ifdef DEBUG - std::cerr << __FILE__ << ": lista de conexiones" << std::endl; + cerr << __FILE__ << ": lista de conexiones" << endl; for (ConnectionList::const_iterator i = connections.begin(); i != connections.end(); i++) { - std::cerr << "\t " << *i << std::endl; + cerr << "\t " << *i << endl; } #endif // DEBUG } /// \todo TODO: ver tema de timeout o como salir de un accept(). -void PlaQui::Server::TCPServer::real_run(void) { +void TCPServer::real_run(void) { #ifdef DEBUG - std::cerr << __FILE__ << ": real_run()" << std::endl; + cerr << __FILE__ << ": real_run()" << endl; #endif // DEBUG Connection* connection; while (!stop) { // Forma grasa de salir del accept: crear conexion que salga al toque. connection = new_connection(socket.accept()); #ifdef DEBUG - std::cerr << __FILE__ << ": real_run(): connection = " << connection - << std::endl; + cerr << __FILE__ << ": real_run(): connection = " << connection + << endl; #endif // DEBUG Glib::Mutex::Lock lock(connections_mutex); // XXX connections_mutex.lock(); connections.push_back(connection); #ifdef DEBUG - std::cerr << __FILE__ << ": real_run(): lista de conexiones" << std::endl; + cerr << __FILE__ << ": real_run(): lista de conexiones" << endl; for (ConnectionList::const_iterator i = connections.begin(); i != connections.end(); i++) { - std::cerr << "\t " << *i << std::endl; + cerr << "\t " << *i << endl; } #endif // DEBUG - // XXX connections_mutex.unlock(); // Si pongo el mutex antes del run(), muere. + // XXX connections_mutex.unlock(); // Si pongo el mutex antes del run(), + // muere. // Conecto la señal para cuando termina una conexión, borrarla. connection->signal_finished().connect( SigC::bind( @@ -109,3 +113,7 @@ void PlaQui::Server::TCPServer::real_run(void) { } } +} // namespace Server + +} // namespace PlaQui + diff --git a/Server/src/transmitter.cpp b/Server/src/transmitter.cpp index 75f622d..4211e51 100644 --- a/Server/src/transmitter.cpp +++ b/Server/src/transmitter.cpp @@ -32,34 +32,44 @@ # include #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__ << ": destructor." << endl; #endif // DEBUG } /// \todo debría conectarse en real_run() (?) -PlaQui::Server::Transmitter::Transmitter(std::string host, int port): +Transmitter::Transmitter(string host, int port): Connection(sockbuf::sock_dgram) { #ifdef DEBUG - std::cerr << __FILE__ << ": host = " << host - << " | port = " << port << std::endl; + cerr << __FILE__ << ": host = " << host + << " | port = " << port << endl; #endif // DEBUG // FIXME - deberia ir en run(). socket->connect(host.c_str(), port); } /// \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; + cerr << "No se pudo conectar a " << socket->peerhost() << + ":" << socket->peerport() << "." << endl; } else { - std::cerr << "Conectado a " << socket->peerhost() << - ":" << socket->peerport() << "." << std::endl; + cerr << "Conectado a " << socket->peerhost() << + ":" << socket->peerport() << "." << endl; } #endif // DEBUG } +} // namespace Server + +} // namespace PlaQui + diff --git a/docs/server.txt b/docs/server.txt index c81a3f8..cc23f63 100644 --- a/docs/server.txt +++ b/docs/server.txt @@ -1,6 +1,6 @@ +----------------------------+ - | PROPUESTA DE SERVIDOR v0.2 | + | PROPUESTA DE SERVIDOR v0.3 | +----------------------------+ $Id$ @@ -51,8 +51,8 @@ problema si consideramos que en pocos milisegundos se recibir video en tiempo real). -Lista de comandos disponibles para el módulo de control: -======================================================== +Descripción de los comandos para el módulo de control: +====================================================== Todos los comandos son rutas de archivos. En un principio no se van a utilizar los 'query string' de los datos pasados por GET ni datos adicionales pasados @@ -143,6 +143,41 @@ stop// |Finaliza la transmisi NOTA: Los nombres entre "<" y ">" denotan un argumento. +Descripción de los comandos para el módulo de control: +====================================================== + +Todas las respuestas consisten de un archivo XML con esta forma (falta hacer la +DTD): + + + +El código es obligatorio e informará si el comando se realizó con éxito y en +caso de no hacerlo, indicará la razón (los códigos faltan definirlos, pero usar +un esquema similar a los códigos de HTTP sería un buen comienzo). +La respuesta también puede tener otros contenidos (listado de plantas, +conexiónes, transmisiones, descripción de una planta completa, etc). Dichos +contenidos irán contenidos en el tag response: + + + + + + + + + + + + + + + + + +TODO: Ver si la lista va dentro o fuera del tag , ver si el tag + está bien, es útil y correcto. + + Características adicionales (a desarrollar si el tiempo lo permite): ==================================================================== -- 2.43.0