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 #include <glibmm/timer.h>
36 # include "plaqui/server/string.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(), ", ") << "])"
116 HTTPResponse* response;
117 //bool stop_controlserver = false;
118 if (command.get_target() == "server") {
119 if (command.get_command() == "status") {
120 response = cmd_server_status();
121 } else if (command.get_command() == "stop") {
122 response = new HTTPResponse(HTTPMessage::OK,
123 "<response desc=\"El server se apagará en instantes...\" />");
124 // XXX - Sin mandar la respuesta enseguida podría ser que el server
125 // cierre la conexión antes de mandar la respuesta.
126 //response->headers["Content-Type"] = "text/xml; charset=iso-8859-1";
127 //controlserver->send(*response);
132 response = new HTTPResponse(HTTPMessage::NOT_FOUND,
133 "<response desc=\"Invalid command for 'server' taget!\" />");
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 HTTPResponse(HTTPMessage::NOT_FOUND,
142 "<response desc=\"Invalid command for 'connection' taget!\" />");
144 } else if (command.get_target() == "transmission") {
145 if (command.get_command() == "list") {
146 response = cmd_transmission_list();
147 } else if (command.get_command() == "start") {
148 response = cmd_transmission_start(command);
149 } else if (command.get_command() == "stop") {
150 response = cmd_transmission_stop(command);
152 response = new HTTPResponse(HTTPMessage::NOT_FOUND,
153 "<response desc=\"Invalid command for 'transmission' taget!\" />");
155 } else if (command.get_target() == "plant") {
156 if (command.get_command() == "list") {
157 response = cmd_plant_list();
158 } else if (command.get_command() == "get") {
159 response = cmd_plant_get(command);
160 } else if (command.get_command() == "set") {
161 response = cmd_plant_set(command);
162 } else if (command.get_command() == "set_frequency") {
163 response = cmd_plant_set_frequency(command);
164 } else if (command.get_command() == "start") {
165 response = cmd_plant_start(command);
166 } else if (command.get_command() == "stop") {
167 response = cmd_plant_stop(command);
169 response = new HTTPResponse(HTTPMessage::NOT_FOUND,
170 "<response desc=\"Invalid command for 'plant' taget!\" />");
173 response = new HTTPResponse(HTTPMessage::NOT_FOUND,
174 "<response desc=\"Invalid taget!\" />");
177 response->headers["Content-Type"] = "text/xml; charset=iso-8859-1";
178 //response->headers["Connection"] = "close";
179 controlserver->send(*response);
181 // FIXME con timeout no debería ser necesario. Verificar cabecera Connection
182 // para saber si hay que finish()earlo o no.
183 //if (stop_controlserver) {
184 // controlserver->finish();
188 HTTPResponse* Server::cmd_server_status(void) const {
191 xml << "<serverstatus>" << endl;
192 xml << "\t<version>" VERSION "</version>" << endl;
193 xml << "\t<authors>" << endl;
194 xml << "\t\t<author>Nicolás Dimov</author>" << endl;
195 xml << "\t\t<author>Leandro Lucarella</author>" << endl;
196 xml << "\t\t<author>Ricardo Markiewicz</author>" << endl;
197 xml << "\t</authors>" << endl;
198 xml << "</serverstatus>" << endl;
199 return new HTTPResponse(HTTPMessage::OK, xml.str());
202 HTTPResponse* Server::cmd_connection_list(void) {
204 TCPServer::ConnectionInfoList cil = get_connected();
206 xml << "<list type=\"connection\">" << endl;
207 for (TCPServer::ConnectionInfoList::const_iterator i = cil.begin();
208 i != cil.end(); i++) {
209 xml << "\t<row>" << endl;
210 xml << "\t\t<cell>" << i->host << "</cell>" << endl;
211 xml << "\t\t<cell>" << i->port << "</cell>" << endl;
212 xml << "\t</row>" << endl;
214 xml << "</list>" << endl;
215 return new HTTPResponse(HTTPMessage::OK, xml.str());
218 HTTPResponse* Server::cmd_connection_stop(const Command& command) {
219 const Command::Arguments& args = command.get_args();
220 Connection::Port port;
221 if (args.size() < 2) {
222 return new HTTPResponse(HTTPMessage::CONFLICT,
223 "<response desc=\"Faltan argumentos.\" />");
224 } else if (disconnect(args[0], to(args[1], port))) {
225 return new HTTPResponse(HTTPMessage::OK,
226 string("<response desc=\"La conexión a ") + args[0] + ":" + args[1]
227 + " se cerrará en instantes...\" />");
229 return new HTTPResponse(HTTPMessage::NOT_FOUND,
230 string("<response desc=\"No existe una conexión a ") + args[0]
231 + ":" + args[1] + "\" />");
235 HTTPResponse* Server::cmd_transmission_list(void) {
238 xml << "<list type=\"transmission\">" << endl;
239 /*TODO plants_mutex.lock();
240 for (PlantList::const_iterator i = plants.begin();
241 i != plants.end(); i++) {
243 xml << " <li>" << (*i)->get_host() << ":"
244 << (*i)->get_port() << " [<a href=\"/transmission/stop/"
245 << (*i)->get_host() << "/" << (*i)->get_port()
246 << "\">desconectar</a>]</li>" << endl;
248 transmissions_mutex.unlock();*/
249 xml << "</list>" << endl;
250 return new HTTPResponse(HTTPMessage::OK, xml.str());
253 HTTPResponse* Server::cmd_transmission_start(const Command& command) {
254 const Command::Arguments& args = command.get_args();
255 if (args.size() < 3) {
256 return new HTTPResponse(HTTPMessage::CONFLICT,
257 "<response desc=\"Faltan argumentos.\" />");
259 string plant = args[0];
260 string host = args[1];
261 Connection::Port port = to(args[2], port);
262 Glib::Mutex::Lock lock(plants_mutex);
263 PlantList::iterator p = plants.find(plant);
264 if (p == plants.end()) {
265 return new HTTPResponse(HTTPMessage::NOT_FOUND,
266 string("<response desc=\"No existe la planta '") + plant + "'.\" />");
267 // TODO - agregar chequeo de que la transmision a ese host:port no
268 // exista para otra planta?
269 } else if (plants[plant]->transmission_start(host, port)) {
270 return new HTTPResponse(HTTPMessage::OK,
271 string("<response desc=\"Se empieza a transmitir la planta '") + plant
272 + "' a " + host + ":" + String().from(port) + ".\" />");
274 return new HTTPResponse(HTTPMessage::INTERNAL_SERVER_ERROR,
275 string("<response desc=\"Error al crear la transmisión a de la planta '")
276 + plant + "' a " + host + ":" + args[2] + ".\" />");
281 HTTPResponse* Server::cmd_transmission_stop(const Command& command) {
282 const Command::Arguments& args = command.get_args();
283 if (args.size() < 2) {
284 return new HTTPResponse(HTTPMessage::CONFLICT,
285 "<response desc=\"Faltan argumentos.\" />");
287 const string& host = args[0];
288 Connection::Port port = to(args[1], port);
289 for (PlantList::iterator i = plants.begin(); i != plants.end(); i++) {
290 // TODO - agregar chequeo para saber si existe una conexion (para
291 // tirar error de que no hay conexion o de que no se pudo
293 if (i->second->transmission_stop(host, port)) {
294 return new HTTPResponse(HTTPMessage::OK,
295 string("<response desc=\"Se finaliza la transmisión de la planta '")
296 + i->first + "' a " + host + ":" + args[1] + ".\" />");
299 return new HTTPResponse(HTTPMessage::NOT_FOUND,
300 string("<response desc=\"No se puede finalizar la transmisión a ")
301 + host + ":" + args[1] + ".\" />");
305 HTTPResponse* Server::cmd_plant_list(void) {
308 xml << "<list type=\"plant\">" << endl;
310 for (PlantList::const_iterator i = plants.begin();
311 i != plants.end(); i++) {
312 xml << "\t<row>" << endl;
313 xml << "\t\t<cell>" << i->first << "</cell>" << endl;
314 xml << "\t</row>" << endl;
316 plants_mutex.unlock();
317 xml << "</list>" << endl;
318 return new HTTPResponse(HTTPMessage::OK, xml.str());
321 HTTPResponse* Server::cmd_plant_get(const Command& command) {
322 if (!command.get_args().size()) {
323 return new HTTPResponse(HTTPMessage::CONFLICT,
324 "<response desc=\"Faltan argumentos.\" />");
326 Glib::Mutex::Lock lock(plants_mutex);
327 string plant = command.get_args()[0];
328 if (plants.find(plant) == plants.end()) {
329 return new HTTPResponse(HTTPMessage::NOT_FOUND,
330 string("<response desc=\"No existe la planta ") + plant + "\" />");
333 string xml = plants[plant]->get_xml();
335 return new HTTPResponse(HTTPMessage::OK, xml);
337 return new HTTPResponse(HTTPMessage::INTERNAL_SERVER_ERROR,
338 ("<response desc=\"No se pudo obtener el XML de la planta ") + plant + "\" />");
342 HTTPResponse* Server::cmd_plant_set(const Command& command) {
343 const Command::Arguments& args = command.get_args();
344 if (args.size() < 4) {
345 return new HTTPResponse(HTTPMessage::CONFLICT,
346 "<response desc=\"Faltan argumentos.\" />");
348 string plant = args[0];
349 string element = args[1];
350 string key = args[2];
352 return new HTTPResponse(HTTPMessage::NOT_FOUND,
353 string("<response desc=\"La clave '") + key + "' es inválida.\" />");
355 string value = args[3];
356 Glib::Mutex::Lock lock(plants_mutex);
357 PlantList::iterator p = plants.find(plant);
358 if (p == plants.end()) {
359 return new HTTPResponse(HTTPMessage::NOT_FOUND,
360 string("<response desc=\"No existe la planta '") + plant + "'.\" />");
363 if ((value == "false") || (value == "0") || (value == "off")
364 || (value == "no")) {
367 if (!plants[plant]->set_open(element, open)) {
368 return new HTTPResponse(HTTPMessage::CONFLICT,
369 string("<response desc=\"No se pudo cambiar el estado del elemento '") + element + "'.\" />");
371 return new HTTPResponse(HTTPMessage::OK,
372 string("<response desc=\"Se cambió el estado del elemento '") + element + "'.\" />");
375 HTTPResponse* Server::cmd_plant_set_frequency(const Command& command) {
376 if (command.get_args().size() < 2) {
377 return new HTTPResponse(HTTPMessage::CONFLICT,
378 "<response desc=\"Faltan argumentos.\" />");
380 Glib::Mutex::Lock lock(plants_mutex);
381 const string name = command.get_args()[0];
382 if (plants.find(name) == plants.end()) {
383 return new HTTPResponse(HTTPMessage::NOT_FOUND,
384 string("<response desc=\"No existe la planta ") + name + "\" />");
387 to(command.get_args()[1], hz);
388 plants[name]->set_frequency(hz);
389 return new HTTPResponse(HTTPMessage::OK,
390 string("<response desc=\"La planta '") + name + "' fue pausada.\" />");
393 HTTPResponse* Server::cmd_plant_start(const Command& command) {
394 if (!command.get_args().size()) {
395 return new HTTPResponse(HTTPMessage::CONFLICT,
396 "<response desc=\"Faltan argumentos.\" />");
398 Glib::Mutex::Lock lock(plants_mutex);
399 const string name = command.get_args()[0];
400 if (plants.find(name) == plants.end()) {
401 return new HTTPResponse(HTTPMessage::NOT_FOUND,
402 string("<response desc=\"No existe la planta ") + name + "\" />");
404 plants[name]->set_paused(false);
405 return new HTTPResponse(HTTPMessage::OK,
406 string("<response desc=\"La planta '") + name + "' fue reanudada.\" />");
409 HTTPResponse* Server::cmd_plant_stop(const Command& command) {
410 if (!command.get_args().size()) {
411 return new HTTPResponse(HTTPMessage::CONFLICT,
412 "<response desc=\"Faltan argumentos.\" />");
414 Glib::Mutex::Lock lock(plants_mutex);
415 const string name = command.get_args()[0];
416 if (plants.find(name) == plants.end()) {
417 return new HTTPResponse(HTTPMessage::NOT_FOUND,
418 string("<response desc=\"No existe la planta ") + name + "\" />");
420 plants[name]->set_paused(true);
421 return new HTTPResponse(HTTPMessage::OK,
422 string("<response desc=\"La planta '") + name + "' fue pausada.\" />");
425 HTTPResponse* Server::cmd_plant_remove(const Command& command) {
426 if (!command.get_args().size()) {
427 return new HTTPResponse(HTTPMessage::CONFLICT,
428 "<response desc=\"Faltan argumentos.\" />");
430 Glib::Mutex::Lock lock(plants_mutex);
431 const string name = command.get_args()[0];
432 if (plants.find(name) == plants.end()) {
433 return new HTTPResponse(HTTPMessage::NOT_FOUND,
434 string("<response desc=\"No existe la planta ") + name + "\" />");
436 plants[name]->finish();
437 return new HTTPResponse(HTTPMessage::OK,
438 string("<response desc=\"La planta '") + name + "' se cerrará en instantes...\" />");
441 } // namespace Server
443 } // namespace PlaQui