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/string.h"
30 #include "plaqui/server/connection.h"
31 #include "plaqui/server/controlserver.h"
32 #include <sigc++/class_slot.h>
33 #include <glibmm/timer.h>
46 Server::~Server(void) {
48 cerr << __FILE__ << "(" << __LINE__ << ")"
49 << ": destructor." << endl;
51 // Mando a terminar todas las plantas.
53 for (PlantList::iterator i = plants.begin(); i != plants.end(); i++) {
56 PlantList::size_type count = plants.size();
57 plants_mutex.unlock();
58 // Espero que terminen realmente.
60 Glib::usleep(10000); // 10 milisegundos
62 count = plants.size();
63 plants_mutex.unlock();
67 Server::Server(const string& plant_filename, const Connection::Port& port)
68 throw(sockerr): TCPServer(port) {
70 cerr << __FILE__ << "(" << __LINE__ << ")"
71 << ": port = " << port << endl;
73 // FIXME - hacer que se puedan cargar mas plantas bien.
74 Glib::Mutex::Lock lock(plants_mutex);
75 plants["default"] = new Plant(plant_filename);
76 plants["default"]->signal_finished().connect(SigC::bind(
77 SigC::slot_class(*this, &Server::on_plant_finished),
79 plants["default"]->run();
82 Connection* Server::new_connection(const sockbuf::sockdesc& sd) {
84 cerr << __FILE__ << "(" << __LINE__ << ")"
85 << ": new_connection(sd = " << sd.sock << ")"
88 ControlServer* connection = new ControlServer(sd);
89 // TODO verificar si el new se hace bien? no creo.
90 connection->signal_command_received().connect(SigC::bind(
91 SigC::slot_class(*this, &Server::on_control_command_received),
97 void Server::on_plant_finished(const char* plant) {
99 cerr << __FILE__ << "(" << __LINE__ << ")"
100 << ": on_plant_finished(plant_name = " << plant << endl;
102 Glib::Mutex::Lock lock(plants_mutex);
106 /// \todo Terminar de implementar.
107 void Server::on_control_command_received(const Command& command,
108 ControlServer* controlserver) {
110 cerr << __FILE__ << "(" << __LINE__ << ")"
111 << ": on_control_command_received(target = "
112 << command.get_target() << ", command = " << command.get_command()
113 << ", args = [" << String::join(command.get_args(), ", ") << "])"
117 if (command.get_target() == "server") {
118 if (command.get_command() == "info") {
119 response = cmd_server_info();
120 } else if (command.get_command() == "stop") {
121 response = new Response(Response::OK,
122 "El server se cerrará en instantes");
123 // XXX - Sin mandar la respuesta enseguida podría ser que el server
124 // cierre la conexión antes de mandar la respuesta.
125 //response->headers["Content-Type"] = "text/xml; charset=iso-8859-1";
126 //controlserver->send(*response);
131 response = new Response(Response::INVALID_COMMAND,
132 command.get_command() + " es un comando inválido para "
133 + "el destino 'server'");
135 } else if (command.get_target() == "connection") {
136 if (command.get_command() == "list") {
137 response = cmd_connection_list();
138 } else if (command.get_command() == "stop") {
139 response = cmd_connection_stop(command);
141 response = new Response(Response::INVALID_COMMAND,
142 command.get_command() + " es un comando inválido para "
143 + "el destino 'connection'");
145 } else if (command.get_target() == "transmission") {
146 if (command.get_command() == "list") {
147 response = cmd_transmission_list();
148 } else if (command.get_command() == "start") {
149 response = cmd_transmission_start(command);
150 } else if (command.get_command() == "stop") {
151 response = cmd_transmission_stop(command);
153 response = new Response(Response::INVALID_COMMAND,
154 command.get_command() + " es un comando inválido para "
155 + "el destino 'transmission'");
157 } else if (command.get_target() == "plant") {
158 if (command.get_command() == "list") {
159 response = cmd_plant_list();
160 } else if (command.get_command() == "get") {
161 response = cmd_plant_get(command);
162 } else if (command.get_command() == "set") {
163 response = cmd_plant_set(command);
164 } else if (command.get_command() == "set_frequency") {
165 response = cmd_plant_set_frequency(command);
166 } else if (command.get_command() == "start") {
167 response = cmd_plant_start(command);
168 } else if (command.get_command() == "stop") {
169 response = cmd_plant_stop(command);
170 } else if (command.get_command() == "remove") {
171 response = cmd_plant_remove(command);
173 response = new Response(Response::INVALID_COMMAND,
174 command.get_command() + " es un comando inválido para "
175 + "el destino 'plant'");
178 response = new Response(Response::INVALID_TARGET, command.get_target()
179 + " es un destino inválido");
182 //response->headers["Connection"] = "close";
183 controlserver->send(*response);
185 // FIXME con timeout no debería ser necesario. Verificar cabecera Connection
186 // para saber si hay que finish()earlo o no.
187 //if (stop_controlserver) {
188 // controlserver->finish();
192 Response* Server::cmd_server_info(void) const {
195 xml << "<serverstatus>" << endl;
196 xml << "\t<version>" VERSION "</version>" << endl;
197 xml << "\t<authors>" << endl;
198 xml << "\t\t<author>Nicolás Dimov</author>" << endl;
199 xml << "\t\t<author>Leandro Lucarella</author>" << endl;
200 xml << "\t\t<author>Ricardo Markiewicz</author>" << endl;
201 xml << "\t</authors>" << endl;
202 xml << "</serverstatus>" << endl;
203 return new Response(xml.str());
206 Response* Server::cmd_connection_list(void) {
208 TCPServer::ConnectionInfoList cil = get_connected();
210 xml << "<list type=\"connection\">" << endl;
211 for (TCPServer::ConnectionInfoList::const_iterator i = cil.begin();
212 i != cil.end(); i++) {
213 xml << "\t<row>" << endl;
214 xml << "\t\t<host>" << i->host << "</host>" << endl;
215 xml << "\t\t<port>" << i->port << "</port>" << endl;
216 xml << "\t</row>" << endl;
218 xml << "</list>" << endl;
219 return new Response(xml.str());
222 Response* Server::cmd_connection_stop(const Command& command) {
223 const Command::Arguments& args = command.get_args();
224 Connection::Port port;
225 if (args.size() < 2) {
226 return new Response(Response::ARGUMENT_MISSING, "Faltan argumentos "
227 " para el comando 'stop' del destino 'connection'");
228 } else if (disconnect(args[0], to(args[1], port))) {
229 return new Response(Response::OK, string("La conexión a ") + args[0]
230 + ":" + args[1] + " se cerrará en instantes");
232 return new Response(Response::CONNECTION_NOT_FOUND,
233 string("No existe una conexión a ") + args[0] + ":" + args[1]);
237 Response* Server::cmd_transmission_list(void) {
240 xml << "<list type=\"transmission\">" << endl;
241 /*TODO plants_mutex.lock();
242 for (PlantList::const_iterator i = plants.begin();
243 i != plants.end(); i++) {
245 xml << " <li>" << (*i)->get_host() << ":"
246 << (*i)->get_port() << " [<a href=\"/transmission/stop/"
247 << (*i)->get_host() << "/" << (*i)->get_port()
248 << "\">desconectar</a>]</li>" << endl;
250 transmissions_mutex.unlock();*/
251 xml << "</list>" << endl;
252 return new Response(xml.str());
255 Response* Server::cmd_transmission_start(const Command& command) {
256 const Command::Arguments& args = command.get_args();
257 if (args.size() < 3) {
258 return new Response(Response::ARGUMENT_MISSING, "Faltan argumentos "
259 " para el comando 'start' del destino 'transmission'");
261 string plant = args[0];
262 string host = args[1];
263 Connection::Port port = to(args[2], port);
264 Glib::Mutex::Lock lock(plants_mutex);
265 PlantList::iterator p = plants.find(plant);
266 if (p == plants.end()) {
267 return new Response(Response::PLANT_NOT_FOUND,
268 string("No existe la planta '") + plant + "'");
269 // FIXME - agregar chequeo de que la transmision a ese host:port no exista.
270 // que use respuesta ALLREADY_EXISTS
271 } else if (plants[plant]->transmission_start(host, port)) {
272 return new Response(Response::OK,
273 string("Se empieza a transmitir la planta '") + plant
274 + "' a " + host + ":" + String().from(port));
276 return new Response(Response::ERROR_STARTING_TRANSMISSION,
277 string("Error al crear la transmisión a de la planta '")
278 + plant + "' a " + host + ":" + args[2]);
283 Response* Server::cmd_transmission_stop(const Command& command) {
284 const Command::Arguments& args = command.get_args();
285 if (args.size() < 2) {
286 return new Response(Response::ARGUMENT_MISSING, "Faltan argumentos "
287 " para el comando 'stop' del destino 'transmission'");
289 const string& host = args[0];
290 Connection::Port port = to(args[1], port);
291 for (PlantList::iterator i = plants.begin(); i != plants.end(); i++) {
292 // TODO - agregar chequeo para saber si existe una conexion (para
293 // tirar error de que no hay conexion o de que no se pudo
295 if (i->second->transmission_stop(host, port)) {
296 return new Response(Response::OK,
297 string("La transmisión de la planta '") + i->first
298 + "' a " + host + ":" + args[1]
299 + " se cerrará en instantes");
302 return new Response(Response::TRANSMISSION_NOT_FOUND,
303 string("No existe una transmisión a ") + host + ":" + args[1]);
307 Response* Server::cmd_plant_list(void) {
308 // FIXME hacer con ResponseList
310 xml << "<list type=\"plant\">" << endl;
312 for (PlantList::const_iterator i = plants.begin();
313 i != plants.end(); i++) {
314 xml << "\t<row>" << endl;
315 xml << "\t\t<name>" << i->first << "</name>" << endl;
316 xml << "\t</row>" << endl;
318 plants_mutex.unlock();
319 xml << "</list>" << endl;
320 return new Response(xml.str());
323 Response* Server::cmd_plant_get(const Command& command) {
324 if (!command.get_args().size()) {
325 return new Response(Response::ARGUMENT_MISSING, "Faltan argumentos "
326 " para el comando 'get' del destino 'plant'");
328 Glib::Mutex::Lock lock(plants_mutex);
329 string plant = command.get_args()[0];
330 if (plants.find(plant) == plants.end()) {
331 return new Response(Response::PLANT_NOT_FOUND,
332 string("No existe la planta '") + plant + "'");
335 string xml = plants[plant]->get_xml();
337 return new Response(xml);
339 return new Response(Response::ERROR_GETING_PLANT_XML,
340 ("No se pudo obtener el XML de la planta '") + plant + "'");
344 Response* Server::cmd_plant_set(const Command& command) {
345 const Command::Arguments& args = command.get_args();
346 if (args.size() < 4) {
347 return new Response(Response::ARGUMENT_MISSING, "Faltan argumentos "
348 " para el comando 'set' del destino 'plant'");
350 string plant = args[0];
351 string element = args[1];
352 string input = args[2];
353 if (input != "open") {
354 return new Response(Response::ELEMENT_INPUT_NOT_FOUND,
355 string("El elemento '") + element + "' de la planta '" + plant
356 + "' no tiene una entrada '" + input + "'");
358 string value = args[3];
359 Glib::Mutex::Lock lock(plants_mutex);
360 PlantList::iterator p = plants.find(plant);
361 if (p == plants.end()) {
362 return new Response(Response::PLANT_NOT_FOUND,
363 string("No existe la planta '") + plant + "'");
366 if ((value == "false") || (value == "0") || (value == "off")
367 || (value == "no")) {
370 if (!plants[plant]->set_open(element, open)) {
371 return new Response(Response::ERROR_CHANGING_ELEMENT_INPUT,
372 string("No se pudo cambiar la entrada '") + input
373 + "' del elemento '" + element + "' de la planta '"
376 return new Response(Response::OK,
377 string("Se cambió la entrada '") + input + "' del elemento '"
378 + element + "' de la planta '" + plant + "' a '" + value + "'");
381 Response* Server::cmd_plant_set_frequency(const Command& command) {
382 if (command.get_args().size() < 2) {
383 return new Response(Response::ARGUMENT_MISSING, "Faltan argumentos "
384 " para el comando 'set_frequency' del destino 'plant'");
386 Glib::Mutex::Lock lock(plants_mutex);
387 const string name = command.get_args()[0];
388 if (plants.find(name) == plants.end()) {
389 return new Response(Response::PLANT_NOT_FOUND,
390 string("No existe la planta '") + name + "'");
393 to(command.get_args()[1], hz);
394 /* TODO poner cantidad real que tomó: hz = */plants[name]->set_frequency(hz);
397 return new Response(Response::OK,
398 string("Se cambió la frecuencia de refresco de la planta '") + name
399 + "' a '" + shz + "' veces por segundo");
402 Response* Server::cmd_plant_start(const Command& command) {
403 if (!command.get_args().size()) {
404 return new Response(Response::ARGUMENT_MISSING, "Faltan argumentos "
405 " para el comando 'start' del destino 'plant'");
407 Glib::Mutex::Lock lock(plants_mutex);
408 const string name = command.get_args()[0];
409 if (plants.find(name) == plants.end()) {
410 return new Response(Response::PLANT_NOT_FOUND,
411 string("No existe la planta '") + name + "'");
413 plants[name]->set_paused(false);
414 return new Response(Response::OK,
415 string("La planta '") + name + "' fue reanudada");
418 Response* Server::cmd_plant_stop(const Command& command) {
419 if (!command.get_args().size()) {
420 return new Response(Response::ARGUMENT_MISSING, "Faltan argumentos "
421 " para el comando 'stop' del destino 'plant'");
423 Glib::Mutex::Lock lock(plants_mutex);
424 const string name = command.get_args()[0];
425 if (plants.find(name) == plants.end()) {
426 return new Response(Response::PLANT_NOT_FOUND,
427 string("No existe la planta '") + name + "'");
429 plants[name]->set_paused(true);
430 return new Response(Response::OK,
431 string("La planta '") + name + "' fue pausada");
434 Response* Server::cmd_plant_remove(const Command& command) {
435 if (!command.get_args().size()) {
436 return new Response(Response::ARGUMENT_MISSING, "Faltan argumentos "
437 " para el comando 'remove' del destino 'plant'");
439 Glib::Mutex::Lock lock(plants_mutex);
440 const string name = command.get_args()[0];
441 if (plants.find(name) == plants.end()) {
442 return new Response(Response::PLANT_NOT_FOUND,
443 string("No existe la planta '") + name + "'");
445 plants[name]->finish();
446 return new Response(Response::OK,
447 string("La planta '") + name + "' fue será removida del servidor "
451 } // namespace Server
453 } // namespace PlaQui