]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Server/src/server.cpp
Se pasa a autoconf + automake.
[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                                         "<response desc=\"El server se apagará en instantes...\" />");
119                 } else {
120                         response = new HTTPResponse(HTTPMessage::NOT_FOUND,
121                                         "<response desc=\"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                                         "<response desc=\"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                                         "<response desc=\"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                                         "<response desc=\"Invalid command for 'plant' taget!\" />");
155                 }
156         } else {
157                 response = new HTTPResponse(HTTPMessage::NOT_FOUND,
158                                 "<response desc=\"Invalid taget!\" />");
159         }
160         // FIXME
161         response->headers["Content-Type"] = "text/xml; charset=iso-8859-1";
162         //response->headers["Connection"] = "close";
163         controlserver->send(*response);
164         delete response;
165         // FIXME con timeout no debería ser necesario. Verificar cabecera Connection
166         // para saber si hay que finish()earlo o no.
167         //if (stop_controlserver) {
168         //      controlserver->finish();
169         //}
170 }
171
172 HTTPResponse* Server::cmd_server_status(void) const {
173         // FIXME
174         stringstream xml;
175         xml << "<serverstatus>" << endl;
176         xml << "\t<version>0.9</version>" << endl;
177         xml << "\t<authors>" << endl;
178         xml << "\t\t<author>Nicolás Dimov</author>" << endl;
179         xml << "\t\t<author>Leandro Lucarella</author>" << endl;
180         xml << "\t\t<author>Ricardo Markiewicz</author>" << endl;
181         xml << "\t</authors>" << endl;
182         xml << "</serverstatus>" << endl;
183         return new HTTPResponse(HTTPMessage::OK, xml.str());
184 }
185
186 HTTPResponse* Server::cmd_connection_list(void) {
187         // FIXME
188         TCPServer::ConnectionInfoList cil = get_connected();
189         stringstream xml;
190         xml << "<list type=\"connection\">" << endl;
191         for (TCPServer::ConnectionInfoList::const_iterator i = cil.begin();
192                         i != cil.end(); i++) {
193                 xml << "\t<row>" << endl;
194                 xml << "\t\t<cell>" << i->host << "</cell>" << endl;
195                 xml << "\t\t<cell>" << i->port << "</cell>" << endl;
196                 xml << "\t</row>" << endl;
197         }
198         xml << "</list>" << endl;
199         return new HTTPResponse(HTTPMessage::OK, xml.str());
200 }
201
202 HTTPResponse* Server::cmd_connection_stop(const Command& command) {
203         const Command::Arguments& args = command.get_args();
204         Connection::Port port;
205         if (args.size() < 2) {
206                 return new HTTPResponse(HTTPMessage::CONFLICT,
207                                 "<response desc=\"Faltan argumentos.\" />");
208         } else if (disconnect(args[0], to(args[1], port))) {
209                 return new HTTPResponse(HTTPMessage::OK,
210                                 string("<response desc=\"La conexión a ") + args[0] + ":" + args[1]
211                                 + " se cerrará en instantes...\" />");
212         } else {
213                 return new HTTPResponse(HTTPMessage::NOT_FOUND,
214                                 string("<response desc=\"No existe una conexión a ") + args[0]
215                                 + ":" + args[1] + "\" />");
216         }
217 }
218
219 HTTPResponse* Server::cmd_transmission_list(void) {
220         // FIXME
221         stringstream xml;
222         xml << "<list type=\"transmission\">" << endl;
223 /*TODO  plants_mutex.lock();
224         for (PlantList::const_iterator i = plants.begin();
225                         i != plants.end(); i++) {
226                 trans
227                 xml << "       <li>" << (*i)->get_host() << ":"
228                         << (*i)->get_port() << " [<a href=\"/transmission/stop/"
229                         << (*i)->get_host() << "/" << (*i)->get_port()
230                         << "\">desconectar</a>]</li>" << endl;
231         }
232         transmissions_mutex.unlock();*/
233         xml << "</list>" << endl;
234         return new HTTPResponse(HTTPMessage::OK, xml.str());
235 }
236
237 HTTPResponse* Server::cmd_transmission_start(const Command& command) {
238         const Command::Arguments& args = command.get_args();
239         if (args.size() < 3) {
240                 return new HTTPResponse(HTTPMessage::CONFLICT,
241                                 "<response desc=\"Faltan argumentos.\" />");
242         } else {
243                 string plant = args[0];
244                 string host = args[1];
245                 Connection::Port port = to(args[2], port);
246                 Glib::Mutex::Lock lock(plants_mutex);
247                 PlantList::iterator p = plants.find(plant);
248                 if (p == plants.end()) {
249                         return new HTTPResponse(HTTPMessage::NOT_FOUND,
250                                         string("<response desc=\"No existe la planta '") + plant + "'.\" />");
251                 // TODO - agregar chequeo de que la transmision a ese host:port no
252                 //        exista para otra planta?
253                 } else if (plants[plant]->transmission_start(host, port)) {
254                         return new HTTPResponse(HTTPMessage::OK,
255                                         string("<response desc=\"Se empieza a transmitir la planta '") + plant
256                                         + "' a " + host + ":" + String().from(port) + ".\" />");
257                 } else {
258                         return new HTTPResponse(HTTPMessage::INTERNAL_SERVER_ERROR,
259                                         string("<response desc=\"Error al crear la transmisión a de la planta '")
260                                         + plant + "' a " + host + ":" + args[2] + ".\" />");
261                 }
262         }
263 }
264
265 HTTPResponse* Server::cmd_transmission_stop(const Command& command) {
266         const Command::Arguments& args = command.get_args();
267         if (args.size() < 2) {
268                 return new HTTPResponse(HTTPMessage::CONFLICT,
269                                 "<response desc=\"Faltan argumentos.\" />");
270         } else {
271                 const string& host = args[0];
272                 Connection::Port port = to(args[1], port);
273                 for (PlantList::iterator i = plants.begin(); i != plants.end(); i++) {
274                         // TODO - agregar chequeo para saber si existe una conexion (para
275                         // tirar error de que no hay conexion o de que no se pudo
276                         // desconectar.
277                         if (i->second->transmission_stop(host, port)) {
278                                 return new HTTPResponse(HTTPMessage::OK,
279                                                 string("<response desc=\"Se finaliza la transmisión de la planta '")
280                                                 + i->first + "' a " + host + ":" + args[1] + ".\" />");
281                         }
282                 }
283                 return new HTTPResponse(HTTPMessage::NOT_FOUND,
284                                 string("<response desc=\"No se puede finalizar la transmisión a ")
285                                 + host + ":" + args[1] + ".\" />");
286         }
287 }
288
289 HTTPResponse* Server::cmd_plant_list(void) {
290         // FIXME
291         stringstream xml;
292         xml << "<list type=\"plant\">" << endl;
293         plants_mutex.lock();
294         for (PlantList::const_iterator i = plants.begin();
295                         i != plants.end(); i++) {
296                 xml << "\t<row>" << endl;
297                 xml << "\t\t<cell>" << i->first << "</cell>" << endl;
298                 xml << "\t</row>" << endl;
299         }
300         plants_mutex.unlock();
301         xml << "</list>" << endl;
302         return new HTTPResponse(HTTPMessage::OK, xml.str());
303 }
304
305 HTTPResponse* Server::cmd_plant_get(const Command& command) {
306         if (!command.get_args().size()) {
307                 return new HTTPResponse(HTTPMessage::CONFLICT,
308                                 "<response desc=\"Faltan argumentos.\" />");
309         }
310         Glib::Mutex::Lock lock(plants_mutex);
311         string plant = command.get_args()[0];
312         if (plants.find(plant) == plants.end()) {
313                 return new HTTPResponse(HTTPMessage::NOT_FOUND,
314                                 string("<response desc=\"No existe la planta ") + plant + "\" />");
315         }
316         string xml = plants[plant]->get_xml();
317         if (xml.length()) {
318                 return new HTTPResponse(HTTPMessage::OK, xml);
319         } else {
320                 return new HTTPResponse(HTTPMessage::INTERNAL_SERVER_ERROR,
321                                 ("<response desc=\"No se pudo obtener el XML de la planta ") + plant + "\" />");
322         }
323 }
324
325 HTTPResponse* Server::cmd_plant_set(const Command& command) {
326         const Command::Arguments& args = command.get_args();
327         if (args.size() < 4) {
328                 return new HTTPResponse(HTTPMessage::CONFLICT,
329                                 "<response desc=\"Faltan argumentos.\" />");
330         }
331         string plant = args[0];
332         string element = args[1];
333         string key = args[2];
334         if (key != "open") {
335                 return new HTTPResponse(HTTPMessage::NOT_FOUND,
336                                 string("<response desc=\"La clave '") + key + "' es inválida.\" />");
337         }
338         string value = args[3];
339         Glib::Mutex::Lock lock(plants_mutex);
340         PlantList::iterator p = plants.find(plant);
341         if (p == plants.end()) {
342                 return new HTTPResponse(HTTPMessage::NOT_FOUND,
343                                 string("<response desc=\"No existe la planta '") + plant + "'.\" />");
344         }
345         bool open = true;
346         if ((value == "false") || (value == "0") || (value == "off")
347                         || (value == "no")) {
348                 open = false;
349         }
350         if (!plants[plant]->set_open(element, open)) {
351                 return new HTTPResponse(HTTPMessage::CONFLICT,
352                                 string("<response desc=\"No se pudo cambiar el estado del elemento '") + element + "'.\" />");
353         }
354         return new HTTPResponse(HTTPMessage::OK,
355                         string("<response desc=\"Se cambió el estado del elemento '") + element + "'.\" />");
356 }
357
358 HTTPResponse* Server::cmd_plant_stop(const Command& command) {
359         if (!command.get_args().size()) {
360                 return new HTTPResponse(HTTPMessage::CONFLICT,
361                                 "<response desc=\"Faltan argumentos.\" />");
362         }
363         Glib::Mutex::Lock lock(plants_mutex);
364         const string name = command.get_args()[0];
365         if (plants.find(name) == plants.end()) {
366                 return new HTTPResponse(HTTPMessage::NOT_FOUND,
367                                 string("<response desc=\"No existe la planta ") + name + "\" />");
368         }
369         // TODO Ver si al frenar la planta se destruye (no deberia!!!)
370         plants[name]->finish();
371         return new HTTPResponse(HTTPMessage::OK,
372                         string("<response desc=\"La planta '") + name + "' se cerrará en instantes...\" />");
373 }
374
375 } // namespace Server
376
377 } // namespace PlaQui
378