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() == "stop") {
163 response = cmd_plant_stop(command);
165 response = new HTTPResponse(HTTPMessage::NOT_FOUND,
166 "<response desc=\"Invalid command for 'plant' taget!\" />");
169 response = new HTTPResponse(HTTPMessage::NOT_FOUND,
170 "<response desc=\"Invalid taget!\" />");
173 response->headers["Content-Type"] = "text/xml; charset=iso-8859-1";
174 //response->headers["Connection"] = "close";
175 controlserver->send(*response);
177 // FIXME con timeout no debería ser necesario. Verificar cabecera Connection
178 // para saber si hay que finish()earlo o no.
179 //if (stop_controlserver) {
180 // controlserver->finish();
184 HTTPResponse* Server::cmd_server_status(void) const {
187 xml << "<serverstatus>" << endl;
188 xml << "\t<version>" VERSION "</version>" << endl;
189 xml << "\t<authors>" << endl;
190 xml << "\t\t<author>Nicolás Dimov</author>" << endl;
191 xml << "\t\t<author>Leandro Lucarella</author>" << endl;
192 xml << "\t\t<author>Ricardo Markiewicz</author>" << endl;
193 xml << "\t</authors>" << endl;
194 xml << "</serverstatus>" << endl;
195 return new HTTPResponse(HTTPMessage::OK, xml.str());
198 HTTPResponse* Server::cmd_connection_list(void) {
200 TCPServer::ConnectionInfoList cil = get_connected();
202 xml << "<list type=\"connection\">" << endl;
203 for (TCPServer::ConnectionInfoList::const_iterator i = cil.begin();
204 i != cil.end(); i++) {
205 xml << "\t<row>" << endl;
206 xml << "\t\t<cell>" << i->host << "</cell>" << endl;
207 xml << "\t\t<cell>" << i->port << "</cell>" << endl;
208 xml << "\t</row>" << endl;
210 xml << "</list>" << endl;
211 return new HTTPResponse(HTTPMessage::OK, xml.str());
214 HTTPResponse* Server::cmd_connection_stop(const Command& command) {
215 const Command::Arguments& args = command.get_args();
216 Connection::Port port;
217 if (args.size() < 2) {
218 return new HTTPResponse(HTTPMessage::CONFLICT,
219 "<response desc=\"Faltan argumentos.\" />");
220 } else if (disconnect(args[0], to(args[1], port))) {
221 return new HTTPResponse(HTTPMessage::OK,
222 string("<response desc=\"La conexión a ") + args[0] + ":" + args[1]
223 + " se cerrará en instantes...\" />");
225 return new HTTPResponse(HTTPMessage::NOT_FOUND,
226 string("<response desc=\"No existe una conexión a ") + args[0]
227 + ":" + args[1] + "\" />");
231 HTTPResponse* Server::cmd_transmission_list(void) {
234 xml << "<list type=\"transmission\">" << endl;
235 /*TODO plants_mutex.lock();
236 for (PlantList::const_iterator i = plants.begin();
237 i != plants.end(); i++) {
239 xml << " <li>" << (*i)->get_host() << ":"
240 << (*i)->get_port() << " [<a href=\"/transmission/stop/"
241 << (*i)->get_host() << "/" << (*i)->get_port()
242 << "\">desconectar</a>]</li>" << endl;
244 transmissions_mutex.unlock();*/
245 xml << "</list>" << endl;
246 return new HTTPResponse(HTTPMessage::OK, xml.str());
249 HTTPResponse* Server::cmd_transmission_start(const Command& command) {
250 const Command::Arguments& args = command.get_args();
251 if (args.size() < 3) {
252 return new HTTPResponse(HTTPMessage::CONFLICT,
253 "<response desc=\"Faltan argumentos.\" />");
255 string plant = args[0];
256 string host = args[1];
257 Connection::Port port = to(args[2], port);
258 Glib::Mutex::Lock lock(plants_mutex);
259 PlantList::iterator p = plants.find(plant);
260 if (p == plants.end()) {
261 return new HTTPResponse(HTTPMessage::NOT_FOUND,
262 string("<response desc=\"No existe la planta '") + plant + "'.\" />");
263 // TODO - agregar chequeo de que la transmision a ese host:port no
264 // exista para otra planta?
265 } else if (plants[plant]->transmission_start(host, port)) {
266 return new HTTPResponse(HTTPMessage::OK,
267 string("<response desc=\"Se empieza a transmitir la planta '") + plant
268 + "' a " + host + ":" + String().from(port) + ".\" />");
270 return new HTTPResponse(HTTPMessage::INTERNAL_SERVER_ERROR,
271 string("<response desc=\"Error al crear la transmisión a de la planta '")
272 + plant + "' a " + host + ":" + args[2] + ".\" />");
277 HTTPResponse* Server::cmd_transmission_stop(const Command& command) {
278 const Command::Arguments& args = command.get_args();
279 if (args.size() < 2) {
280 return new HTTPResponse(HTTPMessage::CONFLICT,
281 "<response desc=\"Faltan argumentos.\" />");
283 const string& host = args[0];
284 Connection::Port port = to(args[1], port);
285 for (PlantList::iterator i = plants.begin(); i != plants.end(); i++) {
286 // TODO - agregar chequeo para saber si existe una conexion (para
287 // tirar error de que no hay conexion o de que no se pudo
289 if (i->second->transmission_stop(host, port)) {
290 return new HTTPResponse(HTTPMessage::OK,
291 string("<response desc=\"Se finaliza la transmisión de la planta '")
292 + i->first + "' a " + host + ":" + args[1] + ".\" />");
295 return new HTTPResponse(HTTPMessage::NOT_FOUND,
296 string("<response desc=\"No se puede finalizar la transmisión a ")
297 + host + ":" + args[1] + ".\" />");
301 HTTPResponse* Server::cmd_plant_list(void) {
304 xml << "<list type=\"plant\">" << endl;
306 for (PlantList::const_iterator i = plants.begin();
307 i != plants.end(); i++) {
308 xml << "\t<row>" << endl;
309 xml << "\t\t<cell>" << i->first << "</cell>" << endl;
310 xml << "\t</row>" << endl;
312 plants_mutex.unlock();
313 xml << "</list>" << endl;
314 return new HTTPResponse(HTTPMessage::OK, xml.str());
317 HTTPResponse* Server::cmd_plant_get(const Command& command) {
318 if (!command.get_args().size()) {
319 return new HTTPResponse(HTTPMessage::CONFLICT,
320 "<response desc=\"Faltan argumentos.\" />");
322 Glib::Mutex::Lock lock(plants_mutex);
323 string plant = command.get_args()[0];
324 if (plants.find(plant) == plants.end()) {
325 return new HTTPResponse(HTTPMessage::NOT_FOUND,
326 string("<response desc=\"No existe la planta ") + plant + "\" />");
329 string xml = plants[plant]->get_xml();
331 return new HTTPResponse(HTTPMessage::OK, xml);
333 return new HTTPResponse(HTTPMessage::INTERNAL_SERVER_ERROR,
334 ("<response desc=\"No se pudo obtener el XML de la planta ") + plant + "\" />");
338 HTTPResponse* Server::cmd_plant_set(const Command& command) {
339 const Command::Arguments& args = command.get_args();
340 if (args.size() < 4) {
341 return new HTTPResponse(HTTPMessage::CONFLICT,
342 "<response desc=\"Faltan argumentos.\" />");
344 string plant = args[0];
345 string element = args[1];
346 string key = args[2];
348 return new HTTPResponse(HTTPMessage::NOT_FOUND,
349 string("<response desc=\"La clave '") + key + "' es inválida.\" />");
351 string value = args[3];
352 Glib::Mutex::Lock lock(plants_mutex);
353 PlantList::iterator p = plants.find(plant);
354 if (p == plants.end()) {
355 return new HTTPResponse(HTTPMessage::NOT_FOUND,
356 string("<response desc=\"No existe la planta '") + plant + "'.\" />");
359 if ((value == "false") || (value == "0") || (value == "off")
360 || (value == "no")) {
363 if (!plants[plant]->set_open(element, open)) {
364 return new HTTPResponse(HTTPMessage::CONFLICT,
365 string("<response desc=\"No se pudo cambiar el estado del elemento '") + element + "'.\" />");
367 return new HTTPResponse(HTTPMessage::OK,
368 string("<response desc=\"Se cambió el estado del elemento '") + element + "'.\" />");
371 HTTPResponse* Server::cmd_plant_stop(const Command& command) {
372 if (!command.get_args().size()) {
373 return new HTTPResponse(HTTPMessage::CONFLICT,
374 "<response desc=\"Faltan argumentos.\" />");
376 Glib::Mutex::Lock lock(plants_mutex);
377 const string name = command.get_args()[0];
378 if (plants.find(name) == plants.end()) {
379 return new HTTPResponse(HTTPMessage::NOT_FOUND,
380 string("<response desc=\"No existe la planta ") + name + "\" />");
382 // TODO Ver si al frenar la planta se destruye (no deberia!!!)
383 plants[name]->finish();
384 return new HTTPResponse(HTTPMessage::OK,
385 string("<response desc=\"La planta '") + name + "' se cerrará en instantes...\" />");
388 } // namespace Server
390 } // namespace PlaQui