]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Server/src/server.cpp
- Se agrega una funcion templateada to() para convertir de un tipo de dato
[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 // FIXME - sacar sstream (a menos que se necesite)
33 #include <sstream>
34 #ifdef DEBUG
35 #       include "plaqui/server/string.h"
36 #       include <iostream>
37 #endif // DEBUG
38
39 using namespace std;
40
41 namespace PlaQui {
42
43 namespace Server {
44
45 Server::~Server(void) {
46 #ifdef DEBUG
47         cerr << __FILE__ <<  ": destructor." << endl;
48 #endif // DEBUG
49         // Termino plantas.
50         Glib::Mutex::Lock lock(plants_mutex);
51         for (PlantList::iterator i = plants.end(); i != plants.end(); i++) {
52                 i->second->finish(true);
53         }
54 }
55
56 Server::Server(const string& plant_filename, const Connection::Port& port):
57                 TCPServer(port) {
58 #ifdef DEBUG
59         cerr << __FILE__ <<  ": port = " << port << endl;
60 #endif // DEBUG
61         // FIXME
62         Glib::Mutex::Lock lock(plants_mutex);
63         plants["default"] = new Plant(plant_filename);
64         plants["default"]->signal_finished().connect(
65                         SigC::bind<const char*>(
66                                 SigC::slot_class(*this, &Server::on_plant_finished),
67                                 "default"));
68         plants["default"]->run();
69 }
70
71 Connection* Server::new_connection(
72                 const sockbuf::sockdesc& sd) {
73 #ifdef DEBUG
74         cerr << __FILE__ <<  ": new_connection(sd = " << sd.sock << ")"
75                 << endl;
76 #endif // DEBUG
77         ControlServer* connection = new ControlServer(sd);
78         // TODO verificar si el new se hace bien? no creo.
79         connection->signal_command_received().connect(
80                         SigC::bind<ControlServer*>(
81                                 SigC::slot_class(*this, &Server::on_control_command_received),
82                                 connection));
83         // TODO: 
84         return connection;
85 }
86
87 void Server::on_plant_updated(const Plant* plant) {
88 #ifdef DEBUG
89         cerr << __FILE__ << ": on_plant_updated(plant = " << plant << ")." << endl;
90 #endif // DEBUG
91 }
92
93 void Server::on_plant_finished(const char* plant) {
94 #ifdef DEBUG
95         cerr << __FILE__ <<  ": on_plant_finished(plant_name = " << plant << endl;
96 #endif // DEBUG
97         Glib::Mutex::Lock lock(plants_mutex);
98         plants.erase(plant);
99 }
100
101 /// \todo Terminar de implementar.
102 void Server::on_control_command_received(const Command& command,
103                 ControlServer* controlserver) {
104 #ifdef DEBUG
105         cerr << __FILE__ <<  ": on_control_command_received(target = "
106                 << command.get_target() << ", command = " << command.get_command()
107                 << ", args = [" << String::join(command.get_args(), ", ") << "])"
108                 << endl;
109 #endif // DEBUG
110         HTTPResponse* response;
111         //bool stop_controlserver = false;
112         if (command.get_target() == "server") {
113                 if (command.get_command() == "status") {
114                         response = cmd_server_status();
115                 } else if (command.get_command() == "stop") {
116                         finish();
117                         response = new HTTPResponse(HTTPMessage::OK,
118                                         "El server se apagará en instantes...");
119                 } else {
120                         response = new HTTPResponse(HTTPMessage::NOT_FOUND,
121                                         "Invalid command for 'server' taget!");
122                 }
123         } else if (command.get_target() == "connection") {
124                 if (command.get_command() == "list") {
125                         response = cmd_connection_list();
126                 } else if (command.get_command() == "stop") {
127                         response = cmd_connection_stop(command);
128                 } else {
129                         response = new HTTPResponse(HTTPMessage::NOT_FOUND,
130                                         "Invalid command for 'connection' taget!");
131                 }
132         } else if (command.get_target() == "transmission") {
133                 if (command.get_command() == "list") {
134                         response = cmd_transmission_list();
135                 } else if (command.get_command() == "start") {
136                         response = cmd_transmission_start(command);
137                 } else if (command.get_command() == "stop") {
138                         response = cmd_transmission_stop(command);
139                 } else {
140                         response = new HTTPResponse(HTTPMessage::NOT_FOUND,
141                                         "Invalid command for 'transmission' taget!");
142                 }
143         } else if (command.get_target() == "plant") {
144                 if (command.get_command() == "list") {
145                         response = cmd_plant_list();
146                 } else if (command.get_command() == "get") {
147                         response = cmd_plant_get(command);
148                 } else if (command.get_command() == "set") {
149                         response = cmd_plant_set(command);
150                 } else if (command.get_command() == "stop") {
151                         response = cmd_plant_stop(command);
152                 } else {
153                         response = new HTTPResponse(HTTPMessage::NOT_FOUND,
154                                         "Invalid command for 'plant' taget!");
155                 }
156         } else {
157                 response = new HTTPResponse(HTTPMessage::NOT_FOUND, "Invalid taget!");
158         }
159         // FIXME
160         response->headers["Content-Type"] = "text/xml; charset=iso-8859-1";
161         //response->headers["Connection"] = "close";
162         controlserver->send(*response);
163         delete response;
164         // FIXME con timeout no debería ser necesario. Verificar cabecera Connection
165         // para saber si hay que finish()earlo o no.
166         //if (stop_controlserver) {
167         //      controlserver->finish();
168         //}
169 }
170
171 HTTPResponse* Server::cmd_server_status(void) const {
172         // FIXME
173         stringstream response_xml;
174         response_xml << "<serverstatus>" << endl;
175         response_xml << "\t<version>0.9</version>" << endl;
176         response_xml << "\t<authors>" << endl;
177         response_xml << "\t\t<author>Nicolás Dimov</author>" << endl;
178         response_xml << "\t\t<author>Leandro Lucarella</author>" << endl;
179         response_xml << "\t\t<author>Ricardo Markiewicz</author>" << endl;
180         response_xml << "\t</authors>" << endl;
181         response_xml << "</serverstatus>" << endl;
182         return new HTTPResponse(HTTPMessage::OK, response_xml.str());
183 }
184
185 HTTPResponse* Server::cmd_connection_list(void) {
186         // FIXME
187         TCPServer::ConnectionInfoList cil = get_connected();
188         stringstream response_xml;
189         response_xml << "<list type=\"connection\">" << endl;
190         for (TCPServer::ConnectionInfoList::const_iterator i = cil.begin();
191                         i != cil.end(); i++) {
192                 response_xml << "\t<row>" << endl;
193                 response_xml << "\t\t<cell>" << i->host << "</cell>" << endl;
194                 response_xml << "\t\t<cell>" << i->port << "</cell>" << endl;
195                 response_xml << "\t</row>" << endl;
196         }
197         response_xml << "</list>" << endl;
198         return new HTTPResponse(HTTPMessage::OK, response_xml.str());
199 }
200
201 HTTPResponse* Server::cmd_connection_stop(const Command& command) {
202         const Command::Arguments& args = command.get_args();
203         Connection::Port port;
204         if (args.size() < 2) {
205                 return new HTTPResponse(HTTPMessage::CONFLICT,
206                                 "Faltan argumentos.");
207         } else if (disconnect(args[0], String(args[1]).to(port))) {
208                 return new HTTPResponse(HTTPMessage::OK,
209                                 string("La conexión a ") + args[0] + ":" + args[1]
210                                 + " se cerrará en instantes...");
211         } else {
212                 return new HTTPResponse(HTTPMessage::NOT_FOUND,
213                                 string("No existe una conexión a ") + args[0]
214                                 + ":" + args[1]);
215         }
216 }
217
218 HTTPResponse* Server::cmd_transmission_list(void) {
219         // FIXME
220         stringstream response_xml;
221         response_xml << "<list type=\"transmission\">" << endl;
222 /*TODO  plants_mutex.lock();
223         for (PlantList::const_iterator i = plants.begin();
224                         i != plants.end(); i++) {
225                 trans
226                 response_xml << "       <li>" << (*i)->get_host() << ":"
227                         << (*i)->get_port() << " [<a href=\"/transmission/stop/"
228                         << (*i)->get_host() << "/" << (*i)->get_port()
229                         << "\">desconectar</a>]</li>" << endl;
230         }
231         transmissions_mutex.unlock();*/
232         response_xml << "</list>" << endl;
233         return new HTTPResponse(HTTPMessage::OK, response_xml.str());
234 }
235
236 HTTPResponse* Server::cmd_transmission_start(const Command& command) {
237         const Command::Arguments& args = command.get_args();
238         if (args.size() < 3) {
239                 return new HTTPResponse(HTTPMessage::CONFLICT,
240                                 "Faltan argumentos.");
241         } else {
242                 string plant = args[0];
243                 string host = args[1];
244                 Connection::Port port = String(args[2]).to(port);
245                 Glib::Mutex::Lock lock(plants_mutex);
246                 PlantList::iterator p = plants.find(plant);
247                 if (p == plants.end()) {
248                         return new HTTPResponse(HTTPMessage::NOT_FOUND,
249                                         string("No existe la planta '") + plant + "'.");
250                 // TODO - agregar chequeo de que la transmision a ese host:port no
251                 //        exista para otra planta?
252                 } else if (plants[plant]->transmission_start(host, port)) {
253                         return new HTTPResponse(HTTPMessage::OK,
254                                         string("Se empieza a transmitir la planta '") + plant
255                                         + "' a " + host + ":" + String().from(port) + ".");
256                 } else {
257                         return new HTTPResponse(HTTPMessage::INTERNAL_SERVER_ERROR,
258                                         string("Error al crear la transmisión a de la planta '")
259                                         + plant + "' a " + host + ":" + args[2] + ".");
260                 }
261         }
262 }
263
264 HTTPResponse* Server::cmd_transmission_stop(const Command& command) {
265         const Command::Arguments& args = command.get_args();
266         if (args.size() < 2) {
267                 return new HTTPResponse(HTTPMessage::CONFLICT,
268                                 "Faltan argumentos.");
269         } else {
270                 const string& host = args[0];
271                 Connection::Port port = String(args[1]).to(port);
272                 for (PlantList::iterator i = plants.begin(); i != plants.end(); i++) {
273                         // TODO - agregar chequeo para saber si existe una conexion (para
274                         // tirar error de que no hay conexion o de que no se pudo
275                         // desconectar.
276                         if (i->second->transmission_stop(host, port)) {
277                                 return new HTTPResponse(HTTPMessage::OK,
278                                                 string("Se finaliza la transmisión de la planta '")
279                                                 + i->first + "' a " + host + ":" + args[1] + ".");
280                         }
281                 }
282                 return new HTTPResponse(HTTPMessage::NOT_FOUND,
283                                 string("No se puede finalizar la transmisión a ")
284                                 + host + ":" + args[1] + ".");
285         }
286 }
287
288 HTTPResponse* Server::cmd_plant_list(void) {
289         // FIXME
290         stringstream response_xml;
291         response_xml << "<list type=\"plant\">" << endl;
292         plants_mutex.lock();
293         for (PlantList::const_iterator i = plants.begin();
294                         i != plants.end(); i++) {
295                 response_xml << "\t<row>" << endl;
296                 response_xml << "\t\t<cell>" << i->first << "</cell>" << endl;
297                 response_xml << "\t</row>" << endl;
298         }
299         plants_mutex.unlock();
300         response_xml << "</list>" << endl;
301         return new HTTPResponse(HTTPMessage::OK, response_xml.str());
302 }
303
304 HTTPResponse* Server::cmd_plant_get(const Command& command) {
305         if (!command.get_args().size()) {
306                 return new HTTPResponse(HTTPMessage::CONFLICT,
307                                 "Faltan argumentos.");
308         }
309         Glib::Mutex::Lock lock(plants_mutex);
310         string plant = command.get_args()[0];
311         if (plants.find(plant) == plants.end()) {
312                 return new HTTPResponse(HTTPMessage::NOT_FOUND,
313                                 string("No existe la planta ") + plant);
314         }
315         string xml = plants[plant]->get_xml();
316         if (xml.length()) {
317                 return new HTTPResponse(HTTPMessage::OK, xml);
318         } else {
319                 return new HTTPResponse(HTTPMessage::INTERNAL_SERVER_ERROR,
320                                 ("No se pudo obtener el XML de la planta ") + plant);
321         }
322 }
323
324 HTTPResponse* Server::cmd_plant_set(const Command& command) {
325         const Command::Arguments& args = command.get_args();
326         if (args.size() < 4) {
327                 return new HTTPResponse(HTTPMessage::CONFLICT,
328                                 "Faltan argumentos.");
329         }
330         string plant = args[0];
331         string element = args[1];
332         string key = args[2];
333         string value = args[3];
334         Glib::Mutex::Lock lock(plants_mutex);
335         PlantList::iterator p = plants.find(plant);
336         if (p == plants.end()) {
337                 return new HTTPResponse(HTTPMessage::NOT_FOUND,
338                                 string("No existe la planta '") + plant + "'.");
339         }
340 /*      if (!plants[plant]->set_element(host, port)) {
341                 return new HTTPResponse(HTTPMessage::INTERNAL_SERVER_ERROR,
342                                 string("Error al crear la transmisión a de la planta '")
343                                 + plant + "' a " + host + ":" + args[2] + ".");
344         }*/
345         return new HTTPResponse(HTTPMessage::OK);
346 }
347
348 HTTPResponse* Server::cmd_plant_stop(const Command& command) {
349         if (!command.get_args().size()) {
350                 return new HTTPResponse(HTTPMessage::CONFLICT,
351                                 "Faltan argumentos.");
352         }
353         Glib::Mutex::Lock lock(plants_mutex);
354         const string name = command.get_args()[0];
355         if (plants.find(name) == plants.end()) {
356                 return new HTTPResponse(HTTPMessage::NOT_FOUND,
357                                 string("No existe la planta ") + name);
358         }
359         // TODO Ver si al frenar la planta se destruye (no deberia!!!)
360         plants[name]->finish();
361         return new HTTPResponse(HTTPMessage::OK,
362                         string("La planta '") + name + "' se cerrará en instantes...");
363 }
364
365 } // namespace Server
366
367 } // namespace PlaQui
368