]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Server/src/server.cpp
- Se cambia la planta por defecto a planta.xml.
[z.facultad/75.42/plaqui.git] / Server / src / server.cpp
1 // vim: set noexpandtab tabstop=4 shiftwidth=4:
2 //----------------------------------------------------------------------------
3 //                                  PlaQui
4 //----------------------------------------------------------------------------
5 // This file is part of PlaQui.
6 //
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
10 // version.
11 //
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
15 // details.
16 //
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 //----------------------------------------------------------------------------
24 //
25 // $Id$
26 //
27
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>
33 #include <sstream>
34 #include <exception>
35 #ifdef DEBUG
36 #       include "plaqui/server/string.h"
37 #       include <iostream>
38 #endif // DEBUG
39
40 using namespace std;
41
42 namespace PlaQui {
43
44 namespace Server {
45
46 Server::~Server(void) {
47 #ifdef DEBUG
48         cerr << __FILE__ << "(" << __LINE__ << ")"
49                 <<  ": destructor." << endl;
50 #endif // DEBUG
51         // Mando a terminar todas las plantas.
52         plants_mutex.lock();
53         for (PlantList::iterator i = plants.begin(); i != plants.end(); i++) {
54                 i->second->finish();
55         }
56         PlantList::size_type count = plants.size();
57         plants_mutex.unlock();
58         // Espero que terminen realmente.
59         while (count) {
60                 Glib::usleep(10000); // 10 milisegundos
61                 plants_mutex.lock();
62                 count = plants.size();
63                 plants_mutex.unlock();
64         }
65 }
66
67 Server::Server(const string& plant_filename, const Connection::Port& port)
68                 throw(sockerr): TCPServer(port) {
69 #ifdef DEBUG
70         cerr << __FILE__ << "(" << __LINE__ << ")"
71                 <<  ": port = " << port << endl;
72 #endif // DEBUG
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),
78                         "default"));
79         plants["default"]->run();
80 }
81
82 Connection* Server::new_connection(const sockbuf::sockdesc& sd) {
83 #ifdef DEBUG
84         cerr << __FILE__ << "(" << __LINE__ << ")"
85                 <<  ": new_connection(sd = " << sd.sock << ")"
86                 << endl;
87 #endif // DEBUG
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),
92                         connection));
93         // TODO: 
94         return connection;
95 }
96
97 void Server::on_plant_finished(const char* plant) {
98 #ifdef DEBUG
99         cerr << __FILE__ << "(" << __LINE__ << ")"
100                 <<  ": on_plant_finished(plant_name = " << plant << endl;
101 #endif // DEBUG
102         Glib::Mutex::Lock lock(plants_mutex);
103         plants.erase(plant);
104 }
105
106 /// \todo Terminar de implementar.
107 void Server::on_control_command_received(const Command& command,
108                 ControlServer* controlserver) {
109 #ifdef DEBUG
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(), ", ") << "])"
114                 << endl;
115 #endif // DEBUG
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);
128                         //delete response;
129                         finish();
130                         //return;
131                 } else {
132                         response = new HTTPResponse(HTTPMessage::NOT_FOUND,
133                                         "<response desc=\"Invalid command for 'server' taget!\" />");
134                 }
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);
140                 } else {
141                         response = new HTTPResponse(HTTPMessage::NOT_FOUND,
142                                         "<response desc=\"Invalid command for 'connection' taget!\" />");
143                 }
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);
151                 } else {
152                         response = new HTTPResponse(HTTPMessage::NOT_FOUND,
153                                         "<response desc=\"Invalid command for 'transmission' taget!\" />");
154                 }
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);
168                 } else {
169                         response = new HTTPResponse(HTTPMessage::NOT_FOUND,
170                                         "<response desc=\"Invalid command for 'plant' taget!\" />");
171                 }
172         } else {
173                 response = new HTTPResponse(HTTPMessage::NOT_FOUND,
174                                 "<response desc=\"Invalid taget!\" />");
175         }
176         // FIXME
177         response->headers["Content-Type"] = "text/xml; charset=iso-8859-1";
178         //response->headers["Connection"] = "close";
179         controlserver->send(*response);
180         delete 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();
185         //}
186 }
187
188 HTTPResponse* Server::cmd_server_status(void) const {
189         // FIXME
190         stringstream xml;
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());
200 }
201
202 HTTPResponse* Server::cmd_connection_list(void) {
203         // FIXME
204         TCPServer::ConnectionInfoList cil = get_connected();
205         stringstream xml;
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;
213         }
214         xml << "</list>" << endl;
215         return new HTTPResponse(HTTPMessage::OK, xml.str());
216 }
217
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...\" />");
228         } else {
229                 return new HTTPResponse(HTTPMessage::NOT_FOUND,
230                                 string("<response desc=\"No existe una conexión a ") + args[0]
231                                 + ":" + args[1] + "\" />");
232         }
233 }
234
235 HTTPResponse* Server::cmd_transmission_list(void) {
236         // FIXME
237         stringstream xml;
238         xml << "<list type=\"transmission\">" << endl;
239 /*TODO  plants_mutex.lock();
240         for (PlantList::const_iterator i = plants.begin();
241                         i != plants.end(); i++) {
242                 trans
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;
247         }
248         transmissions_mutex.unlock();*/
249         xml << "</list>" << endl;
250         return new HTTPResponse(HTTPMessage::OK, xml.str());
251 }
252
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.\" />");
258         } else {
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) + ".\" />");
273                 } else {
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] + ".\" />");
277                 }
278         }
279 }
280
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.\" />");
286         } else {
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
292                         // desconectar.
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] + ".\" />");
297                         }
298                 }
299                 return new HTTPResponse(HTTPMessage::NOT_FOUND,
300                                 string("<response desc=\"No se puede finalizar la transmisión a ")
301                                 + host + ":" + args[1] + ".\" />");
302         }
303 }
304
305 HTTPResponse* Server::cmd_plant_list(void) {
306         // FIXME
307         stringstream xml;
308         xml << "<list type=\"plant\">" << endl;
309         plants_mutex.lock();
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;
315         }
316         plants_mutex.unlock();
317         xml << "</list>" << endl;
318         return new HTTPResponse(HTTPMessage::OK, xml.str());
319 }
320
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.\" />");
325         }
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 + "\" />");
331         }
332         // TODO try/catch?
333         string xml = plants[plant]->get_xml();
334         if (xml.length()) {
335                 return new HTTPResponse(HTTPMessage::OK, xml);
336         } else {
337                 return new HTTPResponse(HTTPMessage::INTERNAL_SERVER_ERROR,
338                                 ("<response desc=\"No se pudo obtener el XML de la planta ") + plant + "\" />");
339         }
340 }
341
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.\" />");
347         }
348         string plant = args[0];
349         string element = args[1];
350         string key = args[2];
351         if (key != "open") {
352                 return new HTTPResponse(HTTPMessage::NOT_FOUND,
353                                 string("<response desc=\"La clave '") + key + "' es inválida.\" />");
354         }
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 + "'.\" />");
361         }
362         bool open = true;
363         if ((value == "false") || (value == "0") || (value == "off")
364                         || (value == "no")) {
365                 open = false;
366         }
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 + "'.\" />");
370         }
371         return new HTTPResponse(HTTPMessage::OK,
372                         string("<response desc=\"Se cambió el estado del elemento '") + element + "'.\" />");
373 }
374
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.\" />");
379         }
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 + "\" />");
385         }
386         unsigned hz;
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.\" />");
391 }
392
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.\" />");
397         }
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 + "\" />");
403         }
404         plants[name]->set_paused(false);
405         return new HTTPResponse(HTTPMessage::OK,
406                         string("<response desc=\"La planta '") + name + "' fue reanudada.\" />");
407 }
408
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.\" />");
413         }
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 + "\" />");
419         }
420         plants[name]->set_paused(true);
421         return new HTTPResponse(HTTPMessage::OK,
422                         string("<response desc=\"La planta '") + name + "' fue pausada.\" />");
423 }
424
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.\" />");
429         }
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 + "\" />");
435         }
436         plants[name]->finish();
437         return new HTTPResponse(HTTPMessage::OK,
438                         string("<response desc=\"La planta '") + name + "' se cerrará en instantes...\" />");
439 }
440
441 } // namespace Server
442
443 } // namespace PlaQui
444