#include <map>
#include <string>
+#include <istream>
+#include <ostream>
namespace PlaQui {
friend std::ostream& operator<<(std::ostream& os,
const HTTPHeaders& h);
+ };
+
+}
}
#ifndef PLAQUI_HTTPMESSAGE_H
#define PLAQUI_HTTPMESSAGE_H
+#include "plaqui/server/httpheaders.h"
#include <string>
namespace PlaQui {
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.
# include <iostream>
#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
+
# include <iostream>
#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
+
//
#include "plaqui/server/controlserver.h"
-#include "plaqui/server/request.h"
+#include "plaqui/server/httprequest.h"
#include <cstring>
#include <sstream>
#ifdef DEBUG
# include <iostream>
#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.
// 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 << "<html>" << std::endl;
- response_xml << " <head>" << std::endl;
- response_xml << " <title>PlaQui v0.1</title>" << std::endl;
- response_xml << " </head>" << std::endl;
- response_xml << " <body>" << std::endl;
- response_xml << " <h1>PlaQui</h1>" << std::endl;
- response_xml << " <p>versión 0.2</p>" << std::endl;
- response_xml << " <h2>Pedido HTTP</h2>" << std::endl;
- response_xml << " <ul>" << std::endl;
+ socket << "Content-Type: text/html; charset=iso-8859-1" << endl;
+ response_xml << "<html>" << endl;
+ response_xml << " <head>" << endl;
+ response_xml << " <title>PlaQui v0.1</title>" << endl;
+ response_xml << " </head>" << endl;
+ response_xml << " <body>" << endl;
+ response_xml << " <h1>PlaQui</h1>" << endl;
+ response_xml << " <p>versión 0.2</p>" << endl;
+ response_xml << " <h2>Pedido HTTP</h2>" << endl;
+ response_xml << " <ul>" << endl;
for (Request::const_iterator i = request.begin(); i != request.end();
i++) {
response_xml << " <li><b>" << i->first << ":</b> "
- << i->second << std::endl;
+ << i->second << endl;
}
- response_xml << " </ul>" << std::endl;
- response_xml << " <h2>Desarrollado por</h2>" << std::endl;
- response_xml << " <ul>" << std::endl;
- response_xml << " <li>Nicolás Dimov.</li>" << std::endl;
- response_xml << " <li>Leandro Lucarella.</li>" << std::endl;
- response_xml << " <li>Ricardo Markiewicz.</li>" << std::endl;
- response_xml << " </ul>" << std::endl;
- response_xml << " <address>" << std::endl;
- response_xml << " Copyleft 2003 - bajo los " << std::endl;
- response_xml << " términos de la licencia GPL" << std::endl;
- response_xml << " </address>" << std::endl;
- response_xml << " </body>" << std::endl;
- response_xml << "</html>" << std::endl;
- socket << "Content-Length: " << response_xml.str().length() << std::endl;
- socket << std::endl;
- socket << response_xml.str() << std::flush;
+ response_xml << " </ul>" << endl;
+ response_xml << " <h2>Desarrollado por</h2>" << endl;
+ response_xml << " <ul>" << endl;
+ response_xml << " <li>Nicolás Dimov.</li>" << endl;
+ response_xml << " <li>Leandro Lucarella.</li>" << endl;
+ response_xml << " <li>Ricardo Markiewicz.</li>" << endl;
+ response_xml << " </ul>" << endl;
+ response_xml << " <address>" << endl;
+ response_xml << " Copyleft 2003 - bajo los " << endl;
+ response_xml << " términos de la licencia GPL" << endl;
+ response_xml << " </address>" << endl;
+ response_xml << " </body>" << endl;
+ response_xml << "</html>" << endl;
+ socket << "Content-Length: " << response_xml.str().length() << endl;
+ socket << endl;
+ socket << response_xml.str() << flush;
}
}
+} // namespace Server
+
+} // namespace PlaQui
+
//
#include "plaqui/server/httpheaders.h"
+#include "plaqui/server/string.h"
//#include <cstdlib>
#ifdef DEBUG
# include <iostream>
#endif // DEBUG
using namespace std;
-using namespace PlaQui::Server;
+
+namespace PlaQui {
+
+namespace Server {
HTTPHeaders::~HTTPHeaders(void) {
#ifdef DEBUG
// 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;
}
return os;
}
+} // namespace Server
+
+} // namespace PlaQui
+
#endif // DEBUG
using namespace std;
-using namespace PlaQui::Server;
+
+namespace PlaQui {
+
+namespace Server {
HTTPMessage::~HTTPMessage(void) {
#ifdef DEBUG
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;
}
}
}
// 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
+
# include <iostream>
#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.
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<HTTPMessage*>(this));
+ is >> static_cast<HTTPMessage&>(req);
+ //PlaQui::Server::operator>>(is, static_cast<HTTPMessage>(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<HTTPMessage*>(this));
+ os << " HTTP/" << req.version << "\r\n" << static_cast<const HTTPMessage&>(req);
+ return os;
}
+} // namespace Server
+
+} // namespace PlaQui
+
#include "plaqui/server/httpresponse.h"
#include "plaqui/server/string.h"
+#include <sstream>
#ifdef DEBUG
# include <iostream>
#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();
// 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<HTTPMessage&>(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<HTTPMessage*>(this));
+ os << static_cast<const HTTPMessage&>(resp);
+ return os;
}
+} // namespace Server
+
+} // namespace PlaQui
+
# include <iostream>
#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
+
# include <iostream>
#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) {
}
-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) {
}
}
-PlaQui::Server::Runnable::SignalFinished&
-PlaQui::Server::Runnable::signal_finished(void) {
+Runnable::SignalFinished& Runnable::signal_finished(void) {
return finished;
}
+} // namespace Server
+
+} // namespace PlaQui
+
# include <iostream>
#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.
}
/// \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
+
# include <iostream>
#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
+
#include "plaqui/server/tcpserver.h"
#include <sigc++/class_slot.h>
-// FIXME - para probar mutex
-#include <glibmm/timer.h>
#ifdef DEBUG
# include <iostream>
#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<Connection*>(
}
}
+} // namespace Server
+
+} // namespace PlaQui
+
# 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__ << ": 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
+
+----------------------------+
- | PROPUESTA DE SERVIDOR v0.2 |
+ | PROPUESTA DE SERVIDOR v0.3 |
+----------------------------+
$Id$
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
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):
+<plaqui version="0.1">
+ <response code="[código]" />
+</plaqui>
+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:
+<plaqui version="0.1">
+ <response code= "[código]">
+ <list type="plants">
+ <item id="[id1]">
+ <prop name="[nombre1]" value="[valor1]" />
+ <prop name="[nombre2]" value="[valor2]" />
+ <!-- ... más ... -->
+ </item>
+ <item id="[id2]">
+ <prop name="[nombre1]" value="[valor3]" />
+ <prop name="[nombre2]" value="[valor4]" />
+ <!-- ... más ... -->
+ </item>
+ </list>
+ </response>
+</plaqui>
+
+TODO: Ver si la lista va dentro o fuera del tag <response>, ver si el tag
+ <plaqui> está bien, es útil y correcto.
+
+
Características adicionales (a desarrollar si el tiempo lo permite):
====================================================================