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* server) {
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(HTTPMessage::OK);
104 if (command.get_target() == "server") {
106 cerr << __FILE__ << ": server" << endl;
108 if (command.get_command() == "status") {
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;
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") {
146 response.set_body("El server se apagará en instantes...");
148 response.status_code = HTTPMessage::NOT_FOUND;
149 response.set_body("Invalid command for 'server' taget!");
151 } else if (command.get_target() == "connection") {
152 if (command.get_command() == "list") {
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>"
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...");
185 response.status_code = HTTPMessage::NOT_FOUND;
186 response.set_body("Invalid command for 'connection' taget!");
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!");
195 response.status_code = HTTPMessage::NOT_FOUND;
196 response.set_body("Invalid Target!");
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.
207 } // namespace Server
209 } // namespace PlaQui