]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Server/src/server.cpp
- Se agrega el método HTTPRequest::method_str() para obtener el método como un
[z.facultad/75.42/plaqui.git] / Server / src / server.cpp
1 // vim: set noexpandtab tabstop=4 shiftwidth=4:
2 //----------------------------------------------------------------------------
3 //                                  PlaQui
4 //----------------------------------------------------------------------------
5 // This file is part of PlaQui.
6 //
7 // PlaQui is free software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the Free Software
9 // Foundation; either version 2 of the License, or (at your option) any later
10 // version.
11 //
12 // PlaQui is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 // details.
16 //
17 // You should have received a copy of the GNU General Public License along
18 // with PlaQui; if not, write to the Free Software Foundation, Inc., 59 Temple
19 // Place, Suite 330, Boston, MA  02111-1307  USA
20 //----------------------------------------------------------------------------
21 // Creado:  Sat Oct 18 18:18:36 2003
22 // Autores: Leandro Lucarella <llucare@fi.uba.ar>
23 //----------------------------------------------------------------------------
24 //
25 // $Id$
26 //
27
28 #include "plaqui/server/server.h"
29 #include "plaqui/server/connection.h"
30 #include "plaqui/server/controlserver.h"
31 #include <sigc++/class_slot.h>
32 // FIXME - sacar sstream (a menos que se necesite)
33 #include <sstream>
34 #ifdef DEBUG
35 #       include "plaqui/server/string.h"
36 #       include <iostream>
37 #endif // DEBUG
38
39 using namespace std;
40
41 namespace PlaQui {
42
43 namespace Server {
44
45 Server::~Server(void) {
46 #ifdef DEBUG
47         cerr << __FILE__ <<  ": destructor." << endl;
48 #endif // DEBUG
49 }
50
51 Server::Server(int port):
52                 TCPServer(port) {
53 #ifdef DEBUG
54         cerr << __FILE__ <<  ": port = " << port << endl;
55 #endif // DEBUG
56 }
57
58 /// \todo Implementar.
59 bool Server::start_transmission(string host, int port) {
60 #ifdef DEBUG
61         cerr << __FILE__ <<  ": start_transmission(host = " << host
62                 << " | port = " << port << ")" << endl;
63 #endif // DEBUG
64         // TODO
65         return false;
66 }
67                         
68 Connection* Server::new_connection(
69                 const sockbuf::sockdesc& sd) {
70 #ifdef DEBUG
71         cerr << __FILE__ <<  ": new_connection(sd = " << sd.sock << ")"
72                 << endl;
73 #endif // DEBUG
74         ControlServer* connection = new ControlServer(sd);
75         // TODO verificar si el new se hace bien? no creo.
76         connection->signal_command_received().connect(
77                         SigC::bind<ControlServer*>(
78                                 SigC::slot_class(*this, &Server::on_control_command_received),
79                                 connection));
80         // TODO: 
81         return connection;
82 }
83
84 /// \todo Implementar.
85 bool Server::stop_transmission(string host, int port) {
86 #ifdef DEBUG
87         cerr << __FILE__ <<  ": stop_transmission(host = " << host
88                 << " | port = " << port << ")" << endl;
89 #endif // DEBUG
90         // TODO
91         return false;
92 }
93
94 /// \todo Implementar.
95 void Server::on_control_command_received(const Command& command,
96                 ControlServer* server) {
97 #ifdef DEBUG
98         cerr << __FILE__ <<  ": on_control_command_received(target = "
99                 << command.get_target() << ", command = " << command.get_command()
100                 << ", args = [" << String::join(command.get_args(), ", ") << "])"
101                 << endl;
102 #endif // DEBUG
103         HTTPResponse response(HTTPMessage::OK);
104         if (command.get_target() == "server") {
105 #ifdef DEBUG
106         cerr << __FILE__ <<  ": server" << endl;
107 #endif // DEBUG
108                 if (command.get_command() == "status") {
109                         // FIXME
110                         stringstream response_xml;
111                         response_xml << "<html>" << endl;
112                         response_xml << "    <head>" << endl;
113                         response_xml << "        <title>PlaQui v0.6</title>" << endl;
114                         response_xml << "    </head>" << endl;
115                         response_xml << "    <body>" << endl;
116                         response_xml << "        <h1>PlaQui</h1>" << endl;
117                         response_xml << "        <p>versión 0.6</p>" << endl;
118                         response_xml << "        <h2>Comando</h2>" << endl;
119                         response_xml << "        <ul>" << endl;
120                         response_xml << "           <li><b>Target:</b> " << command.get_target() << endl;
121                         response_xml << "           <li><b>Command:</b> " << command.get_command() << endl;
122                         response_xml << "           <li><b>Argumentos:</b>" << endl;
123                         response_xml << "               <ol>" << endl;
124                         for (Command::Arguments::const_iterator i = command.get_args().begin();
125                                         i != command.get_args().end(); i++) {
126                                 response_xml << "                   <li>" << *i << "</li>" << endl;
127                         }
128                         response_xml << "               </ol>" << endl;
129                         response_xml << "        </ul>" << endl;
130                         response_xml << "        <h2>Desarrollado por</h2>" << endl;
131                         response_xml << "        <ul>" << endl;
132                         response_xml << "            <li>Nicolás Dimov.</li>" << endl;
133                         response_xml << "            <li>Leandro Lucarella.</li>" << endl;
134                         response_xml << "            <li>Ricardo Markiewicz.</li>" << endl;
135                         response_xml << "        </ul>" << endl;
136                         response_xml << "        <address>" << endl;
137                         response_xml << "             Copyleft 2003 - bajo los " << endl;
138                         response_xml << "             términos de la licencia GPL" << endl;
139                         response_xml << "        </address>" << endl;
140                         response_xml << "    </body>" << endl;
141                         response_xml << "</html>" << endl;
142                         response.status_code = HTTPMessage::OK;
143                         response.set_body(response_xml.str());
144                 } else if (command.get_command() == "stop") {
145                         stop = true;
146                         response.set_body("El server se apagará en instantes...");
147                 } else {
148                         response.status_code = HTTPMessage::NOT_FOUND;
149                         response.set_body("Invalid command for 'server' taget!");
150                 }
151         } else if (command.get_target() == "connection") {
152                 if (command.get_command() == "list") {
153                         // FIXME
154                         TCPServer::ConnectionInfoList cil = get_connected();
155                         stringstream response_xml;
156                         response_xml << "<html>" << endl;
157                         response_xml << "    <head>" << endl;
158                         response_xml << "        <title>PlaQui v0.6</title>" << endl;
159                         response_xml << "    </head>" << endl;
160                         response_xml << "    <body>" << endl;
161                         response_xml << "        <h1>PlaQui</h1>" << endl;
162                         response_xml << "        <p>versión 0.6</p>" << endl;
163                         response_xml << "        <h2>Lista de conexiones:</h2>" << endl;
164                         response_xml << "        <ul>" << endl;
165                         for (TCPServer::ConnectionInfoList::const_iterator i = cil.begin();
166                                         i != cil.end(); i++) {
167                                 response_xml << "       <li>" << i->host
168                                         << ":" << i->port << " [<a href=\"/connection/disconnect/"
169                                         << i->host << "/" << i->port << "\">deconectar</a>]</li>"
170                                         << endl;
171                         }
172                         response_xml << "        </ul>" << endl;
173                         response_xml << "        <address>" << endl;
174                         response_xml << "             Copyleft 2003 - bajo los " << endl;
175                         response_xml << "             términos de la licencia GPL" << endl;
176                         response_xml << "        </address>" << endl;
177                         response_xml << "    </body>" << endl;
178                         response_xml << "</html>" << endl;
179                         response.status_code = HTTPMessage::OK;
180                         response.set_body(response_xml.str());
181                 } else if (command.get_command() == "stop") {
182                         // TODO server->finish();
183                         response.set_body("La conexión se cerrará en instantes...");
184                 } else {
185                         response.status_code = HTTPMessage::NOT_FOUND;
186                         response.set_body("Invalid command for 'connection' taget!");
187                 }
188         } else if (command.get_target() == "transmission") {
189                         response.status_code = HTTPMessage::NOT_FOUND;
190                         response.set_body("Invalid command for 'transmission' taget!");
191         } else if (command.get_target() == "plant") {
192                         response.status_code = HTTPMessage::NOT_FOUND;
193                         response.set_body("Invalid command for 'plant' taget!");
194         } else {
195                 response.status_code = HTTPMessage::NOT_FOUND;
196                 response.set_body("Invalid Target!");
197         }
198         // FIXME
199         response.headers["Content-Type"] = "text/html; charset=iso-8859-1";
200         response.headers["Connection"] = "close";
201         server->send(response);
202         // FIXME con timeout no debería ser necesario. Verificar cabecera Connection
203         // para saber si hay que finish()earlo o no.
204         // server->finish();
205 }
206
207 } // namespace Server
208
209 } // namespace PlaQui
210