1 // vim: set noexpandtab tabstop=4 shiftwidth=4:
2 //----------------------------------------------------------------------------
4 //----------------------------------------------------------------------------
5 // This file is part of PlaQui.
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
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
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 //----------------------------------------------------------------------------
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)
35 # include "plaqui/server/string.h"
45 Server::~Server(void) {
47 cerr << __FILE__ << ": destructor." << endl;
51 Server::Server(int port):
54 cerr << __FILE__ << ": port = " << port << endl;
58 /// \todo Implementar.
59 bool Server::start_transmission(string host, int port) {
61 cerr << __FILE__ << ": start_transmission(host = " << host
62 << " | port = " << port << ")" << endl;
68 Connection* Server::new_connection(
69 const sockbuf::sockdesc& sd) {
71 cerr << __FILE__ << ": new_connection(sd = " << sd.sock << ")"
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),
84 /// \todo Implementar.
85 bool Server::stop_transmission(string host, int port) {
87 cerr << __FILE__ << ": stop_transmission(host = " << host
88 << " | port = " << port << ")" << endl;
94 /// \todo Implementar.
95 void Server::on_control_command_received(const Command& command,
96 ControlServer* controlserver) {
98 cerr << __FILE__ << ": on_control_command_received(target = "
99 << command.get_target() << ", command = " << command.get_command()
100 << ", args = [" << String::join(command.get_args(), ", ") << "])"
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") {
110 response = new HTTPResponse(HTTPMessage::OK,
111 "El server se apagará en instantes...");
113 response = new HTTPResponse(HTTPMessage::NOT_FOUND,
114 "Invalid command for 'server' taget!");
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);
122 response = new HTTPResponse(HTTPMessage::NOT_FOUND,
123 "Invalid command for 'connection' taget!");
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!");
132 response = new HTTPResponse(HTTPMessage::NOT_FOUND, "Invalid taget!");
135 response->headers["Content-Type"] = "text/html; charset=iso-8859-1";
136 //response->headers["Connection"] = "close";
137 controlserver->send(*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();
146 HTTPResponse* Server::cmd_server_status(void) const {
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;
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());
183 HTTPResponse* Server::cmd_connection_list(void) {
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>"
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());
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...");
224 return new HTTPResponse(HTTPMessage::NOT_FOUND,
225 string("No existe una conexión a ") + args[0]
230 } // namespace Server
232 } // namespace PlaQui