]> git.llucax.com Git - z.facultad/75.42/plaqui.git/commitdiff
- Se agrega el objeto Command que encapsula un comando enviado al servidor.
authorLeandro Lucarella <llucax@gmail.com>
Sat, 8 Nov 2003 23:15:07 +0000 (23:15 +0000)
committerLeandro Lucarella <llucax@gmail.com>
Sat, 8 Nov 2003 23:15:07 +0000 (23:15 +0000)
- Se actualiza el controlserver para que use Command.
- Se agregan funciones split() y join() a String.
- Se corrigen bugs insignificantes.

Server/include/plaqui/server/command.h [new file with mode: 0644]
Server/include/plaqui/server/httprequest.h
Server/include/plaqui/server/string.h
Server/src/Makefile
Server/src/command.cpp [new file with mode: 0644]
Server/src/controlserver.cpp
Server/src/httprequest.cpp
Server/src/string.cpp

diff --git a/Server/include/plaqui/server/command.h b/Server/include/plaqui/server/command.h
new file mode 100644 (file)
index 0000000..019cad2
--- /dev/null
@@ -0,0 +1,139 @@
+// vim: set noexpandtab tabstop=4 shiftwidth=4:
+//----------------------------------------------------------------------------
+//                                  PlaQui
+//----------------------------------------------------------------------------
+// This file is part of PlaQui.
+//
+// PlaQui is free software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the Free Software
+// Foundation; either version 2 of the License, or (at your option) any later
+// version.
+//
+// PlaQui is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+// details.
+//
+// You should have received a copy of the GNU General Public License along
+// with PlaQui; if not, write to the Free Software Foundation, Inc., 59 Temple
+// Place, Suite 330, Boston, MA  02111-1307  USA
+//----------------------------------------------------------------------------
+// Creado:  sáb nov  8 17:02:44 ART 2003
+// Autores: Leandro Lucarella <llucare@fi.uba.ar>
+//----------------------------------------------------------------------------
+//
+// $Id$
+//
+
+#ifndef PLAQUI_COMMAND_H
+#define PLAQUI_COMMAND_H
+
+#include "httprequest.h"
+#include <string>
+#include <vector>
+#include <istream>
+#include <ostream>
+
+namespace PlaQui {
+
+namespace Server {
+
+       /// Pedido HTTP.
+       class Command: private HTTPRequest {
+
+               // Tipos.
+
+               public:
+
+                       /// Tipo de métodos HTTP reconocidos.
+                       typedef std::vector<std::string> Arguments;
+
+               // Atributos.
+
+               private:
+
+                       /// Destinatario del comando.
+                       std::string target;
+
+                       /// Comando.
+                       std::string command;
+
+                       /// Lista de argumentos que recibe el comando.
+                       Arguments args;
+
+               // Métodos.
+
+               private:
+
+                       /**
+                        * Construye el URI.
+                        */
+                       void build(void);
+
+               public:
+
+                       /**
+                        * Destructor.
+                        */
+                       virtual ~Command(void);
+
+                       /**
+                        * Constructor.
+                        */
+                       Command(const std::string& target = "",
+                                       const std::string& command = "");
+
+                       /**
+                        * Establece el destino.
+                        */
+                       void set_target(const std::string& _target);
+
+                       /**
+                        * Obtiene el destino.
+                        */
+                       const std::string& get_target(void);
+
+                       /**
+                        * Establece el comando.
+                        */
+                       void set_command(const std::string& _command);
+
+                       /**
+                        * Obtiene el comand.
+                        */
+                       const std::string& get_command(void);
+
+                       /**
+                        * Establece los argumentos.
+                        */
+                       void set_args(const Arguments& _args);
+
+                       /**
+                        * Obtiene los argumentos.
+                        */
+                       const Arguments& get_args(void);
+
+                       /**
+                        * Agrega un argumentos.
+                        */
+                       void add_arg(const std::string& arg);
+
+                       /**
+                        * Obtiene el comando desde un pedido HTTP completo.
+                        */
+                       friend std::istream& operator>>(std::istream& is,
+                                       Command& command);
+
+                       /**
+                        * Convierte el comando a un pedido HTTP completo.
+                        */
+                       friend std::ostream& operator<<(std::ostream& os,
+                                       const Command& command);
+
+       };
+
+}
+
+}
+
+#endif // PLAQUI_COMMAND_H 
index 44b45a9c590f25447dc4e0a91f6b1aa32603973b..ace04f077d8f1bb62d90b2b17f6033cf2e5099af 100644 (file)
@@ -30,6 +30,8 @@
 
 #include "httpmessage.h"
 #include <string>
