]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Server/src/server.cpp
Se agrega sobrecarga de add_arg(unsigned) a pedido de ricky :)
[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* controlserver) {
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;
104         //bool stop_controlserver = false;
105         if (command.get_target() == "server") {
106                 if (command.get_command() == "status") {
107                         response = cmd_server_status();
108                 } else if (command.get_command() == "stop") {
109                         finish();
110                         response = new HTTPResponse(HTTPMessage::OK,
111                                         "El server se apagará en instantes...");
112                 } else {
113                         response = new HTTPResponse(HTTPMessage::NOT_FOUND,
114                                         "Invalid command for 'server' taget!");
115                 }
116         } else if (command.get_target() == "connection") {
117                 if (command.get_command() == "list") {
118                         response = cmd_connection_list();
119                 } else if (command.get_command() == "stop") {
120                         response = cmd_connection_stop(command);
121                 } else {
122                         response = new HTTPResponse(HTTPMessage::NOT_FOUND,
123                                         "Invalid command for 'connection' taget!");
124                 }
125         } else if (command.get_target() == "transmission") {
126                 response = new HTTPResponse(HTTPMessage::NOT_FOUND,
127                                 "Invalid command for 'transmission' taget!");
128         } else if (command.get_target() == "plant") {
129                 response = new HTTPResponse(HTTPMessage::NOT_FOUND,
130                                 "Invalid command for 'plant' taget!");
131         } else {
132                 response = new HTTPResponse(HTTPMessage::NOT_FOUND, "Invalid taget!");
133         }
134         // FIXME
135         response->headers["Content-Type"] = "text/html; charset=iso-8859-1";
136         //response->headers["Connection"] = "close";
137         controlserver->send(*response);
138         delete response;
139         // FIXME con timeout no debería ser necesario. Verificar cabecera Connection
140         // para saber si hay que finish()earlo o no.
141         //if (stop_controlserver) {
142         //      controlserver->finish();
143         //}
144 }
145
146 HTTPResponse* Server::cmd_server_status(void) const {
147         // FIXME
148         stringstream response_xml;
149         response_xml << "<html>" << endl;
150         response_xml << "    <head>" << endl;
151         response_xml << "        <title>PlaQui v0.7</title>" << endl;
152         response_xml << "    </head>" << endl;
153         response_xml << "    <body>" << endl;
154         response_xml << "        <h1>PlaQui</h1>" << endl;
155         response_xml << "        <p>versión 0.7</p>" << endl;
156 /*      response_xml << "        <h2>Comando</h2>" << endl;
157         response_xml << "        <ul>" << endl;
158         response_xml << "           <li><b>Target:</b> " << command.get_target() << endl;
159         response_xml << "           <li><b>Command:</b> " << command.get_command() << endl;
160         response_xml << "           <li><b>Argumentos:</b>" << endl;
161         response_xml << "               <ol>" << endl;
162         for (Command::Arguments::const_iterator i = command.get_args().begin();
163                         i != command.get_args().end(); i++) {
164                 response_xml << "                   <li>" << *i << "</li>" << endl;
165         }
166         response_xml << "               </ol>" << endl;
167         response_xml << "        </ul>" << endl;
168 */      response_xml << "        <h2>Desarrollado por</h2>" << endl;
169         response_xml << "        <ul>" << endl;
170         response_xml << "            <li>Nicolás Dimov.</li>" << endl;
171         response_xml << "            <li>Leandro Lucarella.</li>" << endl;
172         response_xml << "            <li>Ricardo Markiewicz.</li>" << endl;
173         response_xml << "        </ul>" << endl;
174         response_xml << "        <address>" << endl;
175         response_xml << "             Copyleft 2003 - bajo los " << endl;
176         response_xml << "             términos de la licencia GPL" << endl;
177         response_xml << "        </address>" << endl;
178         response_xml << "    </body>" << endl;
179         response_xml << "</html>" << endl;
180         return new HTTPResponse(HTTPMessage::OK, response_xml.str());
181 }
182
183 HTTPResponse* Server::cmd_connection_list(void) {
184         // FIXME
185         TCPServer::ConnectionInfoList cil = get_connected();
186         stringstream response_xml;
187         response_xml << "<html>" << endl;
188         response_xml << "    <head>" << endl;
189         response_xml << "        <title>PlaQui v0.7</title>" << endl;
190         response_xml << "    </head>" << endl;
191         response_xml << "    <body>" << endl;
192         response_xml << "        <h1>PlaQui</h1>" << endl;
193         response_xml << "        <p>versión 0.7</p>" << endl;
194         response_xml << "        <h2>Lista de conexiones:</h2>" << endl;
195         response_xml << "        <ul>" << endl;
196         for (TCPServer::ConnectionInfoList::const_iterator i = cil.begin();
197                         i != cil.end(); i++) {
198                 response_xml << "       <li>" << i->host
199                         << ":" << i->port << " [<a href=\"/connection/stop/"
200                         << i->host << "/" << i->port << "\">deconectar</a>]</li>"
201                         << endl;
202         }
203         response_xml << "        </ul>" << endl;
204         response_xml << "        <address>" << endl;
205         response_xml << "             Copyleft 2003 - bajo los " << endl;
206         response_xml << "             términos de la licencia GPL" << endl;
207         response_xml << "        </address>" << endl;
208         response_xml << "    </body>" << endl;
209         response_xml << "</html>" << endl;
210         return new HTTPResponse(HTTPMessage::OK, response_xml.str());
211 }
212
213 HTTPResponse* Server::cmd_connection_stop(const Command& command) {
214         const Command::Arguments& args = command.get_args();
215         Connection::Port port;
216         if (args.size() < 2) {
217                 return new HTTPResponse(HTTPMessage::CONFLICT,
218                                 "Faltan argumentos.");
219         } else if (disconnect(args[0], String(args[1]).to(port))) {
220                 return new HTTPResponse(HTTPMessage::OK,
221                                 string("La conexión a ") + args[0] + ":" + args[1]
222                                 + " se cerrará en instantes...");
223         } else {
224                 return new HTTPResponse(HTTPMessage::NOT_FOUND,
225                                 string("No existe una conexión a ") + args[0]
226                                 + ":" + args[1]);
227         }
228 }
229
230 } // namespace Server
231
232 } // namespace PlaQui
233