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;
50 Glib::Mutex::Lock lock(plants_mutex);
51 for (PlantList::iterator i = plants.end(); i != plants.end(); i++) {
52 i->second->finish(true);
56 Server::Server(const string& plant_filename, const Connection::Port& port):
59 cerr << __FILE__ << ": port = " << port << endl;
62 Glib::Mutex::Lock lock(plants_mutex);
63 plants["default"] = new Plant(plant_filename);
64 plants["default"]->signal_finished().connect(
65 SigC::bind<const char*>(
66 SigC::slot_class(*this, &Server::on_plant_finished),
68 plants["default"]->run();
71 Connection* Server::new_connection(
72 const sockbuf::sockdesc& sd) {
74 cerr << __FILE__ << ": new_connection(sd = " << sd.sock << ")"
77 ControlServer* connection = new ControlServer(sd);
78 // TODO verificar si el new se hace bien? no creo.
79 connection->signal_command_received().connect(
80 SigC::bind<ControlServer*>(
81 SigC::slot_class(*this, &Server::on_control_command_received),
87 void Server::on_plant_updated(const Plant* plant) {
89 cerr << __FILE__ << ": on_plant_updated(plant = " << plant << ")." << endl;
93 void Server::on_plant_finished(const char* plant) {
95 cerr << __FILE__ << ": on_plant_finished(plant_name = " << plant << endl;
97 Glib::Mutex::Lock lock(plants_mutex);
101 /// \todo Terminar de implementar.
102 void Server::on_control_command_received(const Command& command,
103 ControlServer* controlserver) {
105 cerr << __FILE__ << ": on_control_command_received(target = "
106 << command.get_target() << ", command = " << command.get_command()
107 << ", args = [" << String::join(command.get_args(), ", ") << "])"
110 HTTPResponse* response;
111 //bool stop_controlserver = false;
112 if (command.get_target() == "server") {
113 if (command.get_command() == "status") {
114 response = cmd_server_status();
115 } else if (command.get_command() == "stop") {
117 response = new HTTPResponse(HTTPMessage::OK,
118 "El server se apagará en instantes...");
120 response = new HTTPResponse(HTTPMessage::NOT_FOUND,
121 "Invalid command for 'server' taget!");
123 } else if (command.get_target() == "connection") {
124 if (command.get_command() == "list") {
125 response = cmd_connection_list();
126 } else if (command.get_command() == "stop") {
127 response = cmd_connection_stop(command);
129 response = new HTTPResponse(HTTPMessage::NOT_FOUND,
130 "Invalid command for 'connection' taget!");
132 } else if (command.get_target() == "transmission") {
133 if (command.get_command() == "list") {
134 response = cmd_transmission_list();
135 } else if (command.get_command() == "start") {
136 response = cmd_transmission_start(command);
137 } else if (command.get_command() == "stop") {
138 response = cmd_transmission_stop(command);
140 response = new HTTPResponse(HTTPMessage::NOT_FOUND,
141 "Invalid command for 'transmission' taget!");
143 } else if (command.get_target() == "plant") {
144 if (command.get_command() == "list") {
145 response = cmd_plant_list();
146 } else if (command.get_command() == "get") {
147 response = cmd_plant_get(command);
148 } else if (command.get_command() == "stop") {
149 response = cmd_plant_stop(command);
151 response = new HTTPResponse(HTTPMessage::NOT_FOUND,
152 "Invalid command for 'plant' taget!");
155 response = new HTTPResponse(HTTPMessage::NOT_FOUND, "Invalid taget!");
158 response->headers["Content-Type"] = "text/html; charset=iso-8859-1";
159 //response->headers["Connection"] = "close";
160 controlserver->send(*response);
162 // FIXME con timeout no debería ser necesario. Verificar cabecera Connection
163 // para saber si hay que finish()earlo o no.
164 //if (stop_controlserver) {
165 // controlserver->finish();
169 HTTPResponse* Server::cmd_server_status(void) const {
171 stringstream response_xml;
172 response_xml << "<html>" << endl;
173 response_xml << " <head>" << endl;
174 response_xml << " <title>PlaQui v0.8</title>" << endl;
175 response_xml << " </head>" << endl;
176 response_xml << " <body>" << endl;
177 response_xml << " <h1>PlaQui</h1>" << endl;
178 response_xml << " <p>versión 0.8</p>" << endl;
179 /* response_xml << " <h2>Comando</h2>" << endl;
180 response_xml << " <ul>" << endl;
181 response_xml << " <li><b>Target:</b> " << command.get_target() << endl;
182 response_xml << " <li><b>Command:</b> " << command.get_command() << endl;
183 response_xml << " <li><b>Argumentos:</b>" << endl;
184 response_xml << " <ol>" << endl;
185 for (Command::Arguments::const_iterator i = command.get_args().begin();
186 i != command.get_args().end(); i++) {
187 response_xml << " <li>" << *i << "</li>" << endl;
189 response_xml << " </ol>" << endl;
190 response_xml << " </ul>" << endl;*/
191 response_xml << " <h2>Desarrollado por</h2>" << endl;
192 response_xml << " <ul>" << endl;
193 response_xml << " <li>Nicolás Dimov.</li>" << endl;
194 response_xml << " <li>Leandro Lucarella.</li>" << endl;
195 response_xml << " <li>Ricardo Markiewicz.</li>" << endl;
196 response_xml << " </ul>" << endl;
197 response_xml << " <address>" << endl;
198 response_xml << " Copyleft 2003 - bajo los " << endl;
199 response_xml << " términos de la licencia GPL" << endl;
200 response_xml << " </address>" << endl;
201 response_xml << " </body>" << endl;
202 response_xml << "</html>" << endl;
203 return new HTTPResponse(HTTPMessage::OK, response_xml.str());
206 HTTPResponse* Server::cmd_connection_list(void) {
208 TCPServer::ConnectionInfoList cil = get_connected();
209 stringstream response_xml;
210 response_xml << "<html>" << endl;
211 response_xml << " <head>" << endl;
212 response_xml << " <title>PlaQui v0.8</title>" << endl;
213 response_xml << " </head>" << endl;
214 response_xml << " <body>" << endl;
215 response_xml << " <h1>PlaQui</h1>" << endl;
216 response_xml << " <p>versión 0.8</p>" << endl;
217 response_xml << " <h2>Lista de conexiones:</h2>" << endl;
218 response_xml << " <ul>" << endl;
219 for (TCPServer::ConnectionInfoList::const_iterator i = cil.begin();
220 i != cil.end(); i++) {
221 response_xml << " <li>" << i->host
222 << ":" << i->port << " [<a href=\"/connection/stop/"
223 << i->host << "/" << i->port << "\">deconectar</a>]</li>"
226 response_xml << " </ul>" << endl;
227 response_xml << " <address>" << endl;
228 response_xml << " Copyleft 2003 - bajo los " << endl;
229 response_xml << " términos de la licencia GPL" << endl;
230 response_xml << " </address>" << endl;
231 response_xml << " </body>" << endl;
232 response_xml << "</html>" << endl;
233 return new HTTPResponse(HTTPMessage::OK, response_xml.str());
236 HTTPResponse* Server::cmd_connection_stop(const Command& command) {
237 const Command::Arguments& args = command.get_args();
238 Connection::Port port;
239 if (args.size() < 2) {
240 return new HTTPResponse(HTTPMessage::CONFLICT,
241 "Faltan argumentos.");
242 } else if (disconnect(args[0], String(args[1]).to(port))) {
243 return new HTTPResponse(HTTPMessage::OK,
244 string("La conexión a ") + args[0] + ":" + args[1]
245 + " se cerrará en instantes...");
247 return new HTTPResponse(HTTPMessage::NOT_FOUND,
248 string("No existe una conexión a ") + args[0]
253 HTTPResponse* Server::cmd_transmission_list(void) {
255 stringstream response_xml;
256 response_xml << "<html>" << endl;
257 response_xml << " <head>" << endl;
258 response_xml << " <title>PlaQui v0.8</title>" << endl;
259 response_xml << " </head>" << endl;
260 response_xml << " <body>" << endl;
261 response_xml << " <h1>PlaQui</h1>" << endl;
262 response_xml << " <p>versión 0.8</p>" << endl;
263 response_xml << " <h2>Lista de transmisiones:</h2>" << endl;
264 response_xml << " <ul>" << endl;
265 /*TODO plants_mutex.lock();
266 for (PlantList::const_iterator i = plants.begin();
267 i != plants.end(); i++) {
269 response_xml << " <li>" << (*i)->get_host() << ":"
270 << (*i)->get_port() << " [<a href=\"/transmission/stop/"
271 << (*i)->get_host() << "/" << (*i)->get_port()
272 << "\">desconectar</a>]</li>" << endl;
274 transmissions_mutex.unlock();*/
275 response_xml << " </ul>" << endl;
276 response_xml << " <address>" << endl;
277 response_xml << " Copyleft 2003 - bajo los " << endl;
278 response_xml << " términos de la licencia GPL" << endl;
279 response_xml << " </address>" << endl;
280 response_xml << " </body>" << endl;
281 response_xml << "</html>" << endl;
282 return new HTTPResponse(HTTPMessage::OK, response_xml.str());
285 HTTPResponse* Server::cmd_transmission_start(const Command& command) {
286 const Command::Arguments& args = command.get_args();
287 if (args.size() < 3) {
288 return new HTTPResponse(HTTPMessage::CONFLICT,
289 "Faltan argumentos.");
291 string plant = args[0];
292 string host = args[1];
293 Connection::Port port = String(args[2]).to(port);
294 Glib::Mutex::Lock lock(plants_mutex);
295 PlantList::iterator p = plants.find(plant);
296 if (p == plants.end()) {
297 return new HTTPResponse(HTTPMessage::NOT_FOUND,
298 string("No existe la planta '") + plant + "'.");
299 // TODO - agregar chequeo de que la transmision a ese host:port no
300 // exista para otra planta?
301 } else if (plants[plant]->transmission_start(host, port)) {
302 return new HTTPResponse(HTTPMessage::OK,
303 string("Se empieza a transmitir la planta '") + plant
304 + "' a " + host + ":" + String().from(port) + ".");
306 return new HTTPResponse(HTTPMessage::INTERNAL_SERVER_ERROR,
307 string("Error al crear la transmisión a de la planta '")
308 + plant + "' a " + host + ":" + args[2] + ".");
313 HTTPResponse* Server::cmd_transmission_stop(const Command& command) {
314 const Command::Arguments& args = command.get_args();
315 if (args.size() < 2) {
316 return new HTTPResponse(HTTPMessage::CONFLICT,
317 "Faltan argumentos.");
319 const string& host = args[0];
320 Connection::Port port = String(args[1]).to(port);
321 for (PlantList::iterator i = plants.begin(); i != plants.end(); i++) {
322 // TODO - agregar chequeo para saber si existe una conexion (para
323 // tirar error de que no hay conexion o de que no se pudo
325 if (i->second->transmission_stop(host, port)) {
326 return new HTTPResponse(HTTPMessage::OK,
327 string("Se finaliza la transmisión de la planta '")
328 + i->first + "' a " + host + ":" + args[1] + ".");
331 return new HTTPResponse(HTTPMessage::NOT_FOUND,
332 string("No se puede finalizar la transmisión a ")
333 + host + ":" + args[1] + ".");
337 HTTPResponse* Server::cmd_plant_list(void) {
339 stringstream response_xml;
340 response_xml << "<html>" << endl;
341 response_xml << " <head>" << endl;
342 response_xml << " <title>PlaQui v0.8</title>" << endl;
343 response_xml << " </head>" << endl;
344 response_xml << " <body>" << endl;
345 response_xml << " <h1>PlaQui</h1>" << endl;
346 response_xml << " <p>versión 0.8</p>" << endl;
347 response_xml << " <h2>Lista de plantas:</h2>" << endl;
348 response_xml << " <ul>" << endl;
350 for (PlantList::const_iterator i = plants.begin();
351 i != plants.end(); i++) {
352 response_xml << " <li>" << i->first
353 << " [<a href=\"/plant/stop/" << i->first << "\">parar</a>]</li>"
356 plants_mutex.unlock();
357 response_xml << " </ul>" << endl;
358 response_xml << " <address>" << endl;
359 response_xml << " Copyleft 2003 - bajo los " << endl;
360 response_xml << " términos de la licencia GPL" << endl;
361 response_xml << " </address>" << endl;
362 response_xml << " </body>" << endl;
363 response_xml << "</html>" << endl;
364 return new HTTPResponse(HTTPMessage::OK, response_xml.str());
367 HTTPResponse* Server::cmd_plant_get(const Command& command) {
368 if (!command.get_args().size()) {
369 return new HTTPResponse(HTTPMessage::CONFLICT,
370 "Faltan argumentos.");
372 Glib::Mutex::Lock lock(plants_mutex);
373 string name = command.get_args()[0];
374 if (plants.find(name) == plants.end()) {
375 return new HTTPResponse(HTTPMessage::NOT_FOUND,
376 string("No existe la planta ") + name);
378 string xml = plants[name]->get_xml();
380 return new HTTPResponse(HTTPMessage::OK, xml);
382 return new HTTPResponse(HTTPMessage::INTERNAL_SERVER_ERROR,
383 ("No se pudo obtener el XML de la planta ") + name);
387 HTTPResponse* Server::cmd_plant_stop(const Command& command) {
388 if (!command.get_args().size()) {
389 return new HTTPResponse(HTTPMessage::CONFLICT,
390 "Faltan argumentos.");
392 Glib::Mutex::Lock lock(plants_mutex);
393 const string name = command.get_args()[0];
394 if (plants.find(name) == plants.end()) {
395 return new HTTPResponse(HTTPMessage::NOT_FOUND,
396 string("No existe la planta ") + name);
398 // TODO Ver si al frenar la planta se destruye (no deberia!!!)
399 plants[name]->finish();
400 return new HTTPResponse(HTTPMessage::OK,
401 string("La planta '") + name + "' se cerrará en instantes...");
404 } // namespace Server
406 } // namespace PlaQui