+#include <istream>
+#include <ostream>
 
 namespace PlaQui {
 
index 53752e444cefbb791b2ccd8a24ab35ed045be8ea..74c27cd96c175742f8c856ccc107cd9ae6ca7697 100644 (file)
@@ -29,6 +29,7 @@
 #define PLAQUI_STRING_H
 
 #include <string>
+#include <vector>
 
 namespace PlaQui {
 
@@ -54,6 +55,17 @@ namespace Server {
                         */
                        String(const std::string& str);
 
+                       /**
+                        * Constructor a partir de un vector.
+                        * Convierte el vector en string uniendo sus componentes a traves
+                        * del separador.
+                        *
+                        * \param v   Vector.
+                        * \param sep Separador.
+                        */
+                       String(const std::vector<std::string>& v,
+                                       const std::string& sep);
+
                        /**
                         * Elmina caracteres al inicio y fin de un string.
                         *
@@ -71,6 +83,22 @@ namespace Server {
                         */
                        String& to_lower(void);
 
+                       /**
+                        * Fracciona una cadena convirtiendola en un vector.
+                        *
+                        * \param sep Caracter usado como separador.
+                        */
+                       std::vector<std::string> split(char sep) const;
+
+                       /**
+                        * Concatena los elementos de un vector.
+                        *
+                        * \param v   Vector.
+                        * \param sep Separador.
+                        */
+                       static String join(const std::vector<std::string>& v,
+                                       const std::string& sep);
+
        };
 
 }
index cac6988b1996f65f743c1eca604583c6c2348aa6..683723f899a16f627cbac9d2c09c7d9be4d322c1 100644 (file)
@@ -58,6 +58,10 @@ httprequest_h=$(string_h) $(httpmessage_h) $(INCLUDE_DIR)/httprequest.h
 objects+=httprequest.o
 httprequest.o: $(httprequest_h) httprequest.cpp
 
+command_h=$(string_h) $(httprequest_h) $(INCLUDE_DIR)/command.h
+objects+=command.o
+command.o: $(command_h) command.cpp
+
 httpresponse_h=$(string_h) $(httpmessage_h) $(INCLUDE_DIR)/httprequest.h
 objects+=httpresponse.o
 httpresponse.o: $(httpresponse_h) httpresponse.cpp
@@ -74,7 +78,7 @@ serverconnection_h=$(connection_h) $(INCLUDE_DIR)/serverconnection.h
 objects+=serverconnection.o
 serverconnection.o: $(serverconnection_h) serverconnection.cpp
 
-controlclient_h=$(connection_h) $(INCLUDE_DIR)/controlclient.h
+controlclient_h=$(connection_h) $(command_h) $(INCLUDE_DIR)/controlclient.h
 objects+=controlclient.o
 controlclient.o: $(controlclient_h) controlclient.cpp
 
diff --git a/Server/src/command.cpp b/Server/src/command.cpp
new file mode 100644 (file)
index 0000000..084cf79
--- /dev/null
@@ -0,0 +1,132 @@
+// vim: set noexpandtab tabstop=4 shiftwidth=4:
+//----------------------------------------------------------------------------
+//                                  PlaQui
+//----------------------------------------------------------------------------
+// This file is part of PlaQui.
+//
+// PlaQui is free software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the Free Software
+// Foundation; either version 2 of the License, or (at your option) any later
+// version.
+//
+// PlaQui is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+// details.
+//
+// You should have received a copy of the GNU General Public License along
+// with PlaQui; if not, write to the Free Software Foundation, Inc., 59 Temple
+// Place, Suite 330, Boston, MA  02111-1307  USA
+//----------------------------------------------------------------------------
+// Creado:  sáb nov  8 17:12:10 ART 2003
+// Autores: Leandro Lucarella <llucare@fi.uba.ar>
+//----------------------------------------------------------------------------
+//
+// $Id$
+//
+
+#include "plaqui/server/command.h"
+#include "plaqui/server/string.h"
+#include <algorithm>
+#ifdef DEBUG
+#      include <iostream>
+#endif // DEBUG
+
+using namespace std;
+
+namespace PlaQui {
+
+namespace Server {
+
+Command::~Command(void) {
+#ifdef DEBUG
+       cerr << __FILE__ << ": destructor." << endl;
+#endif // DEBUG
+}
+
+Command::Command(const string& target, const string& command):
+               target(target), command(command) {
+#ifdef DEBUG
+       cerr << __FILE__ << ": target = " << target << " | "
+                       << "command = " << command << endl;
+#endif // DEBUG
+}
+
+void Command::build(void) {
+       uri = string("/") + target + '/' + command + String::join(args, "/");
+#ifdef DEBUG
+       cerr << __FILE__ << ": build() = " << uri << endl;
+#endif // DEBUG
+}
+
+void Command::set_target(const std::string& _target) {
+       target = _target;
+       build();
+}
+
+const std::string& Command::get_target(void) {
+       return target;
+}
+
+void Command::set_command(const std::string& _command) {
+       command = _command;
+       build();
+}
+
+const std::string& Command::get_command(void) {
+       return command;
+}
+
+void Command::set_args(const Command::Arguments& _args) {
+       args = _args;
+       build();
+}
+
+const Command::Arguments& Command::get_args(void) {
+       return args;
+}
+
+void Command::add_arg(const std::string& arg) {
+       args.push_back(arg);
+       build();
+}
+
+istream& operator>>(istream& is, Command& command) {
+#ifdef DEBUG
+       cerr << __FILE__ << ": operator>>()" << endl;
+#endif // DEBUG
+       // Obtengo datos del Request HTTP.
+       is >> static_cast<HTTPRequest&>(command);
+       // Segun la URI, obtengo los fragmentos del comando.
+       String uri(command.uri);
+       command.args = uri.split('/');
+       // Borro el primero porque debe estar vacio porque la URI empieza con /.
+       command.args.erase(command.args.begin());
+       if (command.args.size() > 0) {
+               command.target = command.args[0];
+               command.args.erase(command.args.begin());
+       } else {
+               command.target = "";
+       }
+       if (command.args.size() > 1) {
+               command.command = command.args[1];
+               command.args.erase(command.args.begin());
+       } else {
+               command.command = "";
+       }
+       return is;
+}
+
+ostream& operator<<(ostream& os, const Command& command) {
+#ifdef DEBUG
+       cerr << __FILE__ << ": operator<<()" << endl;
+#endif // DEBUG
+       // Manda el request HTTP con la URI que representa el comando.
+       os << static_cast<const HTTPRequest&>(command);
+       return os;
+}
+
+} // namespace Server
+
+} // namespace PlaQui
+
index 5d67502766e73a4bc98f309c55110279f1d40bc6..133f36eeccc9abb64992180256931253fdcee5a6 100644 (file)
@@ -26,7 +26,8 @@
 //
 
 #include "plaqui/server/controlserver.h"
-#include "plaqui/server/httprequest.h"
+#include "plaqui/server/command.h"
+#include "plaqui/server/string.h"
 #include <cstring>
 #include <sstream>
 #ifdef DEBUG
@@ -58,19 +59,19 @@ void ControlServer::real_run(void) {
 #endif // DEBUG
        //char buf[BUFSIZ];
        while (!stop) {
-               HTTPRequest request;
+               Command command;
                try {
-                       socket >> request;
+                       socket >> command;
                } catch (const char* e) {
-                       cerr << "Error: " << e << endl;
+                       cerr << "    (" << __LINE__ << ") Error: " << e << endl;
                        stop = true;
                        continue;
                } catch (string e) {
-                       cerr << "Error: " << e << endl;
+                       cerr << "    (" << __LINE__ << ") Error: " << e << endl;
                        stop = true;
                        continue;
                } catch (...) {
-                       cerr << "Error desconocido!" << endl;
+                       cerr << "    (" << __LINE__ << ") Error desconocido!" << endl;
                        stop = true;
                        continue;
                }
@@ -113,7 +114,10 @@ void ControlServer::real_run(void) {
                // Command command = parse_command(request.uri);
                //signal_command_received().emit(command);
 #ifdef DEBUG
-               cerr << "Request: " << request << endl;
+               cerr << "Comando: target = " << command.get_target()
+                       << " | command = " << command.get_command()
+                       << " | args = [" << String::join(command.get_args(), ", ") << "]"
+                       << endl;
                //for (HTTPRequest::const_iterator i = request.begin();
                //              i != request.end(); i++) {
                //      cerr << "   " << i->first << ": " << i->second << endl;
@@ -138,10 +142,10 @@ Accept-Ranges: bytes
                response_xml << "    </head>" << endl;
                response_xml << "    <body>" << endl;
                response_xml << "        <h1>PlaQui</h1>" << endl;
-               response_xml << "        <p>versión 0.3</p>" << endl;
-               response_xml << "        <h2>Pedido HTTP</h2>" << endl;
+               response_xml << "        <p>versión 0.4</p>" << endl;
+               response_xml << "        <h2>Comando</h2>" << endl;
                response_xml << "        <ul>" << endl;
-               response_xml << "           <li><b>Versión:</b> " << request.version << endl;
+/*             response_xml << "           <li><b>Versión:</b> " << request.version << endl;
                response_xml << "           <li><b>Método:</b> " << (request.method ? "POST" : "GET") << endl;
                response_xml << "           <li><b>URI:</b> " << request.uri << endl;
                response_xml << "           <li><b>Query:</b> " << request.query << endl;
@@ -149,7 +153,16 @@ Accept-Ranges: bytes
                                i != request.headers.end(); i++) {
                        response_xml << "           <li><b>" << i->first << ":</b> "
                                << i->second << endl;
+               }*/
+               response_xml << "           <li><b>Target:</b> " << command.get_target() << endl;
+               response_xml << "           <li><b>Command:</b> " << command.get_command() << endl;
+               response_xml << "           <li><b>Argumentos:</b>" << endl;
+               response_xml << "               <ol>" << endl;
+               for (Command::Arguments::const_iterator i = command.get_args().begin();
+                               i != command.get_args().end(); i++) {
+                       response_xml << "                   <li>" << *i << "</li>" << endl;
                }
+               response_xml << "               </ol>" << endl;
                response_xml << "        </ul>" << endl;
                response_xml << "        <h2>Desarrollado por</h2>" << endl;
                response_xml << "        <ul>" << endl;
index ce9c5abc02f89abdcddab088023fbb52729436eb..aa5d9cc8079a7f8efa4d75e43ca9e534abf3f9bd 100644 (file)
@@ -43,7 +43,7 @@ HTTPRequest::~HTTPRequest(void) {
 #endif // DEBUG
 }
 
-HTTPRequest::HTTPRequest(const string& uri,    const HTTPMethod& method,
+HTTPRequest::HTTPRequest(const string& uri, const HTTPMethod& method,
                const string& query, const string& version):
                HTTPMessage(version), method(method), uri(uri), query(query) {
 #ifdef DEBUG
index cd2eaadde7be37f25dad1550337c3003229aaf20..b38c7c2835e223eced26bc6fc2ea58533fa1ee23 100644 (file)
@@ -28,6 +28,8 @@
 #include "plaqui/server/string.h"
 #include <cctype>
 #include <algorithm>
+#include <iterator>
+#include <sstream>
 #ifdef DEBUG
 #      include <iostream>
 #endif // DEBUG
@@ -47,6 +49,10 @@ String::String(const string& str):
                string(str.c_str()) {
 }
 
+String::String(const vector<string>& v, const string& sep) {
+       (*this) = join(v, sep);
+}
+
 String& String::trim(const String& clist) {
        erase(0, find_first_not_of(clist));
        erase(find_last_not_of(clist) + 1);
@@ -63,6 +69,27 @@ String& String::to_upper(void) {
        return *this;
 }
 
+vector<string> String::split(char sep) const {
+       vector<string> v;
+       String::size_type start = 0;
+       String::size_type end = find(sep);
+       while (end != String::npos) {
+               v.push_back(substr(start, end - start));
+               start = end + 1;
+               end = find(sep, start);
+       }
+       if (start != length()) {
+               v.push_back(substr(start, end - start));
+       }
+       return v;
+}
+
+String String::join(const vector<string>& v, const string& sep) {
+       stringstream ss;
+       std::copy(v.begin(), v.end(), ostream_iterator<string>(ss, sep.c_str()));
+       return ss.str();
+}
+
 } // namespace Server
 
 } // namespace PlaQui