]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Server/src/server.cpp
Capturo las seniales ANSI C SIGINT, SIGTERM y SIGQUIT para que el server salga
[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() == "stop") {
163                         response = cmd_plant_stop(command);
164                 } else {
165                         response = new HTTPResponse(HTTPMessage::NOT_FOUND,
166                                         "<response desc=\"Invalid command for 'plant' taget!\" />");
167                 }
168         } else {
169                 response = new HTTPResponse(HTTPMessage::NOT_FOUND,
170                                 "<response desc=\"Invalid taget!\" />");
171         }
172         // FIXME
173         response->headers["Content-Type"] = "text/xml; charset=iso-8859-1";
174         //response->headers["Connection"] = "close";
175         controlserver->send(*response);
176         delete 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();
181         //}
182 }
183
184 HTTPResponse* Server::cmd_server_status(void) const {
185         // FIXME
186         stringstream xml;
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());
196 }
197
198 HTTPResponse* Server::cmd_connection_list(void) {
199         // FIXME
200         TCPServer::ConnectionInfoList cil = get_connected();
201         stringstream xml;
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;
209         }
210         xml << "</list>" << endl;
211         return new HTTPResponse(HTTPMessage::OK, xml.str());
212 }
213
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...\" />");
224         } else {
225                 return new HTTPResponse(HTTPMessage::NOT_FOUND,
226                                 string("<response desc=\"No existe una conexión a ") + args[0]
227                                 + ":" + args[1] + "\" />");
228         }
229 }
230
231 HTTPResponse* Server::cmd_transmission_list(void) {
232         // FIXME
233         stringstream xml;
234         xml << "<list type=\"transmission\">" << endl;
235 /*TODO  plants_mutex.lock();
236         for (PlantList::const_iterator i = plants.begin();
237                         i != plants.end(); i++) {
238                 trans
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;
243         }
244         transmissions_mutex.unlock();*/
245         xml << "</list>" << endl;
246         return new HTTPResponse(HTTPMessage::OK, xml.str());
247 }
248
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.\" />");
254         } else {
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) + ".\" />");
269                 } else {
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] + ".\" />");
273                 }
274         }
275 }
276
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.\" />");
282         } else {
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
288                         // desconectar.
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] + ".\" />");
293                         }
294                 }
295                 return new HTTPResponse(HTTPMessage::NOT_FOUND,
296                                 string("<response desc=\"No se puede finalizar la transmisión a ")
297                                 + host + ":" + args[1] + ".\" />");
298         }
299 }
300
301 HTTPResponse* Server::cmd_plant_list(void) {
302         // FIXME
303         stringstream xml;
304         xml << "<list type=\"plant\">" << endl;
305         plants_mutex.lock();
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;
311         }
312         plants_mutex.unlock();
313         xml << "</list>" << endl;
314         return new HTTPResponse(HTTPMessage::OK, xml.str());
315 }
316
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.\" />");
321         }
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 + "\" />");
327         }
328         // TODO try/catch?
329         string xml = plants[plant]->get_xml();
330         if (xml.length()) {
331                 return new HTTPResponse(HTTPMessage::OK, xml);
332         } else {
333                 return new HTTPResponse(HTTPMessage::INTERNAL_SERVER_ERROR,
334                                 ("<response desc=\"No se pudo obtener el XML de la planta ") + plant + "\" />");
335         }
336 }
337
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.\" />");
343         }
344         string plant = args[0];
345         string element = args[1];
346         string key = args[2];
347         if (key != "open") {
348                 return new HTTPResponse(HTTPMessage::NOT_FOUND,
349                                 string("<response desc=\"La clave '") + key + "' es inválida.\" />");
350         }
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 + "'.\" />");
357         }
358         bool open = true;
359         if ((value == "false") || (value == "0") || (value == "off")
360                         || (value == "no")) {
361                 open = false;
362         }
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 + "'.\" />");
366         }
367         return new HTTPResponse(HTTPMessage::OK,
368                         string("<response desc=\"Se cambió el estado del elemento '") + element + "'.\" />");
369 }
370
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.\" />");
375         }
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 + "\" />");
381         }
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...\" />");
386 }
387
388 } // namespace Server
389
390 } // namespace PlaQui
391